reduction_pushdown.slt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 x (f0 int4, f1 string);
  11. statement ok
  12. CREATE TABLE y (f0 int2, f1 string);
  13. # `ReductionPushdown` pushes the Distinct from after the Join into both join inputs.
  14. query T multiline
  15. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  16. SELECT DISTINCT *
  17. FROM x, y
  18. WHERE x.f1 = y.f1
  19. ----
  20. Explained Query:
  21. Project (#0{f0}, #1{f1}, #3{f0}, #1{f1}) // { arity: 4 }
  22. Join on=(#1{f1} = #2{f1}) type=differential // { arity: 4 }
  23. implementation
  24. %0[#1]K » %1[#0]K
  25. ArrangeBy keys=[[#1{f1}]] // { arity: 2 }
  26. Distinct project=[#0{f0}, #1{f1}] // { arity: 2 }
  27. Filter (#1{f1}) IS NOT NULL // { arity: 2 }
  28. ReadStorage materialize.public.x // { arity: 2 }
  29. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  30. Distinct project=[#1{f1}, #0{f0}] // { arity: 2 }
  31. Filter (#1{f1}) IS NOT NULL // { arity: 2 }
  32. ReadStorage materialize.public.y // { arity: 2 }
  33. Source materialize.public.x
  34. filter=((#1{f1}) IS NOT NULL)
  35. Source materialize.public.y
  36. filter=((#1{f1}) IS NOT NULL)
  37. Target cluster: quickstart
  38. EOF
  39. ## -------------------- Tests for WITH MUTUALLY RECURSIVE --------------------
  40. # `ReductionPushdown` pushes the Distinct from after the Join into both join inputs.
  41. query T multiline
  42. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  43. WITH MUTUALLY RECURSIVE
  44. c0(f0 int4, f1 string, f2 int2, f3 string) AS (
  45. (SELECT DISTINCT *
  46. FROM x, y
  47. WHERE x.f1 = y.f1)
  48. UNION ALL
  49. (SELECT *
  50. FROM c0)
  51. )
  52. SELECT * FROM c0;
  53. ----
  54. Explained Query:
  55. With Mutually Recursive
  56. cte l0 =
  57. Union // { arity: 4 }
  58. Project (#0{f0}, #1{f1}, #3{f0}, #1{f1}) // { arity: 4 }
  59. Join on=(#1{f1} = #2{f1}) type=differential // { arity: 4 }
  60. implementation
  61. %0[#1]K » %1[#0]K
  62. ArrangeBy keys=[[#1{f1}]] // { arity: 2 }
  63. Distinct project=[#0{f0}, #1{f1}] // { arity: 2 }
  64. Filter (#1{f1}) IS NOT NULL // { arity: 2 }
  65. ReadStorage materialize.public.x // { arity: 2 }
  66. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  67. Distinct project=[#1{f1}, #0{f0}] // { arity: 2 }
  68. Filter (#1{f1}) IS NOT NULL // { arity: 2 }
  69. ReadStorage materialize.public.y // { arity: 2 }
  70. Get l0 // { arity: 4 }
  71. Return // { arity: 4 }
  72. Get l0 // { arity: 4 }
  73. Source materialize.public.x
  74. filter=((#1{f1}) IS NOT NULL)
  75. Source materialize.public.y
  76. filter=((#1{f1}) IS NOT NULL)
  77. Target cluster: quickstart
  78. EOF
  79. ## Regression test for https://github.com/MaterializeInc/database-issues/issues/8146
  80. statement ok
  81. CREATE TABLE t1 (f1 DOUBLE PRECISION, f2 DOUBLE PRECISION NOT NULL);
  82. statement ok
  83. CREATE TABLE t2 (f1 DOUBLE PRECISION, f2 DOUBLE PRECISION NOT NULL);
  84. statement ok
  85. CREATE TABLE t3 (f1 DOUBLE PRECISION, f2 DOUBLE PRECISION NOT NULL);
  86. statement ok
  87. CREATE MATERIALIZED VIEW pk1 AS SELECT DISTINCT ON (f1) f1 , f2 FROM t1 WHERE f1 IS NOT NULL AND f2 IS NOT NULL;
  88. statement ok
  89. CREATE MATERIALIZED VIEW pk2 AS SELECT DISTINCT ON (f1) f1 , f2 FROM t2 WHERE f1 IS NOT NULL AND f2 IS NOT NULL;
  90. statement ok
  91. CREATE MATERIALIZED VIEW pk3 AS SELECT DISTINCT ON (f1) f1 , f2 FROM t3 WHERE f1 IS NOT NULL AND f2 IS NOT NULL;
  92. query T multiline
  93. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  94. SELECT FROM(SELECT) a JOIN(t2 JOIN pk1 ON NULL) ON(NULL) UNION SELECT FROM(SELECT AVG(a.f2) f2 FROM t2 a RIGHT JOIN t2 USING(f1) WHERE a.f2 IS NULL) a WHERE NULLIF(a.f2, 1) NOT IN(SELECT c FROM(SELECT 0 c FROM pk1 JOIN(SELECT f2 FROM pk1) b ON NULLIF(b.f2, b.f2) < b.f2) d);
  95. ----
  96. Explained Query:
  97. Distinct project=[]
  98. Union
  99. Negate
  100. Distinct project=[]
  101. Project ()
  102. Filter (case when (#1{f2} = #1{f2}) then null else #1{f2} end < #1{f2})
  103. ReadStorage materialize.public.pk1
  104. Constant
  105. - ()
  106. Source materialize.public.t2
  107. Source materialize.public.pk1
  108. filter=((case when (#1{f2} = #1{f2}) then null else #1{f2} end < #1{f2}))
  109. Target cluster: quickstart
  110. EOF
  111. ## Regression test for https://github.com/MaterializeInc/database-issues/issues/9013
  112. query RRR
  113. SELECT
  114. (a1.f1) AS c1,
  115. (a2.f1) AS c2,
  116. (a1.f2) AS c3
  117. FROM (
  118. SELECT
  119. a1.f2 AS f1,
  120. a1.f2 + a1.f1 AS f2
  121. FROM pk1 AS a1
  122. ORDER BY 1, 2
  123. LIMIT 1 OFFSET 1
  124. ) AS a1
  125. LEFT JOIN (
  126. SELECT a1.f2 AS f1
  127. FROM t2 AS a1
  128. ORDER BY 1
  129. LIMIT 1 OFFSET 7
  130. ) AS a2
  131. ON (a1.f2 != 4)
  132. WHERE
  133. a2.f1 IS NULL
  134. AND a1.f1 + a1.f2 = NULLIF(a1.f1, a2.f1)
  135. GROUP BY 1, 2, 3;
  136. ----
  137. query T multiline
  138. EXPLAIN OPTIMIZED PLAN WITH (arity, humanized expressions) AS VERBOSE TEXT FOR
  139. SELECT
  140. (a1.f1) AS c1,
  141. (a2.f1) AS c2,
  142. (a1.f2) AS c3
  143. FROM (
  144. SELECT
  145. a1.f2 AS f1,
  146. a1.f2 + a1.f1 AS f2
  147. FROM pk1 AS a1
  148. ORDER BY 1, 2
  149. LIMIT 1 OFFSET 1
  150. ) AS a1
  151. LEFT JOIN (
  152. SELECT a1.f2 AS f1
  153. FROM t2 AS a1
  154. ORDER BY 1
  155. LIMIT 1 OFFSET 7
  156. ) AS a2
  157. ON (a1.f2 != 4)
  158. WHERE
  159. a2.f1 IS NULL
  160. AND a1.f1 + a1.f2 = NULLIF(a1.f1, a2.f1)
  161. GROUP BY 1, 2, 3;
  162. ----
  163. Explained Query:
  164. With
  165. cte l0 =
  166. Filter (#0{f2} = (#0{f2} + #1{f2})) // { arity: 2 }
  167. TopK order_by=[#0{f2} asc nulls_last, #1 asc nulls_last] limit=1 offset=1 // { arity: 2 }
  168. Project (#1{f2}, #2) // { arity: 2 }
  169. Map ((#1{f2} + #0{f1})) // { arity: 3 }
  170. ReadStorage materialize.public.pk1 // { arity: 2 }
  171. Return // { arity: 3 }
  172. Project (#0{f2}, #3, #2) // { arity: 3 }
  173. Map (null) // { arity: 4 }
  174. Distinct project=[#0{f2}, (#0{f2} + #1{f2}), #1] // { arity: 3 }
  175. Union // { arity: 2 }
  176. Negate // { arity: 2 }
  177. CrossJoin type=differential // { arity: 2 }
  178. ArrangeBy keys=[[]] // { arity: 2 }
  179. Filter (#1{f2} != 4) // { arity: 2 }
  180. Get l0 // { arity: 2 }
  181. ArrangeBy keys=[[]] // { arity: 0 }
  182. Project () // { arity: 0 }
  183. TopK order_by=[#0{f2} asc nulls_last] limit=1 offset=7 // { arity: 1 }
  184. Project (#1{f2}) // { arity: 1 }
  185. ReadStorage materialize.public.t2 // { arity: 2 }
  186. Get l0 // { arity: 2 }
  187. Source materialize.public.t2
  188. Source materialize.public.pk1
  189. Target cluster: quickstart
  190. EOF