monotonic.td 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. $ set-arg-default single-replica-cluster=quickstart
  10. # Test monotonicity analyses which derive from ENVELOPE NONE sources.
  11. # Note that these only test the implementation for monotonic sources,
  12. # they do not test that the analysis doesn't have false positives on
  13. # non-monotonic sources.
  14. # TODO: Once we have support for more "LOAD GENERATOR"-based monotonic
  15. # sources these tests can be migrated to `monotonic.slt`.
  16. $ set non-dbz-schema={
  17. "type": "record",
  18. "name": "cpx",
  19. "fields": [
  20. {"name": "a", "type": "long"},
  21. {"name": "b", "type": "long"}
  22. ]
  23. }
  24. $ kafka-create-topic topic=non-dbz-data
  25. $ kafka-ingest format=avro topic=non-dbz-data schema=${non-dbz-schema} timestamp=1
  26. {"a": 1, "b": 2}
  27. {"a": 1, "b": 1048576}
  28. {"a": 2, "b": 3}
  29. {"a": 2, "b": 4}
  30. > CREATE CONNECTION kafka_conn
  31. TO KAFKA (BROKER '${testdrive.kafka-addr}', SECURITY PROTOCOL PLAINTEXT);
  32. > CREATE SOURCE non_dbz_data
  33. IN CLUSTER ${arg.single-replica-cluster}
  34. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-non-dbz-data-${testdrive.seed}')
  35. FORMAT AVRO USING SCHEMA '${non-dbz-schema}'
  36. ENVELOPE NONE
  37. > CREATE MATERIALIZED VIEW monotonic_min AS SELECT a, min(b) FROM non_dbz_data group by a
  38. > SELECT * FROM monotonic_min
  39. a min
  40. ---
  41. 1 2
  42. 2 3
  43. > CREATE MATERIALIZED VIEW monotonic_max AS SELECT a, max(b) FROM non_dbz_data group by a
  44. > SELECT * FROM monotonic_max
  45. a max
  46. ---
  47. 1 1048576
  48. 2 4
  49. # Smoke test for aggregate fusion
  50. > CREATE MATERIALIZED VIEW monotonic_fused AS SELECT a, min(b), max(b + 1) FROM non_dbz_data group by a
  51. > SELECT * FROM monotonic_fused
  52. a min max
  53. ---
  54. 1 2 1048577
  55. 2 3 5
  56. # Propagating monotonicity analysis through indexed views
  57. # TODO: After https://github.com/MaterializeInc/materialize/pull/13238 is merged, modify these tests to dig into
  58. # the plans with `jq` and just check for `MonotonicTopK` being present.
  59. > CREATE VIEW i1 AS SELECT b FROM non_dbz_data
  60. > CREATE DEFAULT INDEX ON i1
  61. > CREATE VIEW v2 AS SELECT * FROM i1 ORDER BY b LIMIT 3
  62. $ set-regex match=(.User.:\s\d+|\(u\d+\)) replacement=UID
  63. ?[version>=14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v2
  64. Explained Query:
  65. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=3 must_consolidate
  66. ArrangeBy
  67. input_key=[#0{b}]
  68. raw=true
  69. Get::PassArrangements materialize.public.i1
  70. raw=false
  71. arrangements[0]={ key=[#0{b}], permutation=id, thinning=() }
  72. types=[bigint]
  73. Used Indexes:
  74. - materialize.public.i1_primary_idx (*** full scan ***)
  75. Target cluster: quickstart
  76. ?[version<14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v2
  77. Explained Query:
  78. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=3 must_consolidate
  79. ArrangeBy
  80. input_key=[#0]
  81. raw=true
  82. Get::PassArrangements materialize.public.i1
  83. raw=false
  84. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  85. types=[bigint]
  86. Used Indexes:
  87. - materialize.public.i1_primary_idx (*** full scan ***)
  88. Target cluster: quickstart
  89. > CREATE SOURCE non_dbz_data_indexed
  90. IN CLUSTER ${arg.single-replica-cluster}
  91. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-non-dbz-data-${testdrive.seed}')
  92. FORMAT AVRO USING SCHEMA '${non-dbz-schema}'
  93. ENVELOPE NONE
  94. > CREATE DEFAULT INDEX ON non_dbz_data_indexed
  95. # Propagating monotonicity analysis from an indexed source
  96. > CREATE VIEW v3 AS SELECT * FROM non_dbz_data_indexed ORDER BY b LIMIT 3
  97. ?[version>=14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v3
  98. Explained Query:
  99. TopK::MonotonicTopK order_by=[#1 asc nulls_last] limit=3 must_consolidate
  100. ArrangeBy
  101. input_key=[#0{a}, #1{b}]
  102. raw=true
  103. Get::PassArrangements materialize.public.non_dbz_data_indexed
  104. raw=false
  105. arrangements[0]={ key=[#0{a}, #1{b}], permutation=id, thinning=() }
  106. types=[bigint, bigint]
  107. Used Indexes:
  108. - materialize.public.non_dbz_data_indexed_primary_idx (*** full scan ***)
  109. Target cluster: quickstart
  110. ?[version<14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v3
  111. Explained Query:
  112. TopK::MonotonicTopK order_by=[#1 asc nulls_last] limit=3 must_consolidate
  113. ArrangeBy
  114. input_key=[#0, #1]
  115. raw=true
  116. Get::PassArrangements materialize.public.non_dbz_data_indexed
  117. raw=false
  118. arrangements[0]={ key=[#0, #1], permutation=id, thinning=() }
  119. types=[bigint, bigint]
  120. Used Indexes:
  121. - materialize.public.non_dbz_data_indexed_primary_idx (*** full scan ***)
  122. Target cluster: quickstart
  123. > CREATE VIEW i4 AS SELECT b+1 as c FROM i1;
  124. > CREATE DEFAULT INDEX ON i4;
  125. # Propagating monotonicity analysis through 2 indexed views (i1 and i4)
  126. > CREATE VIEW v5 AS SELECT * from i4 ORDER BY c LIMIT 2;
  127. ?[version>=14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v5;
  128. Explained Query:
  129. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=2 must_consolidate
  130. ArrangeBy
  131. input_key=[#0{c}]
  132. raw=true
  133. Get::PassArrangements materialize.public.i4
  134. raw=false
  135. arrangements[0]={ key=[#0{c}], permutation=id, thinning=() }
  136. types=[bigint]
  137. Used Indexes:
  138. - materialize.public.i4_primary_idx (*** full scan ***)
  139. Target cluster: quickstart
  140. ?[version<14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v5;
  141. Explained Query:
  142. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=2 must_consolidate
  143. ArrangeBy
  144. input_key=[#0]
  145. raw=true
  146. Get::PassArrangements materialize.public.i4
  147. raw=false
  148. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  149. types=[bigint]
  150. Used Indexes:
  151. - materialize.public.i4_primary_idx (*** full scan ***)
  152. Target cluster: quickstart
  153. > CREATE VIEW i6 AS SELECT c FROM v5;
  154. > CREATE DEFAULT INDEX ON i6;
  155. # Non-monotonic indexed view -- v7 can't use a monotonic TopK plan, because i6 and v5 are not monotonic
  156. > CREATE VIEW v7 AS SELECT * from i6 ORDER BY c LIMIT 2;
  157. ?[version>=14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v7;
  158. Explained Query:
  159. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=2 must_consolidate
  160. ArrangeBy
  161. input_key=[#0{c}]
  162. raw=true
  163. Get::PassArrangements materialize.public.i6
  164. raw=false
  165. arrangements[0]={ key=[#0{c}], permutation=id, thinning=() }
  166. types=[bigint]
  167. Used Indexes:
  168. - materialize.public.i6_primary_idx (*** full scan ***)
  169. Target cluster: quickstart
  170. ?[version<14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v7;
  171. Explained Query:
  172. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=2 must_consolidate
  173. ArrangeBy
  174. input_key=[#0]
  175. raw=true
  176. Get::PassArrangements materialize.public.i6
  177. raw=false
  178. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  179. types=[bigint]
  180. Used Indexes:
  181. - materialize.public.i6_primary_idx (*** full scan ***)
  182. Target cluster: quickstart
  183. > CREATE VIEW i8 AS SELECT * from (SELECT * FROM i1 UNION ALL SELECT * FROM i1);
  184. > CREATE DEFAULT INDEX ON i8;
  185. > CREATE VIEW i9 AS SELECT * from (SELECT * FROM i1 UNION ALL SELECT * FROM i8);
  186. > CREATE DEFAULT INDEX ON i9;
  187. > CREATE VIEW v10 as SELECT b FROM i9 ORDER BY b LIMIT 2;;
  188. # Propagating monotonicity analysis in a complex situation: (i1, i1) -> i8; (i1, i8) -> i9
  189. ?[version>=14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v10;
  190. Explained Query:
  191. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=2 must_consolidate
  192. ArrangeBy
  193. input_key=[#0{b}]
  194. raw=true
  195. Get::PassArrangements materialize.public.i9
  196. raw=false
  197. arrangements[0]={ key=[#0{b}], permutation=id, thinning=() }
  198. types=[bigint]
  199. Used Indexes:
  200. - materialize.public.i9_primary_idx (*** full scan ***)
  201. Target cluster: quickstart
  202. ?[version<14400] EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v10;
  203. Explained Query:
  204. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=2 must_consolidate
  205. ArrangeBy
  206. input_key=[#0]
  207. raw=true
  208. Get::PassArrangements materialize.public.i9
  209. raw=false
  210. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  211. types=[bigint]
  212. Used Indexes:
  213. - materialize.public.i9_primary_idx (*** full scan ***)
  214. Target cluster: quickstart
  215. # _No_ propagation of monotonicity through materialized views.
  216. > CREATE MATERIALIZED VIEW m1 AS SELECT b FROM non_dbz_data;
  217. > CREATE VIEW v11 AS SELECT * FROM m1 ORDER BY b LIMIT 3;
  218. ? EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR SELECT * FROM v11;
  219. Explained Query:
  220. TopK::MonotonicTopK order_by=[#0 asc nulls_last] limit=3 must_consolidate
  221. Get::PassArrangements materialize.public.m1
  222. raw=true
  223. Source materialize.public.m1
  224. Target cluster: quickstart
  225. # Check arrangements, seeing new arrangements can mean a significant increase
  226. # in memory consumptions and should be understood before adapting the values.
  227. > SET cluster_replica = r1
  228. >[version>=15000] SELECT mdod.dataflow_name, mdod.name
  229. FROM mz_introspection.mz_arrangement_sharing mash
  230. JOIN mz_introspection.mz_dataflow_operator_dataflows mdod ON mash.operator_id = mdod.id
  231. JOIN mz_introspection.mz_compute_exports USING (dataflow_id)
  232. WHERE export_id LIKE 'u%'
  233. "Dataflow: materialize.public.i1_primary_idx" "ArrangeBy[[Column(0, \"b\")]]"
  234. "Dataflow: materialize.public.i1_primary_idx" "ArrangeBy[[Column(0, \"b\")]]-errors"
  235. "Dataflow: materialize.public.i4_primary_idx" "ArrangeBy[[Column(0, \"c\")]]"
  236. "Dataflow: materialize.public.i4_primary_idx" "ArrangeBy[[Column(0, \"c\")]]-errors"
  237. "Dataflow: materialize.public.i6_primary_idx" "ArrangeBy[[Column(0, \"c\")]]"
  238. "Dataflow: materialize.public.i6_primary_idx" "ArrangeBy[[Column(0, \"c\")]]-errors"
  239. "Dataflow: materialize.public.i6_primary_idx" "Arranged TopK input"
  240. "Dataflow: materialize.public.i6_primary_idx" "Reduced TopK input"
  241. "Dataflow: materialize.public.i8_primary_idx" "ArrangeBy[[Column(0, \"b\")]]"
  242. "Dataflow: materialize.public.i8_primary_idx" "ArrangeBy[[Column(0, \"b\")]]-errors"
  243. "Dataflow: materialize.public.i9_primary_idx" "ArrangeBy[[Column(0, \"b\")]]"
  244. "Dataflow: materialize.public.i9_primary_idx" "ArrangeBy[[Column(0, \"b\")]]-errors"
  245. "Dataflow: materialize.public.monotonic_fused" "ArrangeMonotonic [val: empty]"
  246. "Dataflow: materialize.public.monotonic_fused" ReduceMonotonic
  247. "Dataflow: materialize.public.monotonic_max" "ArrangeMonotonic [val: empty]"
  248. "Dataflow: materialize.public.monotonic_max" ReduceMonotonic
  249. "Dataflow: materialize.public.monotonic_min" "ArrangeMonotonic [val: empty]"
  250. "Dataflow: materialize.public.monotonic_min" ReduceMonotonic
  251. "Dataflow: materialize.public.non_dbz_data_indexed_primary_idx" "ArrangeBy[[Column(0, \"a\"), Column(1, \"b\")]]"
  252. "Dataflow: materialize.public.non_dbz_data_indexed_primary_idx" "ArrangeBy[[Column(0, \"a\"), Column(1, \"b\")]]-errors"