persist-fast-path.slt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. mode cockroach
  10. # Verify that the persist fast path only kicks in when it's expected to do so.
  11. # Generate a table, with multiple batches of data and some partial overlaps
  12. statement ok
  13. CREATE TABLE numbers (
  14. value int
  15. );
  16. # Applies when the limit is below some threshold. Mapping and
  17. # projecting is fine.
  18. query T multiline
  19. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * from numbers limit 10;
  20. ----
  21. Explained Query (fast path):
  22. Finish limit=10 output=[#0]
  23. PeekPersist materialize.public.numbers
  24. Target cluster: quickstart
  25. EOF
  26. query T multiline
  27. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT value + 1000 from numbers LIMIT 10;
  28. ----
  29. Explained Query (fast path):
  30. Finish limit=10 output=[#0]
  31. Project (#1)
  32. Map ((#0{value} + 1000))
  33. PeekPersist materialize.public.numbers
  34. Target cluster: quickstart
  35. EOF
  36. query T multiline
  37. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * from numbers LIMIT 10 OFFSET 10;
  38. ----
  39. Explained Query (fast path):
  40. Finish limit=10 offset=10 output=[#0]
  41. PeekPersist materialize.public.numbers
  42. Target cluster: quickstart
  43. EOF
  44. # Check that some fast-path queries succeed.
  45. statement ok
  46. INSERT INTO numbers SELECT generate_series(1, 3);
  47. statement ok
  48. INSERT INTO numbers SELECT generate_series(1, 10);
  49. # We should deterministically return the first N values.
  50. query T valuesort
  51. SELECT value from numbers LIMIT 20;
  52. ----
  53. 1
  54. 1
  55. 2
  56. 2
  57. 3
  58. 3
  59. 4
  60. 5
  61. 6
  62. 7
  63. 8
  64. 9
  65. 10
  66. query T
  67. SELECT value from numbers LIMIT 10 OFFSET 6;
  68. ----
  69. 4
  70. 5
  71. 6
  72. 7
  73. 8
  74. 9
  75. 10
  76. statement ok
  77. INSERT INTO numbers SELECT generate_series(5, 100);
  78. statement ok
  79. INSERT INTO numbers SELECT generate_series(-1, 10);
  80. statement ok
  81. INSERT INTO numbers SELECT generate_series(500, 10000);
  82. # Since we order the data in terms of its structured representation, we see the
  83. # smallest values regardless of which order they were inserted in.
  84. query T valuesort
  85. SELECT value from numbers LIMIT 5;
  86. ----
  87. -1
  88. 0
  89. 1
  90. 1
  91. 1
  92. # Errors should always be returned even when the limit is small
  93. statement ok
  94. CREATE MATERIALIZED VIEW erroring AS SELECT 10.0 / (value - 1) FROM numbers;
  95. query error db error: ERROR: Evaluation error: division by zero
  96. SELECT * FROM erroring LIMIT 10;
  97. # Does not apply when the limit is high, or when mixed with features
  98. # that might require a full scan.
  99. query T multiline
  100. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * from numbers LIMIT 1000;
  101. ----
  102. Explained Query:
  103. Finish limit=1000 output=[#0]
  104. ReadStorage materialize.public.numbers
  105. Source materialize.public.numbers
  106. Target cluster: quickstart
  107. EOF
  108. # ORDER BY is only okay when the ordering matches the shard ordering exactly
  109. query T multiline
  110. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * from numbers ORDER BY value ASC LIMIT 10;
  111. ----
  112. Explained Query (fast path):
  113. Finish order_by=[#0 asc nulls_last] limit=10 output=[#0]
  114. PeekPersist materialize.public.numbers
  115. Target cluster: quickstart
  116. EOF
  117. query T multiline
  118. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * from numbers ORDER BY value DESC LIMIT 10;
  119. ----
  120. Explained Query:
  121. Finish order_by=[#0{value} desc nulls_first] limit=10 output=[#0]
  122. ReadStorage materialize.public.numbers
  123. Source materialize.public.numbers
  124. Target cluster: quickstart
  125. EOF
  126. query T multiline
  127. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT value % 2 from numbers ORDER BY value % 2 LIMIT 10;
  128. ----
  129. Explained Query:
  130. Finish order_by=[#0 asc nulls_last] limit=10 output=[#0]
  131. Project (#1)
  132. Map ((#0{value} % 2))
  133. ReadStorage materialize.public.numbers
  134. Source materialize.public.numbers
  135. Target cluster: quickstart
  136. EOF
  137. query T multiline
  138. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * from numbers ORDER BY value NULLS FIRST LIMIT 10;
  139. ----
  140. Explained Query:
  141. Finish order_by=[#0{value} asc nulls_first] limit=10 output=[#0]
  142. ReadStorage materialize.public.numbers
  143. Source materialize.public.numbers
  144. Target cluster: quickstart
  145. EOF
  146. # Arbitrary filters can't be pushed down... we may need to scan
  147. # an arbitrary number of records to find one that matches.
  148. query T multiline
  149. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * from numbers WHERE value > mz_now() LIMIT 10;
  150. ----
  151. Explained Query:
  152. Finish limit=10 output=[#0]
  153. Filter (integer_to_mz_timestamp(#0{value}) > mz_now())
  154. ReadStorage materialize.public.numbers
  155. Source materialize.public.numbers
  156. filter=((integer_to_mz_timestamp(#0{value}) > mz_now()))
  157. Target cluster: quickstart
  158. EOF
  159. # Check that we bound result set size correctly
  160. statement ok
  161. CREATE TABLE large_rows (a int, b text)
  162. statement ok
  163. INSERT INTO large_rows SELECT * FROM generate_series(1, 100), repeat('a', 100000)
  164. simple conn=mz_system,user=mz_system
  165. ALTER SYSTEM SET max_result_size TO '1MB';
  166. ----
  167. COMPLETE 0
  168. statement ok
  169. SELECT * FROM large_rows LIMIT 1;
  170. # Need to disable the result stash, so that we actually exceed max result size
  171. simple conn=mz_system,user=mz_system
  172. ALTER SYSTEM SET enable_compute_peek_response_stash = false
  173. ----
  174. COMPLETE 0
  175. query error db error: ERROR: result exceeds max size of 1048.6 KB
  176. SELECT * FROM large_rows LIMIT 99;
  177. simple conn=mz_system,user=mz_system
  178. ALTER SYSTEM RESET max_result_size
  179. ----
  180. COMPLETE 0
  181. simple conn=mz_system,user=mz_system
  182. ALTER SYSTEM RESET enable_compute_peek_response_stash
  183. ----
  184. COMPLETE 0
  185. # Does not apply when an index exists.
  186. statement ok
  187. CREATE DEFAULT INDEX ON numbers;
  188. query T multiline
  189. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * from numbers LIMIT 10;
  190. ----
  191. Explained Query (fast path):
  192. Finish limit=10 output=[#0]
  193. ReadIndex on=materialize.public.numbers numbers_primary_idx=[*** full scan ***]
  194. Used Indexes:
  195. - materialize.public.numbers_primary_idx (fast path limit)
  196. Target cluster: quickstart
  197. EOF
  198. # Issue 22577
  199. statement ok
  200. CREATE TABLE t1 (f1 INTEGER);
  201. statement ok
  202. INSERT INTO t1 VALUES (2), (2);
  203. statement ok
  204. INSERT INTO t1 VALUES (1);
  205. query T
  206. SELECT * FROM t1 LIMIT 1 OFFSET 0;
  207. ----
  208. 1
  209. query T
  210. SELECT * FROM t1 LIMIT 1 OFFSET 1;
  211. ----
  212. 2
  213. simple conn=mz_system,user=mz_system
  214. ALTER SYSTEM RESET persist_fast_path_limit
  215. ----
  216. COMPLETE 0