joins.slt 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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 enable_mz_join_core = true;
  12. ----
  13. COMPLETE 0
  14. statement ok
  15. CREATE TABLE l (la int, lb text)
  16. statement ok
  17. CREATE TABLE r (ra int, rb text)
  18. statement ok
  19. INSERT INTO l VALUES (1, 'l1'), (2, 'l2'), (3, 'l3')
  20. statement ok
  21. INSERT INTO r VALUES (1, 'r1'), (3, 'r3'), (4, 'r4')
  22. query ITIT rowsort
  23. SELECT * FROM l LEFT JOIN r ON l.la = r.ra
  24. ----
  25. 1 l1 1 r1
  26. 2 l2 NULL NULL
  27. 3 l3 3 r3
  28. # This test may look the same as the last, but listing out the columns
  29. # explicitly checks for regressions of database-issues#472.
  30. query ITIT rowsort
  31. SELECT l.la, l.lb, r.ra, r.rb FROM l LEFT JOIN r ON l.la = r.ra
  32. ----
  33. 1 l1 1 r1
  34. 2 l2 NULL NULL
  35. 3 l3 3 r3
  36. query ITIT rowsort
  37. SELECT * FROM l RIGHT JOIN r ON l.la = r.ra
  38. ----
  39. NULL NULL 4 r4
  40. 1 l1 1 r1
  41. 3 l3 3 r3
  42. # This test may look the same as the last, but listing out the columns
  43. # explicitly checks for regressions of database-issues#472.
  44. query ITIT rowsort
  45. SELECT l.la, l.lb, r.ra, r.rb FROM l RIGHT JOIN r ON l.la = r.ra
  46. ----
  47. NULL NULL 4 r4
  48. 1 l1 1 r1
  49. 3 l3 3 r3
  50. # Test that columns detected to be equivalent retain the names that the user
  51. # asks for. Protects against regression of database-issues#429.
  52. query II colnames,rowsort
  53. SELECT ra, r.ra FROM l JOIN r ON l.la = r.ra LIMIT 0
  54. ----
  55. ra ra
  56. # Test that columns detected to be equivalent retain the names that the user
  57. # asks for. Protects against regression of database-issues#429.
  58. query ITIT colnames,rowsort
  59. SELECT * FROM l JOIN r ON l.la = r.ra LIMIT 0
  60. ----
  61. la lb ra rb
  62. # Test that columns detected to be equivalent do not interfere with qualified
  63. # wildcard expansion. Protects against regression of database-issues#1389.
  64. query ITIT colnames
  65. SELECT l.*, r.* FROM l JOIN r ON l.la = r.ra LIMIT 0
  66. ----
  67. la lb ra rb
  68. # Like the last test, but with the equality reversed.
  69. query ITIT colnames
  70. SELECT l.*, r.* FROM l JOIN r ON r.ra = l.la LIMIT 0
  71. ----
  72. la lb ra rb
  73. # Test that projections work through join plans with scalars.
  74. query ITT rowsort
  75. SELECT l1.la, l2.lb, l3.lb
  76. FROM l as l1, l as l2, l as l3
  77. WHERE l1.la + 1 = l2.la AND l3.la = l1.la + l2.la
  78. ----
  79. 1 l2 l3
  80. # Test that join plans with scalars work in subqueries
  81. query IT rowsort
  82. SELECT l1.la, l1.lb
  83. FROM l as l1
  84. WHERE l1.la IN (
  85. SELECT l2.la + 1
  86. FROM l AS l2
  87. WHERE l2.la IN (
  88. SELECT l3.la + 1
  89. FROM l as l3
  90. )
  91. )
  92. ----
  93. 3 l3
  94. # Test that scalar expressions are introduced to join plans.
  95. query T multiline
  96. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  97. SELECT l1.la, l2.lb, l3.lb
  98. FROM l as l1, l as l2, l as l3
  99. WHERE l1.la + 1 = l2.la AND l3.la = l1.la + l2.la
  100. ----
  101. Explained Query:
  102. With
  103. cte l0 =
  104. Filter (#0{la}) IS NOT NULL // { arity: 2 }
  105. ReadStorage materialize.public.l // { arity: 2 }
  106. cte l1 =
  107. ArrangeBy keys=[[#0{la}]] // { arity: 2 }
  108. Get l0 // { arity: 2 }
  109. Return // { arity: 3 }
  110. Project (#0{la}, #2{lb}, #4{lb}) // { arity: 3 }
  111. Join on=(#1{la} = (#0{la} + 1) AND #3{la} = (#0{la} + #1{la})) type=delta // { arity: 5 }
  112. implementation
  113. %0:l0 » %1:l1[#0{la}]K » %2:l1[#0{la}]K
  114. %1:l1 » %0:l0[(#0{la} + 1)]K » %2:l1[#0{la}]K
  115. %2:l1 » %0:l0[×] » %1:l1[#0{la}]K
  116. ArrangeBy keys=[[], [(#0{la} + 1)]] // { arity: 1 }
  117. Project (#0{la}) // { arity: 1 }
  118. Get l0 // { arity: 2 }
  119. Get l1 // { arity: 2 }
  120. Get l1 // { arity: 2 }
  121. Source materialize.public.l
  122. filter=((#0{la}) IS NOT NULL)
  123. Target cluster: quickstart
  124. EOF
  125. # Confirm that a +1 can exist in a subquery based join.
  126. # Note that the other +1 is found instead in a filter,
  127. # because subquery planning re-uses the relation it wraps.
  128. # It is perfectly acceptable for this plan to change, esp
  129. # if it improves (i.e. the cross join is removed).
  130. query T multiline
  131. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  132. SELECT l1.la, l1.lb
  133. FROM l as l1
  134. WHERE l1.la IN (
  135. SELECT l2.la + 1
  136. FROM l AS l2
  137. WHERE l2.la IN (
  138. SELECT l3.la + 1
  139. FROM l as l3
  140. )
  141. )
  142. ----
  143. Explained Query:
  144. With
  145. cte l0 =
  146. Project (#0{la}) // { arity: 1 }
  147. ReadStorage materialize.public.l // { arity: 2 }
  148. cte l1 =
  149. CrossJoin type=differential // { arity: 2 }
  150. implementation
  151. %0[×] » %1:l0[×]
  152. ArrangeBy keys=[[]] // { arity: 1 }
  153. Distinct project=[#0{la}] // { arity: 1 }
  154. Get l0 // { arity: 1 }
  155. ArrangeBy keys=[[]] // { arity: 1 }
  156. Get l0 // { arity: 1 }
  157. Return // { arity: 2 }
  158. Project (#0{la}, #1{lb}) // { arity: 2 }
  159. Join on=(#0{la} = #2{la}) type=differential // { arity: 3 }
  160. implementation
  161. %1[#0]UKA » %0:l[#0]K
  162. ArrangeBy keys=[[#0{la}]] // { arity: 2 }
  163. ReadStorage materialize.public.l // { arity: 2 }
  164. ArrangeBy keys=[[#0{la}]] // { arity: 1 }
  165. Distinct project=[#0{la}] // { arity: 1 }
  166. Project (#0{la}) // { arity: 1 }
  167. Join on=(#1{la} = #2{la} = #3) type=delta // { arity: 4 }
  168. implementation
  169. %0:l1 » %1[#0]UKA » %2[#0]UKA
  170. %1 » %2[#0]UKA » %0:l1[#1]Kf
  171. %2 » %1[#0]UKA » %0:l1[#1]Kf
  172. ArrangeBy keys=[[#1{la}]] // { arity: 2 }
  173. Filter (#0{la} = (#1{la} + 1)) // { arity: 2 }
  174. Get l1 // { arity: 2 }
  175. ArrangeBy keys=[[#0{la}]] // { arity: 1 }
  176. Distinct project=[#0{la}] // { arity: 1 }
  177. Project (#1{la}) // { arity: 1 }
  178. Filter (#1{la}) IS NOT NULL // { arity: 2 }
  179. Get l1 // { arity: 2 }
  180. ArrangeBy keys=[[#0]] // { arity: 1 }
  181. Distinct project=[(#0{la} + 1)] // { arity: 1 }
  182. Project (#0{la}) // { arity: 1 }
  183. Filter (#0{la}) IS NOT NULL // { arity: 2 }
  184. ReadStorage materialize.public.l // { arity: 2 }
  185. Source materialize.public.l
  186. Target cluster: quickstart
  187. EOF
  188. # Test that multiplicities in outer joins are preserved.
  189. # Fixed in database-issues#931.
  190. statement ok
  191. CREATE TABLE l2 (la int, lb text)
  192. statement ok
  193. CREATE TABLE r2 (ra int, rb text)
  194. statement ok
  195. INSERT INTO l2 VALUES (1, 'l1'), (2, 'l2'), (2, 'l2'), (3, 'l3')
  196. statement ok
  197. INSERT INTO r2 VALUES (1, 'r1'), (3, 'r3'), (4, 'r4'), (4, 'r4')
  198. query ITIT rowsort
  199. SELECT * FROM l2 LEFT JOIN r2 ON l2.la = r2.ra
  200. ----
  201. 1 l1 1 r1
  202. 2 l2 NULL NULL
  203. 2 l2 NULL NULL
  204. 3 l3 3 r3
  205. query ITIT rowsort
  206. SELECT * FROM l2 RIGHT JOIN r2 ON l2.la = r2.ra
  207. ----
  208. NULL NULL 4 r4
  209. NULL NULL 4 r4
  210. 1 l1 1 r1
  211. 3 l3 3 r3
  212. # Test that outer joins work correctly in correlated subqueries (database-issues#1007)
  213. query IT rowsort
  214. SELECT * FROM l WHERE EXISTS (SELECT * from l as l2 LEFT JOIN r ON l.la = r.ra)
  215. ----
  216. 1 l1
  217. 2 l2
  218. 3 l3
  219. query IT rowsort
  220. SELECT * FROM l WHERE EXISTS (SELECT * from l as l2 LEFT JOIN LATERAL (SELECT * FROM r) r ON l.la = r.ra);
  221. ----
  222. 1 l1
  223. 2 l2
  224. 3 l3
  225. query IT rowsort
  226. SELECT * FROM r WHERE EXISTS (SELECT * from l RIGHT JOIN r as r2 ON l.la = r.ra)
  227. ----
  228. 4 r4
  229. 1 r1
  230. 3 r3
  231. # Regression test for database-issues#1089.
  232. query III colnames
  233. SELECT * FROM
  234. (SELECT 1 AS baz) t1
  235. INNER JOIN (
  236. (SELECT 1 AS foo) t2
  237. INNER JOIN (SELECT 1 AS bar) t3 ON true
  238. ) ON foo = bar;
  239. ----
  240. baz foo bar
  241. 1 1 1
  242. # Test that join planning with constants does not panic due to missing arrangements.
  243. # This test could vanish if we conclude that this is just an anomalous defect is the
  244. # join planning, more than an interesting property to maintain.
  245. query ITIIII rowsort
  246. select * from
  247. l2,
  248. (values (1, 2), (2, 2)) as foo,
  249. (values (2, 3), (3, 3)) as bar
  250. where
  251. foo.column1 = bar.column1
  252. ----
  253. 1 l1 2 2 2 3
  254. 2 l2 2 2 2 3
  255. 2 l2 2 2 2 3
  256. 3 l3 2 2 2 3
  257. # Test that joins with an `(= #x null)` constraint pass records, rather than drop them.
  258. query III rowsort
  259. SELECT * FROM
  260. (((SELECT 1 FROM l2) LEFT JOIN
  261. (SELECT 1 FROM r2) ON false) LEFT JOIN (SELECT 1 FROM r2) ON false);
  262. ----
  263. 1 NULL NULL
  264. 1 NULL NULL
  265. 1 NULL NULL
  266. 1 NULL NULL
  267. # Regression test for database-issues#1218. The gist is that we want to exercise the
  268. # redundant join optimization on a join whose inputs contain projections. This
  269. # turns out to be extremely hard to accomplish because the projection lifting
  270. # optimization is very good at eliminating all but the top-level projection.
  271. # Having this test seems better than not, but it's fragile.
  272. statement ok
  273. CREATE VIEW gh3914 AS VALUES (NULL::int)
  274. query TTI
  275. SELECT * FROM (
  276. (SELECT 'foo')
  277. RIGHT JOIN (
  278. (SELECT true) CROSS JOIN (SELECT 1 FROM gh3914 EXCEPT ALL (SELECT 456 WHERE false))
  279. ) ON true
  280. )
  281. ----
  282. foo true 1
  283. # Test for outer join planning.
  284. query T multiline
  285. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  286. SELECT * FROM l LEFT JOIN r ON l.la = r.ra
  287. ----
  288. Explained Query:
  289. With
  290. cte l0 =
  291. ArrangeBy keys=[[#0{la}]] // { arity: 2 }
  292. Filter (#0{la}) IS NOT NULL // { arity: 2 }
  293. ReadStorage materialize.public.l // { arity: 2 }
  294. cte l1 =
  295. Project (#0{la}, #1{lb}, #3{rb}) // { arity: 3 }
  296. Join on=(#0{la} = #2{ra}) type=differential // { arity: 4 }
  297. implementation
  298. %0:l0[#0{la}]K » %1:r[#0{ra}]K
  299. Get l0 // { arity: 2 }
  300. ArrangeBy keys=[[#0{ra}]] // { arity: 2 }
  301. Filter (#0{ra}) IS NOT NULL // { arity: 2 }
  302. ReadStorage materialize.public.r // { arity: 2 }
  303. Return // { arity: 4 }
  304. Union // { arity: 4 }
  305. Map (null, null) // { arity: 4 }
  306. Union // { arity: 2 }
  307. Negate // { arity: 2 }
  308. Project (#0{la}, #1{lb}) // { arity: 2 }
  309. Join on=(#0{la} = #2{la}) type=differential // { arity: 3 }
  310. implementation
  311. %1[#0]UKA » %0:l0[#0{la}]K
  312. Get l0 // { arity: 2 }
  313. ArrangeBy keys=[[#0{la}]] // { arity: 1 }
  314. Distinct project=[#0{la}] // { arity: 1 }
  315. Project (#0{la}) // { arity: 1 }
  316. Get l1 // { arity: 3 }
  317. ReadStorage materialize.public.l // { arity: 2 }
  318. Project (#0{la}, #1{lb}, #0{la}, #2{rb}) // { arity: 4 }
  319. Get l1 // { arity: 3 }
  320. Source materialize.public.l
  321. Source materialize.public.r
  322. filter=((#0{ra}) IS NOT NULL)
  323. Target cluster: quickstart
  324. EOF
  325. query T multiline
  326. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  327. SELECT * FROM l RIGHT JOIN r ON l.la = r.ra
  328. ----
  329. Explained Query:
  330. With
  331. cte l0 =
  332. ArrangeBy keys=[[#0{ra}]] // { arity: 2 }
  333. Filter (#0{ra}) IS NOT NULL // { arity: 2 }
  334. ReadStorage materialize.public.r // { arity: 2 }
  335. cte l1 =
  336. Project (#0{la}, #1{lb}, #3{rb}) // { arity: 3 }
  337. Join on=(#0{la} = #2{ra}) type=differential // { arity: 4 }
  338. implementation
  339. %0:l[#0{la}]K » %1:l0[#0{ra}]K
  340. ArrangeBy keys=[[#0{la}]] // { arity: 2 }
  341. Filter (#0{la}) IS NOT NULL // { arity: 2 }
  342. ReadStorage materialize.public.l // { arity: 2 }
  343. Get l0 // { arity: 2 }
  344. Return // { arity: 4 }
  345. Union // { arity: 4 }
  346. Project (#2, #3, #0{ra}, #1{rb}) // { arity: 4 }
  347. Map (null, null) // { arity: 4 }
  348. Union // { arity: 2 }
  349. Negate // { arity: 2 }
  350. Project (#0{ra}, #1{rb}) // { arity: 2 }
  351. Join on=(#0{ra} = #2{la}) type=differential // { arity: 3 }
  352. implementation
  353. %1[#0]UKA » %0:l0[#0{ra}]K
  354. Get l0 // { arity: 2 }
  355. ArrangeBy keys=[[#0{la}]] // { arity: 1 }
  356. Distinct project=[#0{la}] // { arity: 1 }
  357. Project (#0{la}) // { arity: 1 }
  358. Get l1 // { arity: 3 }
  359. ReadStorage materialize.public.r // { arity: 2 }
  360. Project (#0{la}, #1{lb}, #0{la}, #2{rb}) // { arity: 4 }
  361. Get l1 // { arity: 3 }
  362. Source materialize.public.l
  363. filter=((#0{la}) IS NOT NULL)
  364. Source materialize.public.r
  365. Target cluster: quickstart
  366. EOF
  367. query T multiline
  368. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  369. SELECT * FROM l FULL JOIN r ON l.la = r.ra
  370. ----
  371. Explained Query:
  372. With
  373. cte l0 =
  374. ArrangeBy keys=[[#0{la}]] // { arity: 2 }
  375. Filter (#0{la}) IS NOT NULL // { arity: 2 }
  376. ReadStorage materialize.public.l // { arity: 2 }
  377. cte l1 =
  378. ArrangeBy keys=[[#0{ra}]] // { arity: 2 }
  379. Filter (#0{ra}) IS NOT NULL // { arity: 2 }
  380. ReadStorage materialize.public.r // { arity: 2 }
  381. cte l2 =
  382. Project (#0{la}, #1{lb}, #3{rb}) // { arity: 3 }
  383. Join on=(#0{la} = #2{ra}) type=differential // { arity: 4 }
  384. implementation
  385. %0:l0[#0{la}]K » %1:l1[#0{ra}]K
  386. Get l0 // { arity: 2 }
  387. Get l1 // { arity: 2 }
  388. cte l3 =
  389. ArrangeBy keys=[[#0{la}]] // { arity: 1 }
  390. Distinct project=[#0{la}] // { arity: 1 }
  391. Project (#0{la}) // { arity: 1 }
  392. Get l2 // { arity: 3 }
  393. Return // { arity: 4 }
  394. Union // { arity: 4 }
  395. Project (#2, #3, #0{ra}, #1{rb}) // { arity: 4 }
  396. Map (null, null) // { arity: 4 }
  397. Union // { arity: 2 }
  398. Negate // { arity: 2 }
  399. Project (#0{ra}, #1{rb}) // { arity: 2 }
  400. Join on=(#0{ra} = #2{la}) type=differential // { arity: 3 }
  401. implementation
  402. %1:l3[#0]UKA » %0:l1[#0{ra}]K
  403. Get l1 // { arity: 2 }
  404. Get l3 // { arity: 1 }
  405. ReadStorage materialize.public.r // { arity: 2 }
  406. Map (null, null) // { arity: 4 }
  407. Union // { arity: 2 }
  408. Negate // { arity: 2 }
  409. Project (#0{la}, #1{lb}) // { arity: 2 }
  410. Join on=(#0{la} = #2{la}) type=differential // { arity: 3 }
  411. implementation
  412. %1:l3[#0]UKA » %0:l0[#0{la}]K
  413. Get l0 // { arity: 2 }
  414. Get l3 // { arity: 1 }
  415. ReadStorage materialize.public.l // { arity: 2 }
  416. Project (#0{la}, #1{lb}, #0{la}, #2{rb}) // { arity: 4 }
  417. Get l2 // { arity: 3 }
  418. Source materialize.public.l
  419. Source materialize.public.r
  420. Target cluster: quickstart
  421. EOF
  422. query ITIT rowsort
  423. SELECT * FROM l INNER JOIN r ON mod(l.la, 2) = mod(r.ra, 2)
  424. ----
  425. 1 l1 1 r1
  426. 1 l1 3 r3
  427. 2 l2 4 r4
  428. 3 l3 1 r1
  429. 3 l3 3 r3
  430. # Test that when both keys are expressions, the join is not planned as a cross
  431. # join. Protects against regression of database-issues#1290.
  432. query T multiline
  433. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM l INNER JOIN r ON mod(l.la, 2) = mod(r.ra, 2)
  434. ----
  435. Explained Query:
  436. Join on=((#0{la} % 2) = (#2{ra} % 2)) type=differential // { arity: 4 }
  437. implementation
  438. %0:l[(#0{la} % 2)]K » %1:r[(#0{ra} % 2)]K
  439. ArrangeBy keys=[[(#0{la} % 2)]] // { arity: 2 }
  440. Filter (#0{la}) IS NOT NULL // { arity: 2 }
  441. ReadStorage materialize.public.l // { arity: 2 }
  442. ArrangeBy keys=[[(#0{ra} % 2)]] // { arity: 2 }
  443. Filter (#0{ra}) IS NOT NULL // { arity: 2 }
  444. ReadStorage materialize.public.r // { arity: 2 }
  445. Source materialize.public.l
  446. filter=((#0{la}) IS NOT NULL)
  447. Source materialize.public.r
  448. filter=((#0{ra}) IS NOT NULL)
  449. Target cluster: quickstart
  450. EOF
  451. # Regression test for database-issues#1355, in which inter-view predicate pushdown would cause
  452. # misoptimizations. Specifically, a predicate that was applied to only one use
  453. # of a view could be incorrectly applied to all uses of that view.
  454. #
  455. # Inter-view (mis-)optimizations don't appear in the output of EXPLAIN, so we
  456. # have to test this rather obliquely.
  457. statement ok
  458. CREATE TABLE t4362 (name text NOT NULL, id int NOT NULL)
  459. statement ok
  460. INSERT INTO t4362 VALUES ('a', 1), ('a', 2), ('b', 3)
  461. statement ok
  462. CREATE VIEW v4362 AS SELECT name, id FROM t4362
  463. query TT
  464. SELECT name, id FROM v4362 WHERE name = 'a'
  465. ----
  466. a 1
  467. a 2
  468. query TT
  469. SELECT name, id FROM v4362 WHERE name = (SELECT name FROM v4362 WHERE id = 1)
  470. ----
  471. a 1
  472. a 2
  473. mode standard
  474. # At the time of writing, for this test to properly validate inter-view
  475. # optimization correctness, exactly one of the `Get materialize.public.t4362`
  476. # nodes must be followed a `Filter` node. If that is not the case in the plan
  477. # below, likely the test is no longer testing for the bug.
  478. query T multiline
  479. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT name, id FROM v4362 WHERE name = (SELECT name FROM v4362 WHERE id = 1)
  480. ----
  481. Explained Query:
  482. With
  483. cte l0 =
  484. Project (#0{name}) // { arity: 1 }
  485. Filter (#1{id} = 1) // { arity: 2 }
  486. ReadStorage materialize.public.t4362 // { arity: 2 }
  487. Return // { arity: 2 }
  488. Project (#0{name}, #1{id}) // { arity: 2 }
  489. Join on=(#0{name} = #2{name}) type=differential // { arity: 3 }
  490. implementation
  491. %0:t4362[#0{name}]K » %1[#0]K
  492. ArrangeBy keys=[[#0{name}]] // { arity: 2 }
  493. ReadStorage materialize.public.t4362 // { arity: 2 }
  494. ArrangeBy keys=[[#0{name}]] // { arity: 1 }
  495. Union // { arity: 1 }
  496. Get l0 // { arity: 1 }
  497. Project (#1) // { arity: 1 }
  498. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  499. Reduce aggregates=[count(*)] // { arity: 1 }
  500. Project () // { arity: 0 }
  501. Get l0 // { arity: 1 }
  502. Source materialize.public.t4362
  503. Target cluster: quickstart
  504. EOF
  505. mode cockroach
  506. # Test implicitly coercible USING columns
  507. statement ok
  508. CREATE TABLE big_l (la int8, lb text)
  509. statement ok
  510. INSERT INTO big_l VALUES (1, 'big_l1'), (3, 'bigl_3'), (5, 'bigl_5')
  511. query ITT
  512. SELECT la, l.lb, big_l.lb FROM l JOIN big_l USING (la)
  513. ----
  514. 1 l1 big_l1
  515. 3 l3 bigl_3
  516. query T multiline
  517. EXPLAIN RAW PLAN FOR SELECT la, l.lb, big_l.lb FROM l JOIN big_l USING (la)
  518. ----
  519. Project (#0, #1, #3)
  520. InnerJoin (integer_to_bigint(#0{la}) = #2{la})
  521. Get materialize.public.l
  522. Get materialize.public.big_l
  523. Target cluster: quickstart
  524. EOF
  525. statement ok
  526. CREATE TABLE join_fail (la date);
  527. query error NATURAL/USING join column "la" types integer and date cannot be matched
  528. SELECT la FROM l JOIN join_fail USING (la)
  529. # test that joins properly handle null keys
  530. statement ok
  531. CREATE TABLE l3 (la int, lb text)
  532. statement ok
  533. CREATE TABLE r3 (ra int, rb text)
  534. statement ok
  535. INSERT INTO l3 VALUES (1, 'l1'), (2, 'l2'), (3, 'l3'), (NULL, 'l4')
  536. statement ok
  537. INSERT INTO r3 VALUES (1, 'r1'), (3, 'r3'), (4, 'r4'), (NULL, 'r5')
  538. query T multiline
  539. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT lb, rb FROM l3 INNER JOIN r3 ON la = ra
  540. ----
  541. Explained Query:
  542. Project (#1{lb}, #3{rb}) // { arity: 2 }
  543. Join on=(#0{la} = #2{ra}) type=differential // { arity: 4 }
  544. implementation
  545. %0:l3[#0{la}]K » %1:r3[#0{ra}]K
  546. ArrangeBy keys=[[#0{la}]] // { arity: 2 }
  547. Filter (#0{la}) IS NOT NULL // { arity: 2 }
  548. ReadStorage materialize.public.l3 // { arity: 2 }
  549. ArrangeBy keys=[[#0{ra}]] // { arity: 2 }
  550. Filter (#0{ra}) IS NOT NULL // { arity: 2 }
  551. ReadStorage materialize.public.r3 // { arity: 2 }
  552. Source materialize.public.l3
  553. filter=((#0{la}) IS NOT NULL)
  554. Source materialize.public.r3
  555. filter=((#0{ra}) IS NOT NULL)
  556. Target cluster: quickstart
  557. EOF
  558. query TT
  559. SELECT lb, rb FROM l3, r3 WHERE la = ra
  560. ----
  561. l1 r1
  562. l3 r3
  563. query T multiline
  564. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT lb, rb FROM l3 INNER JOIN r3 ON la = ra OR (ra IS NULL AND la IS NULL)
  565. ----
  566. Explained Query:
  567. Project (#1{lb}, #3{rb}) // { arity: 2 }
  568. Join on=(#0{la} = #2{ra}) type=differential // { arity: 4 }
  569. implementation
  570. %0:l3[#0{la}]K » %1:r3[#0{ra}]K
  571. ArrangeBy keys=[[#0{la}]] // { arity: 2 }
  572. ReadStorage materialize.public.l3 // { arity: 2 }
  573. ArrangeBy keys=[[#0{ra}]] // { arity: 2 }
  574. ReadStorage materialize.public.r3 // { arity: 2 }
  575. Source materialize.public.l3
  576. Source materialize.public.r3
  577. Target cluster: quickstart
  578. EOF
  579. query TT rowsort
  580. SELECT lb, rb FROM l3 INNER JOIN r3 ON la = ra OR (ra IS NULL AND la IS NULL)
  581. ----
  582. l1 r1
  583. l3 r3
  584. l4 r5
  585. query T multiline
  586. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT lb, rb FROM l3 INNER JOIN r3 ON (la IS NULL AND ra IS NULL) OR la = ra
  587. ----
  588. Explained Query:
  589. Project (#1{lb}, #3{rb}) // { arity: 2 }
  590. Join on=(#0{la} = #2{ra}) type=differential // { arity: 4 }
  591. implementation
  592. %0:l3[#0{la}]K » %1:r3[#0{ra}]K
  593. ArrangeBy keys=[[#0{la}]] // { arity: 2 }
  594. ReadStorage materialize.public.l3 // { arity: 2 }
  595. ArrangeBy keys=[[#0{ra}]] // { arity: 2 }
  596. ReadStorage materialize.public.r3 // { arity: 2 }
  597. Source materialize.public.l3
  598. Source materialize.public.r3
  599. Target cluster: quickstart
  600. EOF
  601. query TT rowsort
  602. SELECT lb, rb FROM l3 INNER JOIN r3 ON (la IS NULL AND ra IS NULL) OR la = ra
  603. ----
  604. l1 r1
  605. l3 r3
  606. l4 r5
  607. # Regression test for https://github.com/MaterializeInc/database-issues/issues/2364
  608. statement ok
  609. CREATE TABLE t1 (f1 int, f2 int);
  610. statement ok
  611. CREATE TABLE t2 (f1 int, f2 int);
  612. statement ok
  613. CREATE TABLE t3 (f1 int, f3 int);
  614. statement ok
  615. CREATE TABLE t4 (f3 int, f4 int);
  616. query T
  617. SELECT f1 FROM t1 JOIN t2 USING (f1);
  618. ----
  619. query error column reference "f2" is ambiguous
  620. SELECT f1, f2 FROM t1 JOIN t2 USING (f1);
  621. query T
  622. SELECT f1, t1.f2 AS f1 FROM t1 JOIN t2 USING (f1);
  623. ----
  624. query error column reference "f1" is ambiguous
  625. SELECT f1, t1.f2 AS f1 FROM t1 JOIN t2 USING (f1) ORDER BY f1;
  626. query error common column name "f2" appears more than once in left table
  627. SELECT * FROM t1 LEFT JOIN t2 USING (f1) RIGHT JOIN t3 USING (f2);
  628. statement ok
  629. INSERT INTO t1 VALUES
  630. (1, 2),
  631. (3, 4);
  632. query TTT
  633. SELECT *
  634. FROM t1
  635. LEFT JOIN t2 USING (f1)
  636. ORDER BY f1;
  637. ----
  638. 1 2 NULL
  639. 3 4 NULL
  640. query TTT
  641. SELECT *
  642. FROM t1
  643. LEFT JOIN t2 USING (f1)
  644. WHERE t2.f1 IS NOT NULL
  645. ORDER BY f1;
  646. ----
  647. statement ok
  648. INSERT INTO t2 VALUES
  649. (3, 4),
  650. (5, 6);
  651. statement ok
  652. INSERT INTO t3 VALUES
  653. (3, 4),
  654. (7, 8);
  655. statement ok
  656. INSERT INTO t4 VALUES
  657. (4, 3),
  658. (9, 10);
  659. # Test join using aliases
  660. # Adapted from: https://github.com/postgres/postgres/blob/master/src/test/regress/sql/join.sql
  661. query T
  662. SELECT * FROM t1 JOIN t2 USING (f1) AS x WHERE t1.f2 = 1;
  663. ----
  664. query III colnames
  665. SELECT * FROM t1 JOIN t2 USING (f1) AS x WHERE t1.f1 = 3;
  666. ----
  667. f1 f2 f2
  668. 3 4 4
  669. query III colnames
  670. SELECT * FROM t1 JOIN t2 USING (f1) AS x;
  671. ----
  672. f1 f2 f2
  673. 3 4 4
  674. query II colnames
  675. SELECT * FROM t1 JOIN t2 USING (f1, f2) AS x;
  676. ----
  677. f1 f2
  678. 3 4
  679. query II colnames
  680. SELECT * FROM (SELECT x.f2 AS f3 FROM t1 JOIN t2 USING (f2) AS x) t5 JOIN t3 USING (f3) AS x;
  681. ----
  682. f3 f1
  683. 4 3
  684. query error table name "x" specified more than once
  685. SELECT * FROM t1 JOIN t2 USING (f1) AS x JOIN t3 USING (f1) AS x;
  686. query IIII colnames
  687. SELECT * FROM t1 JOIN t2 USING (f1) AS x JOIN t3 USING (f1) AS y;
  688. ----
  689. f1 f2 f2 f3
  690. 3 4 4 4
  691. query error column "t1.f1" does not exist
  692. SELECT * FROM (t1 JOIN t2 USING (f1)) AS x WHERE t1.f1 = 1;
  693. query error column "x.f5" does not exist
  694. SELECT * FROM t1 JOIN t2 USING (f1) AS x WHERE x.f5 = 'one';
  695. query error column "x.f4" does not exist
  696. SELECT * FROM (t3 JOIN t4 USING (f3) AS x) AS xx WHERE x.f4 = 1;
  697. query error table name "a1" specified more than once
  698. SELECT * FROM t1 a1 JOIN t2 a2 USING (f1) AS a1
  699. query I colnames
  700. SELECT x.* FROM t3 JOIN t4 USING (f3) AS x WHERE t3.f3 = 4;
  701. ----
  702. f3
  703. 4
  704. query II colnames
  705. SELECT x.* FROM t1 JOIN t2 USING (f1, f2) AS x;
  706. ----
  707. f1 f2
  708. 3 4
  709. query T colnames
  710. SELECT ROW(x.*) FROM t1 JOIN t2 USING (f1) AS x;
  711. ----
  712. row
  713. ("(3)")
  714. statement ok
  715. CREATE VIEW v1 AS SELECT x.* FROM t1 JOIN t2 USING (f1) AS x WHERE x.f1 = 3;
  716. query T colnames
  717. SELECT * FROM v1;
  718. ----
  719. f1
  720. 3
  721. # Ensure the output from `SHOW CREATE VIEW` contains a correctly-formed `AS` part
  722. mode standard
  723. query TT
  724. SHOW CREATE VIEW v1;
  725. ----
  726. materialize.public.v1
  727. CREATE VIEW⏎ materialize.public.v1⏎ AS⏎ SELECT x.*⏎ FROM materialize.public.t1 JOIN materialize.public.t2 USING(f1) AS x⏎ WHERE x.f1 = 3;
  728. mode cockroach
  729. statement ok
  730. DROP VIEW v1;
  731. # Left
  732. query TTTTTT
  733. SELECT *,
  734. f1 IS NULL AS f1_null,
  735. t1.f1 IS NULL AS t1_f1_null,
  736. t2.f1 IS NULL AS t2_f1_null
  737. FROM t1
  738. LEFT JOIN t2 USING (f1)
  739. ORDER BY f1;
  740. ----
  741. 1 2 NULL false false true
  742. 3 4 4 false false false
  743. query TTTTTT
  744. SELECT *,
  745. f1 IS NULL AS f1_null,
  746. t1.f1 IS NULL AS t1_f1_null,
  747. t2.f1 IS NULL AS t2_f1_null
  748. FROM t2
  749. LEFT JOIN t1 USING (f1)
  750. ORDER BY f1;
  751. ----
  752. 3 4 4 false false false
  753. 5 6 NULL false true false
  754. query TTTT
  755. SELECT *
  756. FROM t1
  757. LEFT JOIN t2 USING (f1)
  758. LEFT JOIN t3 USING (f1)
  759. ORDER BY f1;
  760. ----
  761. 1 2 NULL NULL
  762. 3 4 4 4
  763. # Right
  764. query TTTTTT
  765. SELECT *,
  766. f1 IS NULL AS f1_null,
  767. t1.f1 IS NULL AS t1_f1_null,
  768. t2.f1 IS NULL AS t2_f1_null
  769. FROM t1
  770. RIGHT JOIN t2 USING (f1)
  771. ORDER BY f1;
  772. ----
  773. 3 4 4 false false false
  774. 5 NULL 6 false true false
  775. query TTTTTT
  776. SELECT *,
  777. f1 IS NULL AS f1_null,
  778. t1.f1 IS NULL AS t1_f1_null,
  779. t2.f1 IS NULL AS t2_f1_null
  780. FROM t2
  781. RIGHT JOIN t1
  782. USING (f1)
  783. ORDER BY f1;
  784. ----
  785. 1 NULL 2 false false true
  786. 3 4 4 false false false
  787. query TTTT
  788. SELECT *
  789. FROM t1
  790. RIGHT JOIN t2 USING (f1)
  791. RIGHT JOIN t3 USING (f1)
  792. ORDER BY f1;
  793. ----
  794. 3 4 4 4
  795. 7 NULL NULL 8
  796. query TTTT
  797. SELECT *
  798. FROM t1
  799. RIGHT JOIN t2 USING (f1)
  800. LEFT JOIN t3 USING (f1)
  801. ORDER BY f1;
  802. ----
  803. 3 4 4 4
  804. 5 NULL 6 NULL
  805. query TTTT
  806. SELECT *
  807. FROM t1
  808. LEFT JOIN t2 USING (f1)
  809. RIGHT JOIN t3 USING (f1)
  810. ORDER BY f1;
  811. ----
  812. 3 4 4 4
  813. 7 NULL NULL 8
  814. # Inner
  815. query TTTTTT
  816. SELECT *,
  817. f1 IS NULL AS f1_null,
  818. t1.f1 IS NULL AS t1_f1_null,
  819. t2.f1 IS NULL AS t2_f1_null
  820. FROM t1
  821. INNER JOIN t2
  822. USING (f1)
  823. ORDER BY f1;
  824. ----
  825. 3 4 4 false false false
  826. query TTTTTT
  827. SELECT *,
  828. f1 IS NULL AS f1_null,
  829. t1.f1 IS NULL AS t1_f1_null,
  830. t2.f1 IS NULL AS t2_f1_null
  831. FROM t2
  832. INNER JOIN t1
  833. USING (f1)
  834. ORDER BY f1;
  835. ----
  836. 3 4 4 false false false
  837. query TTTT
  838. SELECT *
  839. FROM t1
  840. INNER JOIN t2 USING (f1)
  841. INNER JOIN t3 USING (f1)
  842. ORDER BY f1;
  843. ----
  844. 3 4 4 4
  845. # Full
  846. query TTTTTT
  847. SELECT *,
  848. f1 IS NULL AS f1_null,
  849. t1.f1 IS NULL AS t1_f1_null,
  850. t2.f1 IS NULL AS t2_f1_null
  851. FROM t1
  852. FULL OUTER JOIN t2
  853. USING (f1)
  854. ORDER BY f1;
  855. ----
  856. 1 2 NULL false false true
  857. 3 4 4 false false false
  858. 5 NULL 6 false true false
  859. query TTTTTT
  860. SELECT *,
  861. f1 IS NULL AS f1_null,
  862. t1.f1 IS NULL AS t1_f1_null,
  863. t2.f1 IS NULL AS t2_f1_null
  864. FROM t2
  865. INNER JOIN t1
  866. USING (f1)
  867. ORDER BY f1;
  868. ----
  869. 3 4 4 false false false
  870. query TTTT
  871. SELECT *
  872. FROM t1
  873. FULL OUTER JOIN t2 USING (f1)
  874. FULL OUTER JOIN t3 USING (f1)
  875. ORDER BY f1;
  876. ----
  877. 1 2 NULL NULL
  878. 3 4 4 4
  879. 5 NULL 6 NULL
  880. 7 NULL NULL 8
  881. # Most recent joined cols are always leftmost in return select
  882. query TTTTT colnames
  883. SELECT *
  884. FROM t1
  885. JOIN t2 USING (f1)
  886. JOIN t3 USING (f1)
  887. JOIN t4 USING (f3);
  888. ----
  889. f3 f1 f2 f2 f4
  890. 4 3 4 4 3
  891. # https://github.com/MaterializeInc/materialize/pull/9489#issuecomment-992186563
  892. # Ensure priority does not persist through joins
  893. query error column reference "f2" is ambiguous
  894. SELECT *, f2 IS NULL
  895. FROM t1 AS t1
  896. JOIN t1 AS t2 USING (f1, f2)
  897. JOIN t1 AS t3 USING (f1);
  898. query error common column name "f2" appears more than once in left table
  899. SELECT *
  900. FROM t1 AS t1
  901. JOIN t1 AS t2 USING (f1, f2)
  902. JOIN t1 AS t3 USING (f1)
  903. JOIN t1 AS t4 USING (f2);
  904. # https://github.com/MaterializeInc/materialize/pull/9489#issuecomment-992195117
  905. # Comma-joins of adjacent tables are equivalent to nested cross joins
  906. query TTTTT
  907. SELECT *
  908. FROM t2,
  909. t2 AS x
  910. JOIN t1
  911. USING (f2)
  912. ORDER BY 1;
  913. ----
  914. 3 4 4 3 3
  915. 5 6 4 3 3
  916. query TTTTT
  917. SELECT *
  918. FROM t2
  919. CROSS JOIN (
  920. t2 AS x
  921. JOIN t1
  922. USING (f2)
  923. )
  924. ORDER BY 1;
  925. ----
  926. 3 4 4 3 3
  927. 5 6 4 3 3
  928. # https://github.com/MaterializeInc/materialize/pull/9489#issuecomment-992207932
  929. statement ok
  930. DELETE FROM t1;
  931. statement ok
  932. DELETE FROM t2;
  933. statement ok
  934. INSERT INTO t1 VALUES
  935. (NULL, 0),
  936. (1, 1),
  937. (1, 1),
  938. (2, 2);
  939. statement ok
  940. INSERT INTO t2 VALUES
  941. (NULL, 0),
  942. (NULL, 0),
  943. (1, 1);
  944. query II
  945. SELECT a3.f1,
  946. a4.f1
  947. FROM t2 AS a1
  948. JOIN t1 AS a2 USING (f1), t2 AS a3
  949. JOIN t1 AS a4 USING (f1);
  950. ----
  951. 1 1
  952. 1 1
  953. 1 1
  954. 1 1
  955. # Simple USING column missing from the right table.
  956. query error column "a" specified in USING clause does not exist in right table
  957. SELECT * FROM (SELECT 1 a) s1 JOIN (SELECT 2 b) s2 USING (a)
  958. # Simple USING column missing from the left table.
  959. query error column "b" specified in USING clause does not exist in left table
  960. SELECT * FROM (SELECT 1 a) s1 JOIN (SELECT 2 b) s2 USING (b)
  961. # USING column missing from both tables, but existing in the outer scope.
  962. query error column "c" specified in USING clause does not exist in left table
  963. SELECT (SELECT * FROM (SELECT 1 a) s1 JOIN (SELECT 2 b) s2 USING (c)) FROM (SELECT 3 c) s3
  964. # USING column missing from the right table only but existing in the outer scope.
  965. query error column "a" specified in USING clause does not exist in right table
  966. SELECT (SELECT * FROM (SELECT 1 a) s1 JOIN (SELECT 2 b) s2 USING (a)) FROM (SELECT 3 a) s3
  967. query II colnames
  968. SELECT * FROM l NATURAL JOIN l AS a LIMIT 0
  969. ----
  970. la lb
  971. # Regression test for https://github.com/MaterializeInc/database-issues/issues/5220
  972. # This query fails if JoinImplementation is not in the same fixpoint loop with LiteralLifting.
  973. statement ok
  974. CREATE TABLE IF NOT EXISTS a (a boolean, b boolean);
  975. statement ok
  976. CREATE VIEW IF NOT EXISTS b AS SELECT 2 + 2;
  977. statement ok
  978. CREATE TABLE IF NOT EXISTS c (data jsonb);
  979. query T multiline
  980. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  981. SELECT (SELECT NULL FROM a) AS c1, ref_0."?column?" AS c4 FROM b AS ref_0
  982. WHERE EXISTS (SELECT FROM c AS ref_7 WHERE EXISTS (SELECT ref_0."?column?" AS c2)) OR ref_0."?column?" IS NOT NULL;
  983. ----
  984. Explained Query:
  985. With
  986. cte l0 =
  987. Distinct project=[] // { arity: 0 }
  988. Project () // { arity: 0 }
  989. ReadStorage materialize.public.c // { arity: 1 }
  990. cte l1 =
  991. Union // { arity: 1 }
  992. Project (#2) // { arity: 1 }
  993. Map (null) // { arity: 3 }
  994. ReadStorage materialize.public.a // { arity: 2 }
  995. Project (#1) // { arity: 1 }
  996. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  997. Reduce aggregates=[count(*)] // { arity: 1 }
  998. Project () // { arity: 0 }
  999. ReadStorage materialize.public.a // { arity: 2 }
  1000. Return // { arity: 2 }
  1001. Map (4) // { arity: 2 }
  1002. CrossJoin type=differential // { arity: 1 }
  1003. implementation
  1004. %0[×] » %1[×]
  1005. ArrangeBy keys=[[]] // { arity: 0 }
  1006. Union // { arity: 0 }
  1007. Get l0 // { arity: 0 }
  1008. Negate // { arity: 0 }
  1009. Get l0 // { arity: 0 }
  1010. Constant // { arity: 0 }
  1011. - ()
  1012. ArrangeBy keys=[[]] // { arity: 1 }
  1013. Union // { arity: 1 }
  1014. Get l1 // { arity: 1 }
  1015. Map (null) // { arity: 1 }
  1016. Union // { arity: 0 }
  1017. Negate // { arity: 0 }
  1018. Distinct project=[] // { arity: 0 }
  1019. Project () // { arity: 0 }
  1020. Get l1 // { arity: 1 }
  1021. Constant // { arity: 0 }
  1022. - ()
  1023. Source materialize.public.a
  1024. Source materialize.public.c
  1025. Target cluster: quickstart
  1026. EOF
  1027. # Verify that the fueling logic of the linear join implementation is correct
  1028. # by running a large cross-join that exhausts the avaible fuel (currently
  1029. # 1 million output records) at least once.
  1030. statement ok
  1031. CREATE TABLE fuel_test_1 (a int)
  1032. statement ok
  1033. CREATE TABLE fuel_test_2 (b int)
  1034. statement ok
  1035. INSERT INTO fuel_test_1 SELECT generate_series(1, 2000)
  1036. statement ok
  1037. INSERT INTO fuel_test_2 SELECT generate_series(2001, 4000)
  1038. query T multiline
  1039. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT sum(a + b) FROM fuel_test_1, fuel_test_2
  1040. ----
  1041. Explained Query:
  1042. With
  1043. cte l0 =
  1044. Reduce aggregates=[sum((#0{a} + #1{b}))]
  1045. CrossJoin type=differential
  1046. ArrangeBy keys=[[]]
  1047. ReadStorage materialize.public.fuel_test_1
  1048. ArrangeBy keys=[[]]
  1049. ReadStorage materialize.public.fuel_test_2
  1050. Return
  1051. Union
  1052. Get l0
  1053. Map (null)
  1054. Union
  1055. Negate
  1056. Project ()
  1057. Get l0
  1058. Constant
  1059. - ()
  1060. Source materialize.public.fuel_test_1
  1061. Source materialize.public.fuel_test_2
  1062. Target cluster: quickstart
  1063. EOF
  1064. query I
  1065. SELECT sum(a + b) FROM fuel_test_1, fuel_test_2
  1066. ----
  1067. 16004000000
  1068. # Test the code path for producing errors from joins.
  1069. statement ok
  1070. CREATE TABLE error_test (a int)
  1071. statement ok
  1072. INSERT INTO error_test VALUES (0)
  1073. query error division by zero
  1074. SELECT t1.a / t2.a FROM error_test t1, error_test t2
  1075. # Regression test for https://github.com/MaterializeInc/database-issues/issues/5998
  1076. query
  1077. select
  1078. from
  1079. (select
  1080. ref_4."schema_id" as c0
  1081. from
  1082. mz_catalog.mz_default_privileges as ref_4
  1083. where current_schemas(
  1084. CAST((select max(atthasdef) from pg_attribute)
  1085. as bool)) = mz_internal.mz_normalize_object_name(
  1086. CAST(ref_4."schema_id" as text))
  1087. ) as subq_7
  1088. where (select "id" from mz_internal.mz_comments limit 1)
  1089. = subq_7."c0";
  1090. ----