12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808 |
- # 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.
- simple conn=mz_system,user=mz_system
- ALTER SYSTEM SET unsafe_enable_table_keys = true
- ----
- COMPLETE 0
- statement ok
- CREATE TABLE t (
- a int,
- b int
- );
- statement ok
- CREATE TABLE u (
- c int,
- d int
- );
- statement ok
- CREATE TABLE v (
- e int,
- f int
- );
- statement ok
- CREATE INDEX t_a_idx ON t(a);
- statement ok
- CREATE VIEW ov AS SELECT * FROM t ORDER BY b asc, a desc LIMIT 5;
- statement ok
- CREATE VIEW iv AS
- SELECT * FROM t WHERE a IS NOT NULL;
- statement ok
- CREATE INDEX iv_a_idx ON iv(a);
- statement ok
- CREATE INDEX iv_b_idx ON iv(b);
- # This is an identical index to the above (on the same object, on the same key)
- statement ok
- CREATE INDEX iv_b_idx_2 ON iv(b);
- statement ok
- CREATE MATERIALIZED VIEW mv AS
- SELECT * FROM t WHERE a IS NOT NULL;
- statement ok
- CREATE MATERIALIZED VIEW non_empty_mv AS
- SELECT 1 as x, 2 as y;
- statement ok
- CREATE INDEX non_empty_mv_idx ON non_empty_mv(y + 7);
- statement ok
- CREATE MATERIALIZED VIEW empty_mv AS
- SELECT;
- statement ok
- CREATE INDEX empty_mv_idx ON empty_mv();
- mode cockroach
- # Test target cluster selection for mz_system tables without transactions.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(no fast path, humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM mz_views;
- ----
- Explained Query:
- ReadIndex on=mz_views mz_views_ind=[*** full scan ***]
- Used Indexes:
- - mz_catalog.mz_views_ind (*** full scan ***)
- Target cluster: mz_catalog_server
- EOF
- # Test target cluster selection for mz_system tables inside a transaction.
- statement ok
- BEGIN
- statement ok
- SELECT * FROM t;
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(no fast path, humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM mz_views;
- ----
- Explained Query:
- ReadStorage mz_catalog.mz_views
- Source mz_catalog.mz_views
- Target cluster: quickstart
- EOF
- statement ok
- ROLLBACK
- # Test constant error.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT 1 / 0
- ----
- Explained Query (fast path):
- Error "division by zero"
- Target cluster: quickstart
- EOF
- # Test constant with two elements.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- (SELECT 1, 2) UNION ALL (SELECT 1, 2) UNION ALL (SELECT 3, 4)
- ----
- Explained Query (fast path):
- Constant
- - ((1, 2) x 2)
- - (3, 4)
- Target cluster: mz_catalog_server
- EOF
- # Test catalog queries (index found based on cluster auto-routing).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM mz_internal.mz_source_status_history
- ----
- Explained Query (fast path):
- Project (#1{occurred_at}, #0{source_id}, #2{status}..=#5{replica_id})
- ReadIndex on=mz_internal.mz_source_status_history mz_source_status_history_ind=[*** full scan ***]
- Used Indexes:
- - mz_internal.mz_source_status_history_ind (*** full scan ***)
- Target cluster: mz_catalog_server
- EOF
- # Test fast path rendering on a non-trivial index.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM non_empty_mv where y + 7 = 9
- ----
- Explained Query (fast path):
- Project (#1{x}, #2{y})
- ReadIndex on=materialize.public.non_empty_mv non_empty_mv_idx=[lookup value=(9)]
- Used Indexes:
- - materialize.public.non_empty_mv_idx (lookup)
- Target cluster: quickstart
- EOF
- # Test fast path rendering on an empty index on an empty relation.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM empty_mv
- ----
- Explained Query (fast path):
- ReadIndex on=materialize.public.empty_mv empty_mv_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.empty_mv_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test basic linear chains (fast path).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT 1, a + b as c FROM iv WHERE b = 5 and a < 0 and a + b > 0
- ----
- Explained Query (fast path):
- Project (#4, #3)
- Filter (#1{a} < 0) AND ((#1{a} + #0{b}) > 0)
- Map ((#1{a} + 5), 1)
- ReadIndex on=materialize.public.iv iv_b_idx_2=[lookup value=(5)]
- Used Indexes:
- - materialize.public.iv_b_idx_2 (lookup)
- Target cluster: quickstart
- EOF
- # Test basic linear chains (slow path).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT 1, a + b as c FROM mv WHERE a > 0 and b < 0 and a + b > 0
- ----
- Explained Query:
- Project (#3, #2)
- Filter (#1{b} < 0) AND (#0{a} > 0) AND (#2 > 0)
- Map ((#0{a} + #1{b}), 1)
- ReadStorage materialize.public.mv
- Source materialize.public.mv
- filter=((#0{a} > 0) AND (#1{b} < 0) AND ((#0{a} + #1{b}) > 0))
- Target cluster: quickstart
- EOF
- # Test table functions in the select clause (FlatMap).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT generate_series(a, b) from t
- ----
- Explained Query:
- Project (#2)
- FlatMap generate_series(#0{a}, #1{b}, 1)
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test Threshold, Union, Distinct, Negate.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT a FROM t EXCEPT SELECT b FROM mv
- ----
- Explained Query:
- Threshold
- Union
- Distinct project=[#0{a}]
- Project (#0{a})
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Negate
- Distinct project=[#0{b}]
- Project (#1{b})
- ReadStorage materialize.public.mv
- Source materialize.public.mv
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test Threshold, Union, Distinct, Negate.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT a FROM t EXCEPT ALL SELECT b FROM mv
- ----
- Explained Query:
- Threshold
- Union
- Project (#0{a})
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Negate
- Project (#1{b})
- ReadStorage materialize.public.mv
- Source materialize.public.mv
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test TopK.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM ov
- ----
- Explained Query:
- TopK order_by=[#1{b} asc nulls_last, #0{a} desc nulls_first] limit=5
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test Finish.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM t ORDER BY b asc, a desc LIMIT 5
- ----
- Explained Query (fast path):
- Finish order_by=[#1 asc nulls_last, #0 desc nulls_first] limit=5 output=[#0, #1]
- ReadIndex on=materialize.public.t t_a_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test Reduce (global).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT abs(min(a) - max(a)) FROM t
- ----
- Explained Query:
- With
- cte l0 =
- Reduce aggregates=[min(#0{a}), max(#0{a})]
- Project (#0{a})
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Return
- Project (#2)
- Map (abs((#0{min_a} - #1{max_a})))
- Union
- Get l0
- Map (null, null)
- Union
- Negate
- Project ()
- Get l0
- Constant
- - ()
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test Reduce (local).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT abs(min(a) - max(a)) FROM t GROUP BY b
- ----
- Explained Query:
- Project (#3)
- Map (abs((#1{min_a} - #2{max_a})))
- Reduce group_by=[#1{b}] aggregates=[min(#0{a}), max(#0{a})]
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test EXISTS subqueries.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM t WHERE EXISTS(SELECT * FROM mv WHERE t.a < mv.a) AND EXISTS(SELECT * FROM mv WHERE t.b > mv.b)
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{a}, #1{b})
- Join on=(#0{a} = #2{a}) type=differential
- ArrangeBy keys=[[#0{a}]]
- ReadIndex on=t t_a_idx=[differential join]
- ArrangeBy keys=[[#0{a}]]
- Distinct project=[#0{a}]
- Project (#0{a})
- Filter (#0{a} < #1{a})
- CrossJoin type=differential
- ArrangeBy keys=[[]]
- Distinct project=[#0{a}]
- Project (#0{a})
- ReadIndex on=t t_a_idx=[*** full scan ***]
- ArrangeBy keys=[[]]
- Project (#0{a})
- ReadStorage materialize.public.mv
- Return
- Project (#0{a}, #1{b})
- Join on=(#1{b} = #2{b}) type=differential
- ArrangeBy keys=[[#1{b}]]
- Get l0
- ArrangeBy keys=[[#0{b}]]
- Distinct project=[#0{b}]
- Project (#0{b})
- Filter (#0{b} > #1{b})
- CrossJoin type=differential
- ArrangeBy keys=[[]]
- Distinct project=[#0{b}]
- Project (#1{b})
- Get l0
- ArrangeBy keys=[[]]
- Project (#1{b})
- ReadStorage materialize.public.mv
- Source materialize.public.mv
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***, differential join)
- Target cluster: quickstart
- EOF
- # Test SELECT subqueries.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT (SELECT iv.a FROM iv WHERE iv.b = t.b LIMIT 1), (SELECT mv.a FROM mv WHERE mv.b = t.b LIMIT 1) FROM t
- ----
- Explained Query:
- With
- cte l0 =
- Project (#1{b})
- ReadIndex on=t t_a_idx=[*** full scan ***]
- cte l1 =
- Distinct project=[#0{b}]
- Get l0
- cte l2 =
- ArrangeBy keys=[[#0{b}]]
- Get l1
- cte l3 =
- TopK group_by=[#0{b}] limit=1
- Project (#0{b}, #1{a})
- Filter (#0{b}) IS NOT NULL
- Join on=(#0{b} = #2{b}) type=differential
- Get l2
- ArrangeBy keys=[[#1{b}]]
- ReadIndex on=iv iv_b_idx=[differential join]
- cte l4 =
- TopK group_by=[#0{b}] limit=1
- Project (#0{b}, #1{a})
- Join on=(#0{b} = #2{b}) type=differential
- Get l2
- ArrangeBy keys=[[#1{b}]]
- Filter (#1{b}) IS NOT NULL
- ReadStorage materialize.public.mv
- Return
- Project (#2{a}, #4{a})
- Join on=(#0{b} = #1{b} = #3{b}) type=delta
- ArrangeBy keys=[[#0{b}]]
- Get l0
- ArrangeBy keys=[[#0{b}]]
- Union
- Get l3
- Map (null)
- Union
- Negate
- Project (#0{b})
- Get l3
- Get l1
- ArrangeBy keys=[[#0{b}]]
- Union
- Get l4
- Map (null)
- Union
- Negate
- Project (#0{b})
- Get l4
- Get l1
- Source materialize.public.mv
- filter=((#1{b}) IS NOT NULL)
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- - materialize.public.iv_b_idx (differential join)
- Target cluster: quickstart
- EOF
- # Test outer joins (ON syntax).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT t1.a, t2.a
- FROM t as t1
- LEFT JOIN t as t2 ON t1.b = t2.b
- RIGHT JOIN t as t3 ON t2.b = t3.b
- ----
- Explained Query:
- With
- cte l0 =
- Filter (#1{b}) IS NOT NULL
- ReadIndex on=t t_a_idx=[*** full scan ***]
- cte l1 =
- ArrangeBy keys=[[#1{b}]]
- Get l0
- cte l2 =
- ArrangeBy keys=[[#0{b}]]
- Project (#1{b})
- Get l0
- cte l3 =
- Project (#0{a}..=#2{a})
- Join on=(#1{b} = #3{b} = #4{b}) type=delta
- Get l1
- Get l1
- Get l2
- Return
- Union
- Map (null, null)
- Union
- Negate
- Project ()
- Join on=(#0{b} = #1{b}) type=differential
- Get l2
- ArrangeBy keys=[[#0{b}]]
- Distinct project=[#0{b}]
- Project (#1{b})
- Get l3
- Project ()
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Project (#0{a}, #2{a})
- Get l3
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test a single CTE.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- WITH x AS (SELECT t.a * t.b as v from t) SELECT a.v + b.v FROM x as a, x as b
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]]
- Project (#2)
- Map ((#0{a} * #1{b}))
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Return
- Project (#2)
- Map ((#0{v} + #1{v}))
- CrossJoin type=differential
- Get l0
- Get l0
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test EXPLAIN INDEX for an indexed source
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- INDEX t_a_idx
- ----
- materialize.public.t_a_idx:
- ArrangeBy keys=[[#0{a}]]
- ReadStorage materialize.public.t
- Source materialize.public.t
- Target cluster: quickstart
- EOF
- # Test EXPLAIN INDEX for an indexed view (first index)
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- INDEX iv_a_idx;
- ----
- materialize.public.iv_a_idx:
- ArrangeBy keys=[[#0{a}]]
- ReadGlobalFromSameDataflow materialize.public.iv
- materialize.public.iv:
- Filter (#0{a}) IS NOT NULL
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test EXPLAIN INDEX for an indexed view (based on a prior index)
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- INDEX iv_b_idx;
- ----
- materialize.public.iv_b_idx:
- ArrangeBy keys=[[#1{b}]]
- ReadIndex on=iv iv_a_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.iv_a_idx (*** full scan ***, index export)
- Target cluster: quickstart
- EOF
- # Test EXPLAIN INDEX for an indexed view where the index is exactly the same as a prior index.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- INDEX iv_b_idx_2;
- ----
- materialize.public.iv_b_idx_2:
- ArrangeBy keys=[[#1{b}]]
- ReadIndex on=iv iv_b_idx=[plan root (no new arrangement)]
- Used Indexes:
- - materialize.public.iv_b_idx (plan root (no new arrangement), index export)
- Target cluster: quickstart
- Notices:
- - Notice: Index materialize.public.iv_b_idx_2 is identical to materialize.public.iv_b_idx, which is also defined on iv(b).
- Hint: Please drop all indexes except the first index created on iv(b) and recreate all dependent objects.
- EOF
- # Test multiple CTEs: a case where we cannot pull the let statement up through
- # the join because the local l0 is correlated against the lhs of the enclosing join.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT
- *
- FROM
- (
- SELECT * FROM t
- ) as r1
- CROSS JOIN LATERAL (
- WITH r2 as (
- SELECT MAX(r1.a * t.a) AS m FROM t
- )
- SELECT * FROM r2 WHERE r2.m != r1.a
- ) as r3
- CROSS JOIN LATERAL (
- WITH r4 as (
- SELECT MAX(r1.a * t.a) AS m FROM t
- )
- SELECT * FROM r4 WHERE r4.m != r1.a OR (r4.m IS NOT NULL AND r1.a IS NULL)
- ) as r5;
- ----
- Explained Query:
- With
- cte l0 =
- Project (#0{a})
- ReadIndex on=t t_a_idx=[*** full scan ***]
- cte l1 =
- ArrangeBy keys=[[]]
- Get l0
- cte l2 =
- Project (#0{a}, #1{b}, #3{max})
- Filter (#0{a} != #3{max})
- Join on=(#0{a} = #2{a}) type=differential
- ArrangeBy keys=[[#0{a}]]
- ReadIndex on=t t_a_idx=[differential join]
- ArrangeBy keys=[[#0{a}]]
- Reduce group_by=[#0{a}] aggregates=[max((#0{a} * #1{a}))]
- CrossJoin type=differential
- ArrangeBy keys=[[]]
- Distinct project=[#0{a}]
- Get l0
- Get l1
- Return
- Project (#0{a}..=#2{max}, #4{max})
- Filter (#0{a} != #4{max})
- Join on=(#0{a} = #3{a}) type=differential
- ArrangeBy keys=[[#0{a}]]
- Get l2
- ArrangeBy keys=[[#0{a}]]
- Reduce group_by=[#0{a}] aggregates=[max((#0{a} * #1{a}))]
- CrossJoin type=differential
- ArrangeBy keys=[[]]
- Distinct project=[#0{a}]
- Project (#0{a})
- Get l2
- Get l1
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***, differential join)
- Target cluster: quickstart
- EOF
- # Test cross join.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT t1.a, t2.a FROM t as t1, t as t2
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[]]
- Project (#0{a})
- ReadIndex on=t t_a_idx=[*** full scan ***]
- Return
- CrossJoin type=differential
- Get l0
- Get l0
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test cyclic join.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT t1.a, t2.a
- FROM
- t as t1,
- t as t2,
- t as t3
- WHERE t1.b = t2.b AND t2.b = t3.b
- ----
- Explained Query:
- With
- cte l0 =
- Filter (#1{b}) IS NOT NULL
- ReadIndex on=t t_a_idx=[*** full scan ***]
- cte l1 =
- ArrangeBy keys=[[#1{b}]]
- Get l0
- Return
- Project (#0{a}, #2{a})
- Join on=(#1{b} = #3{b} = #4{b}) type=delta
- Get l1
- Get l1
- ArrangeBy keys=[[#0{b}]]
- Project (#1{b})
- Get l0
- Used Indexes:
- - materialize.public.t_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Create indexes required for differential join tests
- statement ok
- CREATE INDEX u_c_idx ON U(c);
- statement ok
- CREATE INDEX u_d_idx ON U(d);
- statement ok
- CREATE INDEX v_e_idx ON V(e);
- # Temporarily switch off enable_eager_delta_joins
- simple conn=mz_system,user=mz_system
- ALTER SYSTEM SET enable_eager_delta_joins = false
- ----
- COMPLETE 0
- # Test a differential join.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT a, b, c, d, e, f
- FROM t, u, v
- WHERE a = c and d = e and b = f
- ----
- Explained Query:
- Project (#0{a}, #1{b}, #0{a}, #3{d}, #3{d}, #1{b})
- Filter (#0{a}) IS NOT NULL
- Join on=(#0{a} = #2{c} AND #1{b} = #5{f} AND #3{d} = #4{e}) type=differential
- ArrangeBy keys=[[#0{a}]]
- ReadIndex on=t t_a_idx=[differential join]
- ArrangeBy keys=[[#0{c}]]
- ReadIndex on=u u_c_idx=[differential join]
- ArrangeBy keys=[[#0{e}, #1{f}]]
- Filter (#0{e}) IS NOT NULL AND (#1{f}) IS NOT NULL
- ReadIndex on=v v_e_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx (differential join)
- - materialize.public.u_c_idx (differential join)
- - materialize.public.v_e_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Test a differential join WITH(join implementations).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, join implementations) AS VERBOSE TEXT FOR
- SELECT a, b, c, d, e, f
- FROM t, u, v
- WHERE a = c and d = e and b = f
- ----
- Explained Query:
- Project (#0{a}, #1{b}, #0{a}, #3{d}, #3{d}, #1{b})
- Filter (#0{a}) IS NOT NULL
- Join on=(#0{a} = #2{c} AND #1{b} = #5{f} AND #3{d} = #4{e}) type=differential
- implementation
- %0:t[#0{a}]KA » %1:u[#0{c}]KA » %2:v[#0{e}, #1{f}]KK
- ArrangeBy keys=[[#0{a}]]
- ReadIndex on=t t_a_idx=[differential join]
- ArrangeBy keys=[[#0{c}]]
- ReadIndex on=u u_c_idx=[differential join]
- ArrangeBy keys=[[#0{e}, #1{f}]]
- Filter (#0{e}) IS NOT NULL AND (#1{f}) IS NOT NULL
- ReadIndex on=v v_e_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx (differential join)
- - materialize.public.u_c_idx (differential join)
- - materialize.public.v_e_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- simple conn=mz_system,user=mz_system
- ALTER SYSTEM SET enable_eager_delta_joins = true
- ----
- COMPLETE 0
- # Create indexes required for delta join tests
- statement ok
- CREATE INDEX t_b_idx ON T(b);
- # Test a delta join without WITH(join implementations).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT a, b, c, d, e, f
- FROM t, u, v
- WHERE b = c and d = e
- ----
- Explained Query:
- Project (#0{a}, #1{b}, #1{b}, #3{d}, #3{d}, #5{f})
- Filter (#1{b}) IS NOT NULL AND (#3{d}) IS NOT NULL
- Join on=(#1{b} = #2{c} AND #3{d} = #4{e}) type=delta
- ArrangeBy keys=[[#1{b}]]
- ReadIndex on=t t_b_idx=[delta join 1st input (full scan)]
- ArrangeBy keys=[[#0{c}], [#1{d}]]
- ReadIndex on=u u_c_idx=[delta join lookup] u_d_idx=[delta join lookup]
- ArrangeBy keys=[[#0{e}]]
- ReadIndex on=v v_e_idx=[delta join lookup]
- Used Indexes:
- - materialize.public.u_c_idx (delta join lookup)
- - materialize.public.u_d_idx (delta join lookup)
- - materialize.public.v_e_idx (delta join lookup)
- - materialize.public.t_b_idx (delta join 1st input (full scan))
- Target cluster: quickstart
- EOF
- # Test a delta join WITH(join implementations).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(join implementations, humanized expressions) AS VERBOSE TEXT FOR
- SELECT a, b, c, d, e, f
- FROM t, u, v
- WHERE b = c and d = e
- ----
- Explained Query:
- Project (#0{a}, #1{b}, #1{b}, #3{d}, #3{d}, #5{f})
- Filter (#1{b}) IS NOT NULL AND (#3{d}) IS NOT NULL
- Join on=(#1{b} = #2{c} AND #3{d} = #4{e}) type=delta
- implementation
- %0:t » %1:u[#0{c}]KA » %2:v[#0{e}]KA
- %1:u » %0:t[#1{b}]KA » %2:v[#0{e}]KA
- %2:v » %1:u[#1{d}]KA » %0:t[#1{b}]KA
- ArrangeBy keys=[[#1{b}]]
- ReadIndex on=t t_b_idx=[delta join 1st input (full scan)]
- ArrangeBy keys=[[#0{c}], [#1{d}]]
- ReadIndex on=u u_c_idx=[delta join lookup] u_d_idx=[delta join lookup]
- ArrangeBy keys=[[#0{e}]]
- ReadIndex on=v v_e_idx=[delta join lookup]
- Used Indexes:
- - materialize.public.u_c_idx (delta join lookup)
- - materialize.public.u_d_idx (delta join lookup)
- - materialize.public.v_e_idx (delta join lookup)
- - materialize.public.t_b_idx (delta join 1st input (full scan))
- Target cluster: quickstart
- EOF
- # Test a delta join where the first input has both a full scan and a lookup.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(join implementations, humanized expressions) AS VERBOSE TEXT FOR
- SELECT a, b, c, d, e, f
- FROM t, u, v
- WHERE a = c and b = e
- ----
- Explained Query:
- Project (#0{a}, #1{b}, #0{a}, #3{d}, #1{b}, #5{f})
- Filter (#0{a}) IS NOT NULL AND (#1{b}) IS NOT NULL
- Join on=(#0{a} = #2{c} AND #1{b} = #4{e}) type=delta
- implementation
- %0:t » %1:u[#0{c}]KA » %2:v[#0{e}]KA
- %1:u » %0:t[#0{a}]KA » %2:v[#0{e}]KA
- %2:v » %0:t[#1{b}]KA » %1:u[#0{c}]KA
- ArrangeBy keys=[[#0{a}], [#1{b}]]
- ReadIndex on=t t_a_idx=[delta join 1st input (full scan)] t_b_idx=[delta join lookup]
- ArrangeBy keys=[[#0{c}]]
- ReadIndex on=u u_c_idx=[delta join lookup]
- ArrangeBy keys=[[#0{e}]]
- ReadIndex on=v v_e_idx=[delta join lookup]
- Used Indexes:
- - materialize.public.t_a_idx (delta join 1st input (full scan))
- - materialize.public.u_c_idx (delta join lookup)
- - materialize.public.v_e_idx (delta join lookup)
- - materialize.public.t_b_idx (delta join lookup)
- Target cluster: quickstart
- EOF
- # Test an IndexedFilter join.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT a, max(b)
- FROM t
- WHERE a = 0
- GROUP BY a
- ----
- Explained Query:
- Project (#1, #0{max_b})
- Map (0)
- Reduce aggregates=[max(#0{b})]
- Project (#1{b})
- ReadIndex on=materialize.public.t t_a_idx=[lookup value=(0)]
- Used Indexes:
- - materialize.public.t_a_idx (lookup)
- Target cluster: quickstart
- EOF
- # Create index for IndexedFilter test
- statement ok
- CREATE INDEX t_a_b_idx ON T(a,b)
- # Test an IndexedFilter join WITH(join implementations).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(join implementations, humanized expressions) AS VERBOSE TEXT FOR
- SELECT a, max(b)
- FROM t
- WHERE (a = 0 AND b = 1) OR (a = 3 AND b = 4) OR (a = 7 AND b = 8)
- GROUP BY a
- ----
- Explained Query:
- Reduce group_by=[#0{a}] aggregates=[max(#1{b})]
- Project (#0{a}, #1{b})
- ReadIndex on=materialize.public.t t_a_b_idx=[lookup values=[(0, 1); (3, 4); (7, 8)]]
- Used Indexes:
- - materialize.public.t_a_b_idx (lookup)
- Target cluster: quickstart
- EOF
- # Test an IndexedFilter join on fast path WITH(join implementations).
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(join implementations, humanized expressions) AS VERBOSE TEXT FOR
- SELECT *
- FROM t
- WHERE (a = 0 AND b = 1) OR (a = 3 AND b = 4) OR (a = 7 AND b = 8)
- ----
- Explained Query (fast path):
- Project (#0{a}, #1{b})
- ReadIndex on=materialize.public.t t_a_b_idx=[lookup values=[(0, 1); (3, 4); (7, 8)]]
- Used Indexes:
- - materialize.public.t_a_b_idx (lookup)
- Target cluster: quickstart
- EOF
- # Test materialize#17348.
- statement ok
- CREATE TABLE r(f0 INT, f1 INT, f2 INT, f3 INT, f4 INT, f5 INT, f6 INT, f7 INT, f8 INT, f9 INT, f10 INT, f11 INT, f12 INT, f13 INT, f14 INT, f15 INT, f16 INT);
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT *
- FROM r AS r0, r AS r1
- WHERE
- r0.f0=r1.f0 AND
- r0.f2=r1.f2 AND
- r0.f3=r1.f3 AND
- r0.f4=r1.f4 AND
- r0.f6=r1.f6 AND
- r0.f8=r1.f8 AND
- r0.f9=r1.f9 AND
- r0.f11=r1.f11 AND
- r0.f12=r1.f12 AND
- r0.f13=r1.f13 AND
- r0.f15=r1.f15 AND
- r0.f16=r1.f16;
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{f0}, #2{f2}..=#4{f4}, #6{f6}, #8{f8}, #9{f9}, #11{f11}..=#13{f13}, #15{f15}, #16{f16}]]
- Filter (#0{f0}) IS NOT NULL AND (#2{f2}) IS NOT NULL AND (#3{f3}) IS NOT NULL AND (#4{f4}) IS NOT NULL AND (#6{f6}) IS NOT NULL AND (#8{f8}) IS NOT NULL AND (#9{f9}) IS NOT NULL AND (#11{f11}) IS NOT NULL AND (#12{f12}) IS NOT NULL AND (#13{f13}) IS NOT NULL AND (#15{f15}) IS NOT NULL AND (#16{f16}) IS NOT NULL
- ReadStorage materialize.public.r
- Return
- Project (#0{f0}..=#16{f16}, #0{f0}, #18{f1}, #2{f2}..=#4{f4}, #22{f5}, #6{f6}, #24{f7}, #8{f8}, #9{f9}, #27{f10}, #11{f11}..=#13{f13}, #31{f14}, #15{f15}, #16{f16})
- Join on=(#0{f0} = #17{f0} AND #2{f2} = #19{f2} AND #3{f3} = #20{f3} AND #4{f4} = #21{f4} AND #6{f6} = #23{f6} AND #8{f8} = #25{f8} AND #9{f9} = #26{f9} AND #11{f11} = #28{f11} AND #12{f12} = #29{f12} AND #13{f13} = #30{f13} AND #15{f15} = #32{f15} AND #16{f16} = #33{f16}) type=differential
- Get l0
- Get l0
- Source materialize.public.r
- filter=((#0{f0}) IS NOT NULL AND (#2{f2}) IS NOT NULL AND (#3{f3}) IS NOT NULL AND (#4{f4}) IS NOT NULL AND (#6{f6}) IS NOT NULL AND (#8{f8}) IS NOT NULL AND (#9{f9}) IS NOT NULL AND (#11{f11}) IS NOT NULL AND (#12{f12}) IS NOT NULL AND (#13{f13}) IS NOT NULL AND (#15{f15}) IS NOT NULL AND (#16{f16}) IS NOT NULL)
- Target cluster: quickstart
- EOF
- ## linear chains is currently disabled for WMR.
- statement error not supported
- EXPLAIN OPTIMIZED PLAN WITH(linear chains) AS VERBOSE TEXT FOR
- WITH MUTUALLY RECURSIVE
- foo (a int, b int) AS (SELECT 1, 2 UNION SELECT a, 7 FROM bar),
- bar (a int) as (SELECT a FROM foo)
- SELECT * FROM bar;
- # Regression test for database-issues#5674: support mz_now() on select from indexed table
- # ---
- simple conn=mz_system,user=mz_system
- ALTER SYSTEM SET enable_rbac_checks TO false;
- ----
- COMPLETE 0
- statement ok
- DROP SCHEMA IF EXISTS public CASCADE;
- simple conn=mz_system,user=mz_system
- ALTER SYSTEM RESET enable_rbac_checks;
- ----
- COMPLETE 0
- statement ok
- CREATE SCHEMA public;
- statement ok
- CREATE TABLE t(a TIMESTAMP);
- statement ok
- CREATE DEFAULT INDEX ON t;
- # EXPLAIN output is time-dependent, so we don't want show the output here, just
- # assert that the query doesn't fail.
- statement ok
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM t WHERE a < mz_now();
- # Regression test for materialize#19177
- # ---
- statement ok
- DROP SCHEMA IF EXISTS public CASCADE;
- statement ok
- CREATE SCHEMA public;
- statement ok
- CREATE TABLE t1(x text);
- statement ok
- CREATE TABLE t2(x text);
- statement ok
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM t1, t2 WHERE t1.x || mz_internal.mz_session_id() = t2.x || mz_internal.mz_session_id();
- # Regression test for the join visitation part of materialize#19177
- statement ok
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM t1, t2 WHERE t1.x || mz_now() = t2.x || mz_now();
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT lag(x, 3, 'default') IGNORE NULLS OVER (ORDER BY x || x)
- FROM t1;
- ----
- Explained Query:
- Project (#2)
- Map (record_get[0](#1))
- FlatMap unnest_list(#0{lag})
- Reduce aggregates=[lag[ignore_nulls=true, order_by=[#0{x} asc nulls_last]](row(row(row(#0{x}), row(#0{x}, 3, "default")), (#0{x} || #0{x})))]
- ReadStorage materialize.public.t1
- Source materialize.public.t1
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT first_value(x) OVER (ORDER BY x || x ROWS BETWEEN 5 preceding AND CURRENT ROW)
- FROM t1;
- ----
- Explained Query:
- Project (#2)
- Map (record_get[0](#1))
- FlatMap unnest_list(#0{first_value})
- Reduce aggregates=[first_value[order_by=[#0{x} asc nulls_last] rows between 5 preceding and current row](row(row(row(#0{x}), #0{x}), (#0{x} || #0{x})))]
- ReadStorage materialize.public.t1
- Source materialize.public.t1
- Target cluster: quickstart
- EOF
- ## "Used indexes" tests
- statement ok
- CREATE TABLE t (
- a int,
- b int
- );
- statement ok
- CREATE TABLE u (
- c int,
- d int
- );
- # If two indexes exist on the same table, then "Used indexes" should print the one that we are actually going to use
- statement ok
- CREATE INDEX u_c ON u(c);
- statement ok
- CREATE INDEX u_d ON u(d);
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT *
- FROM t, u
- WHERE t.b = u.c;
- ----
- Explained Query:
- Project (#0{a}, #1{b}, #1{b}, #3{d})
- Join on=(#1{b} = #2{c}) type=differential
- ArrangeBy keys=[[#1{b}]]
- Filter (#1{b}) IS NOT NULL
- ReadStorage materialize.public.t
- ArrangeBy keys=[[#0{c}]]
- ReadIndex on=u u_c=[differential join]
- Source materialize.public.t
- filter=((#1{b}) IS NOT NULL)
- Used Indexes:
- - materialize.public.u_c (differential join)
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT *
- FROM t, u
- WHERE t.b = u.d;
- ----
- Explained Query:
- Project (#0{a}..=#2{c}, #1{b})
- Join on=(#1{b} = #3{d}) type=differential
- ArrangeBy keys=[[#1{b}]]
- Filter (#1{b}) IS NOT NULL
- ReadStorage materialize.public.t
- ArrangeBy keys=[[#1{d}]]
- ReadIndex on=u u_d=[differential join]
- Source materialize.public.t
- filter=((#1{b}) IS NOT NULL)
- Used Indexes:
- - materialize.public.u_d (differential join)
- Target cluster: quickstart
- EOF
- statement ok
- DROP INDEX u_c;
- # Let's test the weird situation that two identical indexes exist.
- statement ok
- CREATE INDEX t_a_idx_1 ON t(a);
- statement ok
- CREATE INDEX t_a_idx_2 ON t(a);
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT *
- FROM t, u
- WHERE t.a = u.c
- ----
- Explained Query:
- Project (#0{a}, #1{b}, #0{a}, #3{d})
- Join on=(#0{a} = #2{c}) type=differential
- ArrangeBy keys=[[#0{a}]]
- ReadIndex on=t t_a_idx_1=[differential join]
- ArrangeBy keys=[[#0{c}]]
- Filter (#0{c}) IS NOT NULL
- ReadIndex on=u u_d=[*** full scan ***]
- Used Indexes:
- - materialize.public.u_d (*** full scan ***)
- - materialize.public.t_a_idx_1 (differential join)
- Target cluster: quickstart
- EOF
- # An index is used two times by the same (self) join. We should show a 1st input and a non-1st input usage.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT *
- FROM t AS t1, t AS t2, t AS t3
- WHERE t1.a = t2.a AND t2.a = t3.a;
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{a}]]
- ReadIndex on=t t_a_idx_1=[delta join lookup, delta join 1st input (full scan)]
- Return
- Project (#0{a}, #1{b}, #0{a}, #3{b}, #0{a}, #5{b})
- Filter (#0{a}) IS NOT NULL
- Join on=(#0{a} = #2{a} = #4{a}) type=delta
- Get l0
- Get l0
- Get l0
- Used Indexes:
- - materialize.public.t_a_idx_1 (delta join lookup, delta join 1st input (full scan))
- Target cluster: quickstart
- EOF
- # An index is used in both a join and a full scan.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- (SELECT t1.a + t2.a AS a, t1.b + t2.b AS b
- FROM t AS t1, t AS t2
- WHERE t1.a = t2.a)
- UNION
- (SELECT *
- FROM t
- WHERE b > 5)
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#0{a}]]
- ReadIndex on=t t_a_idx_1=[differential join]
- Return
- Distinct project=[#0{a}, #1{b}]
- Union
- Project (#4, #5)
- Filter (#0{a}) IS NOT NULL
- Map ((#0{a} + #0{a}), (#1{b} + #3{b}))
- Join on=(#0{a} = #2{a}) type=differential
- Get l0
- Get l0
- Filter (#1{b} > 5)
- ReadIndex on=t t_a_idx_1=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx_1 (*** full scan ***, differential join)
- Target cluster: quickstart
- EOF
- # An index exists that can't be used for the join because of having the wrong key.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- (SELECT t1.a + t2.a AS a, t1.b + t2.b AS b
- FROM t AS t1, t AS t2
- WHERE t1.b = t2.b)
- UNION
- (SELECT *
- FROM t
- WHERE b > 5)
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#1{b}]]
- Filter (#1{b}) IS NOT NULL
- ReadIndex on=t t_a_idx_1=[*** full scan ***]
- Return
- Distinct project=[#0{a}, #1{b}]
- Union
- Project (#4, #5)
- Map ((#0{a} + #2{a}), (#1{b} + #1{b}))
- Join on=(#1{b} = #3{b}) type=differential
- Get l0
- Get l0
- Filter (#1{b} > 5)
- ReadIndex on=t t_a_idx_1=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx_1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Similar to the previous test, but exercises the full scan code inside the context loop of the Get case in
- # `collect_index_reqs_inner`, where we don't have an index for the requested key.
- statement ok
- CREATE TABLE t_non_null (
- a int NOT NULL,
- b int NOT NULL
- );
- statement ok
- CREATE INDEX t_non_null_a_idx ON t_non_null(a);
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- (SELECT t1.a + t2.a AS a, t1.b + t2.b AS b
- FROM t_non_null AS t1, t_non_null AS t2
- WHERE t1.b = t2.b)
- UNION
- (SELECT *
- FROM t_non_null
- WHERE b > 5)
- ----
- Explained Query:
- With
- cte l0 =
- ArrangeBy keys=[[#1{b}]]
- ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
- Return
- Distinct project=[#0{a}, #1{b}]
- Union
- Project (#4, #5)
- Map ((#0{a} + #2{a}), (#1{b} + #1{b}))
- Join on=(#1{b} = #3{b}) type=differential
- Get l0
- Get l0
- Filter (#1{b} > 5)
- ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_non_null_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # This has 1 more full scan than the previous test, because the join needs 2 different arrangements.
- # (But we print only one full scan due to deduplication.)
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- (SELECT t1.a + t2.a AS a, t1.b + t2.b AS b
- FROM t_non_null AS t1, t_non_null AS t2
- WHERE t1.b = t2.b + 1)
- UNION
- (SELECT *
- FROM t_non_null
- WHERE b > 5)
- ----
- Explained Query:
- Distinct project=[#0{a}, #1{b}]
- Union
- Project (#4, #5)
- Map ((#0{a} + #2{a}), (#1{b} + #3{b}))
- Join on=(#1{b} = (#3{b} + 1)) type=differential
- ArrangeBy keys=[[#1{b}]]
- ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
- ArrangeBy keys=[[(#1{b} + 1)]]
- ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
- Filter (#1{b} > 5)
- ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_non_null_a_idx (*** full scan ***)
- Target cluster: quickstart
- EOF
- # An index is used in both a lookup and a full scan.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM t
- UNION
- SELECT * FROM t WHERE a = 5;
- ----
- Explained Query:
- Distinct project=[#0{a}, #1{b}]
- Union
- ReadIndex on=t t_a_idx_2=[*** full scan ***]
- Project (#0{a}, #1{b})
- ReadIndex on=materialize.public.t t_a_idx_2=[lookup value=(5)]
- Used Indexes:
- - materialize.public.t_a_idx_2 (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- # Several lookups using different indexes
- statement ok
- CREATE INDEX t_b_idx ON t(b);
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM t
- UNION ALL
- SELECT * FROM t WHERE b = 7
- UNION ALL
- SELECT * FROM t WHERE a = 5
- UNION ALL
- SELECT * FROM u WHERE c = 3
- UNION ALL
- SELECT * FROM u WHERE d = 1;
- ----
- Explained Query:
- Union
- ReadIndex on=t t_b_idx=[*** full scan ***]
- Project (#0{a}, #1{b})
- ReadIndex on=materialize.public.t t_b_idx=[lookup value=(7)]
- Project (#0{a}, #1{b})
- ReadIndex on=materialize.public.t t_a_idx_2=[lookup value=(5)]
- Filter (#0{c} = 3)
- ReadIndex on=u u_d=[*** full scan ***]
- Project (#0{c}, #1{d})
- ReadIndex on=materialize.public.u u_d=[lookup value=(1)]
- Used Indexes:
- - materialize.public.u_d (*** full scan ***, lookup)
- - materialize.public.t_a_idx_2 (lookup)
- - materialize.public.t_b_idx (*** full scan ***, lookup)
- Target cluster: quickstart
- EOF
- # Fast path with a LIMIT and no ORDER BY. This is not a full scan.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT a+b as x
- FROM t
- WHERE a < 7
- LIMIT 3;
- ----
- Explained Query (fast path):
- Finish limit=3 output=[#0]
- Project (#2)
- Filter (#0{a} < 7)
- Map ((#0{a} + #1{b}))
- ReadIndex on=materialize.public.t t_a_idx_1=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx_1 (fast path limit)
- Target cluster: quickstart
- EOF
- # Same query without a LIMIT, so full scan
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT a+b as x
- FROM t
- WHERE a < 7;
- ----
- Explained Query (fast path):
- Project (#2)
- Filter (#0{a} < 7)
- Map ((#0{a} + #1{b}))
- ReadIndex on=materialize.public.t t_a_idx_1=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx_1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- # Same query with a LIMIT + ORDER BY, so full scan
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT a+b as x
- FROM t
- WHERE a < 7
- ORDER BY x
- LIMIT 3;
- ----
- Explained Query (fast path):
- Finish order_by=[#0 asc nulls_last] limit=3 output=[#0]
- Project (#2)
- Filter (#0{a} < 7)
- Map ((#0{a} + #1{b}))
- ReadIndex on=materialize.public.t t_a_idx_1=[*** full scan ***]
- Used Indexes:
- - materialize.public.t_a_idx_1 (*** full scan ***)
- Target cluster: quickstart
- EOF
- # We should choose an index whose key is a unique key (if exists)
- statement ok
- CREATE TABLE t4(
- a int,
- b int primary key,
- c int
- );
- statement ok
- CREATE INDEX t4_idx_a ON t4(a);
- statement ok
- CREATE INDEX t4_idx_b ON t4(b);
- statement ok
- CREATE INDEX t4_idx_c ON t4(c);
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM t4;
- ----
- Explained Query (fast path):
- Project (#1{a}, #0{b}, #2{c})
- ReadIndex on=materialize.public.t4 t4_idx_b=[*** full scan ***]
- Used Indexes:
- - materialize.public.t4_idx_b (*** full scan ***)
- Target cluster: quickstart
- EOF
- # EXPLAIN should work even if there are no replicas.
- statement ok
- CREATE CLUSTER no_replicas (SIZE '1', REPLICATION FACTOR 0);
- statement ok
- SET CLUSTER = no_replicas;
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
- SELECT * FROM t4;
- ----
- Explained Query:
- ReadStorage materialize.public.t4
- Source materialize.public.t4
- Target cluster: no_replicas
- EOF
- statement ok
- CREATE TABLE t5(
- x int,
- y int NOT NULL,
- z int
- );
- statement ok
- CREATE TABLE t6(
- a int NOT NULL,
- b int
- );
- # WITH(EQUIVALENCES)
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(EQUIVALENCES, humanized expressions) AS VERBOSE TEXT FOR
- SELECT *
- FROM t5, t6
- WHERE x = a AND b IN (8,9);
- ----
- Explained Query:
- Project (#0{x}..=#2{z}, #0{x}, #4{b}) // { equivs: "[[#0{x}, #3{x}], [false, (#0{x}) IS NULL, (#1{y}) IS NULL], [true, ((#4{b} = 8) OR (#4{b} = 9))]]" }
- Join on=(#0{x} = #3{a}) type=differential // { equivs: "[[#0{x}, #3{a}], [false, (#0{x}) IS NULL, (#1{y}) IS NULL], [true, ((#4{b} = 8) OR (#4{b} = 9))]]" }
- ArrangeBy keys=[[#0{x}]] // { equivs: "[[false, (#0{x}) IS NULL, (#1{y}) IS NULL]]" }
- Filter (#0{x}) IS NOT NULL // { equivs: "[[false, (#0{x}) IS NULL, (#1{y}) IS NULL]]" }
- ReadStorage materialize.public.t5 // { equivs: "[[false, (#1{y}) IS NULL]]" }
- ArrangeBy keys=[[#0{a}]] // { equivs: "[[false, (#0{a}) IS NULL], [true, ((#1{b} = 8) OR (#1{b} = 9))]]" }
- Filter ((#1{b} = 8) OR (#1{b} = 9)) // { equivs: "[[false, (#0{a}) IS NULL], [true, ((#1{b} = 8) OR (#1{b} = 9))]]" }
- ReadStorage materialize.public.t6 // { equivs: "[[false, (#0{a}) IS NULL]]" }
- Source materialize.public.t5
- filter=((#0{x}) IS NOT NULL)
- Source materialize.public.t6
- filter=(((#1{b} = 8) OR (#1{b} = 9)))
- Target cluster: no_replicas
- EOF
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(EQUIVALENCES, HUMANIZED EXPRESSIONS) AS VERBOSE TEXT FOR
- SELECT *
- FROM t5, t6
- WHERE x = a AND b IN (8,9);
- ----
- Explained Query:
- Project (#0{x}..=#2{z}, #0{x}, #4{b}) // { equivs: "[[#0{x}, #3{x}], [false, (#0{x}) IS NULL, (#1{y}) IS NULL], [true, ((#4{b} = 8) OR (#4{b} = 9))]]" }
- Join on=(#0{x} = #3{a}) type=differential // { equivs: "[[#0{x}, #3{a}], [false, (#0{x}) IS NULL, (#1{y}) IS NULL], [true, ((#4{b} = 8) OR (#4{b} = 9))]]" }
- ArrangeBy keys=[[#0{x}]] // { equivs: "[[false, (#0{x}) IS NULL, (#1{y}) IS NULL]]" }
- Filter (#0{x}) IS NOT NULL // { equivs: "[[false, (#0{x}) IS NULL, (#1{y}) IS NULL]]" }
- ReadStorage materialize.public.t5 // { equivs: "[[false, (#1{y}) IS NULL]]" }
- ArrangeBy keys=[[#0{a}]] // { equivs: "[[false, (#0{a}) IS NULL], [true, ((#1{b} = 8) OR (#1{b} = 9))]]" }
- Filter ((#1{b} = 8) OR (#1{b} = 9)) // { equivs: "[[false, (#0{a}) IS NULL], [true, ((#1{b} = 8) OR (#1{b} = 9))]]" }
- ReadStorage materialize.public.t6 // { equivs: "[[false, (#0{a}) IS NULL]]" }
- Source materialize.public.t5
- filter=((#0{x}) IS NOT NULL)
- Source materialize.public.t6
- filter=(((#1{b} = 8) OR (#1{b} = 9)))
- Target cluster: no_replicas
- EOF
- # `count(*)` is planned as `count(true)`. We take care in EXPLAIN to show `count(true)` as `count(*)` to avoid confusing
- # users.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
- SELECT count(*)
- FROM t5;
- ----
- Explained Query:
- With
- cte l0 =
- Reduce aggregates=[count(*)]
- Project ()
- ReadStorage materialize.public.t5
- Return
- Union
- Get l0
- Map (0)
- Union
- Negate
- Project ()
- Get l0
- Constant
- - ()
- Source materialize.public.t5
- Target cluster: no_replicas
- EOF
- query error DISTINCT \* not supported as function args
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
- SELECT count(distinct *)
- FROM t5;
- # `count(true)` is currently also printed as `count(*)` in EXPLAIN, which I'd say is fine.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
- SELECT count(true)
- FROM t5;
- ----
- Explained Query:
- With
- cte l0 =
- Reduce aggregates=[count(*)]
- Project ()
- ReadStorage materialize.public.t5
- Return
- Union
- Get l0
- Map (0)
- Union
- Negate
- Project ()
- Get l0
- Constant
- - ()
- Source materialize.public.t5
- Target cluster: no_replicas
- EOF
- # But `count(DISTINCT true)` means an entirely different thing, so EXPLAIN shouldn't conflate it with `count(*)`.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
- SELECT count(DISTINCT true)
- FROM t5;
- ----
- Explained Query:
- With
- cte l0 =
- Reduce aggregates=[count(distinct true)]
- Project ()
- ReadStorage materialize.public.t5
- Return
- Union
- Get l0
- Map (0)
- Union
- Negate
- Project ()
- Get l0
- Constant
- - ()
- Source materialize.public.t5
- Target cluster: no_replicas
- EOF
- # OFFSET clause in RowSetFinishing
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
- SELECT a+b
- FROM t4
- OFFSET 1;
- ----
- Explained Query:
- Finish offset=1 output=[#0]
- Project (#3)
- Map ((#0{a} + #1{b}))
- ReadStorage materialize.public.t4
- Source materialize.public.t4
- Target cluster: no_replicas
- EOF
- # OFFSET clause in TopK
- query T multiline
- EXPLAIN OPTIMIZED PLAN AS VERBOSE TEXT FOR
- SELECT a+b, (SELECT a*b FROM t4 OFFSET 1)
- FROM t4;
- ----
- Explained Query:
- With
- cte l0 =
- TopK offset=1
- Project (#3)
- Map ((#0{a} * #1{b}))
- ReadStorage materialize.public.t4
- cte l1 =
- Union
- Get l0
- Project (#1)
- FlatMap guard_subquery_size(#0)
- Reduce aggregates=[count(*)]
- Project ()
- Get l0
- Return
- Project (#3, #2)
- Map ((#0{a} + #1{b}))
- CrossJoin type=differential
- ArrangeBy keys=[[]]
- Project (#0, #1)
- ReadStorage materialize.public.t4
- ArrangeBy keys=[[]]
- Union
- Get l1
- Map (null)
- Union
- Negate
- Distinct project=[]
- Project ()
- Get l1
- Constant
- - ()
- Source materialize.public.t4
- Target cluster: no_replicas
- EOF
|