union.slt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. simple conn=mz_system,user=mz_system
  11. ALTER SYSTEM SET unsafe_enable_table_keys = true
  12. ----
  13. COMPLETE 0
  14. statement ok
  15. CREATE TABLE t1 (key integer PRIMARY KEY, nokey integer)
  16. statement ok
  17. INSERT INTO t1 VALUES (1, 1), (2, 3), (4, 5);
  18. statement ok
  19. CREATE TABLE t2 (key integer PRIMARY KEY, nokey integer)
  20. statement ok
  21. INSERT INTO t2 VALUES (2, 3), (5, 5);
  22. statement ok
  23. CREATE TABLE t3 (f1 INTEGER, f2 INTEGER);
  24. statement ok
  25. INSERT INTO t3 VALUES (2, 3), (5, 5), (5, 5), (6, 1)
  26. # Test that nested unions are fused into a single Union operator
  27. query T multiline
  28. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR (SELECT * FROM t1 UNION ALL SELECT * FROM t1) UNION ALL (SELECT * FROM t2 UNION ALL SELECT * FROM t2);
  29. ----
  30. Explained Query:
  31. Union // { arity: 2 }
  32. ReadStorage materialize.public.t1 // { arity: 2 }
  33. ReadStorage materialize.public.t1 // { arity: 2 }
  34. ReadStorage materialize.public.t2 // { arity: 2 }
  35. ReadStorage materialize.public.t2 // { arity: 2 }
  36. Source materialize.public.t1
  37. Source materialize.public.t2
  38. Target cluster: quickstart
  39. EOF
  40. query II
  41. (SELECT * FROM t1 UNION ALL SELECT * FROM t1) UNION ALL (SELECT * FROM t2 UNION ALL SELECT * FROM t2);
  42. ----
  43. 1 1
  44. 1 1
  45. 2 3
  46. 2 3
  47. 2 3
  48. 2 3
  49. 4 5
  50. 4 5
  51. 5 5
  52. 5 5
  53. # Test that nested negated unions are merged into the parent Union operator by pushing the Negate into their branches
  54. query T multiline
  55. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR (SELECT * FROM t1 UNION ALL SELECT * FROM t1) EXCEPT ALL (SELECT * FROM t2 UNION ALL SELECT * FROM t2);
  56. ----
  57. Explained Query:
  58. With
  59. cte l0 =
  60. Negate // { arity: 2 }
  61. ReadStorage materialize.public.t2 // { arity: 2 }
  62. Return // { arity: 2 }
  63. Threshold // { arity: 2 }
  64. Union // { arity: 2 }
  65. ReadStorage materialize.public.t1 // { arity: 2 }
  66. ReadStorage materialize.public.t1 // { arity: 2 }
  67. Get l0 // { arity: 2 }
  68. Get l0 // { arity: 2 }
  69. Source materialize.public.t1
  70. Source materialize.public.t2
  71. Target cluster: quickstart
  72. EOF
  73. query II
  74. (SELECT * FROM t1 UNION ALL SELECT * FROM t1) EXCEPT ALL (SELECT * FROM t2 UNION ALL SELECT * FROM t2);
  75. ----
  76. 1 1
  77. 1 1
  78. 4 5
  79. 4 5
  80. query T multiline
  81. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t2 EXCEPT ALL SELECT * FROM t1 INTERSECT ALL SELECT * FROM t3;
  82. ----
  83. Explained Query:
  84. Threshold // { arity: 2 }
  85. Union // { arity: 2 }
  86. ReadStorage materialize.public.t2 // { arity: 2 }
  87. Negate // { arity: 2 }
  88. ReadStorage materialize.public.t1 // { arity: 2 }
  89. Threshold // { arity: 2 }
  90. Union // { arity: 2 }
  91. ReadStorage materialize.public.t1 // { arity: 2 }
  92. Negate // { arity: 2 }
  93. ReadStorage materialize.public.t3 // { arity: 2 }
  94. Source materialize.public.t1
  95. Source materialize.public.t2
  96. Source materialize.public.t3
  97. Target cluster: quickstart
  98. EOF
  99. query II
  100. SELECT * FROM t2 EXCEPT ALL SELECT * FROM t1 INTERSECT ALL SELECT * FROM t3;
  101. ----
  102. 5 5
  103. query T multiline
  104. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t2 EXCEPT ALL (SELECT * FROM t1 INTERSECT ALL SELECT f1, null::int FROM t3);
  105. ----
  106. Explained Query:
  107. Threshold // { arity: 2 }
  108. Union // { arity: 2 }
  109. ReadStorage materialize.public.t2 // { arity: 2 }
  110. Negate // { arity: 2 }
  111. ReadStorage materialize.public.t1 // { arity: 2 }
  112. Threshold // { arity: 2 }
  113. Union // { arity: 2 }
  114. ReadStorage materialize.public.t1 // { arity: 2 }
  115. Negate // { arity: 2 }
  116. Project (#0{f1}, #2) // { arity: 2 }
  117. Map (null) // { arity: 3 }
  118. ReadStorage materialize.public.t3 // { arity: 2 }
  119. Source materialize.public.t1
  120. Source materialize.public.t2
  121. Source materialize.public.t3
  122. Target cluster: quickstart
  123. EOF
  124. query II
  125. SELECT * FROM t2 EXCEPT ALL (SELECT * FROM t1 INTERSECT ALL SELECT f1, null::int FROM t3);
  126. ----
  127. 2 3
  128. 5 5
  129. query T multiline
  130. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT a1.* FROM t3 AS a1 LEFT JOIN t2 AS a2 ON (a1.f1 = a2.nokey);
  131. ----
  132. Explained Query:
  133. With
  134. cte l0 =
  135. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  136. Filter (#0{f1}) IS NOT NULL // { arity: 2 }
  137. ReadStorage materialize.public.t3 // { arity: 2 }
  138. cte l1 =
  139. Project (#0{f1}, #1{f2}) // { arity: 2 }
  140. Join on=(#0{f1} = #2{nokey}) type=differential // { arity: 3 }
  141. implementation
  142. %0:l0[#0{f1}]K » %1:t2[#0{nokey}]K
  143. Get l0 // { arity: 2 }
  144. ArrangeBy keys=[[#0{nokey}]] // { arity: 1 }
  145. Project (#1{nokey}) // { arity: 1 }
  146. Filter (#1{nokey}) IS NOT NULL // { arity: 2 }
  147. ReadStorage materialize.public.t2 // { arity: 2 }
  148. Return // { arity: 2 }
  149. Union // { arity: 2 }
  150. Negate // { arity: 2 }
  151. Project (#0{f1}, #1{f2}) // { arity: 2 }
  152. Join on=(#0{f1} = #2{f1}) type=differential // { arity: 3 }
  153. implementation
  154. %1[#0]UKA » %0:l0[#0{f1}]K
  155. Get l0 // { arity: 2 }
  156. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  157. Distinct project=[#0{f1}] // { arity: 1 }
  158. Project (#0{f1}) // { arity: 1 }
  159. Get l1 // { arity: 2 }
  160. ReadStorage materialize.public.t3 // { arity: 2 }
  161. Get l1 // { arity: 2 }
  162. Source materialize.public.t2
  163. filter=((#1{nokey}) IS NOT NULL)
  164. Source materialize.public.t3
  165. Target cluster: quickstart
  166. EOF
  167. query II rowsort
  168. SELECT a1.* FROM t3 AS a1 LEFT JOIN t2 AS a2 ON (a1.f1 = a2.nokey);
  169. ----
  170. 2 3
  171. 5 5
  172. 5 5
  173. 6 1