optimized_plan_as_text_redacted.slt 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. simple conn=mz_system,user=mz_system
  10. ALTER SYSTEM SET unsafe_enable_table_keys = true
  11. ----
  12. COMPLETE 0
  13. statement ok
  14. CREATE TABLE t (
  15. a int,
  16. b int
  17. );
  18. statement ok
  19. CREATE TABLE u (
  20. c int,
  21. d int
  22. );
  23. statement ok
  24. CREATE TABLE v (
  25. e int,
  26. f int
  27. );
  28. statement ok
  29. CREATE INDEX t_a_idx ON t(a);
  30. statement ok
  31. CREATE VIEW ov AS SELECT * FROM t ORDER BY b asc, a desc LIMIT 5;
  32. statement ok
  33. CREATE VIEW iv AS
  34. SELECT * FROM t WHERE a IS NOT NULL;
  35. statement ok
  36. CREATE INDEX iv_a_idx ON iv(a);
  37. statement ok
  38. CREATE INDEX iv_b_idx ON iv(b);
  39. # This is an identical index to the above (on the same object, on the same key)
  40. statement ok
  41. CREATE INDEX iv_b_idx_2 ON iv(b);
  42. statement ok
  43. CREATE MATERIALIZED VIEW mv AS
  44. SELECT * FROM t WHERE a IS NOT NULL;
  45. mode cockroach
  46. # Test constant error.
  47. query T multiline
  48. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  49. SELECT 1 / 0
  50. ----
  51. Explained Query (fast path):
  52. Error █
  53. Target cluster: quickstart
  54. EOF
  55. # Test constant with two elements.
  56. query T multiline
  57. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  58. (SELECT 1, 2) UNION ALL (SELECT 1, 2) UNION ALL (SELECT 3, 4)
  59. ----
  60. Explained Query (fast path):
  61. Constant
  62. - ((█, █) x 2)
  63. - (█, █)
  64. Target cluster: mz_catalog_server
  65. EOF
  66. # Test catalog queries (index found based on cluster auto-routing).
  67. query T multiline
  68. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM mz_internal.mz_source_status_history
  69. ----
  70. Explained Query (fast path):
  71. Project (#1{occurred_at}, #0{source_id}, #2{status}..=#5{replica_id})
  72. ReadIndex on=mz_internal.mz_source_status_history mz_source_status_history_ind=[*** full scan ***]
  73. Used Indexes:
  74. - mz_internal.mz_source_status_history_ind (*** full scan ***)
  75. Target cluster: mz_catalog_server
  76. EOF
  77. # Test basic linear chains (fast path).
  78. query T multiline
  79. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  80. SELECT 1, a + b as c FROM t WHERE a = 5 and b < 0 and a + b > 0
  81. ----
  82. Explained Query (fast path):
  83. Project (#4, #3)
  84. Filter (#1{b} < █) AND ((#0{a} + #1{b}) > █)
  85. Map ((█ + #1{b}), █)
  86. ReadIndex on=materialize.public.t t_a_idx=[lookup value=(█)]
  87. Used Indexes:
  88. - materialize.public.t_a_idx (lookup)
  89. Target cluster: quickstart
  90. EOF
  91. # Test basic linear chains (slow path).
  92. query T multiline
  93. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  94. SELECT 1, a + b as c FROM mv WHERE a > 0 and b < 0 and a + b > 0
  95. ----
  96. Explained Query:
  97. Project (#3, #2)
  98. Filter (#1{b} < █) AND (#0{a} > █) AND (#2 > █)
  99. Map ((#0{a} + #1{b}), █)
  100. ReadStorage materialize.public.mv
  101. Source materialize.public.mv
  102. filter=((#0{a} > █) AND (#1{b} < █) AND ((#0{a} + #1{b}) > █))
  103. Target cluster: quickstart
  104. EOF
  105. # Test table functions in the select clause (FlatMap).
  106. query T multiline
  107. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  108. SELECT generate_series(a, b) from t
  109. ----
  110. Explained Query:
  111. Project (#2)
  112. FlatMap generate_series(#0{a}, #1{b}, █)
  113. ReadIndex on=t t_a_idx=[*** full scan ***]
  114. Used Indexes:
  115. - materialize.public.t_a_idx (*** full scan ***)
  116. Target cluster: quickstart
  117. EOF
  118. # Test TopK.
  119. query T multiline
  120. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  121. SELECT * FROM ov
  122. ----
  123. Explained Query:
  124. TopK order_by=[#1{b} asc nulls_last, #0{a} desc nulls_first] limit=█
  125. ReadIndex on=t t_a_idx=[*** full scan ***]
  126. Used Indexes:
  127. - materialize.public.t_a_idx (*** full scan ***)
  128. Target cluster: quickstart
  129. EOF
  130. # Test Finish.
  131. query T multiline
  132. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  133. SELECT * FROM t ORDER BY b asc, a desc LIMIT 5
  134. ----
  135. Explained Query (fast path):
  136. Finish order_by=[#1 asc nulls_last, #0 desc nulls_first] limit=5 output=[#0, #1]
  137. ReadIndex on=materialize.public.t t_a_idx=[*** full scan ***]
  138. Used Indexes:
  139. - materialize.public.t_a_idx (*** full scan ***)
  140. Target cluster: quickstart
  141. EOF
  142. # Test Reduce (global).
  143. query T multiline
  144. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  145. SELECT abs(min(a) - max(a)) FROM t
  146. ----
  147. Explained Query:
  148. With
  149. cte l0 =
  150. Reduce aggregates=[min(#0{a}), max(#0{a})]
  151. Project (#0{a})
  152. ReadIndex on=t t_a_idx=[*** full scan ***]
  153. Return
  154. Project (#2)
  155. Map (abs((#0{min_a} - #1{max_a})))
  156. Union
  157. Get l0
  158. Map (█, █)
  159. Union
  160. Negate
  161. Project ()
  162. Get l0
  163. Constant
  164. - ()
  165. Used Indexes:
  166. - materialize.public.t_a_idx (*** full scan ***)
  167. Target cluster: quickstart
  168. EOF
  169. # Test Reduce (local).
  170. query T multiline
  171. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  172. SELECT abs(min(a) - max(a)) FROM t GROUP BY b
  173. ----
  174. Explained Query:
  175. Project (#3)
  176. Map (abs((#1{min_a} - #2{max_a})))
  177. Reduce group_by=[#1{b}] aggregates=[min(#0{a}), max(#0{a})]
  178. ReadIndex on=t t_a_idx=[*** full scan ***]
  179. Used Indexes:
  180. - materialize.public.t_a_idx (*** full scan ***)
  181. Target cluster: quickstart
  182. EOF
  183. # Test EXISTS subqueries.
  184. query T multiline
  185. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  186. SELECT * FROM t WHERE EXISTS(SELECT * FROM mv WHERE t.a < mv.a) AND EXISTS(SELECT * FROM mv WHERE t.b > mv.b)
  187. ----
  188. Explained Query:
  189. With
  190. cte l0 =
  191. Project (#0{a}, #1{b})
  192. Join on=(#0{a} = #2{a}) type=differential
  193. ArrangeBy keys=[[#0{a}]]
  194. ReadIndex on=t t_a_idx=[differential join]
  195. ArrangeBy keys=[[#0{a}]]
  196. Distinct project=[#0{a}]
  197. Project (#0{a})
  198. Filter (#0{a} < #1{a})
  199. CrossJoin type=differential
  200. ArrangeBy keys=[[]]
  201. Distinct project=[#0{a}]
  202. Project (#0{a})
  203. ReadIndex on=t t_a_idx=[*** full scan ***]
  204. ArrangeBy keys=[[]]
  205. Project (#0{a})
  206. ReadStorage materialize.public.mv
  207. Return
  208. Project (#0{a}, #1{b})
  209. Join on=(#1{b} = #2{b}) type=differential
  210. ArrangeBy keys=[[#1{b}]]
  211. Get l0
  212. ArrangeBy keys=[[#0{b}]]
  213. Distinct project=[#0{b}]
  214. Project (#0{b})
  215. Filter (#0{b} > #1{b})
  216. CrossJoin type=differential
  217. ArrangeBy keys=[[]]
  218. Distinct project=[#0{b}]
  219. Project (#1{b})
  220. Get l0
  221. ArrangeBy keys=[[]]
  222. Project (#1{b})
  223. ReadStorage materialize.public.mv
  224. Source materialize.public.mv
  225. Used Indexes:
  226. - materialize.public.t_a_idx (*** full scan ***, differential join)
  227. Target cluster: quickstart
  228. EOF
  229. # Test SELECT subqueries.
  230. query T multiline
  231. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  232. 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
  233. ----
  234. Explained Query:
  235. With
  236. cte l0 =
  237. Project (#1{b})
  238. ReadIndex on=t t_a_idx=[*** full scan ***]
  239. cte l1 =
  240. Distinct project=[#0{b}]
  241. Get l0
  242. cte l2 =
  243. ArrangeBy keys=[[#0{b}]]
  244. Get l1
  245. cte l3 =
  246. TopK group_by=[#0{b}] limit=█
  247. Project (#0{b}, #1{a})
  248. Filter (#0{b}) IS NOT NULL
  249. Join on=(#0{b} = #2{b}) type=differential
  250. Get l2
  251. ArrangeBy keys=[[#1{b}]]
  252. ReadIndex on=iv iv_b_idx=[differential join]
  253. cte l4 =
  254. TopK group_by=[#0{b}] limit=█
  255. Project (#0{b}, #1{a})
  256. Join on=(#0{b} = #2{b}) type=differential
  257. Get l2
  258. ArrangeBy keys=[[#1{b}]]
  259. Filter (#1{b}) IS NOT NULL
  260. ReadStorage materialize.public.mv
  261. Return
  262. Project (#2{a}, #4{a})
  263. Join on=(#0{b} = #1{b} = #3{b}) type=delta
  264. ArrangeBy keys=[[#0{b}]]
  265. Get l0
  266. ArrangeBy keys=[[#0{b}]]
  267. Union
  268. Get l3
  269. Map (█)
  270. Union
  271. Negate
  272. Project (#0{b})
  273. Get l3
  274. Get l1
  275. ArrangeBy keys=[[#0{b}]]
  276. Union
  277. Get l4
  278. Map (█)
  279. Union
  280. Negate
  281. Project (#0{b})
  282. Get l4
  283. Get l1
  284. Source materialize.public.mv
  285. filter=((#1{b}) IS NOT NULL)
  286. Used Indexes:
  287. - materialize.public.t_a_idx (*** full scan ***)
  288. - materialize.public.iv_b_idx (differential join)
  289. Target cluster: quickstart
  290. EOF
  291. # Test redaction with the equivalences analysis.
  292. query T multiline
  293. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted, equivalences) AS VERBOSE TEXT FOR
  294. SELECT * FROM t WHERE EXISTS(SELECT * FROM mv WHERE t.a < mv.a) AND EXISTS(SELECT * FROM mv WHERE t.b > mv.b)
  295. ----
  296. Explained Query:
  297. With
  298. cte l0 =
  299. Project (#0{a}, #1{b}) // { equivs: "[[█, (#0{a}) IS NULL]]" }
  300. Join on=(#0{a} = #2{a}) type=differential // { equivs: "[[#0{a}, #2{a}], [█, (#0{a}) IS NULL]]" }
  301. ArrangeBy keys=[[#0{a}]] // { equivs: "[]" }
  302. ReadIndex on=t t_a_idx=[differential join] // { equivs: "[]" }
  303. ArrangeBy keys=[[#0{a}]] // { equivs: "[[█, (#0{a}) IS NULL]]" }
  304. Distinct project=[#0{a}] // { equivs: "[[█, (#0{a}) IS NULL]]" }
  305. Project (#0{a}) // { equivs: "[[█, (#0{a}) IS NULL]]" }
  306. Filter (#0{a} < #1{a}) // { equivs: "[[█, (#0{a}) IS NULL, (#1{a}) IS NULL], [█, (#0{a} < #1{a})]]" }
  307. CrossJoin type=differential // { equivs: "[[█, (#1{a}) IS NULL]]" }
  308. ArrangeBy keys=[[]] // { equivs: "[]" }
  309. Distinct project=[#0{a}] // { equivs: "[]" }
  310. Project (#0{a}) // { equivs: "[]" }
  311. ReadIndex on=t t_a_idx=[*** full scan ***] // { equivs: "[]" }
  312. ArrangeBy keys=[[]] // { equivs: "[[█, (#0{a}) IS NULL]]" }
  313. Project (#0{a}) // { equivs: "[[█, (#0{a}) IS NULL]]" }
  314. ReadStorage materialize.public.mv // { equivs: "[[█, (#0{a}) IS NULL]]" }
  315. Return // { equivs: "[[█, (#0{a}) IS NULL, (#1{b}) IS NULL]]" }
  316. Project (#0{a}, #1{b}) // { equivs: "[[█, (#0{a}) IS NULL, (#1{b}) IS NULL]]" }
  317. Join on=(#1{b} = #2{b}) type=differential // { equivs: "[[#1{b}, #2{b}], [█, (#0{a}) IS NULL, (#1{b}) IS NULL]]" }
  318. ArrangeBy keys=[[#1{b}]] // { equivs: "[[█, (#0{a}) IS NULL]]" }
  319. Get l0 // { equivs: "[[█, (#0{a}) IS NULL]]" }
  320. ArrangeBy keys=[[#0{b}]] // { equivs: "[[█, (#0{b}) IS NULL]]" }
  321. Distinct project=[#0{b}] // { equivs: "[[█, (#0{b}) IS NULL]]" }
  322. Project (#0{b}) // { equivs: "[[█, (#0{b}) IS NULL]]" }
  323. Filter (#0{b} > #1{b}) // { equivs: "[[█, (#0{b}) IS NULL, (#1{b}) IS NULL], [█, (#0{b} > #1{b})]]" }
  324. CrossJoin type=differential // { equivs: "[]" }
  325. ArrangeBy keys=[[]] // { equivs: "[]" }
  326. Distinct project=[#0{b}] // { equivs: "[]" }
  327. Project (#1{b}) // { equivs: "[]" }
  328. Get l0 // { equivs: "[[█, (#0{a}) IS NULL]]" }
  329. ArrangeBy keys=[[]] // { equivs: "[]" }
  330. Project (#1{b}) // { equivs: "[]" }
  331. ReadStorage materialize.public.mv // { equivs: "[[█, (#0{a}) IS NULL]]" }
  332. Source materialize.public.mv
  333. Used Indexes:
  334. - materialize.public.t_a_idx (*** full scan ***, differential join)
  335. Target cluster: quickstart
  336. EOF
  337. # Test outer joins (ON syntax).
  338. query T multiline
  339. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  340. SELECT t1.a, t2.a
  341. FROM t as t1
  342. LEFT JOIN t as t2 ON t1.b = t2.b
  343. RIGHT JOIN t as t3 ON t2.b = t3.b
  344. ----
  345. Explained Query:
  346. With
  347. cte l0 =
  348. Filter (#1{b}) IS NOT NULL
  349. ReadIndex on=t t_a_idx=[*** full scan ***]
  350. cte l1 =
  351. ArrangeBy keys=[[#1{b}]]
  352. Get l0
  353. cte l2 =
  354. ArrangeBy keys=[[#0{b}]]
  355. Project (#1{b})
  356. Get l0
  357. cte l3 =
  358. Project (#0{a}..=#2{a})
  359. Join on=(#1{b} = #3{b} = #4{b}) type=delta
  360. Get l1
  361. Get l1
  362. Get l2
  363. Return
  364. Union
  365. Map (█, █)
  366. Union
  367. Negate
  368. Project ()
  369. Join on=(#0{b} = #1{b}) type=differential
  370. Get l2
  371. ArrangeBy keys=[[#0{b}]]
  372. Distinct project=[#0{b}]
  373. Project (#1{b})
  374. Get l3
  375. Project ()
  376. ReadIndex on=t t_a_idx=[*** full scan ***]
  377. Project (#0{a}, #2{a})
  378. Get l3
  379. Used Indexes:
  380. - materialize.public.t_a_idx (*** full scan ***)
  381. Target cluster: quickstart
  382. EOF
  383. # Test an IndexedFilter join.
  384. query T multiline
  385. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  386. SELECT a, max(b)
  387. FROM t
  388. WHERE a = 0
  389. GROUP BY a
  390. ----
  391. Explained Query:
  392. Project (#1, #0{max_b})
  393. Map (█)
  394. Reduce aggregates=[max(#0{b})]
  395. Project (#1{b})
  396. ReadIndex on=materialize.public.t t_a_idx=[lookup value=(█)]
  397. Used Indexes:
  398. - materialize.public.t_a_idx (lookup)
  399. Target cluster: quickstart
  400. EOF
  401. # Create index for IndexedFilter test
  402. statement ok
  403. CREATE INDEX t_a_b_idx ON T(a,b)
  404. # Test an IndexedFilter join WITH(join implementations).
  405. query T multiline
  406. EXPLAIN OPTIMIZED PLAN WITH(join implementations, humanized expressions) AS VERBOSE TEXT FOR
  407. SELECT a, max(b)
  408. FROM t
  409. WHERE (a = 0 AND b = 1) OR (a = 3 AND b = 4) OR (a = 7 AND b = 8)
  410. GROUP BY a
  411. ----
  412. Explained Query:
  413. Reduce group_by=[#0{a}] aggregates=[max(#1{b})]
  414. Project (#0{a}, #1{b})
  415. ReadIndex on=materialize.public.t t_a_b_idx=[lookup values=[(0, 1); (3, 4); (7, 8)]]
  416. Used Indexes:
  417. - materialize.public.t_a_b_idx (lookup)
  418. Target cluster: quickstart
  419. EOF
  420. # Test an IndexedFilter join on fast path WITH(join implementations).
  421. query T multiline
  422. EXPLAIN OPTIMIZED PLAN WITH(join implementations, humanized expressions) AS VERBOSE TEXT FOR
  423. SELECT *
  424. FROM t
  425. WHERE (a = 0 AND b = 1) OR (a = 3 AND b = 4) OR (a = 7 AND b = 8)
  426. ----
  427. Explained Query (fast path):
  428. Project (#0{a}, #1{b})
  429. ReadIndex on=materialize.public.t t_a_b_idx=[lookup values=[(0, 1); (3, 4); (7, 8)]]
  430. Used Indexes:
  431. - materialize.public.t_a_b_idx (lookup)
  432. Target cluster: quickstart
  433. EOF
  434. # Test materialize#17348.
  435. statement ok
  436. 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);
  437. query T multiline
  438. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT *
  439. FROM r AS r0, r AS r1
  440. WHERE
  441. r0.f0=r1.f0 AND
  442. r0.f2=r1.f2 AND
  443. r0.f3=r1.f3 AND
  444. r0.f4=r1.f4 AND
  445. r0.f6=r1.f6 AND
  446. r0.f8=r1.f8 AND
  447. r0.f9=r1.f9 AND
  448. r0.f11=r1.f11 AND
  449. r0.f12=r1.f12 AND
  450. r0.f13=r1.f13 AND
  451. r0.f15=r1.f15 AND
  452. r0.f16=r1.f16;
  453. ----
  454. Explained Query:
  455. With
  456. cte l0 =
  457. ArrangeBy keys=[[#0{f0}, #2{f2}..=#4{f4}, #6{f6}, #8{f8}, #9{f9}, #11{f11}..=#13{f13}, #15{f15}, #16{f16}]]
  458. 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
  459. ReadStorage materialize.public.r
  460. Return
  461. 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})
  462. 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
  463. Get l0
  464. Get l0
  465. Source materialize.public.r
  466. 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)
  467. Target cluster: quickstart
  468. EOF
  469. ## linear chains is currently disabled for WMR.
  470. statement error not supported
  471. EXPLAIN OPTIMIZED PLAN WITH(linear chains) AS VERBOSE TEXT FOR
  472. WITH MUTUALLY RECURSIVE
  473. foo (a int, b int) AS (SELECT 1, 2 UNION SELECT a, 7 FROM bar),
  474. bar (a int) as (SELECT a FROM foo)
  475. SELECT * FROM bar;
  476. # Regression test for database-issues#5674: support mz_now() on select from indexed table
  477. # ---
  478. simple conn=mz_system,user=mz_system
  479. ALTER SYSTEM SET enable_rbac_checks TO false;
  480. ----
  481. COMPLETE 0
  482. statement ok
  483. DROP SCHEMA IF EXISTS public CASCADE;
  484. simple conn=mz_system,user=mz_system
  485. ALTER SYSTEM RESET enable_rbac_checks;
  486. ----
  487. COMPLETE 0
  488. statement ok
  489. CREATE SCHEMA public;
  490. statement ok
  491. CREATE TABLE t(a TIMESTAMP);
  492. statement ok
  493. CREATE DEFAULT INDEX ON t;
  494. # EXPLAIN output is time-dependent, so we don't want show the output here, just
  495. # assert that the query doesn't fail.
  496. statement ok
  497. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM t WHERE a < mz_now();
  498. # Regression test for materialize#19177
  499. # ---
  500. statement ok
  501. DROP SCHEMA IF EXISTS public CASCADE;
  502. statement ok
  503. CREATE SCHEMA public;
  504. statement ok
  505. CREATE TABLE t1(x text);
  506. statement ok
  507. CREATE TABLE t2(x text);
  508. statement ok
  509. 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();
  510. # Regression test for the join visitation part of materialize#19177
  511. statement ok
  512. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM t1, t2 WHERE t1.x || mz_now() = t2.x || mz_now();
  513. query T multiline
  514. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  515. SELECT lag(x, 3, 'default') IGNORE NULLS OVER (ORDER BY x || x)
  516. FROM t1;
  517. ----
  518. Explained Query:
  519. Project (#2)
  520. Map (record_get[0](#1))
  521. FlatMap unnest_list(#0{lag})
  522. Reduce aggregates=[lag[ignore_nulls=true, order_by=[#0{x} asc nulls_last]](row(row(row(#0{x}), row(#0{x}, █, █)), (#0{x} || #0{x})))]
  523. ReadStorage materialize.public.t1
  524. Source materialize.public.t1
  525. Target cluster: quickstart
  526. EOF
  527. query T multiline
  528. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  529. SELECT first_value(x) OVER (ORDER BY x || x ROWS BETWEEN 5 preceding AND CURRENT ROW)
  530. FROM t1;
  531. ----
  532. Explained Query:
  533. Project (#2)
  534. Map (record_get[0](#1))
  535. FlatMap unnest_list(#0{first_value})
  536. 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})))]
  537. ReadStorage materialize.public.t1
  538. Source materialize.public.t1
  539. Target cluster: quickstart
  540. EOF
  541. ## "Used indexes" tests
  542. statement ok
  543. CREATE TABLE t (
  544. a int,
  545. b int
  546. );
  547. statement ok
  548. CREATE TABLE u (
  549. c int,
  550. d int
  551. );
  552. # If two indexes exist on the same table, then "Used indexes" should print the one that we are actually going to use
  553. statement ok
  554. CREATE INDEX u_c ON u(c);
  555. statement ok
  556. CREATE INDEX u_d ON u(d);
  557. query T multiline
  558. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  559. SELECT *
  560. FROM t, u
  561. WHERE t.b = u.c;
  562. ----
  563. Explained Query:
  564. Project (#0{a}, #1{b}, #1{b}, #3{d})
  565. Join on=(#1{b} = #2{c}) type=differential
  566. ArrangeBy keys=[[#1{b}]]
  567. Filter (#1{b}) IS NOT NULL
  568. ReadStorage materialize.public.t
  569. ArrangeBy keys=[[#0{c}]]
  570. ReadIndex on=u u_c=[differential join]
  571. Source materialize.public.t
  572. filter=((#1{b}) IS NOT NULL)
  573. Used Indexes:
  574. - materialize.public.u_c (differential join)
  575. Target cluster: quickstart
  576. EOF
  577. query T multiline
  578. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  579. SELECT *
  580. FROM t, u
  581. WHERE t.b = u.d;
  582. ----
  583. Explained Query:
  584. Project (#0{a}..=#2{c}, #1{b})
  585. Join on=(#1{b} = #3{d}) type=differential
  586. ArrangeBy keys=[[#1{b}]]
  587. Filter (#1{b}) IS NOT NULL
  588. ReadStorage materialize.public.t
  589. ArrangeBy keys=[[#1{d}]]
  590. ReadIndex on=u u_d=[differential join]
  591. Source materialize.public.t
  592. filter=((#1{b}) IS NOT NULL)
  593. Used Indexes:
  594. - materialize.public.u_d (differential join)
  595. Target cluster: quickstart
  596. EOF
  597. statement ok
  598. DROP INDEX u_c;
  599. # Let's test the weird situation that two identical indexes exist.
  600. statement ok
  601. CREATE INDEX t_a_idx_1 ON t(a);
  602. statement ok
  603. CREATE INDEX t_a_idx_2 ON t(a);
  604. query T multiline
  605. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  606. SELECT *
  607. FROM t, u
  608. WHERE t.a = u.c
  609. ----
  610. Explained Query:
  611. Project (#0{a}, #1{b}, #0{a}, #3{d})
  612. Join on=(#0{a} = #2{c}) type=differential
  613. ArrangeBy keys=[[#0{a}]]
  614. ReadIndex on=t t_a_idx_1=[differential join]
  615. ArrangeBy keys=[[#0{c}]]
  616. Filter (#0{c}) IS NOT NULL
  617. ReadIndex on=u u_d=[*** full scan ***]
  618. Used Indexes:
  619. - materialize.public.u_d (*** full scan ***)
  620. - materialize.public.t_a_idx_1 (differential join)
  621. Target cluster: quickstart
  622. EOF
  623. # An index is used two times by the same (self) join. We should show a 1st input and a non-1st input usage.
  624. query T multiline
  625. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  626. SELECT *
  627. FROM t AS t1, t AS t2, t AS t3
  628. WHERE t1.a = t2.a AND t2.a = t3.a;
  629. ----
  630. Explained Query:
  631. With
  632. cte l0 =
  633. ArrangeBy keys=[[#0{a}]]
  634. ReadIndex on=t t_a_idx_1=[delta join lookup, delta join 1st input (full scan)]
  635. Return
  636. Project (#0{a}, #1{b}, #0{a}, #3{b}, #0{a}, #5{b})
  637. Filter (#0{a}) IS NOT NULL
  638. Join on=(#0{a} = #2{a} = #4{a}) type=delta
  639. Get l0
  640. Get l0
  641. Get l0
  642. Used Indexes:
  643. - materialize.public.t_a_idx_1 (delta join lookup, delta join 1st input (full scan))
  644. Target cluster: quickstart
  645. EOF
  646. # An index is used in both a join and a full scan.
  647. query T multiline
  648. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  649. (SELECT t1.a + t2.a AS a, t1.b + t2.b AS b
  650. FROM t AS t1, t AS t2
  651. WHERE t1.a = t2.a)
  652. UNION
  653. (SELECT *
  654. FROM t
  655. WHERE b > 5)
  656. ----
  657. Explained Query:
  658. With
  659. cte l0 =
  660. ArrangeBy keys=[[#0{a}]]
  661. ReadIndex on=t t_a_idx_1=[differential join]
  662. Return
  663. Distinct project=[#0{a}, #1{b}]
  664. Union
  665. Project (#4, #5)
  666. Filter (#0{a}) IS NOT NULL
  667. Map ((#0{a} + #0{a}), (#1{b} + #3{b}))
  668. Join on=(#0{a} = #2{a}) type=differential
  669. Get l0
  670. Get l0
  671. Filter (#1{b} > █)
  672. ReadIndex on=t t_a_idx_1=[*** full scan ***]
  673. Used Indexes:
  674. - materialize.public.t_a_idx_1 (*** full scan ***, differential join)
  675. Target cluster: quickstart
  676. EOF
  677. # An index exists that can't be used for the join because of having the wrong key.
  678. query T multiline
  679. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  680. (SELECT t1.a + t2.a AS a, t1.b + t2.b AS b
  681. FROM t AS t1, t AS t2
  682. WHERE t1.b = t2.b)
  683. UNION
  684. (SELECT *
  685. FROM t
  686. WHERE b > 5)
  687. ----
  688. Explained Query:
  689. With
  690. cte l0 =
  691. ArrangeBy keys=[[#1{b}]]
  692. Filter (#1{b}) IS NOT NULL
  693. ReadIndex on=t t_a_idx_1=[*** full scan ***]
  694. Return
  695. Distinct project=[#0{a}, #1{b}]
  696. Union
  697. Project (#4, #5)
  698. Map ((#0{a} + #2{a}), (#1{b} + #1{b}))
  699. Join on=(#1{b} = #3{b}) type=differential
  700. Get l0
  701. Get l0
  702. Filter (#1{b} > █)
  703. ReadIndex on=t t_a_idx_1=[*** full scan ***]
  704. Used Indexes:
  705. - materialize.public.t_a_idx_1 (*** full scan ***)
  706. Target cluster: quickstart
  707. EOF
  708. # Similar to the previous test, but exercises the full scan code inside the context loop of the Get case in
  709. # `collect_index_reqs_inner`, where we don't have an index for the requested key.
  710. statement ok
  711. CREATE TABLE t_non_null (
  712. a int NOT NULL,
  713. b int NOT NULL
  714. );
  715. statement ok
  716. CREATE INDEX t_non_null_a_idx ON t_non_null(a);
  717. query T multiline
  718. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  719. (SELECT t1.a + t2.a AS a, t1.b + t2.b AS b
  720. FROM t_non_null AS t1, t_non_null AS t2
  721. WHERE t1.b = t2.b)
  722. UNION
  723. (SELECT *
  724. FROM t_non_null
  725. WHERE b > 5)
  726. ----
  727. Explained Query:
  728. With
  729. cte l0 =
  730. ArrangeBy keys=[[#1{b}]]
  731. ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
  732. Return
  733. Distinct project=[#0{a}, #1{b}]
  734. Union
  735. Project (#4, #5)
  736. Map ((#0{a} + #2{a}), (#1{b} + #1{b}))
  737. Join on=(#1{b} = #3{b}) type=differential
  738. Get l0
  739. Get l0
  740. Filter (#1{b} > █)
  741. ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
  742. Used Indexes:
  743. - materialize.public.t_non_null_a_idx (*** full scan ***)
  744. Target cluster: quickstart
  745. EOF
  746. # This has 1 more full scan than the previous test, because the join needs 2 different arrangements.
  747. # (But we print only one full scan due to deduplication.)
  748. query T multiline
  749. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  750. (SELECT t1.a + t2.a AS a, t1.b + t2.b AS b
  751. FROM t_non_null AS t1, t_non_null AS t2
  752. WHERE t1.b = t2.b + 1)
  753. UNION
  754. (SELECT *
  755. FROM t_non_null
  756. WHERE b > 5)
  757. ----
  758. Explained Query:
  759. Distinct project=[#0{a}, #1{b}]
  760. Union
  761. Project (#4, #5)
  762. Map ((#0{a} + #2{a}), (#1{b} + #3{b}))
  763. Join on=(#1{b} = (#3{b} + █)) type=differential
  764. ArrangeBy keys=[[#1{b}]]
  765. ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
  766. ArrangeBy keys=[[(#1{b} + █)]]
  767. ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
  768. Filter (#1{b} > █)
  769. ReadIndex on=t_non_null t_non_null_a_idx=[*** full scan ***]
  770. Used Indexes:
  771. - materialize.public.t_non_null_a_idx (*** full scan ***)
  772. Target cluster: quickstart
  773. EOF
  774. # An index is used in both a lookup and a full scan.
  775. query T multiline
  776. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  777. SELECT * FROM t
  778. UNION
  779. SELECT * FROM t WHERE a = 5;
  780. ----
  781. Explained Query:
  782. Distinct project=[#0{a}, #1{b}]
  783. Union
  784. ReadIndex on=t t_a_idx_2=[*** full scan ***]
  785. Project (#0{a}, #1{b})
  786. ReadIndex on=materialize.public.t t_a_idx_2=[lookup value=(█)]
  787. Used Indexes:
  788. - materialize.public.t_a_idx_2 (*** full scan ***, lookup)
  789. Target cluster: quickstart
  790. EOF
  791. # Several lookups using different indexes
  792. statement ok
  793. CREATE INDEX t_b_idx ON t(b);
  794. query T multiline
  795. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  796. SELECT * FROM t
  797. UNION ALL
  798. SELECT * FROM t WHERE b = 7
  799. UNION ALL
  800. SELECT * FROM t WHERE a = 5
  801. UNION ALL
  802. SELECT * FROM u WHERE c = 3
  803. UNION ALL
  804. SELECT * FROM u WHERE d = 1;
  805. ----
  806. Explained Query:
  807. Union
  808. ReadIndex on=t t_b_idx=[*** full scan ***]
  809. Project (#0{a}, #1{b})
  810. ReadIndex on=materialize.public.t t_b_idx=[lookup value=(█)]
  811. Project (#0{a}, #1{b})
  812. ReadIndex on=materialize.public.t t_a_idx_2=[lookup value=(█)]
  813. Filter (#0{c} = █)
  814. ReadIndex on=u u_d=[*** full scan ***]
  815. Project (#0{c}, #1{d})
  816. ReadIndex on=materialize.public.u u_d=[lookup value=(█)]
  817. Used Indexes:
  818. - materialize.public.u_d (*** full scan ***, lookup)
  819. - materialize.public.t_a_idx_2 (lookup)
  820. - materialize.public.t_b_idx (*** full scan ***, lookup)
  821. Target cluster: quickstart
  822. EOF
  823. # Fast path with a LIMIT and no ORDER BY. This is not a full scan.
  824. query T multiline
  825. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  826. SELECT a+b as x
  827. FROM t
  828. WHERE a < 7
  829. LIMIT 3;
  830. ----
  831. Explained Query (fast path):
  832. Finish limit=3 output=[#0]
  833. Project (#2)
  834. Filter (#0{a} < █)
  835. Map ((#0{a} + #1{b}))
  836. ReadIndex on=materialize.public.t t_a_idx_1=[*** full scan ***]
  837. Used Indexes:
  838. - materialize.public.t_a_idx_1 (fast path limit)
  839. Target cluster: quickstart
  840. EOF
  841. # Same query without a LIMIT, so full scan
  842. query T multiline
  843. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  844. SELECT a+b as x
  845. FROM t
  846. WHERE a < 7;
  847. ----
  848. Explained Query (fast path):
  849. Project (#2)
  850. Filter (#0{a} < █)
  851. Map ((#0{a} + #1{b}))
  852. ReadIndex on=materialize.public.t t_a_idx_1=[*** full scan ***]
  853. Used Indexes:
  854. - materialize.public.t_a_idx_1 (*** full scan ***)
  855. Target cluster: quickstart
  856. EOF
  857. # Same query with a LIMIT + ORDER BY, so full scan
  858. query T multiline
  859. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, redacted) AS VERBOSE TEXT FOR
  860. SELECT a+b as x
  861. FROM t
  862. WHERE a < 7
  863. ORDER BY x
  864. LIMIT 3;
  865. ----
  866. Explained Query (fast path):
  867. Finish order_by=[#0 asc nulls_last] limit=3 output=[#0]
  868. Project (#2)
  869. Filter (#0{a} < █)
  870. Map ((#0{a} + #1{b}))
  871. ReadIndex on=materialize.public.t t_a_idx_1=[*** full scan ***]
  872. Used Indexes:
  873. - materialize.public.t_a_idx_1 (*** full scan ***)
  874. Target cluster: quickstart
  875. EOF