union_cancel.slt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. simple conn=mz_system,user=mz_system
  10. ALTER SYSTEM SET unsafe_enable_table_keys = true
  11. ----
  12. COMPLETE 0
  13. mode cockroach
  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 (4, 5), (5, 5), (5, 5), (null, null)
  26. query T multiline
  27. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1
  28. ----
  29. Explained Query:
  30. ReadStorage materialize.public.t1 // { arity: 2 }
  31. Source materialize.public.t1
  32. Target cluster: quickstart
  33. EOF
  34. query II
  35. SELECT * FROM t1
  36. ----
  37. 1 1
  38. 2 3
  39. 4 5
  40. query T multiline
  41. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT a1.* FROM t1 AS a1 LEFT JOIN t1 AS a2 ON (a1.key = a2.key)
  42. ----
  43. Explained Query:
  44. ReadStorage materialize.public.t1 // { arity: 2 }
  45. Source materialize.public.t1
  46. Target cluster: quickstart
  47. EOF
  48. query II
  49. SELECT a1.* FROM t1 AS a1 LEFT JOIN t1 AS a2 ON (a1.key = a2.key)
  50. ----
  51. 1 1
  52. 2 3
  53. 4 5
  54. query T multiline
  55. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1 LEFT JOIN t1 AS a2 ON (a1.key = a2.key) WHERE a1.nokey = 1
  56. ----
  57. Explained Query:
  58. Project (#0{key}, #1{nokey}, #0{key}, #1{nokey}) // { arity: 4 }
  59. Filter (#1{nokey} = 1) // { arity: 2 }
  60. ReadStorage materialize.public.t1 // { arity: 2 }
  61. Source materialize.public.t1
  62. filter=((#1{nokey} = 1))
  63. Target cluster: quickstart
  64. EOF
  65. query IIII
  66. SELECT * FROM t1 AS a1 LEFT JOIN t1 AS a2 ON (a1.key = a2.key) WHERE a1.nokey = 1
  67. ----
  68. 1 1 1 1
  69. query T multiline
  70. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 EXCEPT ALL SELECT * FROM t1
  71. ----
  72. Explained Query (fast path):
  73. Constant <empty>
  74. Target cluster: quickstart
  75. EOF
  76. query II
  77. SELECT * FROM t1 EXCEPT ALL SELECT * FROM t1
  78. ----
  79. query T multiline
  80. 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 t1
  81. ----
  82. Explained Query:
  83. ReadStorage materialize.public.t1 // { arity: 2 }
  84. Source materialize.public.t1
  85. Target cluster: quickstart
  86. EOF
  87. query II
  88. SELECT * FROM t1 UNION ALL SELECT * FROM t1 EXCEPT ALL SELECT * FROM t1
  89. ----
  90. 1 1
  91. 2 3
  92. 4 5
  93. query T multiline
  94. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 EXCEPT ALL SELECT * FROM t1 UNION ALL SELECT * FROM t1
  95. ----
  96. Explained Query:
  97. ReadStorage materialize.public.t1 // { arity: 2 }
  98. Source materialize.public.t1
  99. Target cluster: quickstart
  100. EOF
  101. query II
  102. SELECT * FROM t1 EXCEPT ALL SELECT * FROM t1 UNION ALL SELECT * FROM t1
  103. ----
  104. 1 1
  105. 2 3
  106. 4 5
  107. query T multiline
  108. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 UNION ALL SELECT * FROM t2 EXCEPT ALL SELECT * FROM t1
  109. ----
  110. Explained Query:
  111. ReadStorage materialize.public.t2 // { arity: 2 }
  112. Source materialize.public.t2
  113. Target cluster: quickstart
  114. EOF
  115. query II
  116. SELECT * FROM t1 UNION ALL SELECT * FROM t2 EXCEPT ALL SELECT * FROM t1
  117. ----
  118. 2 3
  119. 5 5
  120. query T multiline
  121. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t2 UNION ALL SELECT * FROM t1 EXCEPT ALL SELECT * FROM t1
  122. ----
  123. Explained Query:
  124. ReadStorage materialize.public.t2 // { arity: 2 }
  125. Source materialize.public.t2
  126. Target cluster: quickstart
  127. EOF
  128. query II
  129. SELECT * FROM t2 UNION ALL SELECT * FROM t1 EXCEPT ALL SELECT * FROM t1
  130. ----
  131. 2 3
  132. 5 5
  133. query T multiline
  134. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t2 EXCEPT ALL SELECT * FROM t1 UNION ALL SELECT * FROM t1
  135. ----
  136. Explained Query:
  137. Union // { arity: 2 }
  138. Threshold // { arity: 2 }
  139. Union // { arity: 2 }
  140. ReadStorage materialize.public.t2 // { arity: 2 }
  141. Negate // { arity: 2 }
  142. ReadStorage materialize.public.t1 // { arity: 2 }
  143. ReadStorage materialize.public.t1 // { arity: 2 }
  144. Source materialize.public.t1
  145. Source materialize.public.t2
  146. Target cluster: quickstart
  147. EOF
  148. query II
  149. SELECT * FROM t2 EXCEPT ALL SELECT * FROM t1 UNION ALL SELECT * FROM t1
  150. ----
  151. 1 1
  152. 2 3
  153. 4 5
  154. 5 5
  155. query T multiline
  156. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  157. WITH t3_with_key AS (select f1 as key, sum(f2) as nokey from t3 group by f1)
  158. SELECT a1.* FROM t3_with_key AS a1 LEFT JOIN t3_with_key AS a2 ON (a1.key = a2.key)
  159. ----
  160. Explained Query:
  161. Reduce group_by=[#0{f1}] aggregates=[sum(#1{f2})] // { arity: 2 }
  162. ReadStorage materialize.public.t3 // { arity: 2 }
  163. Source materialize.public.t3
  164. Target cluster: quickstart
  165. EOF
  166. query II rowsort
  167. WITH t3_with_key AS (select f1 as key, sum(f2) as nokey from t3 group by f1)
  168. SELECT a1.* FROM t3_with_key AS a1 LEFT JOIN t3_with_key AS a2 ON (a1.key = a2.key)
  169. ----
  170. NULL NULL
  171. 4 5
  172. 5 10
  173. query T multiline
  174. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  175. WITH t3_with_key AS (select f1 as key, sum(f2) as nokey from t3 group by f1)
  176. SELECT a1.* FROM t3 AS a1 LEFT JOIN t3_with_key AS a2 ON (a1.f1 = a2.key);
  177. ----
  178. Explained Query:
  179. ReadStorage materialize.public.t3 // { arity: 2 }
  180. Source materialize.public.t3
  181. Target cluster: quickstart
  182. EOF
  183. query II rowsort
  184. WITH t3_with_key AS (select f1 as key, sum(f2) as nokey from t3 group by f1)
  185. SELECT a1.* FROM t3 AS a1 LEFT JOIN t3_with_key AS a2 ON (a1.f1 = a2.key)
  186. ----
  187. NULL NULL
  188. 4 5
  189. 5 5
  190. 5 5
  191. query T multiline
  192. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  193. WITH t3_with_key AS (select f1 as key, sum(f2) as nokey from t3 group by f1)
  194. SELECT a1.* FROM t3 AS a1 LEFT JOIN t3_with_key AS a2 ON (a1.f1 = a2.key or (a1.f1 is null and a2.key is null));
  195. ----
  196. Explained Query:
  197. ReadStorage materialize.public.t3 // { arity: 2 }
  198. Source materialize.public.t3
  199. Target cluster: quickstart
  200. EOF
  201. query II rowsort
  202. WITH t3_with_key AS (select f1 as key, sum(f2) as nokey from t3 group by f1)
  203. SELECT a1.* FROM t3 AS a1 LEFT JOIN t3_with_key AS a2 ON (a1.f1 = a2.key or (a1.f1 is null and a2.key is null));
  204. ----
  205. NULL NULL
  206. 4 5
  207. 5 5
  208. 5 5
  209. statement ok
  210. CREATE TABLE init(n int, m int, s string);
  211. # Union branch cancellation should happen inside WMR.
  212. query T multiline
  213. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  214. WITH MUTUALLY RECURSIVE
  215. c0(n int) AS (
  216. (SELECT n FROM init)
  217. UNION ALL
  218. (SELECT * FROM c2)
  219. ),
  220. c1(n int) AS (
  221. (SELECT n+n FROM c0)
  222. UNION ALL
  223. ((SELECT n+3 FROM c0) EXCEPT ALL (SELECT n+3 FROM c0))
  224. ),
  225. c2(n int) AS (
  226. (SELECT * FROM c0)
  227. UNION ALL
  228. (SELECT * FROM c1)
  229. UNION ALL
  230. (SELECT * FROM c1)
  231. )
  232. SELECT * FROM c2;
  233. ----
  234. Explained Query:
  235. With Mutually Recursive
  236. cte l0 =
  237. Union
  238. Project (#0{n})
  239. ReadStorage materialize.public.init
  240. Get l2
  241. cte l1 =
  242. Project (#1)
  243. Map ((#0{n} + #0{n}))
  244. Get l0
  245. cte l2 =
  246. Union
  247. Get l0
  248. Get l1
  249. Get l1
  250. Return
  251. Get l2
  252. Source materialize.public.init
  253. Target cluster: quickstart
  254. EOF