relation_cse.slt 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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 Common subexpression elimination for Relations.
  11. # PR https://github.com/MaterializeInc/materialize/pull/7715
  12. #
  13. statement ok
  14. CREATE TABLE t1 (f1 INTEGER, f2 INTEGER);
  15. statement ok
  16. CREATE INDEX i1 ON t1 (f1);
  17. statement ok
  18. CREATE TABLE t2 (f1 INTEGER, f2 INTEGER);
  19. ##
  20. ## Tests around the placement of CSEs in the top-level query itself
  21. ##
  22. # no CSE detection here, no predicates in query
  23. query T multiline
  24. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1 , t1 AS a2;
  25. ----
  26. Explained Query:
  27. With
  28. cte l0 =
  29. ArrangeBy keys=[[]] // { arity: 2 }
  30. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  31. Return // { arity: 4 }
  32. CrossJoin type=differential // { arity: 4 }
  33. implementation
  34. %0:l0[×] » %1:l0[×]
  35. Get l0 // { arity: 2 }
  36. Get l0 // { arity: 2 }
  37. Used Indexes:
  38. - materialize.public.i1 (*** full scan ***)
  39. Target cluster: quickstart
  40. EOF
  41. #
  42. # Inner joins
  43. #
  44. query T multiline
  45. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1 , t1 AS a2, t1 AS a3;
  46. ----
  47. Explained Query:
  48. With
  49. cte l0 =
  50. ArrangeBy keys=[[]] // { arity: 2 }
  51. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  52. Return // { arity: 6 }
  53. CrossJoin type=delta // { arity: 6 }
  54. implementation
  55. %0:l0 » %1:l0[×] » %2:l0[×]
  56. %1:l0 » %0:l0[×] » %2:l0[×]
  57. %2:l0 » %0:l0[×] » %1:l0[×]
  58. Get l0 // { arity: 2 }
  59. Get l0 // { arity: 2 }
  60. Get l0 // { arity: 2 }
  61. Used Indexes:
  62. - materialize.public.i1 (*** full scan ***)
  63. Target cluster: quickstart
  64. EOF
  65. query T multiline
  66. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1 , t1 AS a2 WHERE a1.f1 = 1 AND a2.f1 = 1;
  67. ----
  68. Explained Query:
  69. With
  70. cte l0 =
  71. ArrangeBy keys=[[]] // { arity: 2 }
  72. Project (#0{f1}, #1{f2}) // { arity: 2 }
  73. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  74. Return // { arity: 4 }
  75. CrossJoin type=differential // { arity: 4 }
  76. implementation
  77. %0:l0[×]e » %1:l0[×]e
  78. Get l0 // { arity: 2 }
  79. Get l0 // { arity: 2 }
  80. Used Indexes:
  81. - materialize.public.i1 (lookup)
  82. Target cluster: quickstart
  83. EOF
  84. query T multiline
  85. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1 , t1 AS a2, t1 AS a3 WHERE a1.f1 = 1 AND a2.f1 = 1 AND a3.f1 = 1;
  86. ----
  87. Explained Query:
  88. With
  89. cte l0 =
  90. ArrangeBy keys=[[]] // { arity: 2 }
  91. Project (#0{f1}, #1{f2}) // { arity: 2 }
  92. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  93. Return // { arity: 6 }
  94. CrossJoin type=delta // { arity: 6 }
  95. implementation
  96. %0:l0 » %1:l0[×]e » %2:l0[×]e
  97. %1:l0 » %0:l0[×]e » %2:l0[×]e
  98. %2:l0 » %0:l0[×]e » %1:l0[×]e
  99. Get l0 // { arity: 2 }
  100. Get l0 // { arity: 2 }
  101. Get l0 // { arity: 2 }
  102. Used Indexes:
  103. - materialize.public.i1 (lookup)
  104. Target cluster: quickstart
  105. EOF
  106. #
  107. # Outer join
  108. #
  109. query T multiline
  110. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1) WHERE a1.f1 = 1 AND a2.f1 = 1;
  111. ----
  112. Explained Query:
  113. With
  114. cte l0 =
  115. Project (#0{f1}, #1{f2}) // { arity: 2 }
  116. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  117. Return // { arity: 3 }
  118. CrossJoin type=differential // { arity: 3 }
  119. implementation
  120. %0:l0[×]e » %1:l0[×]e
  121. ArrangeBy keys=[[]] // { arity: 2 }
  122. Get l0 // { arity: 2 }
  123. ArrangeBy keys=[[]] // { arity: 1 }
  124. Project (#1{f2}) // { arity: 1 }
  125. Get l0 // { arity: 2 }
  126. Used Indexes:
  127. - materialize.public.i1 (lookup)
  128. Target cluster: quickstart
  129. EOF
  130. #
  131. # Subqueries
  132. #
  133. query T multiline
  134. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1) AND f2 = (SELECT f1 FROM t1);
  135. ----
  136. Explained Query:
  137. With
  138. cte l0 =
  139. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  140. Union // { arity: 1 }
  141. Project (#0{f1}) // { arity: 1 }
  142. Filter (#0{f1}) IS NOT NULL // { arity: 2 }
  143. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  144. Project (#1) // { arity: 1 }
  145. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  146. Reduce aggregates=[count(*)] // { arity: 1 }
  147. Project () // { arity: 0 }
  148. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  149. Return // { arity: 2 }
  150. Project (#0{f1}, #1{f2}) // { arity: 2 }
  151. Join on=(#0{f1} = #2{f1} AND #1{f2} = #3{f1}) type=delta // { arity: 4 }
  152. implementation
  153. %0:t1 » %1:l0[#0]K » %2:l0[#0]K
  154. %1:l0 » %0:t1[#0{f1}]KA » %2:l0[#0]K
  155. %2:l0 » %0:t1[#1{f2}]K » %1:l0[#0]K
  156. ArrangeBy keys=[[#0{f1}], [#1{f2}]] // { arity: 2 }
  157. Filter (#0{f1}) IS NOT NULL AND (#1{f2}) IS NOT NULL // { arity: 2 }
  158. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  159. Get l0 // { arity: 1 }
  160. Get l0 // { arity: 1 }
  161. Used Indexes:
  162. - materialize.public.i1 (*** full scan ***)
  163. Target cluster: quickstart
  164. EOF
  165. query T multiline
  166. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1 WHERE f1 = 1) AND f2 = (SELECT f1 FROM t1 WHERE f1 = 1);
  167. ----
  168. Explained Query:
  169. With
  170. cte l0 =
  171. Project (#0{f1}) // { arity: 1 }
  172. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  173. cte l1 =
  174. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  175. Union // { arity: 1 }
  176. Get l0 // { arity: 1 }
  177. Project (#1) // { arity: 1 }
  178. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  179. Reduce aggregates=[count(*)] // { arity: 1 }
  180. Project () // { arity: 0 }
  181. Get l0 // { arity: 1 }
  182. Return // { arity: 2 }
  183. Project (#0{f1}, #1{f2}) // { arity: 2 }
  184. Join on=(#0{f1} = #2{f1} AND #1{f2} = #3{f1}) type=delta // { arity: 4 }
  185. implementation
  186. %0:t1 » %1:l1[#0]K » %2:l1[#0]K
  187. %1:l1 » %0:t1[#0{f1}]KA » %2:l1[#0]K
  188. %2:l1 » %0:t1[#1{f2}]K » %1:l1[#0]K
  189. ArrangeBy keys=[[#0{f1}], [#1{f2}]] // { arity: 2 }
  190. Filter (#0{f1}) IS NOT NULL AND (#1{f2}) IS NOT NULL // { arity: 2 }
  191. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  192. Get l1 // { arity: 1 }
  193. Get l1 // { arity: 1 }
  194. Used Indexes:
  195. - materialize.public.i1 (*** full scan ***, lookup)
  196. Target cluster: quickstart
  197. EOF
  198. query T multiline
  199. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1) AND EXISTS (SELECT f1 FROM t1);
  200. ----
  201. Explained Query:
  202. Project (#0{f1}, #1{f2}) // { arity: 2 }
  203. Join on=(#0{f1} = #2{f1}) type=differential // { arity: 3 }
  204. implementation
  205. %0:t1[#0{f1}]KA » %1[#0]K
  206. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  207. ReadIndex on=t1 i1=[differential join] // { arity: 2 }
  208. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  209. Union // { arity: 1 }
  210. Project (#0{f1}) // { arity: 1 }
  211. Filter (#0{f1}) IS NOT NULL // { arity: 2 }
  212. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  213. Project (#1) // { arity: 1 }
  214. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  215. Reduce aggregates=[count(*)] // { arity: 1 }
  216. Project () // { arity: 0 }
  217. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  218. Used Indexes:
  219. - materialize.public.i1 (*** full scan ***, differential join)
  220. Target cluster: quickstart
  221. EOF
  222. query T multiline
  223. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1) OR f2 = (SELECT f1 FROM t1);
  224. ----
  225. Explained Query:
  226. With
  227. cte l0 =
  228. Union // { arity: 1 }
  229. Project (#0{f1}) // { arity: 1 }
  230. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  231. Project (#1) // { arity: 1 }
  232. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  233. Reduce aggregates=[count(*)] // { arity: 1 }
  234. Project () // { arity: 0 }
  235. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  236. cte l1 =
  237. ArrangeBy keys=[[]] // { arity: 1 }
  238. Union // { arity: 1 }
  239. Get l0 // { arity: 1 }
  240. Map (null) // { arity: 1 }
  241. Union // { arity: 0 }
  242. Negate // { arity: 0 }
  243. Distinct project=[] // { arity: 0 }
  244. Project () // { arity: 0 }
  245. Get l0 // { arity: 1 }
  246. Constant // { arity: 0 }
  247. - ()
  248. Return // { arity: 2 }
  249. Project (#0{f1}, #1{f2}) // { arity: 2 }
  250. Filter ((#0{f1} = #2{f1}) OR (#1{f2} = #3{f1})) // { arity: 4 }
  251. CrossJoin type=delta // { arity: 4 }
  252. implementation
  253. %0:t1 » %1:l1[×] » %2:l1[×]
  254. %1:l1 » %0:t1[×] » %2:l1[×]
  255. %2:l1 » %0:t1[×] » %1:l1[×]
  256. ArrangeBy keys=[[]] // { arity: 2 }
  257. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  258. Get l1 // { arity: 1 }
  259. Get l1 // { arity: 1 }
  260. Used Indexes:
  261. - materialize.public.i1 (*** full scan ***)
  262. Target cluster: quickstart
  263. EOF
  264. #
  265. # CSEs in derived tables
  266. #
  267. query T multiline
  268. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT *
  269. FROM (SELECT * FROM t1 WHERE f1 = 1) AS a1
  270. JOIN (SELECT * FROM t1 WHERE f1 = 1) AS a2
  271. ON TRUE
  272. ----
  273. Explained Query:
  274. With
  275. cte l0 =
  276. ArrangeBy keys=[[]] // { arity: 2 }
  277. Project (#0{f1}, #1{f2}) // { arity: 2 }
  278. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  279. Return // { arity: 4 }
  280. CrossJoin type=differential // { arity: 4 }
  281. implementation
  282. %0:l0[×]e » %1:l0[×]e
  283. Get l0 // { arity: 2 }
  284. Get l0 // { arity: 2 }
  285. Used Indexes:
  286. - materialize.public.i1 (lookup)
  287. Target cluster: quickstart
  288. EOF
  289. query T multiline
  290. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT *
  291. FROM (SELECT * FROM t1 WHERE f1 = 1) AS a1
  292. , (SELECT * FROM t1 WHERE f1 = 1) AS a2
  293. WHERE a1.f2 = 2
  294. AND a2.f2 = 2
  295. ----
  296. Explained Query:
  297. With
  298. cte l0 =
  299. ArrangeBy keys=[[]] // { arity: 2 }
  300. Project (#0{f1}, #1{f2}) // { arity: 2 }
  301. Filter (#1{f2} = 2) // { arity: 3 }
  302. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  303. Return // { arity: 4 }
  304. CrossJoin type=differential // { arity: 4 }
  305. implementation
  306. %0:l0[×]ef » %1:l0[×]ef
  307. Get l0 // { arity: 2 }
  308. Get l0 // { arity: 2 }
  309. Used Indexes:
  310. - materialize.public.i1 (lookup)
  311. Target cluster: quickstart
  312. EOF
  313. # No CSE with only partially-overlapping predicates
  314. query T multiline
  315. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT *
  316. FROM (SELECT * FROM t1 WHERE f1 = 1) AS a1
  317. , (SELECT * FROM t1 WHERE f1 = 1) AS a2
  318. WHERE a1.f2 = 2
  319. AND a2.f2 = 3
  320. ----
  321. Explained Query:
  322. With
  323. cte l0 =
  324. Project (#0{f1}, #1{f2}) // { arity: 2 }
  325. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  326. Return // { arity: 4 }
  327. CrossJoin type=differential // { arity: 4 }
  328. implementation
  329. %0:l0[×]ef » %1:l0[×]ef
  330. ArrangeBy keys=[[]] // { arity: 2 }
  331. Filter (#1{f2} = 2) // { arity: 2 }
  332. Get l0 // { arity: 2 }
  333. ArrangeBy keys=[[]] // { arity: 2 }
  334. Filter (#1{f2} = 3) // { arity: 2 }
  335. Get l0 // { arity: 2 }
  336. Used Indexes:
  337. - materialize.public.i1 (lookup)
  338. Target cluster: quickstart
  339. EOF
  340. #
  341. # CSEs in UNION branches
  342. #
  343. query T multiline
  344. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 = 1 UNION ALL SELECT * FROM t1 WHERE f1 = 1 UNION ALL SELECT * FROM t1 WHERE f1 = 1;
  345. ----
  346. Explained Query:
  347. With
  348. cte l0 =
  349. Project (#0{f1}, #1{f2}) // { arity: 2 }
  350. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  351. Return // { arity: 2 }
  352. Union // { arity: 2 }
  353. Get l0 // { arity: 2 }
  354. Get l0 // { arity: 2 }
  355. Get l0 // { arity: 2 }
  356. Used Indexes:
  357. - materialize.public.i1 (lookup)
  358. Target cluster: quickstart
  359. EOF
  360. query T multiline
  361. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 = 1 UNION ALL SELECT * FROM t1 WHERE f1 = 1 UNION SELECT * FROM t1 WHERE f1 = 1;
  362. ----
  363. Explained Query:
  364. With
  365. cte l0 =
  366. Project (#1{f2}) // { arity: 1 }
  367. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  368. Return // { arity: 2 }
  369. Project (#1, #0{f2}) // { arity: 2 }
  370. Map (1) // { arity: 2 }
  371. Distinct project=[#0{f2}] // { arity: 1 }
  372. Union // { arity: 1 }
  373. Get l0 // { arity: 1 }
  374. Get l0 // { arity: 1 }
  375. Get l0 // { arity: 1 }
  376. Used Indexes:
  377. - materialize.public.i1 (lookup)
  378. Target cluster: quickstart
  379. EOF
  380. #
  381. # CSEs in the SELECT list
  382. #
  383. query T multiline
  384. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT f1 FROM t1 WHERE f1 = 1) , (SELECT f1 FROM t1 WHERE f1 = 1) FROM t1;
  385. ----
  386. Explained Query:
  387. With
  388. cte l0 =
  389. Project (#0{f1}) // { arity: 1 }
  390. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  391. cte l1 =
  392. Union // { arity: 1 }
  393. Get l0 // { arity: 1 }
  394. Project (#1) // { arity: 1 }
  395. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  396. Reduce aggregates=[count(*)] // { arity: 1 }
  397. Project () // { arity: 0 }
  398. Get l0 // { arity: 1 }
  399. Return // { arity: 2 }
  400. Project (#0{f1}, #0{f1}) // { arity: 2 }
  401. CrossJoin type=differential // { arity: 1 }
  402. implementation
  403. %0:t1[×] » %1[×]
  404. ArrangeBy keys=[[]] // { arity: 0 }
  405. Project () // { arity: 0 }
  406. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  407. ArrangeBy keys=[[]] // { arity: 1 }
  408. Union // { arity: 1 }
  409. Get l1 // { arity: 1 }
  410. Map (null) // { arity: 1 }
  411. Union // { arity: 0 }
  412. Negate // { arity: 0 }
  413. Distinct project=[] // { arity: 0 }
  414. Project () // { arity: 0 }
  415. Get l1 // { arity: 1 }
  416. Constant // { arity: 0 }
  417. - ()
  418. Used Indexes:
  419. - materialize.public.i1 (*** full scan ***, lookup)
  420. Target cluster: quickstart
  421. EOF
  422. query T multiline
  423. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT MIN((SELECT f1 FROM t1 WHERE f1 = 1)), MAX((SELECT f1 FROM t1 WHERE f1 = 1)) FROM t1;
  424. ----
  425. Explained Query:
  426. With
  427. cte l0 =
  428. Project (#0{f1}) // { arity: 1 }
  429. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  430. cte l1 =
  431. Union // { arity: 1 }
  432. Get l0 // { arity: 1 }
  433. Project (#1) // { arity: 1 }
  434. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  435. Reduce aggregates=[count(*)] // { arity: 1 }
  436. Project () // { arity: 0 }
  437. Get l0 // { arity: 1 }
  438. cte l2 =
  439. ArrangeBy keys=[[]] // { arity: 1 }
  440. Union // { arity: 1 }
  441. Get l1 // { arity: 1 }
  442. Map (null) // { arity: 1 }
  443. Union // { arity: 0 }
  444. Negate // { arity: 0 }
  445. Distinct project=[] // { arity: 0 }
  446. Project () // { arity: 0 }
  447. Get l1 // { arity: 1 }
  448. Constant // { arity: 0 }
  449. - ()
  450. cte l3 =
  451. Reduce aggregates=[min(#0{f1}), max(#1{f1})] // { arity: 2 }
  452. CrossJoin type=delta // { arity: 2 }
  453. implementation
  454. %0:t1 » %1:l2[×] » %2:l2[×]
  455. %1:l2 » %0:t1[×] » %2:l2[×]
  456. %2:l2 » %0:t1[×] » %1:l2[×]
  457. ArrangeBy keys=[[]] // { arity: 0 }
  458. Project () // { arity: 0 }
  459. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  460. Get l2 // { arity: 1 }
  461. Get l2 // { arity: 1 }
  462. Return // { arity: 2 }
  463. Union // { arity: 2 }
  464. Get l3 // { arity: 2 }
  465. Map (null, null) // { arity: 2 }
  466. Union // { arity: 0 }
  467. Negate // { arity: 0 }
  468. Project () // { arity: 0 }
  469. Get l3 // { arity: 2 }
  470. Constant // { arity: 0 }
  471. - ()
  472. Used Indexes:
  473. - materialize.public.i1 (*** full scan ***, lookup)
  474. Target cluster: quickstart
  475. EOF
  476. #
  477. # CSEs at two distinct positions within the query
  478. #
  479. query T multiline
  480. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT f1 FROM t1 WHERE f1 = 1) FROM t1 WHERE EXISTS (SELECT f1 FROM t1 WHERE f1 = 1);
  481. ----
  482. Explained Query:
  483. With
  484. cte l0 =
  485. Project (#0{f1}) // { arity: 1 }
  486. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  487. cte l1 =
  488. Project () // { arity: 0 }
  489. Get l0 // { arity: 1 }
  490. cte l2 =
  491. Union // { arity: 1 }
  492. Get l0 // { arity: 1 }
  493. Project (#1) // { arity: 1 }
  494. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  495. Reduce aggregates=[count(*)] // { arity: 1 }
  496. Get l1 // { arity: 0 }
  497. Return // { arity: 1 }
  498. CrossJoin type=delta // { arity: 1 }
  499. implementation
  500. %0:t1 » %1[×]UA » %2[×]
  501. %1 » %0:t1[×] » %2[×]
  502. %2 » %1[×]UA » %0:t1[×]
  503. ArrangeBy keys=[[]] // { arity: 0 }
  504. Project () // { arity: 0 }
  505. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  506. ArrangeBy keys=[[]] // { arity: 0 }
  507. Distinct project=[] // { arity: 0 }
  508. Get l1 // { arity: 0 }
  509. ArrangeBy keys=[[]] // { arity: 1 }
  510. Union // { arity: 1 }
  511. Get l2 // { arity: 1 }
  512. Map (null) // { arity: 1 }
  513. Union // { arity: 0 }
  514. Negate // { arity: 0 }
  515. Distinct project=[] // { arity: 0 }
  516. Project () // { arity: 0 }
  517. Get l2 // { arity: 1 }
  518. Constant // { arity: 0 }
  519. - ()
  520. Used Indexes:
  521. - materialize.public.i1 (*** full scan ***, lookup)
  522. Target cluster: quickstart
  523. EOF
  524. query T multiline
  525. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT f1 FROM t1 WHERE f1 = 1) FROM t1
  526. UNION ALL
  527. SELECT f1 FROM t1 WHERE f1 = 1
  528. ----
  529. Explained Query:
  530. With
  531. cte l0 =
  532. Project (#0{f1}) // { arity: 1 }
  533. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  534. cte l1 =
  535. Union // { arity: 1 }
  536. Get l0 // { arity: 1 }
  537. Project (#1) // { arity: 1 }
  538. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  539. Reduce aggregates=[count(*)] // { arity: 1 }
  540. Project () // { arity: 0 }
  541. Get l0 // { arity: 1 }
  542. Return // { arity: 1 }
  543. Union // { arity: 1 }
  544. CrossJoin type=differential // { arity: 1 }
  545. implementation
  546. %0:t1[×] » %1[×]
  547. ArrangeBy keys=[[]] // { arity: 0 }
  548. Project () // { arity: 0 }
  549. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  550. ArrangeBy keys=[[]] // { arity: 1 }
  551. Union // { arity: 1 }
  552. Get l1 // { arity: 1 }
  553. Map (null) // { arity: 1 }
  554. Union // { arity: 0 }
  555. Negate // { arity: 0 }
  556. Distinct project=[] // { arity: 0 }
  557. Project () // { arity: 0 }
  558. Get l1 // { arity: 1 }
  559. Constant // { arity: 0 }
  560. - ()
  561. Get l0 // { arity: 1 }
  562. Used Indexes:
  563. - materialize.public.i1 (*** full scan ***, lookup)
  564. Target cluster: quickstart
  565. EOF
  566. ##
  567. ## Tests around the contents of the CSE itself
  568. ##
  569. #
  570. # CSE containing a join
  571. #
  572. query T multiline
  573. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  574. SELECT * FROM t1 AS a1, t1 AS a2
  575. UNION ALL
  576. SELECT * FROM t1 AS a1, t1 AS a2
  577. ----
  578. Explained Query:
  579. With
  580. cte l0 =
  581. ArrangeBy keys=[[]] // { arity: 2 }
  582. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  583. cte l1 =
  584. CrossJoin type=differential // { arity: 4 }
  585. implementation
  586. %0:l0[×] » %1:l0[×]
  587. Get l0 // { arity: 2 }
  588. Get l0 // { arity: 2 }
  589. Return // { arity: 4 }
  590. Union // { arity: 4 }
  591. Get l1 // { arity: 4 }
  592. Get l1 // { arity: 4 }
  593. Used Indexes:
  594. - materialize.public.i1 (*** full scan ***)
  595. Target cluster: quickstart
  596. EOF
  597. query T multiline
  598. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  599. SELECT * FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)
  600. UNION ALL
  601. SELECT * FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)
  602. ----
  603. Explained Query:
  604. With
  605. cte l0 =
  606. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  607. ReadIndex on=t1 i1=[differential join] // { arity: 2 }
  608. cte l1 =
  609. Project (#0{f1}, #1{f2}, #3{f2}) // { arity: 3 }
  610. Filter (#0{f1}) IS NOT NULL // { arity: 4 }
  611. Join on=(#0{f1} = #2{f1}) type=differential // { arity: 4 }
  612. implementation
  613. %0:l0[#0{f1}]KA » %1:l0[#0{f1}]KA
  614. Get l0 // { arity: 2 }
  615. Get l0 // { arity: 2 }
  616. cte l2 =
  617. Map (null) // { arity: 3 }
  618. Union // { arity: 2 }
  619. Negate // { arity: 2 }
  620. Project (#0{f1}, #1{f2}) // { arity: 2 }
  621. Join on=(#0{f1} = #2{f1}) type=differential // { arity: 3 }
  622. implementation
  623. %1[#0]UKA » %0:l0[#0{f1}]KA
  624. Get l0 // { arity: 2 }
  625. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  626. Distinct project=[#0{f1}] // { arity: 1 }
  627. Project (#0{f1}) // { arity: 1 }
  628. Get l1 // { arity: 3 }
  629. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  630. Return // { arity: 3 }
  631. Union // { arity: 3 }
  632. Get l2 // { arity: 3 }
  633. Get l1 // { arity: 3 }
  634. Get l2 // { arity: 3 }
  635. Get l1 // { arity: 3 }
  636. Used Indexes:
  637. - materialize.public.i1 (*** full scan ***, differential join)
  638. Target cluster: quickstart
  639. EOF
  640. query T multiline
  641. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  642. SELECT * FROM (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) WHERE f1 = 1
  643. UNION ALL
  644. SELECT * FROM (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) WHERE f1 = 2
  645. ----
  646. Explained Query:
  647. With
  648. cte l0 =
  649. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  650. ReadIndex on=t1 i1=[lookup] // { arity: 2 }
  651. cte l1 =
  652. Project (#0{f1}) // { arity: 1 }
  653. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  654. cte l2 =
  655. Project (#0{f1}) // { arity: 1 }
  656. ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
  657. Return // { arity: 1 }
  658. Union // { arity: 1 }
  659. CrossJoin type=differential // { arity: 1 }
  660. implementation
  661. %0:l1[×]e » %1:l1[×]e
  662. ArrangeBy keys=[[]] // { arity: 0 }
  663. Project () // { arity: 0 }
  664. Get l1 // { arity: 1 }
  665. ArrangeBy keys=[[]] // { arity: 1 }
  666. Get l1 // { arity: 1 }
  667. CrossJoin type=differential // { arity: 1 }
  668. implementation
  669. %0:l2[×]e » %1:l2[×]e
  670. ArrangeBy keys=[[]] // { arity: 0 }
  671. Project () // { arity: 0 }
  672. Get l2 // { arity: 1 }
  673. ArrangeBy keys=[[]] // { arity: 1 }
  674. Get l2 // { arity: 1 }
  675. Used Indexes:
  676. - materialize.public.i1 (lookup)
  677. Target cluster: quickstart
  678. EOF
  679. query T multiline
  680. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  681. SELECT * FROM
  682. (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) AS s1,
  683. (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) AS s2
  684. WHERE s1.f1 = 1 AND s2.f1 = 1
  685. ----
  686. Explained Query:
  687. With
  688. cte l0 =
  689. Project (#0{f1}) // { arity: 1 }
  690. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  691. cte l1 =
  692. ArrangeBy keys=[[]] // { arity: 0 }
  693. Project () // { arity: 0 }
  694. Get l0 // { arity: 1 }
  695. cte l2 =
  696. ArrangeBy keys=[[]] // { arity: 1 }
  697. Get l0 // { arity: 1 }
  698. Return // { arity: 2 }
  699. CrossJoin type=delta // { arity: 2 }
  700. implementation
  701. %0:l1 » %1:l2[×]e » %2:l1[×]e » %3:l2[×]e
  702. %1:l2 » %0:l1[×]e » %2:l1[×]e » %3:l2[×]e
  703. %2:l1 » %0:l1[×]e » %1:l2[×]e » %3:l2[×]e
  704. %3:l2 » %0:l1[×]e » %1:l2[×]e » %2:l1[×]e
  705. Get l1 // { arity: 0 }
  706. Get l2 // { arity: 1 }
  707. Get l1 // { arity: 0 }
  708. Get l2 // { arity: 1 }
  709. Used Indexes:
  710. - materialize.public.i1 (lookup)
  711. Target cluster: quickstart
  712. EOF
  713. query T multiline
  714. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  715. SELECT * FROM
  716. (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) AS s1,
  717. (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) AS s2
  718. WHERE s1.f1 = 1 AND s2.f1 = 2
  719. ----
  720. Explained Query:
  721. With
  722. cte l0 =
  723. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  724. ReadIndex on=t1 i1=[lookup] // { arity: 2 }
  725. cte l1 =
  726. Project (#0{f1}) // { arity: 1 }
  727. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  728. cte l2 =
  729. Project (#0{f1}) // { arity: 1 }
  730. ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
  731. Return // { arity: 2 }
  732. CrossJoin type=delta // { arity: 2 }
  733. implementation
  734. %0:l1 » %1:l1[×]e » %2:l2[×]e » %3:l2[×]e
  735. %1:l1 » %0:l1[×]e » %2:l2[×]e » %3:l2[×]e
  736. %2:l2 » %0:l1[×]e » %1:l1[×]e » %3:l2[×]e
  737. %3:l2 » %0:l1[×]e » %1:l1[×]e » %2:l2[×]e
  738. ArrangeBy keys=[[]] // { arity: 0 }
  739. Project () // { arity: 0 }
  740. Get l1 // { arity: 1 }
  741. ArrangeBy keys=[[]] // { arity: 1 }
  742. Get l1 // { arity: 1 }
  743. ArrangeBy keys=[[]] // { arity: 0 }
  744. Project () // { arity: 0 }
  745. Get l2 // { arity: 1 }
  746. ArrangeBy keys=[[]] // { arity: 1 }
  747. Get l2 // { arity: 1 }
  748. Used Indexes:
  749. - materialize.public.i1 (lookup)
  750. Target cluster: quickstart
  751. EOF
  752. #
  753. # CSE containing a conjunction (AND)
  754. #
  755. query T multiline
  756. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  757. SELECT * FROM t1 WHERE f1 = 1 AND f2 = 2
  758. UNION ALL
  759. SELECT * FROM t1 WHERE f1 = 1 AND f2 = 2
  760. ----
  761. Explained Query:
  762. With
  763. cte l0 =
  764. Project (#0{f1}, #1{f2}) // { arity: 2 }
  765. Filter (#1{f2} = 2) // { arity: 3 }
  766. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  767. Return // { arity: 2 }
  768. Union // { arity: 2 }
  769. Get l0 // { arity: 2 }
  770. Get l0 // { arity: 2 }
  771. Used Indexes:
  772. - materialize.public.i1 (lookup)
  773. Target cluster: quickstart
  774. EOF
  775. #
  776. # CSE containing a disjunction (OR)
  777. #
  778. query T multiline
  779. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  780. SELECT * FROM t1 WHERE f1 = 1 OR f1 = 2
  781. UNION ALL
  782. SELECT * FROM t1 WHERE f1 = 1 OR f1 = 2
  783. ----
  784. Explained Query:
  785. With
  786. cte l0 =
  787. Project (#0{f1}, #1{f2}) // { arity: 2 }
  788. ReadIndex on=materialize.public.t1 i1=[lookup values=[(1); (2)]] // { arity: 3 }
  789. Return // { arity: 2 }
  790. Union // { arity: 2 }
  791. Get l0 // { arity: 2 }
  792. Get l0 // { arity: 2 }
  793. Used Indexes:
  794. - materialize.public.i1 (lookup)
  795. Target cluster: quickstart
  796. EOF
  797. #
  798. # CSE containing a subquery
  799. #
  800. query T multiline
  801. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  802. SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1)
  803. UNION ALL
  804. SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1)
  805. ----
  806. Explained Query:
  807. With
  808. cte l0 =
  809. CrossJoin type=differential // { arity: 2 }
  810. implementation
  811. %1[×]UA » %0:t1[×]
  812. ArrangeBy keys=[[]] // { arity: 2 }
  813. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  814. ArrangeBy keys=[[]] // { arity: 0 }
  815. Distinct project=[] // { arity: 0 }
  816. Project () // { arity: 0 }
  817. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  818. Return // { arity: 2 }
  819. Union // { arity: 2 }
  820. Get l0 // { arity: 2 }
  821. Get l0 // { arity: 2 }
  822. Used Indexes:
  823. - materialize.public.i1 (*** full scan ***, lookup)
  824. Target cluster: quickstart
  825. EOF
  826. query T multiline
  827. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  828. SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1 WHERE f1 = 1)
  829. UNION ALL
  830. SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1 WHERE f1 = 1)
  831. ----
  832. Explained Query:
  833. With
  834. cte l0 =
  835. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  836. ReadIndex on=t1 i1=[differential join, lookup] // { arity: 2 }
  837. cte l1 =
  838. Project (#0{f1}) // { arity: 1 }
  839. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  840. cte l2 =
  841. Project (#0{f1}, #1{f2}) // { arity: 2 }
  842. Join on=(#0{f1} = #2{f1}) type=differential // { arity: 3 }
  843. implementation
  844. %0:l0[#0{f1}]KA » %1[#0]K
  845. Get l0 // { arity: 2 }
  846. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  847. Union // { arity: 1 }
  848. Get l1 // { arity: 1 }
  849. Project (#1) // { arity: 1 }
  850. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  851. Reduce aggregates=[count(*)] // { arity: 1 }
  852. Project () // { arity: 0 }
  853. Get l1 // { arity: 1 }
  854. Return // { arity: 2 }
  855. Union // { arity: 2 }
  856. Get l2 // { arity: 2 }
  857. Get l2 // { arity: 2 }
  858. Used Indexes:
  859. - materialize.public.i1 (differential join, lookup)
  860. Target cluster: quickstart
  861. EOF
  862. #
  863. # CSE containing a derived table
  864. #
  865. query T multiline
  866. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  867. SELECT f1 + 1 FROM (SELECT f1 + 2 AS f1 FROM t1)
  868. UNION ALL
  869. SELECT f1 + 1 FROM (SELECT f1 + 2 AS f1 FROM t1)
  870. ----
  871. Explained Query:
  872. With
  873. cte l0 =
  874. Project (#2) // { arity: 1 }
  875. Map (((#0{f1} + 2) + 1)) // { arity: 3 }
  876. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  877. Return // { arity: 1 }
  878. Union // { arity: 1 }
  879. Get l0 // { arity: 1 }
  880. Get l0 // { arity: 1 }
  881. Used Indexes:
  882. - materialize.public.i1 (*** full scan ***)
  883. Target cluster: quickstart
  884. EOF
  885. #
  886. # CSEs containing an aggregate
  887. #
  888. query T multiline
  889. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  890. SELECT MIN(f1) FROM t1
  891. UNION ALL
  892. SELECT MIN(f1) FROM t1;
  893. ----
  894. Explained Query:
  895. With
  896. cte l0 =
  897. Reduce aggregates=[min(#0{f1})] // { arity: 1 }
  898. Project (#0{f1}) // { arity: 1 }
  899. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  900. cte l1 =
  901. Map (null) // { arity: 1 }
  902. Union // { arity: 0 }
  903. Negate // { arity: 0 }
  904. Project () // { arity: 0 }
  905. Get l0 // { arity: 1 }
  906. Constant // { arity: 0 }
  907. - ()
  908. Return // { arity: 1 }
  909. Union // { arity: 1 }
  910. Get l0 // { arity: 1 }
  911. Get l1 // { arity: 1 }
  912. Get l0 // { arity: 1 }
  913. Get l1 // { arity: 1 }
  914. Used Indexes:
  915. - materialize.public.i1 (*** full scan ***)
  916. Target cluster: quickstart
  917. EOF
  918. query T multiline
  919. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  920. SELECT DISTINCT f1 FROM t1
  921. UNION ALL
  922. SELECT DISTINCT f1 FROM t1
  923. ----
  924. Explained Query:
  925. With
  926. cte l0 =
  927. Distinct project=[#0{f1}] // { arity: 1 }
  928. Project (#0{f1}) // { arity: 1 }
  929. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  930. Return // { arity: 1 }
  931. Union // { arity: 1 }
  932. Get l0 // { arity: 1 }
  933. Get l0 // { arity: 1 }
  934. Used Indexes:
  935. - materialize.public.i1 (*** full scan ***)
  936. Target cluster: quickstart
  937. EOF
  938. query T multiline
  939. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  940. SELECT f1, COUNT(*) FROM t1 GROUP BY f1
  941. UNION ALL
  942. SELECT f1, COUNT(*) FROM t1 GROUP BY f1
  943. ----
  944. Explained Query:
  945. With
  946. cte l0 =
  947. Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
  948. Project (#0{f1}) // { arity: 1 }
  949. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  950. Return // { arity: 2 }
  951. Union // { arity: 2 }
  952. Get l0 // { arity: 2 }
  953. Get l0 // { arity: 2 }
  954. Used Indexes:
  955. - materialize.public.i1 (*** full scan ***)
  956. Target cluster: quickstart
  957. EOF
  958. #
  959. # CSEs containing an expression / function
  960. query T multiline
  961. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  962. SELECT f1 + f1 + f1 + f1 FROM t1
  963. UNION ALL
  964. SELECT f1 + f1 + f1 + f1 FROM t1
  965. ----
  966. Explained Query:
  967. With
  968. cte l0 =
  969. Project (#2) // { arity: 1 }
  970. Map ((((#0{f1} + #0{f1}) + #0{f1}) + #0{f1})) // { arity: 3 }
  971. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  972. Return // { arity: 1 }
  973. Union // { arity: 1 }
  974. Get l0 // { arity: 1 }
  975. Get l0 // { arity: 1 }
  976. Used Indexes:
  977. - materialize.public.i1 (*** full scan ***)
  978. Target cluster: quickstart
  979. EOF
  980. query T multiline
  981. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  982. SELECT ABS(f1) FROM t1
  983. UNION ALL
  984. SELECT ABS(f1) FROM t1
  985. ----
  986. Explained Query:
  987. With
  988. cte l0 =
  989. Project (#2) // { arity: 1 }
  990. Map (abs(#0{f1})) // { arity: 3 }
  991. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  992. Return // { arity: 1 }
  993. Union // { arity: 1 }
  994. Get l0 // { arity: 1 }
  995. Get l0 // { arity: 1 }
  996. Used Indexes:
  997. - materialize.public.i1 (*** full scan ***)
  998. Target cluster: quickstart
  999. EOF
  1000. ##
  1001. ## Nested CSEs
  1002. ##
  1003. query T multiline
  1004. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1005. (SELECT * FROM t1 WHERE f1 = 1 UNION ALL SELECT * FROM t1 WHERE f1 = 1)
  1006. UNION ALL
  1007. (SELECT * FROM t1 WHERE f1 = 1 UNION ALL SELECT * FROM t1 WHERE f1 = 1)
  1008. ----
  1009. Explained Query:
  1010. With
  1011. cte l0 =
  1012. Project (#0{f1}, #1{f2}) // { arity: 2 }
  1013. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1014. Return // { arity: 2 }
  1015. Union // { arity: 2 }
  1016. Get l0 // { arity: 2 }
  1017. Get l0 // { arity: 2 }
  1018. Get l0 // { arity: 2 }
  1019. Get l0 // { arity: 2 }
  1020. Used Indexes:
  1021. - materialize.public.i1 (lookup)
  1022. Target cluster: quickstart
  1023. EOF
  1024. query T multiline
  1025. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1026. (SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
  1027. UNION ALL
  1028. (SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
  1029. ----
  1030. Explained Query:
  1031. With
  1032. cte l0 =
  1033. CrossJoin type=differential // { arity: 2 }
  1034. implementation
  1035. %1[×]UA » %0:t1[×]
  1036. ArrangeBy keys=[[]] // { arity: 2 }
  1037. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  1038. ArrangeBy keys=[[]] // { arity: 0 }
  1039. Distinct project=[] // { arity: 0 }
  1040. Project () // { arity: 0 }
  1041. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1042. Return // { arity: 2 }
  1043. Union // { arity: 2 }
  1044. Get l0 // { arity: 2 }
  1045. Get l0 // { arity: 2 }
  1046. Used Indexes:
  1047. - materialize.public.i1 (*** full scan ***, lookup)
  1048. Target cluster: quickstart
  1049. EOF
  1050. ##
  1051. ## Deeper-placed CSEs (where a higher-level construct is not a CSE but a lower-level one is)
  1052. ##
  1053. query T multiline
  1054. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1055. (SELECT f1 FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
  1056. UNION ALL
  1057. (SELECT f2 FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
  1058. ----
  1059. Explained Query:
  1060. With
  1061. cte l0 =
  1062. CrossJoin type=differential // { arity: 2 }
  1063. implementation
  1064. %1[×]UA » %0:t1[×]
  1065. ArrangeBy keys=[[]] // { arity: 2 }
  1066. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  1067. ArrangeBy keys=[[]] // { arity: 0 }
  1068. Distinct project=[] // { arity: 0 }
  1069. Project () // { arity: 0 }
  1070. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1071. Return // { arity: 1 }
  1072. Union // { arity: 1 }
  1073. Project (#0{f1}) // { arity: 1 }
  1074. Get l0 // { arity: 2 }
  1075. Project (#1{f2}) // { arity: 1 }
  1076. Get l0 // { arity: 2 }
  1077. Used Indexes:
  1078. - materialize.public.i1 (*** full scan ***, lookup)
  1079. Target cluster: quickstart
  1080. EOF
  1081. query T multiline
  1082. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1083. (SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
  1084. UNION ALL
  1085. (SELECT * FROM t2 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
  1086. ----
  1087. Explained Query:
  1088. With
  1089. cte l0 =
  1090. ArrangeBy keys=[[]] // { arity: 0 }
  1091. Distinct project=[] // { arity: 0 }
  1092. Project () // { arity: 0 }
  1093. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1094. Return // { arity: 2 }
  1095. Union // { arity: 2 }
  1096. CrossJoin type=differential // { arity: 2 }
  1097. implementation
  1098. %1:l0[×]UA » %0:t1[×]
  1099. ArrangeBy keys=[[]] // { arity: 2 }
  1100. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  1101. Get l0 // { arity: 0 }
  1102. CrossJoin type=differential // { arity: 2 }
  1103. implementation
  1104. %1:l0[×]UA » %0:t2[×]
  1105. ArrangeBy keys=[[]] // { arity: 2 }
  1106. ReadStorage materialize.public.t2 // { arity: 2 }
  1107. Get l0 // { arity: 0 }
  1108. Source materialize.public.t2
  1109. Used Indexes:
  1110. - materialize.public.i1 (*** full scan ***, lookup)
  1111. Target cluster: quickstart
  1112. EOF
  1113. query T multiline
  1114. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM
  1115. (SELECT f1 FROM t2 UNION ALL SELECT f1 FROM t1 WHERE f1 = 1) ,
  1116. (SELECT f2 FROM t2 UNION ALL SELECT f1 FROM t1 WHERE f1 = 1)
  1117. ----
  1118. Explained Query:
  1119. With
  1120. cte l0 =
  1121. Project (#0{f1}) // { arity: 1 }
  1122. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1123. Return // { arity: 2 }
  1124. CrossJoin type=differential // { arity: 2 }
  1125. implementation
  1126. %0[×] » %1[×]
  1127. ArrangeBy keys=[[]] // { arity: 1 }
  1128. Union // { arity: 1 }
  1129. Project (#0{f1}) // { arity: 1 }
  1130. ReadStorage materialize.public.t2 // { arity: 2 }
  1131. Get l0 // { arity: 1 }
  1132. ArrangeBy keys=[[]] // { arity: 1 }
  1133. Union // { arity: 1 }
  1134. Project (#1{f2}) // { arity: 1 }
  1135. ReadStorage materialize.public.t2 // { arity: 2 }
  1136. Get l0 // { arity: 1 }
  1137. Source materialize.public.t2
  1138. Used Indexes:
  1139. - materialize.public.i1 (lookup)
  1140. Target cluster: quickstart
  1141. EOF
  1142. # Same predicate, different projections
  1143. query T multiline
  1144. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1145. SELECT f1 FROM t1 WHERE f1 = 1
  1146. UNION ALL
  1147. SELECT f2 FROM t1 WHERE f1 = 1
  1148. ----
  1149. Explained Query:
  1150. With
  1151. cte l0 =
  1152. Project (#0{f1}, #1{f2}) // { arity: 2 }
  1153. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1154. Return // { arity: 1 }
  1155. Union // { arity: 1 }
  1156. Project (#0{f1}) // { arity: 1 }
  1157. Get l0 // { arity: 2 }
  1158. Project (#1{f2}) // { arity: 1 }
  1159. Get l0 // { arity: 2 }
  1160. Used Indexes:
  1161. - materialize.public.i1 (lookup)
  1162. Target cluster: quickstart
  1163. EOF
  1164. ##
  1165. ## Negative cases - CSEs should not be identified where they do not exist
  1166. ##
  1167. #
  1168. query T multiline
  1169. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1, t1 AS a2 WHERE a1.f1 = 1 AND a2.f1 = 2
  1170. ----
  1171. Explained Query:
  1172. With
  1173. cte l0 =
  1174. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  1175. ReadIndex on=t1 i1=[lookup] // { arity: 2 }
  1176. Return // { arity: 4 }
  1177. CrossJoin type=differential // { arity: 4 }
  1178. implementation
  1179. %0:t1[×]e » %1:t1[×]e
  1180. ArrangeBy keys=[[]] // { arity: 2 }
  1181. Project (#0{f1}, #1{f2}) // { arity: 2 }
  1182. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1183. ArrangeBy keys=[[]] // { arity: 2 }
  1184. Project (#0{f1}, #1{f2}) // { arity: 2 }
  1185. ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
  1186. Used Indexes:
  1187. - materialize.public.i1 (lookup)
  1188. Target cluster: quickstart
  1189. EOF
  1190. query T multiline
  1191. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1192. SELECT * FROM (SELECT a2.f1 AS f1 FROM t1 AS a1 JOIN t1 AS a2 USING (f1)) WHERE f1 = 1
  1193. UNION ALL
  1194. SELECT * FROM (SELECT a2.f1 AS f1 FROM t1 AS a1 JOIN t1 AS a2 USING (f1)) WHERE f1 = 2
  1195. ----
  1196. Explained Query:
  1197. With
  1198. cte l0 =
  1199. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  1200. ReadIndex on=t1 i1=[lookup] // { arity: 2 }
  1201. cte l1 =
  1202. Project (#0{f1}) // { arity: 1 }
  1203. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1204. cte l2 =
  1205. Project (#0{f1}) // { arity: 1 }
  1206. ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
  1207. Return // { arity: 1 }
  1208. Union // { arity: 1 }
  1209. CrossJoin type=differential // { arity: 1 }
  1210. implementation
  1211. %0:l1[×]e » %1:l1[×]e
  1212. ArrangeBy keys=[[]] // { arity: 0 }
  1213. Project () // { arity: 0 }
  1214. Get l1 // { arity: 1 }
  1215. ArrangeBy keys=[[]] // { arity: 1 }
  1216. Get l1 // { arity: 1 }
  1217. CrossJoin type=differential // { arity: 1 }
  1218. implementation
  1219. %0:l2[×]e » %1:l2[×]e
  1220. ArrangeBy keys=[[]] // { arity: 0 }
  1221. Project () // { arity: 0 }
  1222. Get l2 // { arity: 1 }
  1223. ArrangeBy keys=[[]] // { arity: 1 }
  1224. Get l2 // { arity: 1 }
  1225. Used Indexes:
  1226. - materialize.public.i1 (lookup)
  1227. Target cluster: quickstart
  1228. EOF
  1229. query T multiline
  1230. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1231. SELECT * FROM t1 WHERE f1 = 1
  1232. UNION ALL
  1233. SELECT * FROM t1 WHERE f1 = 2
  1234. ----
  1235. Explained Query:
  1236. With
  1237. cte l0 =
  1238. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  1239. ReadIndex on=t1 i1=[lookup] // { arity: 2 }
  1240. Return // { arity: 2 }
  1241. Union // { arity: 2 }
  1242. Project (#0{f1}, #1{f2}) // { arity: 2 }
  1243. ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
  1244. Project (#0{f1}, #1{f2}) // { arity: 2 }
  1245. ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
  1246. Used Indexes:
  1247. - materialize.public.i1 (lookup)
  1248. Target cluster: quickstart
  1249. EOF
  1250. query T multiline
  1251. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1252. SELECT MIN(f1) FROM t1
  1253. UNION ALL
  1254. SELECT MAX(f1) FROM t1
  1255. ----
  1256. Explained Query:
  1257. With
  1258. cte l0 =
  1259. Project (#0{f1}) // { arity: 1 }
  1260. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  1261. cte l1 =
  1262. Reduce aggregates=[min(#0{f1})] // { arity: 1 }
  1263. Get l0 // { arity: 1 }
  1264. cte l2 =
  1265. Reduce aggregates=[max(#0{f1})] // { arity: 1 }
  1266. Get l0 // { arity: 1 }
  1267. Return // { arity: 1 }
  1268. Union // { arity: 1 }
  1269. Get l1 // { arity: 1 }
  1270. Map (null) // { arity: 1 }
  1271. Union // { arity: 0 }
  1272. Negate // { arity: 0 }
  1273. Project () // { arity: 0 }
  1274. Get l1 // { arity: 1 }
  1275. Constant // { arity: 0 }
  1276. - ()
  1277. Get l2 // { arity: 1 }
  1278. Map (null) // { arity: 1 }
  1279. Union // { arity: 0 }
  1280. Negate // { arity: 0 }
  1281. Project () // { arity: 0 }
  1282. Get l2 // { arity: 1 }
  1283. Constant // { arity: 0 }
  1284. - ()
  1285. Used Indexes:
  1286. - materialize.public.i1 (*** full scan ***)
  1287. Target cluster: quickstart
  1288. EOF
  1289. query T multiline
  1290. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1291. SELECT MIN(f1) FROM t1
  1292. UNION ALL
  1293. SELECT MIN(f2) FROM t1
  1294. ----
  1295. Explained Query:
  1296. With
  1297. cte l0 =
  1298. Reduce aggregates=[min(#0{f1})] // { arity: 1 }
  1299. Project (#0{f1}) // { arity: 1 }
  1300. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  1301. cte l1 =
  1302. Reduce aggregates=[min(#0{f2})] // { arity: 1 }
  1303. Project (#1{f2}) // { arity: 1 }
  1304. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  1305. Return // { arity: 1 }
  1306. Union // { arity: 1 }
  1307. Get l0 // { arity: 1 }
  1308. Map (null) // { arity: 1 }
  1309. Union // { arity: 0 }
  1310. Negate // { arity: 0 }
  1311. Project () // { arity: 0 }
  1312. Get l0 // { arity: 1 }
  1313. Constant // { arity: 0 }
  1314. - ()
  1315. Get l1 // { arity: 1 }
  1316. Map (null) // { arity: 1 }
  1317. Union // { arity: 0 }
  1318. Negate // { arity: 0 }
  1319. Project () // { arity: 0 }
  1320. Get l1 // { arity: 1 }
  1321. Constant // { arity: 0 }
  1322. - ()
  1323. Used Indexes:
  1324. - materialize.public.i1 (*** full scan ***)
  1325. Target cluster: quickstart
  1326. EOF
  1327. # WITH MUTUALLY RECURSIVE support
  1328. # -------------------------------
  1329. # Basic support for recursive queries.
  1330. # With materialize#27389 this stopped testing what it says it tests; see issue database-issues#8294.
  1331. query T multiline
  1332. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1333. WITH MUTUALLY RECURSIVE
  1334. c0(f1 INTEGER, f2 INTEGER) AS (
  1335. SELECT f1, f2 FROM t1
  1336. UNION ALL
  1337. SELECT f1, f2 FROM t1
  1338. ),
  1339. c1(f1 INTEGER, f2 INTEGER) AS (
  1340. SELECT * FROM c0 WHERE f2 > 7
  1341. UNION ALL
  1342. SELECT * FROM c1 WHERE f2 > 7
  1343. UNION ALL
  1344. SELECT * FROM c1 WHERE f2 > 7
  1345. UNION ALL
  1346. SELECT * FROM c2 WHERE f2 > 7
  1347. UNION ALL
  1348. SELECT * FROM c2 WHERE f2 > 7
  1349. ),
  1350. c2(f1 INTEGER, f2 INTEGER) AS (
  1351. SELECT * FROM c0 WHERE f2 > 7
  1352. UNION ALL
  1353. SELECT * FROM c1 WHERE f2 > 7
  1354. UNION ALL
  1355. SELECT * FROM c1 WHERE f2 > 7
  1356. UNION ALL
  1357. SELECT * FROM c2 WHERE f2 > 7
  1358. UNION ALL
  1359. SELECT * FROM c2 WHERE f2 > 7
  1360. )
  1361. SELECT * FROM c0 WHERE f1 > 7
  1362. UNION ALL
  1363. SELECT * FROM c1 WHERE f1 > 7
  1364. UNION ALL
  1365. SELECT * FROM c2 WHERE f1 > 7
  1366. ----
  1367. Explained Query:
  1368. With
  1369. cte l0 =
  1370. Union // { arity: 2 }
  1371. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  1372. ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
  1373. cte l1 =
  1374. Filter (#1{f2} > 7) // { arity: 2 }
  1375. Get l0 // { arity: 2 }
  1376. Return // { arity: 2 }
  1377. With Mutually Recursive
  1378. cte l2 =
  1379. Union // { arity: 2 }
  1380. Get l1 // { arity: 2 }
  1381. Get l2 // { arity: 2 }
  1382. Get l2 // { arity: 2 }
  1383. Get l3 // { arity: 2 }
  1384. Get l3 // { arity: 2 }
  1385. cte l3 =
  1386. Union // { arity: 2 }
  1387. Get l1 // { arity: 2 }
  1388. Get l2 // { arity: 2 }
  1389. Get l2 // { arity: 2 }
  1390. Get l3 // { arity: 2 }
  1391. Get l3 // { arity: 2 }
  1392. Return // { arity: 2 }
  1393. Union // { arity: 2 }
  1394. Filter (#0{f1} > 7) // { arity: 2 }
  1395. Get l0 // { arity: 2 }
  1396. Filter (#0{f1} > 7) // { arity: 2 }
  1397. Get l2 // { arity: 2 }
  1398. Filter (#0{f1} > 7) // { arity: 2 }
  1399. Get l3 // { arity: 2 }
  1400. Used Indexes:
  1401. - materialize.public.i1 (*** full scan ***)
  1402. Target cluster: quickstart
  1403. EOF
  1404. # Correlated WMR block.
  1405. query T multiline
  1406. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1407. SELECT bound, (
  1408. WITH MUTUALLY RECURSIVE
  1409. numbers (n int) as (
  1410. VALUES (1)
  1411. UNION ALL
  1412. (
  1413. WITH rebound AS (SELECT * FROM numbers)
  1414. SELECT distinct t1.n + t2.n AS n
  1415. FROM rebound AS t1, rebound AS t2
  1416. WHERE t1.n <= bound AND t2.n <= bound
  1417. )
  1418. )
  1419. SELECT count(*) FROM numbers
  1420. )
  1421. FROM (
  1422. SELECT generate_series AS bound FROM generate_series(1, 10)
  1423. );
  1424. ----
  1425. Explained Query:
  1426. With Mutually Recursive
  1427. cte l0 =
  1428. ArrangeBy keys=[[#0]] // { arity: 2 }
  1429. Filter (#1{n} <= #0{bound}) // { arity: 2 }
  1430. Get l1 // { arity: 2 }
  1431. cte l1 =
  1432. Union // { arity: 2 }
  1433. Distinct project=[#0, (#1{n} + #2{n})] monotonic // { arity: 2 }
  1434. Project (#0, #1, #3) // { arity: 3 }
  1435. Join on=(#0 = #2) type=differential // { arity: 4 }
  1436. implementation
  1437. %0:l0[#0]Kf » %1:l0[#0]Kf
  1438. Get l0 // { arity: 2 }
  1439. Get l0 // { arity: 2 }
  1440. Constant // { arity: 2 }
  1441. - (1, 1)
  1442. - (2, 1)
  1443. - (3, 1)
  1444. - (4, 1)
  1445. - (5, 1)
  1446. - (6, 1)
  1447. - (7, 1)
  1448. - (8, 1)
  1449. - (9, 1)
  1450. - (10, 1)
  1451. Return // { arity: 2 }
  1452. With
  1453. cte l2 =
  1454. Reduce group_by=[#0] aggregates=[count(*)] monotonic // { arity: 2 }
  1455. Project (#0) // { arity: 1 }
  1456. Get l1 // { arity: 2 }
  1457. cte l3 =
  1458. ArrangeBy keys=[[#0]] // { arity: 1 }
  1459. Constant // { arity: 1 }
  1460. - (1)
  1461. - (2)
  1462. - (3)
  1463. - (4)
  1464. - (5)
  1465. - (6)
  1466. - (7)
  1467. - (8)
  1468. - (9)
  1469. - (10)
  1470. cte l4 =
  1471. Union // { arity: 2 }
  1472. Get l2 // { arity: 2 }
  1473. Project (#0, #2) // { arity: 2 }
  1474. Map (0) // { arity: 3 }
  1475. Join on=(#0 = #1) type=differential // { arity: 2 }
  1476. implementation
  1477. %1:l3[#0]UK » %0[#0]K
  1478. ArrangeBy keys=[[#0]] // { arity: 1 }
  1479. Union // { arity: 1 }
  1480. Negate // { arity: 1 }
  1481. Project (#0) // { arity: 1 }
  1482. Get l2 // { arity: 2 }
  1483. Constant // { arity: 1 }
  1484. - (1)
  1485. - (2)
  1486. - (3)
  1487. - (4)
  1488. - (5)
  1489. - (6)
  1490. - (7)
  1491. - (8)
  1492. - (9)
  1493. - (10)
  1494. Get l3 // { arity: 1 }
  1495. cte l5 =
  1496. Union // { arity: 2 }
  1497. Get l4 // { arity: 2 }
  1498. Project (#0, #2) // { arity: 2 }
  1499. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  1500. Reduce group_by=[#0] aggregates=[count(*)] // { arity: 2 }
  1501. Project (#0) // { arity: 1 }
  1502. Get l4 // { arity: 2 }
  1503. Return // { arity: 2 }
  1504. Project (#0, #3{count}) // { arity: 2 }
  1505. Join on=(#0 = #1 = #2) type=delta // { arity: 4 }
  1506. implementation
  1507. %0 » %1:l3[#0]UK » %2[#0]K
  1508. %1:l3 » %0[#0]UK » %2[#0]K
  1509. %2 » %0[#0]UK » %1:l3[#0]UK
  1510. ArrangeBy keys=[[#0]] // { arity: 1 }
  1511. Constant // { arity: 1 }
  1512. - (1)
  1513. - (2)
  1514. - (3)
  1515. - (4)
  1516. - (5)
  1517. - (6)
  1518. - (7)
  1519. - (8)
  1520. - (9)
  1521. - (10)
  1522. Get l3 // { arity: 1 }
  1523. ArrangeBy keys=[[#0]] // { arity: 2 }
  1524. Union // { arity: 2 }
  1525. Get l5 // { arity: 2 }
  1526. Project (#0, #2) // { arity: 2 }
  1527. Map (null) // { arity: 3 }
  1528. Join on=(#0 = #1) type=differential // { arity: 2 }
  1529. implementation
  1530. %1:l3[#0]UK » %0[#0]K
  1531. ArrangeBy keys=[[#0]] // { arity: 1 }
  1532. Union // { arity: 1 }
  1533. Negate // { arity: 1 }
  1534. Distinct project=[#0] // { arity: 1 }
  1535. Project (#0) // { arity: 1 }
  1536. Get l5 // { arity: 2 }
  1537. Constant // { arity: 1 }
  1538. - (1)
  1539. - (2)
  1540. - (3)
  1541. - (4)
  1542. - (5)
  1543. - (6)
  1544. - (7)
  1545. - (8)
  1546. - (9)
  1547. - (10)
  1548. Get l3 // { arity: 1 }
  1549. Target cluster: quickstart
  1550. EOF