scalar_subqueries_select_list.slt 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. #
  10. # Test that multiple scalar subqueries are properly flattened by materialize#8582 whenever possible.
  11. #
  12. # For eligible queries:
  13. # - a (number_of_subqueries + 1)-way join is expected at the bottom of the plan
  14. # - any comonality of the subqueries will be detected by the CSE transform
  15. # - the operators that check for more than 1 row returned from the subquery will appear only once
  16. #
  17. mode cockroach
  18. statement ok
  19. CREATE TABLE t1 (
  20. f1 INTEGER
  21. )
  22. statement ok
  23. INSERT INTO t1 VALUES (1), (2), (3)
  24. statement ok
  25. CREATE TABLE t2 (
  26. f1 INTEGER
  27. )
  28. statement ok
  29. INSERT INTO t2 VALUES (1), (2), (3)
  30. statement ok
  31. CREATE TABLE t3 (
  32. f1 INTEGER
  33. )
  34. statement ok
  35. INSERT INTO t3 VALUES (1), (2), (3)
  36. #
  37. # Uncorrelated
  38. #
  39. query T multiline
  40. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT * FROM t1), (SELECT * FROM t1) FROM t2
  41. ----
  42. Explained Query:
  43. With
  44. cte l0 =
  45. Union // { arity: 1 }
  46. ReadStorage materialize.public.t1 // { arity: 1 }
  47. Project (#1) // { arity: 1 }
  48. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  49. Reduce aggregates=[count(*)] // { arity: 1 }
  50. Project () // { arity: 0 }
  51. ReadStorage materialize.public.t1 // { arity: 1 }
  52. Return // { arity: 2 }
  53. Project (#0{f1}, #0{f1}) // { arity: 2 }
  54. CrossJoin type=differential // { arity: 1 }
  55. implementation
  56. %0:t2[×] » %1[×]
  57. ArrangeBy keys=[[]] // { arity: 0 }
  58. Project () // { arity: 0 }
  59. ReadStorage materialize.public.t2 // { arity: 1 }
  60. ArrangeBy keys=[[]] // { arity: 1 }
  61. Union // { arity: 1 }
  62. Get l0 // { arity: 1 }
  63. Map (null) // { arity: 1 }
  64. Union // { arity: 0 }
  65. Negate // { arity: 0 }
  66. Distinct project=[] // { arity: 0 }
  67. Project () // { arity: 0 }
  68. Get l0 // { arity: 1 }
  69. Constant // { arity: 0 }
  70. - ()
  71. Source materialize.public.t1
  72. Source materialize.public.t2
  73. Target cluster: quickstart
  74. EOF
  75. #
  76. # Correlated, identical subqueries
  77. #
  78. query T multiline
  79. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT * FROM t1 WHERE t1.f1 = t2.f1) , (SELECT * FROM t1 WHERE t1.f1 = t2.f1) FROM t2
  80. ----
  81. Explained Query:
  82. With
  83. cte l0 =
  84. Distinct project=[#0{f1}] // { arity: 1 }
  85. ReadStorage materialize.public.t2 // { arity: 1 }
  86. cte l1 =
  87. Project (#0{f1}) // { arity: 1 }
  88. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 2 }
  89. implementation
  90. %0:l0[#0{f1}]UKA » %1:t1[#0{f1}]K
  91. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  92. Get l0 // { arity: 1 }
  93. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  94. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  95. ReadStorage materialize.public.t1 // { arity: 1 }
  96. cte l2 =
  97. Union // { arity: 2 }
  98. Project (#0{f1}, #0{f1}) // { arity: 2 }
  99. Get l1 // { arity: 1 }
  100. Project (#0{f1}, #2) // { arity: 2 }
  101. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  102. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  103. Get l1 // { arity: 1 }
  104. Return // { arity: 2 }
  105. Project (#2{f1}, #2{f1}) // { arity: 2 }
  106. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 3 }
  107. implementation
  108. %0:t2[#0]K » %1[#0]K
  109. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  110. ReadStorage materialize.public.t2 // { arity: 1 }
  111. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  112. Union // { arity: 2 }
  113. Get l2 // { arity: 2 }
  114. Map (null) // { arity: 2 }
  115. Union // { arity: 1 }
  116. Negate // { arity: 1 }
  117. Distinct project=[#0{f1}] // { arity: 1 }
  118. Project (#0{f1}) // { arity: 1 }
  119. Get l2 // { arity: 2 }
  120. Get l0 // { arity: 1 }
  121. Source materialize.public.t1
  122. filter=((#0{f1}) IS NOT NULL)
  123. Source materialize.public.t2
  124. Target cluster: quickstart
  125. EOF
  126. query II rowsort
  127. SELECT (SELECT * FROM t1 WHERE t1.f1 = t2.f1) , (SELECT * FROM t1 WHERE t1.f1 = t2.f1) FROM t2
  128. ----
  129. 1 1
  130. 2 2
  131. 3 3
  132. query I
  133. SELECT COUNT(*) FROM (SELECT (SELECT * FROM t1 WHERE t1.f1 = t2.f1) , (SELECT * FROM t1 WHERE t1.f1 = t2.f1) FROM t2)
  134. ----
  135. 3
  136. query T multiline
  137. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT * FROM t1 WHERE t1.f1 = t2.f1 + 1 UNION ALL SELECT * FROM t1 WHERE t1.f1 = t2.f1 + 2) , (SELECT * FROM t1 WHERE t1.f1 = t2.f1 + 1 UNION ALL SELECT * FROM t1 WHERE t1.f1 = t2.f1 + 2) FROM t2
  138. ----
  139. Explained Query:
  140. With
  141. cte l0 =
  142. Distinct project=[#0{f1}] // { arity: 1 }
  143. ReadStorage materialize.public.t2 // { arity: 1 }
  144. cte l1 =
  145. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  146. Get l0 // { arity: 1 }
  147. cte l2 =
  148. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  149. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  150. ReadStorage materialize.public.t1 // { arity: 1 }
  151. cte l3 =
  152. Union // { arity: 2 }
  153. Join on=(#1{f1} = (#0{f1} + 1)) type=differential // { arity: 2 }
  154. implementation
  155. %0:l1[(#0{f1} + 1)]K » %1:l2[#0{f1}]K
  156. ArrangeBy keys=[[(#0{f1} + 1)]] // { arity: 1 }
  157. Get l1 // { arity: 1 }
  158. Get l2 // { arity: 1 }
  159. Join on=(#1{f1} = (#0{f1} + 2)) type=differential // { arity: 2 }
  160. implementation
  161. %0:l1[(#0{f1} + 2)]K » %1:l2[#0{f1}]K
  162. ArrangeBy keys=[[(#0{f1} + 2)]] // { arity: 1 }
  163. Get l1 // { arity: 1 }
  164. Get l2 // { arity: 1 }
  165. cte l4 =
  166. Union // { arity: 2 }
  167. Get l3 // { arity: 2 }
  168. Project (#0{f1}, #2) // { arity: 2 }
  169. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  170. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  171. Project (#0{f1}) // { arity: 1 }
  172. Get l3 // { arity: 2 }
  173. Return // { arity: 2 }
  174. Project (#2{f1}, #2{f1}) // { arity: 2 }
  175. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 3 }
  176. implementation
  177. %0:t2[#0]K » %1[#0]K
  178. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  179. ReadStorage materialize.public.t2 // { arity: 1 }
  180. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  181. Union // { arity: 2 }
  182. Get l4 // { arity: 2 }
  183. Map (null) // { arity: 2 }
  184. Union // { arity: 1 }
  185. Negate // { arity: 1 }
  186. Distinct project=[#0{f1}] // { arity: 1 }
  187. Project (#0{f1}) // { arity: 1 }
  188. Get l4 // { arity: 2 }
  189. Get l0 // { arity: 1 }
  190. Source materialize.public.t1
  191. filter=((#0{f1}) IS NOT NULL)
  192. Source materialize.public.t2
  193. Target cluster: quickstart
  194. EOF
  195. #
  196. # Correlated, with distinct WHERE clauses
  197. #
  198. query T multiline
  199. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT * FROM t1 WHERE t1.f1 = t2.f1 + 1) , (SELECT * FROM t1 WHERE t1.f1 = t2.f1 + 2) FROM t2
  200. ----
  201. Explained Query:
  202. With
  203. cte l0 =
  204. Distinct project=[#0{f1}] // { arity: 1 }
  205. ReadStorage materialize.public.t2 // { arity: 1 }
  206. cte l1 =
  207. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  208. Get l0 // { arity: 1 }
  209. cte l2 =
  210. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  211. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  212. ReadStorage materialize.public.t1 // { arity: 1 }
  213. cte l3 =
  214. Join on=(#1{f1} = (#0{f1} + 1)) type=differential // { arity: 2 }
  215. implementation
  216. %0:l1[(#0{f1} + 1)]K » %1:l2[#0{f1}]K
  217. ArrangeBy keys=[[(#0{f1} + 1)]] // { arity: 1 }
  218. Get l1 // { arity: 1 }
  219. Get l2 // { arity: 1 }
  220. cte l4 =
  221. Union // { arity: 2 }
  222. Get l3 // { arity: 2 }
  223. Project (#0{f1}, #2) // { arity: 2 }
  224. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  225. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  226. Project (#0{f1}) // { arity: 1 }
  227. Get l3 // { arity: 2 }
  228. cte l5 =
  229. Join on=(#1{f1} = (#0{f1} + 2)) type=differential // { arity: 2 }
  230. implementation
  231. %0:l1[(#0{f1} + 2)]K » %1:l2[#0{f1}]K
  232. ArrangeBy keys=[[(#0{f1} + 2)]] // { arity: 1 }
  233. Get l1 // { arity: 1 }
  234. Get l2 // { arity: 1 }
  235. cte l6 =
  236. Union // { arity: 2 }
  237. Get l5 // { arity: 2 }
  238. Project (#0{f1}, #2) // { arity: 2 }
  239. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  240. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  241. Project (#0{f1}) // { arity: 1 }
  242. Get l5 // { arity: 2 }
  243. Return // { arity: 2 }
  244. Project (#2{f1}, #4{f1}) // { arity: 2 }
  245. Join on=(#0{f1} = #1{f1} = #3{f1}) type=delta // { arity: 5 }
  246. implementation
  247. %0:t2 » %1[#0]K » %2[#0]K
  248. %1 » %0:t2[#0]K » %2[#0]K
  249. %2 » %0:t2[#0]K » %1[#0]K
  250. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  251. ReadStorage materialize.public.t2 // { arity: 1 }
  252. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  253. Union // { arity: 2 }
  254. Get l4 // { arity: 2 }
  255. Map (null) // { arity: 2 }
  256. Union // { arity: 1 }
  257. Negate // { arity: 1 }
  258. Distinct project=[#0{f1}] // { arity: 1 }
  259. Project (#0{f1}) // { arity: 1 }
  260. Get l4 // { arity: 2 }
  261. Get l0 // { arity: 1 }
  262. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  263. Union // { arity: 2 }
  264. Get l6 // { arity: 2 }
  265. Map (null) // { arity: 2 }
  266. Union // { arity: 1 }
  267. Negate // { arity: 1 }
  268. Distinct project=[#0{f1}] // { arity: 1 }
  269. Project (#0{f1}) // { arity: 1 }
  270. Get l6 // { arity: 2 }
  271. Get l0 // { arity: 1 }
  272. Source materialize.public.t1
  273. filter=((#0{f1}) IS NOT NULL)
  274. Source materialize.public.t2
  275. Target cluster: quickstart
  276. EOF
  277. query II rowsort
  278. SELECT (SELECT * FROM t1 WHERE t1.f1 = t2.f1 + 1) , (SELECT * FROM t1 WHERE t1.f1 = t2.f1 + 2) FROM t2
  279. ----
  280. NULL NULL
  281. 3 NULL
  282. 2 3
  283. #
  284. # Correlated, with distinct SELECT lists
  285. #
  286. query T multiline
  287. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT f1 + 1 FROM t1 WHERE t1.f1 = t2.f1) , (SELECT f1 + 2 FROM t1 WHERE t1.f1 = t2.f1) FROM t2
  288. ----
  289. Explained Query:
  290. With
  291. cte l0 =
  292. Distinct project=[#0{f1}] // { arity: 1 }
  293. ReadStorage materialize.public.t2 // { arity: 1 }
  294. cte l1 =
  295. Project (#0{f1}) // { arity: 1 }
  296. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 2 }
  297. implementation
  298. %0:l0[#0{f1}]UKA » %1:t1[#0{f1}]K
  299. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  300. Get l0 // { arity: 1 }
  301. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  302. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  303. ReadStorage materialize.public.t1 // { arity: 1 }
  304. cte l2 =
  305. Project (#0{f1}, #2) // { arity: 2 }
  306. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  307. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  308. Get l1 // { arity: 1 }
  309. cte l3 =
  310. Union // { arity: 2 }
  311. Map ((#0{f1} + 1)) // { arity: 2 }
  312. Get l1 // { arity: 1 }
  313. Get l2 // { arity: 2 }
  314. cte l4 =
  315. Union // { arity: 2 }
  316. Map ((#0{f1} + 2)) // { arity: 2 }
  317. Get l1 // { arity: 1 }
  318. Get l2 // { arity: 2 }
  319. Return // { arity: 2 }
  320. Project (#2, #4) // { arity: 2 }
  321. Join on=(#0{f1} = #1{f1} = #3{f1}) type=delta // { arity: 5 }
  322. implementation
  323. %0:t2 » %1[#0]K » %2[#0]K
  324. %1 » %0:t2[#0]K » %2[#0]K
  325. %2 » %0:t2[#0]K » %1[#0]K
  326. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  327. ReadStorage materialize.public.t2 // { arity: 1 }
  328. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  329. Union // { arity: 2 }
  330. Get l3 // { arity: 2 }
  331. Map (null) // { arity: 2 }
  332. Union // { arity: 1 }
  333. Negate // { arity: 1 }
  334. Distinct project=[#0{f1}] // { arity: 1 }
  335. Project (#0{f1}) // { arity: 1 }
  336. Get l3 // { arity: 2 }
  337. Get l0 // { arity: 1 }
  338. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  339. Union // { arity: 2 }
  340. Get l4 // { arity: 2 }
  341. Map (null) // { arity: 2 }
  342. Union // { arity: 1 }
  343. Negate // { arity: 1 }
  344. Distinct project=[#0{f1}] // { arity: 1 }
  345. Project (#0{f1}) // { arity: 1 }
  346. Get l4 // { arity: 2 }
  347. Get l0 // { arity: 1 }
  348. Source materialize.public.t1
  349. filter=((#0{f1}) IS NOT NULL)
  350. Source materialize.public.t2
  351. Target cluster: quickstart
  352. EOF
  353. query II
  354. SELECT (SELECT f1 + 1 FROM t1 WHERE t1.f1 = t2.f1) , (SELECT f1 + 2 FROM t1 WHERE t1.f1 = t2.f1) FROM t2
  355. ----
  356. 2 3
  357. 3 4
  358. 4 5
  359. query T multiline
  360. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT MIN(f1) FROM t1 WHERE t1.f1 = t2.f1) , (SELECT MAX(f1) FROM t1 WHERE t1.f1 = t2.f1) FROM t2
  361. ----
  362. Explained Query:
  363. With
  364. cte l0 =
  365. Distinct project=[#0{f1}] // { arity: 1 }
  366. ReadStorage materialize.public.t2 // { arity: 1 }
  367. cte l1 =
  368. Project (#0{f1}) // { arity: 1 }
  369. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 2 }
  370. implementation
  371. %0:l0[#0{f1}]UKA » %1:t1[#0{f1}]K
  372. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  373. Get l0 // { arity: 1 }
  374. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  375. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  376. ReadStorage materialize.public.t1 // { arity: 1 }
  377. cte l2 =
  378. Reduce group_by=[#0{f1}] aggregates=[min(#0{f1})] // { arity: 2 }
  379. Get l1 // { arity: 1 }
  380. cte l3 =
  381. Union // { arity: 2 }
  382. Get l2 // { arity: 2 }
  383. Map (null) // { arity: 2 }
  384. Union // { arity: 1 }
  385. Negate // { arity: 1 }
  386. Project (#0{f1}) // { arity: 1 }
  387. Get l2 // { arity: 2 }
  388. Get l0 // { arity: 1 }
  389. cte l4 =
  390. Reduce group_by=[#0{f1}] aggregates=[max(#0{f1})] // { arity: 2 }
  391. Get l1 // { arity: 1 }
  392. cte l5 =
  393. Union // { arity: 2 }
  394. Get l4 // { arity: 2 }
  395. Map (null) // { arity: 2 }
  396. Union // { arity: 1 }
  397. Negate // { arity: 1 }
  398. Project (#0{f1}) // { arity: 1 }
  399. Get l4 // { arity: 2 }
  400. Get l0 // { arity: 1 }
  401. Return // { arity: 2 }
  402. Project (#2{min_f1}, #4{max_f1}) // { arity: 2 }
  403. Join on=(#0{f1} = #1{f1} = #3{f1}) type=delta // { arity: 5 }
  404. implementation
  405. %0:t2 » %1[#0]UK » %2[#0]UK
  406. %1 » %2[#0]UK » %0:t2[#0]K
  407. %2 » %1[#0]UK » %0:t2[#0]K
  408. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  409. ReadStorage materialize.public.t2 // { arity: 1 }
  410. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  411. Union // { arity: 2 }
  412. Get l3 // { arity: 2 }
  413. Map (null) // { arity: 2 }
  414. Union // { arity: 1 }
  415. Negate // { arity: 1 }
  416. Project (#0{f1}) // { arity: 1 }
  417. Get l3 // { arity: 2 }
  418. Get l0 // { arity: 1 }
  419. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  420. Union // { arity: 2 }
  421. Get l5 // { arity: 2 }
  422. Map (null) // { arity: 2 }
  423. Union // { arity: 1 }
  424. Negate // { arity: 1 }
  425. Project (#0{f1}) // { arity: 1 }
  426. Get l5 // { arity: 2 }
  427. Get l0 // { arity: 1 }
  428. Source materialize.public.t1
  429. filter=((#0{f1}) IS NOT NULL)
  430. Source materialize.public.t2
  431. Target cluster: quickstart
  432. EOF
  433. query II
  434. SELECT (SELECT MIN(f1) FROM t1 WHERE t1.f1 = t2.f1) , (SELECT MAX(f1) FROM t1 WHERE t1.f1 = t2.f1) FROM t2
  435. ----
  436. 1 1
  437. 2 2
  438. 3 3
  439. #
  440. # Nested
  441. #
  442. query T multiline
  443. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT (SELECT * FROM t1 WHERE t1.f1 = t2.f1) FROM t2 WHERE t2.f1 = t3.f1) FROM t3
  444. ----
  445. Explained Query:
  446. With
  447. cte l0 =
  448. Distinct project=[#0{f1}] // { arity: 1 }
  449. ReadStorage materialize.public.t3 // { arity: 1 }
  450. cte l1 =
  451. Project (#0{f1}) // { arity: 1 }
  452. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 2 }
  453. implementation
  454. %0:l0[#0{f1}]UKA » %1:t2[#0{f1}]K
  455. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  456. Get l0 // { arity: 1 }
  457. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  458. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  459. ReadStorage materialize.public.t2 // { arity: 1 }
  460. cte l2 =
  461. Distinct project=[#0{f1}] // { arity: 1 }
  462. Get l1 // { arity: 1 }
  463. cte l3 =
  464. Project (#0{f1}) // { arity: 1 }
  465. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 2 }
  466. implementation
  467. %0:l2[#0{f1}]UKA » %1:t1[#0{f1}]K
  468. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  469. Get l2 // { arity: 1 }
  470. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  471. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  472. ReadStorage materialize.public.t1 // { arity: 1 }
  473. cte l4 =
  474. Union // { arity: 2 }
  475. Project (#0{f1}, #0{f1}) // { arity: 2 }
  476. Get l3 // { arity: 1 }
  477. Project (#0{f1}, #2) // { arity: 2 }
  478. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  479. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  480. Get l3 // { arity: 1 }
  481. cte l5 =
  482. Project (#0{f1}, #2{f1}) // { arity: 2 }
  483. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 3 }
  484. implementation
  485. %0:l1[#0]K » %1[#0]K
  486. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  487. Get l1 // { arity: 1 }
  488. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  489. Union // { arity: 2 }
  490. Get l4 // { arity: 2 }
  491. Map (null) // { arity: 2 }
  492. Union // { arity: 1 }
  493. Negate // { arity: 1 }
  494. Distinct project=[#0{f1}] // { arity: 1 }
  495. Project (#0{f1}) // { arity: 1 }
  496. Get l4 // { arity: 2 }
  497. Get l2 // { arity: 1 }
  498. cte l6 =
  499. Union // { arity: 2 }
  500. Get l5 // { arity: 2 }
  501. Project (#0{f1}, #2) // { arity: 2 }
  502. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  503. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  504. Project (#0{f1}) // { arity: 1 }
  505. Get l5 // { arity: 2 }
  506. Return // { arity: 1 }
  507. Project (#2{f1}) // { arity: 1 }
  508. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 3 }
  509. implementation
  510. %0:t3[#0]K » %1[#0]K
  511. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  512. ReadStorage materialize.public.t3 // { arity: 1 }
  513. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  514. Union // { arity: 2 }
  515. Get l6 // { arity: 2 }
  516. Map (null) // { arity: 2 }
  517. Union // { arity: 1 }
  518. Negate // { arity: 1 }
  519. Distinct project=[#0{f1}] // { arity: 1 }
  520. Project (#0{f1}) // { arity: 1 }
  521. Get l6 // { arity: 2 }
  522. Get l0 // { arity: 1 }
  523. Source materialize.public.t1
  524. filter=((#0{f1}) IS NOT NULL)
  525. Source materialize.public.t2
  526. filter=((#0{f1}) IS NOT NULL)
  527. Source materialize.public.t3
  528. Target cluster: quickstart
  529. EOF
  530. query I rowsort
  531. SELECT (SELECT (SELECT * FROM t1 WHERE t1.f1 = t2.f1) FROM t2 WHERE t2.f1 = t3.f1) FROM t3
  532. ----
  533. 1
  534. 2
  535. 3
  536. #
  537. # Inside an aggregate
  538. #
  539. query T multiline
  540. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT MIN((SELECT f1 FROM t1 WHERE t1.f1 = t2.f1)), MAX((SELECT f1 FROM t1 WHERE t1.f1 = t2.f1)) FROM t2;
  541. ----
  542. Explained Query:
  543. With
  544. cte l0 =
  545. Distinct project=[#0{f1}] // { arity: 1 }
  546. ReadStorage materialize.public.t2 // { arity: 1 }
  547. cte l1 =
  548. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  549. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  550. ReadStorage materialize.public.t1 // { arity: 1 }
  551. cte l2 =
  552. Project (#0{f1}) // { arity: 1 }
  553. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 2 }
  554. implementation
  555. %0:l0[#0{f1}]UKA » %1:l1[#0{f1}]K
  556. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  557. Get l0 // { arity: 1 }
  558. Get l1 // { arity: 1 }
  559. cte l3 =
  560. Union // { arity: 2 }
  561. Project (#0{f1}, #0{f1}) // { arity: 2 }
  562. Get l2 // { arity: 1 }
  563. Project (#0{f1}, #2) // { arity: 2 }
  564. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  565. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  566. Get l2 // { arity: 1 }
  567. cte l4 =
  568. Project (#0{f1}, #2{f1}) // { arity: 2 }
  569. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 3 }
  570. implementation
  571. %0:t2[#0]K » %1[#0]K
  572. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  573. ReadStorage materialize.public.t2 // { arity: 1 }
  574. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  575. Union // { arity: 2 }
  576. Get l3 // { arity: 2 }
  577. Map (null) // { arity: 2 }
  578. Union // { arity: 1 }
  579. Negate // { arity: 1 }
  580. Distinct project=[#0{f1}] // { arity: 1 }
  581. Project (#0{f1}) // { arity: 1 }
  582. Get l3 // { arity: 2 }
  583. Get l0 // { arity: 1 }
  584. cte l5 =
  585. Distinct project=[#0{f1}] // { arity: 1 }
  586. Project (#0{f1}) // { arity: 1 }
  587. Get l4 // { arity: 2 }
  588. cte l6 =
  589. Project (#0{f1}) // { arity: 1 }
  590. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 2 }
  591. implementation
  592. %0:l5[#0{f1}]UKA » %1:l1[#0{f1}]K
  593. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  594. Get l5 // { arity: 1 }
  595. Get l1 // { arity: 1 }
  596. cte l7 =
  597. Union // { arity: 2 }
  598. Project (#0{f1}, #0{f1}) // { arity: 2 }
  599. Get l6 // { arity: 1 }
  600. Project (#0{f1}, #2) // { arity: 2 }
  601. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  602. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  603. Get l6 // { arity: 1 }
  604. cte l8 =
  605. Reduce aggregates=[min(#0{f1}), max(#1{f1})] // { arity: 2 }
  606. Project (#1{f1}, #3{f1}) // { arity: 2 }
  607. Join on=(#0{f1} = #2{f1}) type=differential // { arity: 4 }
  608. implementation
  609. %0:l4[#0]K » %1[#0]K
  610. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  611. Get l4 // { arity: 2 }
  612. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  613. Union // { arity: 2 }
  614. Get l7 // { arity: 2 }
  615. Map (null) // { arity: 2 }
  616. Union // { arity: 1 }
  617. Negate // { arity: 1 }
  618. Distinct project=[#0{f1}] // { arity: 1 }
  619. Project (#0{f1}) // { arity: 1 }
  620. Get l7 // { arity: 2 }
  621. Get l5 // { arity: 1 }
  622. Return // { arity: 2 }
  623. Union // { arity: 2 }
  624. Get l8 // { arity: 2 }
  625. Map (null, null) // { arity: 2 }
  626. Union // { arity: 0 }
  627. Negate // { arity: 0 }
  628. Project () // { arity: 0 }
  629. Get l8 // { arity: 2 }
  630. Constant // { arity: 0 }
  631. - ()
  632. Source materialize.public.t1
  633. filter=((#0{f1}) IS NOT NULL)
  634. Source materialize.public.t2
  635. Target cluster: quickstart
  636. EOF
  637. query II
  638. SELECT MIN((SELECT f1 FROM t1 WHERE t1.f1 = t2.f1)), MAX((SELECT f1 FROM t1 WHERE t1.f1 = t2.f1)) FROM t2;
  639. ----
  640. 1 3
  641. #
  642. # Join on the inside
  643. #
  644. query T multiline
  645. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  646. (SELECT t1.f1 FROM t1, t2 WHERE t1.f1 = t3.f1 AND t2.f1 = t3.f1),
  647. (SELECT t1.f1 FROM t1, t2 WHERE t1.f1 = t3.f1 AND t2.f1 = t3.f1)
  648. FROM t3
  649. ----
  650. Explained Query:
  651. With
  652. cte l0 =
  653. Distinct project=[#0{f1}] // { arity: 1 }
  654. ReadStorage materialize.public.t3 // { arity: 1 }
  655. cte l1 =
  656. Project (#0{f1}) // { arity: 1 }
  657. Join on=(#0{f1} = #1{f1} = #2{f1}) type=delta // { arity: 3 }
  658. implementation
  659. %0:l0 » %1:t1[#0{f1}]K » %2:t2[#0{f1}]K
  660. %1:t1 » %0:l0[#0]UKA » %2:t2[#0{f1}]K
  661. %2:t2 » %0:l0[#0]UKA » %1:t1[#0{f1}]K
  662. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  663. Get l0 // { arity: 1 }
  664. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  665. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  666. ReadStorage materialize.public.t1 // { arity: 1 }
  667. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  668. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  669. ReadStorage materialize.public.t2 // { arity: 1 }
  670. cte l2 =
  671. Union // { arity: 2 }
  672. Project (#0{f1}, #0{f1}) // { arity: 2 }
  673. Get l1 // { arity: 1 }
  674. Project (#0{f1}, #2) // { arity: 2 }
  675. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  676. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  677. Get l1 // { arity: 1 }
  678. Return // { arity: 2 }
  679. Project (#2{f1}, #2{f1}) // { arity: 2 }
  680. Join on=(#0{f1} = #1{f1}) type=differential // { arity: 3 }
  681. implementation
  682. %0:t3[#0]K » %1[#0]K
  683. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  684. ReadStorage materialize.public.t3 // { arity: 1 }
  685. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  686. Union // { arity: 2 }
  687. Get l2 // { arity: 2 }
  688. Map (null) // { arity: 2 }
  689. Union // { arity: 1 }
  690. Negate // { arity: 1 }
  691. Distinct project=[#0{f1}] // { arity: 1 }
  692. Project (#0{f1}) // { arity: 1 }
  693. Get l2 // { arity: 2 }
  694. Get l0 // { arity: 1 }
  695. Source materialize.public.t1
  696. filter=((#0{f1}) IS NOT NULL)
  697. Source materialize.public.t2
  698. filter=((#0{f1}) IS NOT NULL)
  699. Source materialize.public.t3
  700. Target cluster: quickstart
  701. EOF
  702. query II
  703. SELECT
  704. (SELECT t1.f1 FROM t1, t2 WHERE t1.f1 = t3.f1 AND t2.f1 = t3.f1),
  705. (SELECT t1.f1 FROM t1, t2 WHERE t1.f1 = t3.f1 AND t2.f1 = t3.f1)
  706. FROM t3
  707. ----
  708. 1 1
  709. 2 2
  710. 3 3
  711. #
  712. # Join on the outside
  713. #
  714. query T multiline
  715. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  716. (SELECT * FROM t1 WHERE t1.f1 = t2.f1 AND t1.f1 = t3.f1),
  717. (SELECT * FROM t1 WHERE t1.f1 = t2.f1 AND t1.f1 = t3.f1)
  718. FROM t2, t3
  719. ----
  720. Explained Query:
  721. With
  722. cte l0 =
  723. CrossJoin type=differential // { arity: 2 }
  724. implementation
  725. %0:t2[×] » %1:t3[×]
  726. ArrangeBy keys=[[]] // { arity: 1 }
  727. ReadStorage materialize.public.t2 // { arity: 1 }
  728. ArrangeBy keys=[[]] // { arity: 1 }
  729. ReadStorage materialize.public.t3 // { arity: 1 }
  730. cte l1 =
  731. Distinct project=[#0{f1}, #1{f1}] // { arity: 2 }
  732. Get l0 // { arity: 2 }
  733. cte l2 =
  734. Project (#0{f1}, #1{f1}) // { arity: 2 }
  735. Join on=(#0{f1} = #2{f1}) type=differential // { arity: 3 }
  736. implementation
  737. %0:l1[#0{f1}]UKf » %1:t1[#0{f1}]Kf
  738. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  739. Filter (#0{f1} = #1{f1}) // { arity: 2 }
  740. Get l1 // { arity: 2 }
  741. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  742. Filter (#0{f1}) IS NOT NULL // { arity: 1 }
  743. ReadStorage materialize.public.t1 // { arity: 1 }
  744. cte l3 =
  745. Union // { arity: 3 }
  746. Project (#0{f1}, #1{f1}, #0{f1}) // { arity: 3 }
  747. Get l2 // { arity: 2 }
  748. Project (#0{f1}, #1{f1}, #3) // { arity: 3 }
  749. FlatMap guard_subquery_size(#2{count}) // { arity: 4 }
  750. Reduce group_by=[#0{f1}, #1{f1}] aggregates=[count(*)] // { arity: 3 }
  751. Get l2 // { arity: 2 }
  752. Return // { arity: 2 }
  753. Project (#4{f1}, #4{f1}) // { arity: 2 }
  754. Join on=(#0{f1} = #2{f1} AND #1{f1} = #3{f1}) type=differential // { arity: 5 }
  755. implementation
  756. %0:l0[#0, #1]KK » %1[#0, #1]KK
  757. ArrangeBy keys=[[#0{f1}, #1{f1}]] // { arity: 2 }
  758. Get l0 // { arity: 2 }
  759. ArrangeBy keys=[[#0{f1}, #1{f1}]] // { arity: 3 }
  760. Union // { arity: 3 }
  761. Get l3 // { arity: 3 }
  762. Map (null) // { arity: 3 }
  763. Union // { arity: 2 }
  764. Negate // { arity: 2 }
  765. Distinct project=[#0{f1}, #1{f1}] // { arity: 2 }
  766. Project (#0{f1}, #1{f1}) // { arity: 2 }
  767. Get l3 // { arity: 3 }
  768. Get l1 // { arity: 2 }
  769. Source materialize.public.t1
  770. filter=((#0{f1}) IS NOT NULL)
  771. Source materialize.public.t2
  772. Source materialize.public.t3
  773. Target cluster: quickstart
  774. EOF
  775. query II rowsort
  776. SELECT
  777. (SELECT * FROM t1 WHERE t1.f1 = t2.f1 AND t1.f1 = t3.f1),
  778. (SELECT * FROM t1 WHERE t1.f1 = t2.f1 AND t1.f1 = t3.f1)
  779. FROM t2, t3
  780. ----
  781. NULL NULL
  782. NULL NULL
  783. NULL NULL
  784. NULL NULL
  785. NULL NULL
  786. NULL NULL
  787. 1 1
  788. 2 2
  789. 3 3
  790. query T multiline
  791. EXPLAIN RAW PLAN WITH(types) FOR SELECT CASE (SELECT 1) WHEN 1 THEN 0 ELSE 2 END, 'TEXT';
  792. ----
  793. With
  794. cte [l1 as subquery-1] =
  795. Map (1)
  796. Constant
  797. - ()
  798. Return
  799. Map (case when (select(Get l1) = 1) then 0 else 2 end, "TEXT")
  800. Constant
  801. - ()
  802. Target cluster: quickstart
  803. EOF
  804. query T multiline
  805. EXPLAIN DECORRELATED PLAN WITH(arity, types) FOR SELECT CASE (SELECT 1) WHEN 1 THEN 0 ELSE 2 END, 'TEXT';
  806. ----
  807. With
  808. cte l0 =
  809. Project (#0) // { arity: 1, types: "(integer)" }
  810. Map (1) // { arity: 1, types: "(integer)" }
  811. Constant // { arity: 0, types: "()" }
  812. - ()
  813. cte l1 =
  814. Union // { arity: 1, types: "(integer)" }
  815. Get l0 // { arity: 1, types: "(integer)" }
  816. Project (#1) // { arity: 1, types: "(integer)" }
  817. FlatMap guard_subquery_size(#0) // { arity: 2, types: "(bigint, integer)" }
  818. Reduce aggregates=[count(*)] // { arity: 1, types: "(bigint)" }
  819. Get l0 // { arity: 1, types: "(integer)" }
  820. Return // { arity: 2, types: "(integer, text)" }
  821. Project (#1, #2) // { arity: 2, types: "(integer, text)" }
  822. Map (case when (#0 = 1) then 0 else 2 end, "TEXT") // { arity: 3, types: "(integer?, integer, text)" }
  823. Project (#0) // { arity: 1, types: "(integer?)" }
  824. CrossJoin // { arity: 1, types: "(integer?)" }
  825. Constant // { arity: 0, types: "()" }
  826. - ()
  827. Union // { arity: 1, types: "(integer?)" }
  828. Get l1 // { arity: 1, types: "(integer)" }
  829. CrossJoin // { arity: 1, types: "(integer?)" }
  830. Project () // { arity: 0, types: "()" }
  831. CrossJoin // { arity: 0, types: "()" }
  832. Union // { arity: 0, types: "()" }
  833. Negate // { arity: 0, types: "()" }
  834. Distinct project=[] // { arity: 0, types: "()" }
  835. Get l1 // { arity: 1, types: "(integer)" }
  836. Distinct project=[] // { arity: 0, types: "()" }
  837. Constant // { arity: 0, types: "()" }
  838. - ()
  839. Constant // { arity: 0, types: "()" }
  840. - ()
  841. Constant // { arity: 1, types: "(integer?)" }
  842. - (null)
  843. Target cluster: quickstart
  844. EOF
  845. query IT
  846. SELECT CASE (SELECT 1) WHEN 1 THEN 0 ELSE 2 END, 'TEXT';
  847. ----
  848. 0 TEXT