mir_unique_keys.slt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. statement ok
  10. CREATE TABLE t (
  11. a int,
  12. b int
  13. )
  14. statement ok
  15. CREATE TABLE u (
  16. c int,
  17. d int
  18. )
  19. # A global aggregation has a key []
  20. query T multiline
  21. EXPLAIN OPTIMIZED PLAN WITH(keys, humanized expressions) AS VERBOSE TEXT FOR SELECT sum(a) FROM t
  22. ----
  23. Explained Query:
  24. With
  25. cte l0 =
  26. Reduce aggregates=[sum(#0{a})] // { keys: "([])" }
  27. Project (#0{a}) // { keys: "()" }
  28. ReadStorage materialize.public.t // { keys: "()" }
  29. Return // { keys: "([])" }
  30. Union // { keys: "([])" }
  31. Get l0 // { keys: "([])" }
  32. Map (null) // { keys: "()" }
  33. Union // { keys: "()" }
  34. Negate // { keys: "()" }
  35. Project () // { keys: "([])" }
  36. Get l0 // { keys: "([])" }
  37. Constant // { keys: "([])" }
  38. - ()
  39. Source materialize.public.t
  40. Target cluster: quickstart
  41. EOF
  42. # all columns that have unique values are unique keys of an ok constant
  43. query T multiline
  44. EXPLAIN OPTIMIZED PLAN WITH(keys, no fast path, humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM (VALUES (1, 2, 3), (4, 2, 4));
  45. ----
  46. Explained Query:
  47. Constant // { keys: "([0], [2])" }
  48. - (1, 2, 3)
  49. - (4, 2, 4)
  50. Target cluster: quickstart
  51. EOF
  52. statement ok
  53. CREATE VIEW v as SELECT c, d FROM u GROUP BY c, d;
  54. statement ok
  55. CREATE DEFAULT INDEX on v;
  56. # join + unique key sets being split by a predicate `<column1> = <column2>`
  57. query T multiline
  58. EXPLAIN OPTIMIZED PLAN WITH(keys, humanized expressions) AS VERBOSE TEXT FOR
  59. SELECT * FROM
  60. (SELECT sum(a) as a, b FROM t GROUP BY b) t
  61. INNER JOIN
  62. (SELECT * FROM v WHERE c = d) u
  63. ON t.b = u.d;
  64. ----
  65. Explained Query:
  66. Project (#1{sum_a}, #0{b}, #2{c}, #0{b}) // { keys: "([1])" }
  67. Join on=(#0{b} = #3{d}) type=differential // { keys: "([0])" }
  68. ArrangeBy keys=[[#0{b}]] // { keys: "([0])" }
  69. Reduce group_by=[#1{b}] aggregates=[sum(#0{a})] // { keys: "([0])" }
  70. Filter (#1{b}) IS NOT NULL // { keys: "()" }
  71. ReadStorage materialize.public.t // { keys: "()" }
  72. ArrangeBy keys=[[#1{d}]] // { keys: "([0], [1])" }
  73. Filter (#0{c} = #1{d}) // { keys: "([0], [1])" }
  74. ReadIndex on=v v_primary_idx=[*** full scan ***] // { keys: "([0, 1])" }
  75. Source materialize.public.t
  76. filter=((#1{b}) IS NOT NULL)
  77. Used Indexes:
  78. - materialize.public.v_primary_idx (*** full scan ***)
  79. Target cluster: quickstart
  80. EOF
  81. # topk limit = 1 + filter column = constant
  82. query T multiline
  83. EXPLAIN OPTIMIZED PLAN WITH(keys, humanized expressions) AS VERBOSE TEXT FOR
  84. (SELECT a, c::double FROM
  85. (SELECT DISTINCT c FROM u) grp,
  86. LATERAL (
  87. SELECT a FROM t
  88. WHERE b = grp.c
  89. LIMIT 1
  90. ))
  91. EXCEPT ALL
  92. (SELECT c, d::double FROM v WHERE c = 1)
  93. ----
  94. Explained Query:
  95. Threshold // { keys: "()" }
  96. Union // { keys: "()" }
  97. Project (#1{a}, #2) // { keys: "([1])" }
  98. Map (integer_to_double(#0{c})) // { keys: "([0], [2])" }
  99. TopK group_by=[#0{c}] limit=1 // { keys: "([0])" }
  100. Project (#0{c}, #1{a}) // { keys: "()" }
  101. Join on=(#0{c} = #2{b}) type=differential // { keys: "()" }
  102. ArrangeBy keys=[[#0{c}]] // { keys: "([0])" }
  103. Distinct project=[#0{c}] // { keys: "([0])" }
  104. Project (#0{c}) // { keys: "()" }
  105. Filter (#0{c}) IS NOT NULL // { keys: "()" }
  106. ReadStorage materialize.public.u // { keys: "()" }
  107. ArrangeBy keys=[[#1{b}]] // { keys: "()" }
  108. Filter (#1{b}) IS NOT NULL // { keys: "()" }
  109. ReadStorage materialize.public.t // { keys: "()" }
  110. Negate // { keys: "()" }
  111. Project (#0{c}, #2) // { keys: "([1])" }
  112. Filter (#0{c} = 1) // { keys: "([1], [2])" }
  113. Map (integer_to_double(#1{d})) // { keys: "([0, 1], [0, 2])" }
  114. ReadIndex on=v v_primary_idx=[*** full scan ***] // { keys: "([0, 1])" }
  115. Source materialize.public.t
  116. filter=((#1{b}) IS NOT NULL)
  117. Source materialize.public.u
  118. filter=((#0{c}) IS NOT NULL)
  119. Used Indexes:
  120. - materialize.public.v_primary_idx (*** full scan ***)
  121. Target cluster: quickstart
  122. Notices:
  123. - Notice: Index materialize.public.v_primary_idx on v(c, d) is too wide to use for literal equalities `c = 1`.
  124. Hint: If your literal equalities filter out many rows, create an index whose key exactly matches your literal equalities: (c).
  125. EOF
  126. query T multiline
  127. EXPLAIN OPTIMIZED PLAN WITH(keys, humanized expressions) AS VERBOSE TEXT FOR
  128. SELECT 1 = (Select * FROM generate_series(1, 100000) limit 3)
  129. ----
  130. Explained Query:
  131. With
  132. cte l0 =
  133. TopK limit=3 monotonic // { keys: "()" }
  134. FlatMap generate_series(1, 100000, 1) // { keys: "()" }
  135. Constant // { keys: "([])" }
  136. - ()
  137. cte l1 =
  138. Union // { keys: "()" }
  139. Get l0 // { keys: "()" }
  140. Project (#1) // { keys: "()" }
  141. FlatMap guard_subquery_size(#0{count}) // { keys: "()" }
  142. Reduce aggregates=[count(*)] // { keys: "([])" }
  143. Project () // { keys: "()" }
  144. Get l0 // { keys: "()" }
  145. Return // { keys: "()" }
  146. Project (#1) // { keys: "()" }
  147. Map ((#0 = 1)) // { keys: "()" }
  148. Union // { keys: "()" }
  149. Get l1 // { keys: "()" }
  150. Map (null) // { keys: "()" }
  151. Union // { keys: "()" }
  152. Negate // { keys: "()" }
  153. Distinct project=[] // { keys: "([])" }
  154. Project () // { keys: "()" }
  155. Get l1 // { keys: "()" }
  156. Constant // { keys: "([])" }
  157. - ()
  158. Target cluster: quickstart
  159. EOF