outer_join.slt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. statement ok
  11. CREATE TABLE a(a INTEGER);
  12. statement ok
  13. CREATE TABLE b(b INTEGER);
  14. statement ok
  15. CREATE TABLE c(c INTEGER);
  16. statement ok
  17. INSERT INTO a VALUES (1);
  18. statement ok
  19. INSERT INTO b VALUES (2);
  20. statement ok
  21. INSERT INTO c VALUES (3);
  22. query III rowsort
  23. SELECT * FROM a, b full join c on b = c;
  24. ----
  25. 1 NULL 3
  26. 1 2 NULL
  27. query III
  28. SELECT * FROM a, b right join c on b = c;
  29. ----
  30. 1 NULL 3
  31. query III
  32. SELECT * FROM a, b left join c on b = c;
  33. ----
  34. 1 2 NULL
  35. query III
  36. SELECT * FROM a CROSS JOIN b JOIN LATERAL(SELECT a.a FROM c) x ON TRUE;
  37. ----
  38. 1 2 1
  39. query III
  40. SELECT * FROM a, b FULL JOIN LATERAL(SELECT a.a FROM c) x ON TRUE;
  41. ----
  42. 1 2 1
  43. query III
  44. SELECT * FROM a CROSS JOIN (b FULL JOIN LATERAL(SELECT a.a FROM c) x ON TRUE);
  45. ----
  46. 1 2 1
  47. statement ok
  48. CREATE TABLE t1 (a int, b int);
  49. statement ok
  50. CREATE TABLE t2 (a int, c int);
  51. statement ok
  52. INSERT INTO t1 VALUES (1, 2), (2, 3);
  53. statement ok
  54. INSERT INTO t2 VALUES (2, 4), (5, 7);
  55. query IIII rowsort
  56. SELECT * FROM generate_series(1, 2), LATERAL (SELECT * FROM t1) _ NATURAL RIGHT JOIN t2;
  57. ----
  58. 1 2 3 4
  59. 2 2 3 4
  60. 1 5 NULL 7
  61. 2 5 NULL 7
  62. statement ok
  63. create table left(x int, y int);
  64. statement ok
  65. create table right1(x int, y int);
  66. statement ok
  67. create view right1_keyed(x, y) as select distinct on(x) * from right1;
  68. statement ok
  69. create table right2(x int, y int);
  70. statement ok
  71. insert into left values (0,0);
  72. statement ok
  73. insert into right2 values (0,0);
  74. # `consolidate_output` should be true when there is a negated input to a Union.
  75. query T multiline
  76. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR
  77. select *
  78. from
  79. left
  80. LEFT JOIN right1_keyed ON left.x = right1_keyed.x
  81. LEFT JOIN right2 ON left.x = right2.x;
  82. ----
  83. Explained Query:
  84. With
  85. cte l0 =
  86. TopK::MonotonicTop1 group_by=[#0] must_consolidate
  87. Get::Collection materialize.public.right1
  88. raw=true
  89. cte l1 =
  90. Reduce::Distinct
  91. val_plan
  92. project=()
  93. key_plan=id
  94. Union
  95. Get::Collection materialize.public.left
  96. project=(#0)
  97. raw=true
  98. Constant
  99. - (null)
  100. Return
  101. Join::Delta
  102. plan_path[0]
  103. delta_stage[1]
  104. closure
  105. project=(#0..=#3, #7, #8)
  106. map=((#5) IS NULL, case when #6 then null else #0 end, case when #6 then null else #4 end)
  107. lookup={ relation=2, key=[#0] }
  108. stream={ key=[#0], thinning=(#1..=#3) }
  109. delta_stage[0]
  110. closure
  111. project=(#0, #1, #5, #6)
  112. map=((#3) IS NULL, case when #4 then null else #0 end, case when #4 then null else #2 end)
  113. lookup={ relation=1, key=[#0] }
  114. stream={ key=[#0], thinning=(#1) }
  115. source={ relation=0, key=[#0] }
  116. plan_path[1]
  117. delta_stage[1]
  118. closure
  119. project=(#1, #2, #4, #3, #8, #9)
  120. map=((#6) IS NULL, case when #7 then null else #1 end, case when #7 then null else #5 end)
  121. lookup={ relation=2, key=[#0] }
  122. stream={ key=[#2], thinning=(#0, #1, #3, #4) }
  123. delta_stage[0]
  124. closure
  125. project=(#0, #3, #0, #2, #4)
  126. map=(case when #1 then null else #0 end)
  127. lookup={ relation=0, key=[#0] }
  128. stream={ key=[#0], thinning=(#1, #2) }
  129. initial_closure
  130. project=(#0, #3, #4)
  131. map=((#2) IS NULL, case when #3 then null else #1 end)
  132. source={ relation=1, key=[#0] }
  133. plan_path[2]
  134. delta_stage[1]
  135. closure
  136. project=(#1, #2, #8, #9, #4, #3)
  137. map=((#6) IS NULL, case when #7 then null else #1 end, case when #7 then null else #5 end)
  138. lookup={ relation=1, key=[#0] }
  139. stream={ key=[#2], thinning=(#0, #1, #3, #4) }
  140. delta_stage[0]
  141. closure
  142. project=(#0, #3, #0, #2, #4)
  143. map=(case when #1 then null else #0 end)
  144. lookup={ relation=0, key=[#0] }
  145. stream={ key=[#0], thinning=(#1, #2) }
  146. initial_closure
  147. project=(#0, #3, #4)
  148. map=((#2) IS NULL, case when #3 then null else #1 end)
  149. source={ relation=2, key=[#0] }
  150. ArrangeBy
  151. raw=true
  152. arrangements[0]={ key=[#0], permutation=id, thinning=(#1) }
  153. types=[integer?, integer?]
  154. Get::PassArrangements materialize.public.left
  155. raw=true
  156. ArrangeBy
  157. raw=true
  158. arrangements[0]={ key=[#0], permutation=id, thinning=(#1, #2) }
  159. types=[integer?, integer?, boolean?]
  160. Union
  161. Get::Collection l0
  162. project=(#0..=#2)
  163. map=(true)
  164. raw=true
  165. Mfp
  166. project=(#0..=#2)
  167. map=(null, null)
  168. input_key=#0
  169. Threshold::Basic ensure_arrangement={ key=[#0], permutation=id, thinning=() }
  170. ArrangeBy
  171. raw=false
  172. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  173. types=[integer?]
  174. Union consolidate_output=true
  175. Negate
  176. Get::Collection l0
  177. project=(#0)
  178. raw=true
  179. ArrangeBy
  180. input_key=[#0]
  181. raw=true
  182. Get::PassArrangements l1
  183. raw=false
  184. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  185. ArrangeBy
  186. raw=true
  187. arrangements[0]={ key=[#0], permutation=id, thinning=(#1, #2) }
  188. types=[integer?, integer?, boolean?]
  189. Union
  190. Get::Collection materialize.public.right2
  191. project=(#0..=#2)
  192. map=(true)
  193. raw=true
  194. Mfp
  195. project=(#0..=#2)
  196. map=(null, null)
  197. input_key=#0
  198. Threshold::Basic ensure_arrangement={ key=[#0], permutation=id, thinning=() }
  199. ArrangeBy
  200. raw=false
  201. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  202. types=[integer?]
  203. Union consolidate_output=true
  204. Negate
  205. Get::Collection materialize.public.right2
  206. project=(#0)
  207. raw=true
  208. ArrangeBy
  209. input_key=[#0]
  210. raw=true
  211. Get::PassArrangements l1
  212. raw=false
  213. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  214. Source materialize.public.left
  215. Source materialize.public.right1
  216. filter=((#0) IS NOT NULL)
  217. Source materialize.public.right2
  218. filter=((#0) IS NOT NULL)
  219. Target cluster: quickstart
  220. EOF
  221. query IIIIII
  222. select *
  223. from
  224. left
  225. LEFT JOIN right1_keyed ON left.x = right1_keyed.x
  226. LEFT JOIN right2 ON left.x = right2.x;
  227. ----
  228. 0 0 NULL NULL 0 0