outer_join_simplification.slt 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 foo_raw (a int4, b int8, u text)
  12. statement ok
  13. create materialized view foo as select * from foo_raw where a is not null and b is not null
  14. statement ok
  15. create table bar_raw (a int4, v text)
  16. statement ok
  17. create materialized view bar as select distinct on (a) a, v from bar_raw
  18. statement ok
  19. create materialized view ban_nn as select * from bar where a is not null
  20. statement ok
  21. create table baz_raw (b int8, c int2, w text)
  22. statement ok
  23. create materialized view baz as select distinct on (b) b, c, w from baz_raw where b is not null
  24. statement ok
  25. create table quux_raw (c int2, x text)
  26. statement ok
  27. create materialized view quux as select distinct on (c) c, x from quux_raw where c is not null
  28. # Demonstrate core semijoin idempotence simplification: semijoin removal.
  29. # The resulting plan should have 1 join with 2 inputs and no distinct operators.
  30. query T multiline
  31. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR select * from (with keys as (
  32. select distinct foo.a from foo, bar where foo.a = bar.a
  33. )
  34. select * from foo, keys where foo.a = keys.a)
  35. ----
  36. Explained Query:
  37. Project (#0{a}..=#2{u}, #0{a})
  38. Join on=(#0{a} = #3{a}) type=differential
  39. ArrangeBy keys=[[#0{a}]]
  40. ReadStorage materialize.public.foo
  41. ArrangeBy keys=[[#0{a}]]
  42. Project (#0{a})
  43. Filter (#0{a}) IS NOT NULL
  44. ReadStorage materialize.public.bar
  45. Source materialize.public.foo
  46. Source materialize.public.bar
  47. filter=((#0{a}) IS NOT NULL)
  48. Target cluster: quickstart
  49. EOF
  50. # Ensure LEFT JOIN is planned with only one join.
  51. query T multiline
  52. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  53. select * from
  54. foo_raw left join bar on foo_raw.a = bar.a;
  55. ----
  56. Explained Query:
  57. With
  58. cte l0 =
  59. Project (#0{a}..=#2{u}, #4{v})
  60. Join on=(#0{a} = #3{a}) type=differential
  61. ArrangeBy keys=[[#0{a}]]
  62. Filter (#0{a}) IS NOT NULL
  63. ReadStorage materialize.public.foo_raw
  64. ArrangeBy keys=[[#0{a}]]
  65. Filter (#0{a}) IS NOT NULL
  66. ReadStorage materialize.public.bar
  67. Return
  68. Union
  69. Map (null, null)
  70. Union
  71. Negate
  72. Project (#0{a}..=#2{u})
  73. Get l0
  74. ReadStorage materialize.public.foo_raw
  75. Project (#0{a}..=#2{u}, #0{a}, #3{v})
  76. Get l0
  77. Source materialize.public.foo_raw
  78. Source materialize.public.bar
  79. filter=((#0{a}) IS NOT NULL)
  80. Target cluster: quickstart
  81. EOF
  82. # RIGHT JOIN should be planned with only one join.
  83. # At the moment, we do not see the join symmetry.
  84. query T multiline
  85. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  86. select * from
  87. bar right join foo_raw on foo_raw.a = bar.a;
  88. ----
  89. Explained Query:
  90. With
  91. cte l0 =
  92. ArrangeBy keys=[[#0{a}]]
  93. Filter (#0{a}) IS NOT NULL
  94. ReadStorage materialize.public.foo_raw
  95. cte l1 =
  96. Filter (#0{a}) IS NOT NULL
  97. ReadStorage materialize.public.bar
  98. Return
  99. Union
  100. Project (#3, #4, #0{a}..=#2{u})
  101. Map (null, null)
  102. Union
  103. Negate
  104. Project (#0{a}..=#2{u})
  105. Join on=(#0{a} = #3{a}) type=differential
  106. Get l0
  107. ArrangeBy keys=[[#0{a}]]
  108. Project (#0{a})
  109. Get l1
  110. ReadStorage materialize.public.foo_raw
  111. Project (#0{a}, #1{v}, #0{a}, #3{b}, #4{u})
  112. Join on=(#0{a} = #2{a}) type=differential
  113. ArrangeBy keys=[[#0{a}]]
  114. Get l1
  115. Get l0
  116. Source materialize.public.foo_raw
  117. Source materialize.public.bar
  118. filter=((#0{a}) IS NOT NULL)
  119. Target cluster: quickstart
  120. EOF
  121. # Ensure that multiple left joins still produce one join operator each.
  122. query T multiline
  123. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  124. select * from
  125. foo left join bar on foo.a = bar.a
  126. left join baz on foo.b = baz.b
  127. left join quux on baz.c = quux.c;
  128. ----
  129. Explained Query:
  130. Project (#0{a}..=#2{u}, #14, #15, #17..=#19, #21, #22)
  131. Map ((#5) IS NULL, case when #13 then null else #0{a} end, case when #13 then null else #4{v} end, (#9) IS NULL, case when #16 then null else #1{b} end, case when #16 then null else #7{c} end, case when #16 then null else #8{w} end, (#12) IS NULL, case when #20 then null else #10{c} end, case when #20 then null else #11{x} end)
  132. Join on=(#0{a} = #3{a} AND #1{b} = #6{b} AND #10{c} = case when (#9) IS NULL then null else #7{c} end) type=delta
  133. ArrangeBy keys=[[#0{a}], [#1{b}]]
  134. ReadStorage materialize.public.foo
  135. ArrangeBy keys=[[#0{a}]]
  136. Union
  137. Filter (#0{a}) IS NOT NULL
  138. Map (true)
  139. ReadStorage materialize.public.bar
  140. Map (null, null)
  141. Threshold
  142. Union
  143. Negate
  144. Project (#0{a})
  145. Filter (#0{a}) IS NOT NULL
  146. ReadStorage materialize.public.bar
  147. Distinct project=[#0{a}]
  148. Project (#0{a})
  149. ReadStorage materialize.public.foo
  150. ArrangeBy keys=[[#0{b}], [case when (#3) IS NULL then null else #1{c} end]]
  151. Union
  152. Map (true)
  153. ReadStorage materialize.public.baz
  154. Map (null, null, null)
  155. Threshold
  156. Union
  157. Negate
  158. Project (#0{b})
  159. ReadStorage materialize.public.baz
  160. Distinct project=[#0{b}]
  161. Project (#1{b})
  162. ReadStorage materialize.public.foo
  163. ArrangeBy keys=[[#0{c}]]
  164. Union
  165. Map (true)
  166. ReadStorage materialize.public.quux
  167. Map (null, null)
  168. Threshold
  169. Union
  170. Negate
  171. Project (#0{c})
  172. ReadStorage materialize.public.quux
  173. Distinct project=[#0{c}]
  174. Union
  175. Project (#1{c})
  176. ReadStorage materialize.public.baz
  177. Constant
  178. - (null)
  179. Source materialize.public.foo
  180. Source materialize.public.bar
  181. filter=((#0{a}) IS NOT NULL)
  182. Source materialize.public.baz
  183. Source materialize.public.quux
  184. Target cluster: quickstart
  185. EOF
  186. # Record how we do not yet fully optimize projections in left join stacks.
  187. # This plan appears to require further projection pushdown to cancel the last join.
  188. query T multiline
  189. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  190. select count(*) from
  191. foo left join bar on foo.a = bar.a
  192. left join baz on foo.b = baz.b
  193. left join quux on baz.c = quux.c;
  194. ----
  195. Explained Query:
  196. With
  197. cte l0 =
  198. Project (#0{a})
  199. Filter (#0{a}) IS NOT NULL
  200. ReadStorage materialize.public.bar
  201. cte l1 =
  202. Project (#0{c})
  203. ReadStorage materialize.public.quux
  204. cte l2 =
  205. Reduce aggregates=[count(*)]
  206. Project ()
  207. Join on=(#0{a} = #2{a} AND #1{b} = #3{b} AND #6{c} = case when (#5) IS NULL then null else #4{c} end) type=delta
  208. ArrangeBy keys=[[#0{a}], [#1{b}]]
  209. Project (#0{a}, #1{b})
  210. ReadStorage materialize.public.foo
  211. ArrangeBy keys=[[#0{a}]]
  212. Union
  213. Get l0
  214. Threshold
  215. Union
  216. Negate
  217. Get l0
  218. Distinct project=[#0{a}]
  219. Project (#0{a})
  220. ReadStorage materialize.public.foo
  221. ArrangeBy keys=[[#0{b}], [case when (#2) IS NULL then null else #1{c} end]]
  222. Union
  223. Project (#0{b}, #1{c}, #3)
  224. Map (true)
  225. ReadStorage materialize.public.baz
  226. Map (null, null)
  227. Threshold
  228. Union
  229. Negate
  230. Project (#0{b})
  231. ReadStorage materialize.public.baz
  232. Distinct project=[#0{b}]
  233. Project (#1{b})
  234. ReadStorage materialize.public.foo
  235. ArrangeBy keys=[[#0{c}]]
  236. Union
  237. Get l1
  238. Threshold
  239. Union
  240. Negate
  241. Get l1
  242. Distinct project=[#0{c}]
  243. Union
  244. Project (#1{c})
  245. ReadStorage materialize.public.baz
  246. Constant
  247. - (null)
  248. Return
  249. Union
  250. Get l2
  251. Map (0)
  252. Union
  253. Negate
  254. Project ()
  255. Get l2
  256. Constant
  257. - ()
  258. Source materialize.public.foo
  259. Source materialize.public.bar
  260. filter=((#0{a}) IS NOT NULL)
  261. Source materialize.public.baz
  262. Source materialize.public.quux
  263. Target cluster: quickstart
  264. EOF
  265. # Record how we can push filters through left joins to their source.
  266. query T multiline
  267. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  268. select * from
  269. foo left join baz on foo.b = baz.b
  270. left join quux on baz.c = quux.c
  271. where foo.a = 7;
  272. ----
  273. Explained Query:
  274. Project (#0{a}..=#2{u}, #11..=#13, #15, #16)
  275. Map ((#6) IS NULL, case when #10 then null else #1{b} end, case when #10 then null else #4{c} end, case when #10 then null else #5{w} end, (#9) IS NULL, case when #14 then null else #7{c} end, case when #14 then null else #8{x} end)
  276. Join on=(#1{b} = #3{b} AND #7{c} = case when (#6) IS NULL then null else #4{c} end) type=delta
  277. ArrangeBy keys=[[#1{b}]]
  278. Filter (#0{a} = 7)
  279. ReadStorage materialize.public.foo
  280. ArrangeBy keys=[[#0{b}], [case when (#3) IS NULL then null else #1{c} end]]
  281. Union
  282. Map (true)
  283. ReadStorage materialize.public.baz
  284. Map (null, null, null)
  285. Threshold
  286. Union
  287. Negate
  288. Project (#0{b})
  289. ReadStorage materialize.public.baz
  290. Distinct project=[#0{b}]
  291. Project (#1{b})
  292. ReadStorage materialize.public.foo
  293. ArrangeBy keys=[[#0{c}]]
  294. Union
  295. Map (true)
  296. ReadStorage materialize.public.quux
  297. Map (null, null)
  298. Threshold
  299. Union
  300. Negate
  301. Project (#0{c})
  302. ReadStorage materialize.public.quux
  303. Distinct project=[#0{c}]
  304. Union
  305. Project (#1{c})
  306. ReadStorage materialize.public.baz
  307. Constant
  308. - (null)
  309. Source materialize.public.foo
  310. Source materialize.public.baz
  311. Source materialize.public.quux
  312. Target cluster: quickstart
  313. EOF
  314. ## -------------------- Tests for WITH MUTUALLY RECURSIVE --------------------
  315. # Trivial test: Just marking SemijoinIdempotence recursion_safe should already handle this, because the semijoin pattern
  316. # in this test doesn't reference any ids that are defined inside the WMR. Therefore, SemijoinIdempotence doesn't need to
  317. # collect info about ids inside a LetRec.
  318. # The resulting plan should have 1 join with 2 inputs.
  319. query T multiline
  320. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  321. with mutually recursive
  322. c0(a int4, b int8, u text, key int4) as (
  323. select * from (
  324. with keys as (
  325. select distinct foo.a from foo, bar where foo.a = bar.a
  326. )
  327. select * from foo, keys where foo.a = keys.a
  328. )
  329. union
  330. select * from c0
  331. )
  332. select * from c0;
  333. ----
  334. Explained Query:
  335. With Mutually Recursive
  336. cte l0 =
  337. Distinct project=[#0{a}..=#3{a}]
  338. Union
  339. Project (#0{a}..=#2{u}, #0{a})
  340. Join on=(#0{a} = #3{a}) type=differential
  341. ArrangeBy keys=[[#0{a}]]
  342. ReadStorage materialize.public.foo
  343. ArrangeBy keys=[[#0{a}]]
  344. Project (#0{a})
  345. Filter (#0{a}) IS NOT NULL
  346. ReadStorage materialize.public.bar
  347. Get l0
  348. Return
  349. Get l0
  350. Source materialize.public.foo
  351. Source materialize.public.bar
  352. filter=((#0{a}) IS NOT NULL)
  353. Target cluster: quickstart
  354. EOF
  355. # Manually written idempotent semijoin.
  356. # The resulting plan should have 1 join with 2 inputs.
  357. # Here, SemijoinIdempotence relies on the keys introduced by the `select distinct on` being propagated through the
  358. # recursive Get. Note that in this test even if SemijoinIdempotence wouldn't work, RedundantJoin would step in. (But
  359. # see later a similar situation, but with a LEFT JOIN, where RedundantJoin wouldn't be able to eliminate a join.)
  360. # With materialize#27389 this stopped testing a thing; see issue database-issues#8294.
  361. query T multiline
  362. EXPLAIN OPTIMIZED PLAN WITH (keys) AS VERBOSE TEXT FOR
  363. with mutually recursive
  364. c0(a int4, b int8, u text, key int4) as (
  365. select distinct on (1) * from (
  366. select * from (
  367. with keys as (
  368. select distinct foo.a from foo, (select a, u from c0) as likebar where foo.a = likebar.a
  369. )
  370. select * from foo, keys where foo.a = keys.a
  371. )
  372. union
  373. select * from c0
  374. )
  375. )
  376. select * from c0;
  377. ----
  378. Explained Query (fast path):
  379. Constant <empty>
  380. Target cluster: quickstart
  381. EOF
  382. # Manually written idempotent semijoin.
  383. # Similar to the previous test, but the recursive Get is at the other input. This means that the input that should have
  384. # a known key is the static one, for which key inference works fine.
  385. # The resulting plan should have 1 join with 2 inputs.
  386. # With materialize#27389 this stopped testing a thing; see issue database-issues#8294.
  387. query T multiline
  388. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  389. with mutually recursive
  390. c0(a int4, b int8, u text, key int4) as (
  391. select * from (
  392. with keys as (
  393. select distinct c0.a from c0, bar where c0.a = bar.a
  394. )
  395. select c0.a, c0.b, c0.u, c0.key from c0, keys where c0.a = keys.a
  396. )
  397. union
  398. select * from c0
  399. )
  400. select * from c0;
  401. ----
  402. Explained Query (fast path):
  403. Constant <empty>
  404. Target cluster: quickstart
  405. EOF
  406. # Manually written idempotent semijoin.
  407. # Similar to the previous test, but the CTE from inside c0 is manually lifted to the enclosing LetRec.
  408. # The resulting plan should have 1 join with 2 inputs.
  409. # With materialize#27389 this stopped testing a thing; see issue database-issues#8294.
  410. query T multiline
  411. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  412. with mutually recursive
  413. keys(a int4) as (
  414. select distinct c0.a from c0, bar where c0.a = bar.a
  415. ),
  416. c0(a int4, b int8, u text, key int4) as (
  417. select c0.a, c0.b, c0.u, c0.key from c0, keys where c0.a = keys.a
  418. union
  419. select * from c0
  420. )
  421. select * from c0;
  422. ----
  423. Explained Query (fast path):
  424. Constant <empty>
  425. Target cluster: quickstart
  426. EOF
  427. # Manually written idempotent semijoin.
  428. # Similar to the previous test, but instead of using bar directly, we add an extra cte (bar2), which will recursively
  429. # refer to `c0`. `keys` will refer to `bar2` instead of `c0`.
  430. # The resulting plan should have 1 join with 2 inputs.
  431. # With materialize#27389 this stopped testing a thing; see issue database-issues#8294.
  432. query T multiline
  433. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  434. with mutually recursive
  435. bar2(a int4, v text) as (
  436. select * from bar
  437. union
  438. select a, u from c0
  439. ),
  440. keys(a int4) as (
  441. select distinct c0.a from c0, (select distinct on(a) * from bar2) as bar3 where c0.a = bar3.a
  442. ),
  443. c0(a int4, b int8, u text, key int4) as (
  444. select c0.a, c0.b, c0.u, c0.key from c0, keys where c0.a = keys.a
  445. union
  446. select * from c0
  447. )
  448. select * from c0;
  449. ----
  450. Explained Query (fast path):
  451. Constant <empty>
  452. Target cluster: quickstart
  453. EOF
  454. # Similar to the previous test, but SemijoinIdempotence should NOT kick in here! (That is, 2 joins should be in the
  455. # plan.) This is because we reordered the bindings compared to the previous test, and now the `bar2` reference in `keys`
  456. # changes meaning when `bar2` is being assigned after `key`. To correctly handle this situation, we need the expirations
  457. # in SemijoinIdempotence. To demonstrate this, if we comment out the two `do_expirations` lines, SemijoinIdempotence
  458. # incorrectly transforms this plan.
  459. # With materialize#27389 this stopped testing a thing; see issue database-issues#8294.
  460. query T multiline
  461. EXPLAIN OPTIMIZED PLAN WITH (keys) AS VERBOSE TEXT FOR
  462. with mutually recursive
  463. keys(a int4) as (
  464. select distinct c0.a from c0, (select distinct on(a) * from bar2) as bar3 where c0.a = bar3.a
  465. ),
  466. bar2(a int4, v text) as (
  467. select * from bar
  468. union
  469. select a, u from c0
  470. ),
  471. c0(a int4, b int8, u text, key int4) as (
  472. select c0.a, c0.b, c0.u, c0.key from c0, keys where c0.a = keys.a
  473. union
  474. select * from c0
  475. )
  476. select * from c0;
  477. ----
  478. Explained Query (fast path):
  479. Constant <empty>
  480. Target cluster: quickstart
  481. EOF
  482. # Manually written idempotent semijoin.
  483. # Another negative test. Similar to the first test with `bar2`, but both `keys` and `bar2` are being referenced from the
  484. # body, which means that SemijoinIdempotence can't kick in, as it would eliminate the intermediate Join's result, which
  485. # is now being referenced from the body.
  486. # With materialize#27389 this stopped testing a thing; see issue database-issues#8294.
  487. query T multiline
  488. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  489. with mutually recursive
  490. bar2(a int4, v text) as (
  491. select * from bar
  492. union
  493. select a, u from c0
  494. ),
  495. keys(a int4) as (
  496. select distinct c0.a from c0, (select distinct on(a) * from bar2) as bar3 where c0.a = bar3.a
  497. ),
  498. c0(a int4, b int8, u text, key int4) as (
  499. select c0.a, c0.b, c0.u, c0.key from c0, keys where c0.a = keys.a
  500. union
  501. select * from c0
  502. )
  503. select * from c0
  504. union all
  505. select a, -1, v, a from bar2
  506. union all
  507. select a, -2, 'keys', a from keys;
  508. ----
  509. Explained Query:
  510. Project (#0{a}, #2, #1{v}, #0{a})
  511. Map (-1)
  512. ReadStorage materialize.public.bar
  513. Source materialize.public.bar
  514. Target cluster: quickstart
  515. EOF
  516. # Test that LEFT JOIN inside WMR is planned with only one join.
  517. # Here, SemijoinIdempotence relies on the keys introduced by the `select distinct on` being propagated through the
  518. # recursive Get.
  519. query T multiline
  520. EXPLAIN OPTIMIZED PLAN WITH(keys, humanized expressions) AS VERBOSE TEXT FOR
  521. with mutually recursive
  522. c0(a int4, b int8, u text, a2 int4, v2 text) as (
  523. select distinct on (1) * from (
  524. select foo_raw.a, foo_raw.b, foo_raw.u, c0.a, c0.u from
  525. foo_raw left join c0 on foo_raw.a = c0.a
  526. union
  527. select *, a, u from foo_raw
  528. )
  529. )
  530. select * from c0
  531. ----
  532. Explained Query:
  533. With Mutually Recursive
  534. cte l0 =
  535. Project (#0{a}..=#2{u}, #4) // { keys: "()" }
  536. Join on=(#0{a} = #3) type=differential // { keys: "()" }
  537. ArrangeBy keys=[[#0{a}]] // { keys: "()" }
  538. Filter (#0{a}) IS NOT NULL // { keys: "()" }
  539. ReadStorage materialize.public.foo_raw // { keys: "()" }
  540. ArrangeBy keys=[[#0]] // { keys: "([0])" }
  541. Project (#0, #2) // { keys: "([0])" }
  542. Filter (#0{a}) IS NOT NULL // { keys: "([0])" }
  543. Get l1 // { keys: "([0])" }
  544. cte l1 =
  545. TopK group_by=[#0{a}] limit=1 // { keys: "([0])" }
  546. Distinct project=[#0{a}..=#4{u}] // { keys: "([0, 1, 2, 3, 4])" }
  547. Union // { keys: "()" }
  548. Map (null, null) // { keys: "()" }
  549. Union // { keys: "()" }
  550. Negate // { keys: "()" }
  551. Project (#0{a}..=#2{u}) // { keys: "()" }
  552. Get l0 // { keys: "()" }
  553. ReadStorage materialize.public.foo_raw // { keys: "()" }
  554. Project (#0{a}..=#2{u}, #0{a}, #3) // { keys: "()" }
  555. Get l0 // { keys: "()" }
  556. Project (#0{a}..=#2{u}, #0{a}, #2{u}) // { keys: "()" }
  557. ReadStorage materialize.public.foo_raw // { keys: "()" }
  558. Return // { keys: "([0])" }
  559. Get l1 // { keys: "([0])" }
  560. Source materialize.public.foo_raw
  561. Target cluster: quickstart
  562. EOF
  563. # Test that LEFT JOIN inside WMR is planned with only one join.
  564. # Similar to the previous test, but the recursive Get is at the other input. This means that the input that should have
  565. # a known key is the static one, for which we don't need to go propagate a key through a recursive Get.
  566. query T multiline
  567. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  568. with mutually recursive
  569. c0(a int4, b int8, u text, a_bar int4, v_bar text) as (
  570. (
  571. select c0.a, c0.b, c0.u, bar.a, bar.v from
  572. c0 left join bar on c0.a = bar.a
  573. )
  574. union
  575. select *, a, u from foo_raw
  576. )
  577. select * from c0
  578. ----
  579. Explained Query:
  580. With Mutually Recursive
  581. cte l0 =
  582. Project (#0..=#2, #4{v})
  583. Join on=(#0{a} = #3{a}) type=differential
  584. ArrangeBy keys=[[#0{a}]]
  585. Project (#0..=#2)
  586. Filter (#0{a}) IS NOT NULL
  587. Get l1
  588. ArrangeBy keys=[[#0{a}]]
  589. Filter (#0{a}) IS NOT NULL
  590. ReadStorage materialize.public.bar
  591. cte l1 =
  592. Distinct project=[#0{a}..=#4{v}]
  593. Union
  594. Map (null, null)
  595. Union
  596. Negate
  597. Project (#0..=#2)
  598. Get l0
  599. Project (#0{a}..=#2{u})
  600. Get l1
  601. Project (#0..=#2, #0, #3{v})
  602. Get l0
  603. Project (#0{a}..=#2{u}, #0{a}, #2{u})
  604. ReadStorage materialize.public.foo_raw
  605. Return
  606. Get l1
  607. Source materialize.public.foo_raw
  608. Source materialize.public.bar
  609. filter=((#0{a}) IS NOT NULL)
  610. Target cluster: quickstart
  611. EOF
  612. # Ensure LEFT JOIN inside WMR is planned with only one join.
  613. # A variation of the previous one.
  614. query T multiline
  615. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  616. with mutually recursive
  617. c0(a int4, b int8, u text, a_bar int4, v_bar text) as (
  618. (
  619. select * from
  620. foo_raw left join bar on foo_raw.a = bar.a
  621. )
  622. union
  623. select * from c0
  624. )
  625. select * from c0
  626. ----
  627. Explained Query:
  628. With
  629. cte l0 =
  630. Project (#0{a}..=#2{u}, #4{v})
  631. Join on=(#0{a} = #3{a}) type=differential
  632. ArrangeBy keys=[[#0{a}]]
  633. Filter (#0{a}) IS NOT NULL
  634. ReadStorage materialize.public.foo_raw
  635. ArrangeBy keys=[[#0{a}]]
  636. Filter (#0{a}) IS NOT NULL
  637. ReadStorage materialize.public.bar
  638. Return
  639. With Mutually Recursive
  640. cte l1 =
  641. Distinct project=[#0{a}..=#4{v}]
  642. Union
  643. Map (null, null)
  644. Union
  645. Negate
  646. Project (#0{a}..=#2{u})
  647. Get l0
  648. ReadStorage materialize.public.foo_raw
  649. Project (#0{a}..=#2{u}, #0{a}, #3{v})
  650. Get l0
  651. Get l1
  652. Return
  653. Get l1
  654. Source materialize.public.foo_raw
  655. Source materialize.public.bar
  656. filter=((#0{a}) IS NOT NULL)
  657. Target cluster: quickstart
  658. EOF
  659. # Ensure that multiple left joins inside a WMR still produce one join operator each.
  660. query T multiline
  661. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  662. with mutually recursive
  663. c0(a int4, b int8, u text) as (
  664. (
  665. select c0.a + bar.a, c0.b + baz.b, c0.u || cast(baz.c + quux.c as text) from
  666. c0 left join bar on c0.a = bar.a
  667. left join baz on c0.b = baz.b
  668. left join quux on baz.c = quux.c
  669. )
  670. union
  671. select * from foo
  672. )
  673. select * from c0;
  674. ----
  675. Explained Query:
  676. With Mutually Recursive
  677. cte l0 =
  678. Union
  679. Project (#0{a}, #1{b})
  680. Get l1
  681. Constant
  682. - (null, null)
  683. cte l1 =
  684. Distinct project=[#0{a}..=#2{u}]
  685. Union
  686. Project (#11..=#13)
  687. Map ((#7) IS NULL, (#0{a} + case when (#4) IS NULL then null else #0 end), (#1{b} + case when #10 then null else #1 end), (#2{u} || smallint_to_text((case when #10 then null else #6{c} end + case when (#9) IS NULL then null else #8{c} end))))
  688. Join on=(#0 = #3{a} AND #1 = #5{b} AND #8{c} = case when (#7) IS NULL then null else #6{c} end) type=delta
  689. ArrangeBy keys=[[#0{a}], [#1{b}]]
  690. Get l1
  691. ArrangeBy keys=[[#0{a}]]
  692. Union
  693. Project (#0{a}, #2)
  694. Filter (#0{a}) IS NOT NULL
  695. Map (true)
  696. ReadStorage materialize.public.bar
  697. Map (null)
  698. Threshold
  699. Union
  700. Negate
  701. Project (#0{a})
  702. Filter (#0{a}) IS NOT NULL
  703. ReadStorage materialize.public.bar
  704. Distinct project=[#0]
  705. Project (#0)
  706. Get l0
  707. ArrangeBy keys=[[#0{b}], [case when (#2) IS NULL then null else #1{c} end]]
  708. Union
  709. Project (#0{b}, #1{c}, #3)
  710. Map (true)
  711. ReadStorage materialize.public.baz
  712. Map (null, null)
  713. Threshold
  714. Union
  715. Negate
  716. Project (#0{b})
  717. ReadStorage materialize.public.baz
  718. Distinct project=[#0]
  719. Project (#1)
  720. Get l0
  721. ArrangeBy keys=[[#0{c}]]
  722. Union
  723. Project (#0{c}, #2)
  724. Map (true)
  725. ReadStorage materialize.public.quux
  726. Map (null)
  727. Threshold
  728. Union
  729. Negate
  730. Project (#0{c})
  731. ReadStorage materialize.public.quux
  732. Distinct project=[#0{c}]
  733. Union
  734. Project (#1{c})
  735. ReadStorage materialize.public.baz
  736. Constant
  737. - (null)
  738. ReadStorage materialize.public.foo
  739. Return
  740. Get l1
  741. Source materialize.public.foo
  742. Source materialize.public.bar
  743. filter=((#0{a}) IS NOT NULL)
  744. Source materialize.public.baz
  745. Source materialize.public.quux
  746. Target cluster: quickstart
  747. EOF
  748. # Ensure that when the info coming from the right side of a left join is not used, then the join is optimized out.
  749. # The plan currently has one join, but maybe that could be optimized out too: the join with baz is currently present,
  750. # because baz.c is used in the condition of the join with quux. However, the join with quux is optimized out.
  751. query T multiline
  752. EXPLAIN OPTIMIZED PLAN WITH(arity, humanized expressions) AS VERBOSE TEXT FOR
  753. with mutually recursive
  754. c0(a int4, b int8, u text) as (
  755. (
  756. select c0.a, c0.b, c0.u from
  757. c0 left join bar on c0.a = bar.a
  758. left join baz on c0.b = baz.b
  759. left join quux on baz.c = quux.c
  760. )
  761. union
  762. select * from foo
  763. )
  764. select * from c0;
  765. ----
  766. Explained Query:
  767. With
  768. cte l0 =
  769. Project (#0{a}) // { arity: 1 }
  770. Filter (#0{a}) IS NOT NULL // { arity: 2 }
  771. ReadStorage materialize.public.bar // { arity: 2 }
  772. cte l1 =
  773. Project (#0{c}) // { arity: 1 }
  774. ReadStorage materialize.public.quux // { arity: 2 }
  775. Return // { arity: 3 }
  776. With Mutually Recursive
  777. cte l2 =
  778. Distinct project=[#0{a}..=#2{u}] // { arity: 3 }
  779. Union // { arity: 3 }
  780. Project (#0..=#2) // { arity: 3 }
  781. Join on=(#0 = #3{a} AND #1 = #4{b} AND #7{c} = case when (#6) IS NULL then null else #5{c} end) type=delta // { arity: 8 }
  782. ArrangeBy keys=[[#0{a}], [#1{b}]] // { arity: 3 }
  783. Get l2 // { arity: 3 }
  784. ArrangeBy keys=[[#0{a}]] // { arity: 1 }
  785. Union // { arity: 1 }
  786. Get l0 // { arity: 1 }
  787. Threshold // { arity: 1 }
  788. Union // { arity: 1 }
  789. Negate // { arity: 1 }
  790. Get l0 // { arity: 1 }
  791. Distinct project=[#0] // { arity: 1 }
  792. Project (#0{a}) // { arity: 1 }
  793. Get l2 // { arity: 3 }
  794. ArrangeBy keys=[[#0{b}], [case when (#2) IS NULL then null else #1{c} end]] // { arity: 3 }
  795. Union // { arity: 3 }
  796. Project (#0{b}, #1{c}, #3) // { arity: 3 }
  797. Map (true) // { arity: 4 }
  798. ReadStorage materialize.public.baz // { arity: 3 }
  799. Map (null, null) // { arity: 3 }
  800. Threshold // { arity: 1 }
  801. Union // { arity: 1 }
  802. Negate // { arity: 1 }
  803. Project (#0{b}) // { arity: 1 }
  804. ReadStorage materialize.public.baz // { arity: 3 }
  805. Distinct project=[#0] // { arity: 1 }
  806. Project (#1{b}) // { arity: 1 }
  807. Get l2 // { arity: 3 }
  808. ArrangeBy keys=[[#0{c}]] // { arity: 1 }
  809. Union // { arity: 1 }
  810. Get l1 // { arity: 1 }
  811. Threshold // { arity: 1 }
  812. Union // { arity: 1 }
  813. Negate // { arity: 1 }
  814. Get l1 // { arity: 1 }
  815. Distinct project=[#0{c}] // { arity: 1 }
  816. Union // { arity: 1 }
  817. Project (#1{c}) // { arity: 1 }
  818. ReadStorage materialize.public.baz // { arity: 3 }
  819. Constant // { arity: 1 }
  820. - (null)
  821. ReadStorage materialize.public.foo // { arity: 3 }
  822. Return // { arity: 3 }
  823. Get l2 // { arity: 3 }
  824. Source materialize.public.foo
  825. Source materialize.public.bar
  826. filter=((#0{a}) IS NOT NULL)
  827. Source materialize.public.baz
  828. Source materialize.public.quux
  829. Target cluster: quickstart
  830. EOF