12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760 |
- # Copyright Materialize, Inc. and contributors. All rights reserved.
- #
- # Use of this software is governed by the Business Source License
- # included in the LICENSE file at the root of this repository.
- #
- # As of the Change Date specified in that file, in accordance with
- # the Business Source License, use of this software will be governed
- # by the Apache License, Version 2.0.
- #
- # Test Common subexpression elimination for Relations.
- # PR https://github.com/MaterializeInc/materialize/pull/7715
- #
- statement ok
- CREATE TABLE t1 (f1 INTEGER, f2 INTEGER);
- statement ok
- CREATE INDEX i1 ON t1 (f1);
- statement ok
- CREATE TABLE t2 (f1 INTEGER, f2 INTEGER);
- ##
- ## Tests around the placement of CSEs in the top-level query itself
- ##
- # no CSE detection here, no predicates in query
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1 , t1 AS a2;
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 4 }
- CrossJoin type=differential // { arity: 4 }
- implementation
- %0:l0[×] » %1:l0[×]
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- #
- # Inner joins
- #
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1 , t1 AS a2, t1 AS a3;
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 6 }
- CrossJoin type=delta // { arity: 6 }
- implementation
- %0:l0 » %1:l0[×] » %2:l0[×]
- %1:l0 » %0:l0[×] » %2:l0[×]
- %2:l0 » %0:l0[×] » %1:l0[×]
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- query T multiline
- 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;
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]] // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 4 }
- CrossJoin type=differential // { arity: 4 }
- implementation
- %0:l0[×]e » %1:l0[×]e
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- 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;
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]] // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 6 }
- CrossJoin type=delta // { arity: 6 }
- implementation
- %0:l0 » %1:l0[×]e » %2:l0[×]e
- %1:l0 » %0:l0[×]e » %2:l0[×]e
- %2:l0 » %0:l0[×]e » %1:l0[×]e
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- #
- # Outer join
- #
- query T multiline
- 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;
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 3 }
- CrossJoin type=differential // { arity: 3 }
- implementation
- %0:l0[×]e » %1:l0[×]e
- ArrangeBy keys=[[]] // { arity: 2 }
- Get l0 // { arity: 2 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Project (#1{f2}) // { arity: 1 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- #
- # Subqueries
- #
- query T multiline
- 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);
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
- Union // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- Filter (#0{f1}) IS NOT NULL // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Project () // { arity: 0 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- Join on=(#0{f1} = #2{f1} AND #1{f2} = #3{f1}) type=delta // { arity: 4 }
- implementation
- %0:t1 » %1:l0[#0]K » %2:l0[#0]K
- %1:l0 » %0:t1[#0{f1}]KA » %2:l0[#0]K
- %2:l0 » %0:t1[#1{f2}]K » %1:l0[#0]K
- ArrangeBy keys=[[#0{f1}], [#1{f2}]] // { arity: 2 }
- Filter (#0{f1}) IS NOT NULL AND (#1{f2}) IS NOT NULL // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Get l0 // { arity: 1 }
- Get l0 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- query T multiline
- 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);
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l1 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- Return // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- Join on=(#0{f1} = #2{f1} AND #1{f2} = #3{f1}) type=delta // { arity: 4 }
- implementation
- %0:t1 » %1:l1[#0]K » %2:l1[#0]K
- %1:l1 » %0:t1[#0{f1}]KA » %2:l1[#0]K
- %2:l1 » %0:t1[#1{f2}]K » %1:l1[#0]K
- ArrangeBy keys=[[#0{f1}], [#1{f2}]] // { arity: 2 }
- Filter (#0{f1}) IS NOT NULL AND (#1{f2}) IS NOT NULL // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Get l1 // { arity: 1 }
- Get l1 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- 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);
- ----
- Explained Query:
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- Join on=(#0{f1} = #2{f1}) type=differential // { arity: 3 }
- implementation
- %0:t1[#0{f1}]KA » %1[#0]K
- ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
- ReadIndex on=t1 i1=[differential join] // { arity: 2 }
- ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
- Union // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- Filter (#0{f1}) IS NOT NULL // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Project () // { arity: 0 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, differential join)
- Target cluster: quickstart
- EOF
- query T multiline
- 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);
- ----
- Explained Query:
- With
- cte l0 =
- Union // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Project () // { arity: 0 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- cte l1 =
- ArrangeBy keys=[[]] // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Return // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- Filter ((#0{f1} = #2{f1}) OR (#1{f2} = #3{f1})) // { arity: 4 }
- CrossJoin type=delta // { arity: 4 }
- implementation
- %0:t1 » %1:l1[×] » %2:l1[×]
- %1:l1 » %0:t1[×] » %2:l1[×]
- %2:l1 » %0:t1[×] » %1:l1[×]
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Get l1 // { arity: 1 }
- Get l1 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- #
- # CSEs in derived tables
- #
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT *
- FROM (SELECT * FROM t1 WHERE f1 = 1) AS a1
- JOIN (SELECT * FROM t1 WHERE f1 = 1) AS a2
- ON TRUE
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]] // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 4 }
- CrossJoin type=differential // { arity: 4 }
- implementation
- %0:l0[×]e » %1:l0[×]e
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT *
- FROM (SELECT * FROM t1 WHERE f1 = 1) AS a1
- , (SELECT * FROM t1 WHERE f1 = 1) AS a2
- WHERE a1.f2 = 2
- AND a2.f2 = 2
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]] // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- Filter (#1{f2} = 2) // { arity: 3 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 4 }
- CrossJoin type=differential // { arity: 4 }
- implementation
- %0:l0[×]ef » %1:l0[×]ef
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- # No CSE with only partially-overlapping predicates
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT *
- FROM (SELECT * FROM t1 WHERE f1 = 1) AS a1
- , (SELECT * FROM t1 WHERE f1 = 1) AS a2
- WHERE a1.f2 = 2
- AND a2.f2 = 3
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 4 }
- CrossJoin type=differential // { arity: 4 }
- implementation
- %0:l0[×]ef » %1:l0[×]ef
- ArrangeBy keys=[[]] // { arity: 2 }
- Filter (#1{f2} = 2) // { arity: 2 }
- Get l0 // { arity: 2 }
- ArrangeBy keys=[[]] // { arity: 2 }
- Filter (#1{f2} = 3) // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- #
- # CSEs in UNION branches
- #
- query T multiline
- 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;
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- 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;
- ----
- Explained Query:
- With
- cte l0 =
- Project (#1{f2}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 2 }
- Project (#1, #0{f2}) // { arity: 2 }
- Map (1) // { arity: 2 }
- Distinct project=[#0{f2}] // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Get l0 // { arity: 1 }
- Get l0 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- #
- # CSEs in the SELECT list
- #
- query T multiline
- 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;
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l1 =
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- Return // { arity: 2 }
- Project (#0{f1}, #0{f1}) // { arity: 2 }
- CrossJoin type=differential // { arity: 1 }
- implementation
- %0:t1[×] » %1[×]
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Union // { arity: 1 }
- Get l1 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- 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;
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l1 =
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- cte l2 =
- ArrangeBy keys=[[]] // { arity: 1 }
- Union // { arity: 1 }
- Get l1 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- cte l3 =
- Reduce aggregates=[min(#0{f1}), max(#1{f1})] // { arity: 2 }
- CrossJoin type=delta // { arity: 2 }
- implementation
- %0:t1 » %1:l2[×] » %2:l2[×]
- %1:l2 » %0:t1[×] » %2:l2[×]
- %2:l2 » %0:t1[×] » %1:l2[×]
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Get l2 // { arity: 1 }
- Get l2 // { arity: 1 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l3 // { arity: 2 }
- Map (null, null) // { arity: 2 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Project () // { arity: 0 }
- Get l3 // { arity: 2 }
- Constant // { arity: 0 }
- - ()
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- #
- # CSEs at two distinct positions within the query
- #
- query T multiline
- 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);
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l1 =
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- cte l2 =
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Get l1 // { arity: 0 }
- Return // { arity: 1 }
- CrossJoin type=delta // { arity: 1 }
- implementation
- %0:t1 » %1[×]UA » %2[×]
- %1 » %0:t1[×] » %2[×]
- %2 » %1[×]UA » %0:t1[×]
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- ArrangeBy keys=[[]] // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Get l1 // { arity: 0 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Union // { arity: 1 }
- Get l2 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- Get l2 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT (SELECT f1 FROM t1 WHERE f1 = 1) FROM t1
- UNION ALL
- SELECT f1 FROM t1 WHERE f1 = 1
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l1 =
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- CrossJoin type=differential // { arity: 1 }
- implementation
- %0:t1[×] » %1[×]
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Union // { arity: 1 }
- Get l1 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Get l0 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- ##
- ## Tests around the contents of the CSE itself
- ##
- #
- # CSE containing a join
- #
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT * FROM t1 AS a1, t1 AS a2
- UNION ALL
- SELECT * FROM t1 AS a1, t1 AS a2
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- cte l1 =
- CrossJoin type=differential // { arity: 4 }
- implementation
- %0:l0[×] » %1:l0[×]
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Return // { arity: 4 }
- Union // { arity: 4 }
- Get l1 // { arity: 4 }
- Get l1 // { arity: 4 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- query T multiline
- 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)
- UNION ALL
- SELECT * FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
- ReadIndex on=t1 i1=[differential join] // { arity: 2 }
- cte l1 =
- Project (#0{f1}, #1{f2}, #3{f2}) // { arity: 3 }
- Filter (#0{f1}) IS NOT NULL // { arity: 4 }
- Join on=(#0{f1} = #2{f1}) type=differential // { arity: 4 }
- implementation
- %0:l0[#0{f1}]KA » %1:l0[#0{f1}]KA
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- cte l2 =
- Map (null) // { arity: 3 }
- Union // { arity: 2 }
- Negate // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- Join on=(#0{f1} = #2{f1}) type=differential // { arity: 3 }
- implementation
- %1[#0]UKA » %0:l0[#0{f1}]KA
- Get l0 // { arity: 2 }
- ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
- Distinct project=[#0{f1}] // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- Get l1 // { arity: 3 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 3 }
- Union // { arity: 3 }
- Get l2 // { arity: 3 }
- Get l1 // { arity: 3 }
- Get l2 // { arity: 3 }
- Get l1 // { arity: 3 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, differential join)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT * FROM (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) WHERE f1 = 1
- UNION ALL
- SELECT * FROM (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) WHERE f1 = 2
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
- ReadIndex on=t1 i1=[lookup] // { arity: 2 }
- cte l1 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l2 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- CrossJoin type=differential // { arity: 1 }
- implementation
- %0:l1[×]e » %1:l1[×]e
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Get l1 // { arity: 1 }
- CrossJoin type=differential // { arity: 1 }
- implementation
- %0:l2[×]e » %1:l2[×]e
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- Get l2 // { arity: 1 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Get l2 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT * FROM
- (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) AS s1,
- (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) AS s2
- WHERE s1.f1 = 1 AND s2.f1 = 1
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l1 =
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- cte l2 =
- ArrangeBy keys=[[]] // { arity: 1 }
- Get l0 // { arity: 1 }
- Return // { arity: 2 }
- CrossJoin type=delta // { arity: 2 }
- implementation
- %0:l1 » %1:l2[×]e » %2:l1[×]e » %3:l2[×]e
- %1:l2 » %0:l1[×]e » %2:l1[×]e » %3:l2[×]e
- %2:l1 » %0:l1[×]e » %1:l2[×]e » %3:l2[×]e
- %3:l2 » %0:l1[×]e » %1:l2[×]e » %2:l1[×]e
- Get l1 // { arity: 0 }
- Get l2 // { arity: 1 }
- Get l1 // { arity: 0 }
- Get l2 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT * FROM
- (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) AS s1,
- (SELECT a2.f1 AS f1 FROM t1 AS a1 LEFT JOIN t1 AS a2 USING (f1)) AS s2
- WHERE s1.f1 = 1 AND s2.f1 = 2
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
- ReadIndex on=t1 i1=[lookup] // { arity: 2 }
- cte l1 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l2 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
- Return // { arity: 2 }
- CrossJoin type=delta // { arity: 2 }
- implementation
- %0:l1 » %1:l1[×]e » %2:l2[×]e » %3:l2[×]e
- %1:l1 » %0:l1[×]e » %2:l2[×]e » %3:l2[×]e
- %2:l2 » %0:l1[×]e » %1:l1[×]e » %3:l2[×]e
- %3:l2 » %0:l1[×]e » %1:l1[×]e » %2:l2[×]e
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Get l1 // { arity: 1 }
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- Get l2 // { arity: 1 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Get l2 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- #
- # CSE containing a conjunction (AND)
- #
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT * FROM t1 WHERE f1 = 1 AND f2 = 2
- UNION ALL
- SELECT * FROM t1 WHERE f1 = 1 AND f2 = 2
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- Filter (#1{f2} = 2) // { arity: 3 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- #
- # CSE containing a disjunction (OR)
- #
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT * FROM t1 WHERE f1 = 1 OR f1 = 2
- UNION ALL
- SELECT * FROM t1 WHERE f1 = 1 OR f1 = 2
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup values=[(1); (2)]] // { arity: 3 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- #
- # CSE containing a subquery
- #
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1)
- UNION ALL
- SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1)
- ----
- Explained Query:
- With
- cte l0 =
- CrossJoin type=differential // { arity: 2 }
- implementation
- %1[×]UA » %0:t1[×]
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- ArrangeBy keys=[[]] // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- 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)
- UNION ALL
- SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1 WHERE f1 = 1)
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
- ReadIndex on=t1 i1=[differential join, lookup] // { arity: 2 }
- cte l1 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l2 =
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- Join on=(#0{f1} = #2{f1}) type=differential // { arity: 3 }
- implementation
- %0:l0[#0{f1}]KA » %1[#0]K
- Get l0 // { arity: 2 }
- ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
- Union // { arity: 1 }
- Get l1 // { arity: 1 }
- Project (#1) // { arity: 1 }
- FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
- Reduce aggregates=[count(*)] // { arity: 1 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l2 // { arity: 2 }
- Get l2 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (differential join, lookup)
- Target cluster: quickstart
- EOF
- #
- # CSE containing a derived table
- #
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT f1 + 1 FROM (SELECT f1 + 2 AS f1 FROM t1)
- UNION ALL
- SELECT f1 + 1 FROM (SELECT f1 + 2 AS f1 FROM t1)
- ----
- Explained Query:
- With
- cte l0 =
- Project (#2) // { arity: 1 }
- Map (((#0{f1} + 2) + 1)) // { arity: 3 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Get l0 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- #
- # CSEs containing an aggregate
- #
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT MIN(f1) FROM t1
- UNION ALL
- SELECT MIN(f1) FROM t1;
- ----
- Explained Query:
- With
- cte l0 =
- Reduce aggregates=[min(#0{f1})] // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- cte l1 =
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Return // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Get l1 // { arity: 1 }
- Get l0 // { arity: 1 }
- Get l1 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT DISTINCT f1 FROM t1
- UNION ALL
- SELECT DISTINCT f1 FROM t1
- ----
- Explained Query:
- With
- cte l0 =
- Distinct project=[#0{f1}] // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Get l0 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT f1, COUNT(*) FROM t1 GROUP BY f1
- UNION ALL
- SELECT f1, COUNT(*) FROM t1 GROUP BY f1
- ----
- Explained Query:
- With
- cte l0 =
- Reduce group_by=[#0{f1}] aggregates=[count(*)] // { arity: 2 }
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- #
- # CSEs containing an expression / function
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT f1 + f1 + f1 + f1 FROM t1
- UNION ALL
- SELECT f1 + f1 + f1 + f1 FROM t1
- ----
- Explained Query:
- With
- cte l0 =
- Project (#2) // { arity: 1 }
- Map ((((#0{f1} + #0{f1}) + #0{f1}) + #0{f1})) // { arity: 3 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Get l0 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT ABS(f1) FROM t1
- UNION ALL
- SELECT ABS(f1) FROM t1
- ----
- Explained Query:
- With
- cte l0 =
- Project (#2) // { arity: 1 }
- Map (abs(#0{f1})) // { arity: 3 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Get l0 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- ##
- ## Nested CSEs
- ##
- query T multiline
- 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 UNION ALL SELECT * FROM t1 WHERE f1 = 1)
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- (SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
- UNION ALL
- (SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
- ----
- Explained Query:
- With
- cte l0 =
- CrossJoin type=differential // { arity: 2 }
- implementation
- %1[×]UA » %0:t1[×]
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- ArrangeBy keys=[[]] // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- ##
- ## Deeper-placed CSEs (where a higher-level construct is not a CSE but a lower-level one is)
- ##
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- (SELECT f1 FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
- UNION ALL
- (SELECT f2 FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
- ----
- Explained Query:
- With
- cte l0 =
- CrossJoin type=differential // { arity: 2 }
- implementation
- %1[×]UA » %0:t1[×]
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- ArrangeBy keys=[[]] // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- Get l0 // { arity: 2 }
- Project (#1{f2}) // { arity: 1 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- (SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
- UNION ALL
- (SELECT * FROM t2 WHERE EXISTS (SELECT * FROM t1 WHERE f1 = 1))
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]] // { arity: 0 }
- Distinct project=[] // { arity: 0 }
- Project () // { arity: 0 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- CrossJoin type=differential // { arity: 2 }
- implementation
- %1:l0[×]UA » %0:t1[×]
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Get l0 // { arity: 0 }
- CrossJoin type=differential // { arity: 2 }
- implementation
- %1:l0[×]UA » %0:t2[×]
- ArrangeBy keys=[[]] // { arity: 2 }
- ReadStorage materialize.public.t2 // { arity: 2 }
- Get l0 // { arity: 0 }
- Source materialize.public.t2
- Used Indexes:
- - materialize.public.i1 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM
- (SELECT f1 FROM t2 UNION ALL SELECT f1 FROM t1 WHERE f1 = 1) ,
- (SELECT f2 FROM t2 UNION ALL SELECT f1 FROM t1 WHERE f1 = 1)
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 2 }
- CrossJoin type=differential // { arity: 2 }
- implementation
- %0[×] » %1[×]
- ArrangeBy keys=[[]] // { arity: 1 }
- Union // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- ReadStorage materialize.public.t2 // { arity: 2 }
- Get l0 // { arity: 1 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Union // { arity: 1 }
- Project (#1{f2}) // { arity: 1 }
- ReadStorage materialize.public.t2 // { arity: 2 }
- Get l0 // { arity: 1 }
- Source materialize.public.t2
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- # Same predicate, different projections
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT f1 FROM t1 WHERE f1 = 1
- UNION ALL
- SELECT f2 FROM t1 WHERE f1 = 1
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- Get l0 // { arity: 2 }
- Project (#1{f2}) // { arity: 1 }
- Get l0 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- ##
- ## Negative cases - CSEs should not be identified where they do not exist
- ##
- #
- query T multiline
- 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
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
- ReadIndex on=t1 i1=[lookup] // { arity: 2 }
- Return // { arity: 4 }
- CrossJoin type=differential // { arity: 4 }
- implementation
- %0:t1[×]e » %1:t1[×]e
- ArrangeBy keys=[[]] // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- ArrangeBy keys=[[]] // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT * FROM (SELECT a2.f1 AS f1 FROM t1 AS a1 JOIN t1 AS a2 USING (f1)) WHERE f1 = 1
- UNION ALL
- SELECT * FROM (SELECT a2.f1 AS f1 FROM t1 AS a1 JOIN t1 AS a2 USING (f1)) WHERE f1 = 2
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
- ReadIndex on=t1 i1=[lookup] // { arity: 2 }
- cte l1 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- cte l2 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- CrossJoin type=differential // { arity: 1 }
- implementation
- %0:l1[×]e » %1:l1[×]e
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Get l1 // { arity: 1 }
- CrossJoin type=differential // { arity: 1 }
- implementation
- %0:l2[×]e » %1:l2[×]e
- ArrangeBy keys=[[]] // { arity: 0 }
- Project () // { arity: 0 }
- Get l2 // { arity: 1 }
- ArrangeBy keys=[[]] // { arity: 1 }
- Get l2 // { arity: 1 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- 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 = 2
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
- ReadIndex on=t1 i1=[lookup] // { arity: 2 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(1)] // { arity: 3 }
- Project (#0{f1}, #1{f2}) // { arity: 2 }
- ReadIndex on=materialize.public.t1 i1=[lookup value=(2)] // { arity: 3 }
- Used Indexes:
- - materialize.public.i1 (lookup)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT MIN(f1) FROM t1
- UNION ALL
- SELECT MAX(f1) FROM t1
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- cte l1 =
- Reduce aggregates=[min(#0{f1})] // { arity: 1 }
- Get l0 // { arity: 1 }
- cte l2 =
- Reduce aggregates=[max(#0{f1})] // { arity: 1 }
- Get l0 // { arity: 1 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- Get l1 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Get l2 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Project () // { arity: 0 }
- Get l2 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT MIN(f1) FROM t1
- UNION ALL
- SELECT MIN(f2) FROM t1
- ----
- Explained Query:
- With
- cte l0 =
- Reduce aggregates=[min(#0{f1})] // { arity: 1 }
- Project (#0{f1}) // { arity: 1 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- cte l1 =
- Reduce aggregates=[min(#0{f2})] // { arity: 1 }
- Project (#1{f2}) // { arity: 1 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- Return // { arity: 1 }
- Union // { arity: 1 }
- Get l0 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Project () // { arity: 0 }
- Get l0 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Get l1 // { arity: 1 }
- Map (null) // { arity: 1 }
- Union // { arity: 0 }
- Negate // { arity: 0 }
- Project () // { arity: 0 }
- Get l1 // { arity: 1 }
- Constant // { arity: 0 }
- - ()
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- # WITH MUTUALLY RECURSIVE support
- # -------------------------------
- # Basic support for recursive queries.
- # With materialize#27389 this stopped testing what it says it tests; see issue database-issues#8294.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- WITH MUTUALLY RECURSIVE
- c0(f1 INTEGER, f2 INTEGER) AS (
- SELECT f1, f2 FROM t1
- UNION ALL
- SELECT f1, f2 FROM t1
- ),
- c1(f1 INTEGER, f2 INTEGER) AS (
- SELECT * FROM c0 WHERE f2 > 7
- UNION ALL
- SELECT * FROM c1 WHERE f2 > 7
- UNION ALL
- SELECT * FROM c1 WHERE f2 > 7
- UNION ALL
- SELECT * FROM c2 WHERE f2 > 7
- UNION ALL
- SELECT * FROM c2 WHERE f2 > 7
- ),
- c2(f1 INTEGER, f2 INTEGER) AS (
- SELECT * FROM c0 WHERE f2 > 7
- UNION ALL
- SELECT * FROM c1 WHERE f2 > 7
- UNION ALL
- SELECT * FROM c1 WHERE f2 > 7
- UNION ALL
- SELECT * FROM c2 WHERE f2 > 7
- UNION ALL
- SELECT * FROM c2 WHERE f2 > 7
- )
- SELECT * FROM c0 WHERE f1 > 7
- UNION ALL
- SELECT * FROM c1 WHERE f1 > 7
- UNION ALL
- SELECT * FROM c2 WHERE f1 > 7
- ----
- Explained Query:
- With
- cte l0 =
- Union // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- ReadIndex on=t1 i1=[*** full scan ***] // { arity: 2 }
- cte l1 =
- Filter (#1{f2} > 7) // { arity: 2 }
- Get l0 // { arity: 2 }
- Return // { arity: 2 }
- With Mutually Recursive
- cte l2 =
- Union // { arity: 2 }
- Get l1 // { arity: 2 }
- Get l2 // { arity: 2 }
- Get l2 // { arity: 2 }
- Get l3 // { arity: 2 }
- Get l3 // { arity: 2 }
- cte l3 =
- Union // { arity: 2 }
- Get l1 // { arity: 2 }
- Get l2 // { arity: 2 }
- Get l2 // { arity: 2 }
- Get l3 // { arity: 2 }
- Get l3 // { arity: 2 }
- Return // { arity: 2 }
- Union // { arity: 2 }
- Filter (#0{f1} > 7) // { arity: 2 }
- Get l0 // { arity: 2 }
- Filter (#0{f1} > 7) // { arity: 2 }
- Get l2 // { arity: 2 }
- Filter (#0{f1} > 7) // { arity: 2 }
- Get l3 // { arity: 2 }
- Used Indexes:
- - materialize.public.i1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Correlated WMR block.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
- SELECT bound, (
- WITH MUTUALLY RECURSIVE
- numbers (n int) as (
- VALUES (1)
- UNION ALL
- (
- WITH rebound AS (SELECT * FROM numbers)
- SELECT distinct t1.n + t2.n AS n
- FROM rebound AS t1, rebound AS t2
- WHERE t1.n <= bound AND t2.n <= bound
- )
- )
- SELECT count(*) FROM numbers
- )
- FROM (
- SELECT generate_series AS bound FROM generate_series(1, 10)
- );
- ----
- Explained Query:
- With Mutually Recursive
- cte l0 =
- ArrangeBy keys=[[#0]] // { arity: 2 }
- Filter (#1{n} <= #0{bound}) // { arity: 2 }
- Get l1 // { arity: 2 }
- cte l1 =
- Union // { arity: 2 }
- Distinct project=[#0, (#1{n} + #2{n})] monotonic // { arity: 2 }
- Project (#0, #1, #3) // { arity: 3 }
- Join on=(#0 = #2) type=differential // { arity: 4 }
- implementation
- %0:l0[#0]Kf » %1:l0[#0]Kf
- Get l0 // { arity: 2 }
- Get l0 // { arity: 2 }
- Constant // { arity: 2 }
- - (1, 1)
- - (2, 1)
- - (3, 1)
- - (4, 1)
- - (5, 1)
- - (6, 1)
- - (7, 1)
- - (8, 1)
- - (9, 1)
- - (10, 1)
- Return // { arity: 2 }
- With
- cte l2 =
- Reduce group_by=[#0] aggregates=[count(*)] monotonic // { arity: 2 }
- Project (#0) // { arity: 1 }
- Get l1 // { arity: 2 }
- cte l3 =
- ArrangeBy keys=[[#0]] // { arity: 1 }
- Constant // { arity: 1 }
- - (1)
- - (2)
- - (3)
- - (4)
- - (5)
- - (6)
- - (7)
- - (8)
- - (9)
- - (10)
- cte l4 =
- Union // { arity: 2 }
- Get l2 // { arity: 2 }
- Project (#0, #2) // { arity: 2 }
- Map (0) // { arity: 3 }
- Join on=(#0 = #1) type=differential // { arity: 2 }
- implementation
- %1:l3[#0]UK » %0[#0]K
- ArrangeBy keys=[[#0]] // { arity: 1 }
- Union // { arity: 1 }
- Negate // { arity: 1 }
- Project (#0) // { arity: 1 }
- Get l2 // { arity: 2 }
- Constant // { arity: 1 }
- - (1)
- - (2)
- - (3)
- - (4)
- - (5)
- - (6)
- - (7)
- - (8)
- - (9)
- - (10)
- Get l3 // { arity: 1 }
- cte l5 =
- Union // { arity: 2 }
- Get l4 // { arity: 2 }
- Project (#0, #2) // { arity: 2 }
- FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
- Reduce group_by=[#0] aggregates=[count(*)] // { arity: 2 }
- Project (#0) // { arity: 1 }
- Get l4 // { arity: 2 }
- Return // { arity: 2 }
- Project (#0, #3{count}) // { arity: 2 }
- Join on=(#0 = #1 = #2) type=delta // { arity: 4 }
- implementation
- %0 » %1:l3[#0]UK » %2[#0]K
- %1:l3 » %0[#0]UK » %2[#0]K
- %2 » %0[#0]UK » %1:l3[#0]UK
- ArrangeBy keys=[[#0]] // { arity: 1 }
- Constant // { arity: 1 }
- - (1)
- - (2)
- - (3)
- - (4)
- - (5)
- - (6)
- - (7)
- - (8)
- - (9)
- - (10)
- Get l3 // { arity: 1 }
- ArrangeBy keys=[[#0]] // { arity: 2 }
- Union // { arity: 2 }
- Get l5 // { arity: 2 }
- Project (#0, #2) // { arity: 2 }
- Map (null) // { arity: 3 }
- Join on=(#0 = #1) type=differential // { arity: 2 }
- implementation
- %1:l3[#0]UK » %0[#0]K
- ArrangeBy keys=[[#0]] // { arity: 1 }
- Union // { arity: 1 }
- Negate // { arity: 1 }
- Distinct project=[#0] // { arity: 1 }
- Project (#0) // { arity: 1 }
- Get l5 // { arity: 2 }
- Constant // { arity: 1 }
- - (1)
- - (2)
- - (3)
- - (4)
- - (5)
- - (6)
- - (7)
- - (8)
- - (9)
- - (10)
- Get l3 // { arity: 1 }
- Target cluster: quickstart
- EOF
|