literal_lifting.slt 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. #
  10. # Test various cases of literal lifting
  11. #
  12. mode cockroach
  13. simple conn=mz_system,user=mz_system
  14. ALTER SYSTEM SET unsafe_enable_table_keys = true
  15. ----
  16. COMPLETE 0
  17. statement ok
  18. create table t (
  19. a int,
  20. b int
  21. )
  22. query T multiline
  23. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  24. (select null::int, 1, 2)
  25. union all
  26. (select a, b, 2 from t)
  27. union all
  28. (select a, b, 2 from t)
  29. ----
  30. Explained Query:
  31. Map (2) // { arity: 3 }
  32. Union // { arity: 2 }
  33. ReadStorage materialize.public.t // { arity: 2 }
  34. ReadStorage materialize.public.t // { arity: 2 }
  35. Constant // { arity: 2 }
  36. - (null, 1)
  37. Source materialize.public.t
  38. Target cluster: quickstart
  39. EOF
  40. query T multiline
  41. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  42. (select null::int, 1, 2, 3)
  43. union all
  44. (select a, b, 2, 3 from t)
  45. union all
  46. (select a, b, 2, 3 from t)
  47. ----
  48. Explained Query:
  49. Map (2, 3) // { arity: 4 }
  50. Union // { arity: 2 }
  51. ReadStorage materialize.public.t // { arity: 2 }
  52. ReadStorage materialize.public.t // { arity: 2 }
  53. Constant // { arity: 2 }
  54. - (null, 1)
  55. Source materialize.public.t
  56. Target cluster: quickstart
  57. EOF
  58. query T multiline
  59. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  60. (select null::int, 1, 2)
  61. union all
  62. (select a, b, 2 from t)
  63. union all
  64. (select a, b, 3 from t)
  65. ----
  66. Explained Query:
  67. Union // { arity: 3 }
  68. Map (2) // { arity: 3 }
  69. ReadStorage materialize.public.t // { arity: 2 }
  70. Map (3) // { arity: 3 }
  71. ReadStorage materialize.public.t // { arity: 2 }
  72. Constant // { arity: 3 }
  73. - (null, 1, 2)
  74. Source materialize.public.t
  75. Target cluster: quickstart
  76. EOF
  77. # Group key literal lifting
  78. query T multiline
  79. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  80. select a, b from t where a = 1 group by a, b
  81. ----
  82. Explained Query:
  83. Project (#1, #0{b}) // { arity: 2 }
  84. Map (1) // { arity: 2 }
  85. Distinct project=[#0{b}] // { arity: 1 }
  86. Project (#1{b}) // { arity: 1 }
  87. Filter (#0{a} = 1) // { arity: 2 }
  88. ReadStorage materialize.public.t // { arity: 2 }
  89. Source materialize.public.t
  90. filter=((#0{a} = 1))
  91. Target cluster: quickstart
  92. EOF
  93. query T multiline
  94. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  95. select a, b from t where b = 1 group by a, b
  96. ----
  97. Explained Query:
  98. Map (1) // { arity: 2 }
  99. Distinct project=[#0{a}] // { arity: 1 }
  100. Project (#0{a}) // { arity: 1 }
  101. Filter (#1{b} = 1) // { arity: 2 }
  102. ReadStorage materialize.public.t // { arity: 2 }
  103. Source materialize.public.t
  104. filter=((#1{b} = 1))
  105. Target cluster: quickstart
  106. EOF
  107. # Permute literals in Map operator so they can be lifted
  108. query T multiline
  109. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  110. select * from (select 1, a+1 from t), t;
  111. ----
  112. Explained Query:
  113. Project (#3, #0..=#2{b}) // { arity: 4 }
  114. Map (1) // { arity: 4 }
  115. CrossJoin type=differential // { arity: 3 }
  116. implementation
  117. %0:t[×] » %1:t[×]
  118. ArrangeBy keys=[[]] // { arity: 1 }
  119. Project (#2) // { arity: 1 }
  120. Map ((#0{a} + 1)) // { arity: 3 }
  121. ReadStorage materialize.public.t // { arity: 2 }
  122. ArrangeBy keys=[[]] // { arity: 2 }
  123. ReadStorage materialize.public.t // { arity: 2 }
  124. Source materialize.public.t
  125. Target cluster: quickstart
  126. EOF
  127. query T multiline
  128. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  129. select * from (select b+1, 2, 1, a+1 from t), t;
  130. ----
  131. Explained Query:
  132. Project (#0, #4, #5, #1..=#3{b}) // { arity: 6 }
  133. Map (2, 1) // { arity: 6 }
  134. CrossJoin type=differential // { arity: 4 }
  135. implementation
  136. %0:t[×] » %1:t[×]
  137. ArrangeBy keys=[[]] // { arity: 2 }
  138. Project (#2, #3) // { arity: 2 }
  139. Map ((#1{b} + 1), (#0{a} + 1)) // { arity: 4 }
  140. ReadStorage materialize.public.t // { arity: 2 }
  141. ArrangeBy keys=[[]] // { arity: 2 }
  142. ReadStorage materialize.public.t // { arity: 2 }
  143. Source materialize.public.t
  144. Target cluster: quickstart
  145. EOF
  146. query T multiline
  147. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  148. select * from (select 3, b+1, 2, a+2, 1, a+1 from t), t;
  149. ----
  150. Explained Query:
  151. Project (#5, #0, #6, #1, #7, #2..=#4{b}) // { arity: 8 }
  152. Map (3, 2, 1) // { arity: 8 }
  153. CrossJoin type=differential // { arity: 5 }
  154. implementation
  155. %0:t[×] » %1:t[×]
  156. ArrangeBy keys=[[]] // { arity: 3 }
  157. Project (#2..=#4) // { arity: 3 }
  158. Map ((#1{b} + 1), (#0{a} + 2), (#0{a} + 1)) // { arity: 5 }
  159. ReadStorage materialize.public.t // { arity: 2 }
  160. ArrangeBy keys=[[]] // { arity: 2 }
  161. ReadStorage materialize.public.t // { arity: 2 }
  162. Source materialize.public.t
  163. Target cluster: quickstart
  164. EOF
  165. query T multiline
  166. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  167. select a+1 from (select 1 as a, b from t);
  168. ----
  169. Explained Query:
  170. Project (#2) // { arity: 1 }
  171. Map (2) // { arity: 3 }
  172. ReadStorage materialize.public.t // { arity: 2 }
  173. Source materialize.public.t
  174. Target cluster: quickstart
  175. EOF
  176. query T multiline
  177. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  178. select z+1 from (select 2 as y, a, 1 as z, b from t);
  179. ----
  180. Explained Query:
  181. Project (#2) // { arity: 1 }
  182. Map (2) // { arity: 3 }
  183. ReadStorage materialize.public.t // { arity: 2 }
  184. Source materialize.public.t
  185. Target cluster: quickstart
  186. EOF
  187. # Extract common values in all rows in Constant operator
  188. query T multiline
  189. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  190. select c1, c1 + a from (select 1 as c1, x as c2, 3 as c3 from generate_series(1, 3) as x union all select 1, x, 3 from generate_series(5, 8) as x), t;
  191. ----
  192. Explained Query:
  193. Project (#2, #1) // { arity: 2 }
  194. Map ((1 + #0{a}), 1) // { arity: 3 }
  195. CrossJoin type=differential // { arity: 1 }
  196. implementation
  197. %0[×] » %1:t[×]
  198. ArrangeBy keys=[[]] // { arity: 0 }
  199. Constant // { arity: 0 }
  200. - (() x 7)
  201. ArrangeBy keys=[[]] // { arity: 1 }
  202. Project (#0{a}) // { arity: 1 }
  203. ReadStorage materialize.public.t // { arity: 2 }
  204. Source materialize.public.t
  205. Target cluster: quickstart
  206. EOF
  207. query T multiline
  208. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  209. select * from (select 1 as f1, 2 as f2), generate_series(f1, f2);
  210. ----
  211. Explained Query (fast path):
  212. Constant
  213. - (1, 2, 1)
  214. - (1, 2, 2)
  215. Target cluster: quickstart
  216. EOF
  217. # ... check keys are updated properly
  218. query T multiline
  219. EXPLAIN OPTIMIZED PLAN WITH(types, humanized expressions) AS VERBOSE TEXT FOR
  220. select c.* from (select f1, f2 from (select f2, f1 from (select 1 as f1), generate_series(2, 4) as f2) group by f2, f1) as c, t;
  221. ----
  222. Explained Query:
  223. Project (#1, #0) // { types: "(integer, integer)" }
  224. Map (1) // { types: "(integer, integer)" }
  225. CrossJoin type=differential // { types: "(integer)" }
  226. ArrangeBy keys=[[]] // { types: "(integer)" }
  227. Constant // { types: "(integer)" }
  228. - (2)
  229. - (3)
  230. - (4)
  231. ArrangeBy keys=[[]] // { types: "()" }
  232. Project () // { types: "()" }
  233. ReadStorage materialize.public.t // { types: "(integer?, integer?)" }
  234. Source materialize.public.t
  235. Target cluster: quickstart
  236. EOF
  237. query T multiline
  238. EXPLAIN OPTIMIZED PLAN WITH(types, humanized expressions) AS VERBOSE TEXT FOR
  239. select c.* from (select f2, f1, f3 from (select f3, f2, f1 from generate_series(2, 4) as f2, generate_series(3, 5) as f3, (select 1 as f1)) group by f2, f3, f1) as c, t;
  240. ----
  241. Explained Query:
  242. Project (#0, #2, #1) // { types: "(integer, integer, integer)" }
  243. Map (1) // { types: "(integer, integer, integer)" }
  244. CrossJoin type=differential // { types: "(integer, integer)" }
  245. ArrangeBy keys=[[]] // { types: "(integer, integer)" }
  246. Constant // { types: "(integer, integer)" }
  247. - (2, 3)
  248. - (2, 4)
  249. - (2, 5)
  250. - (3, 3)
  251. - (3, 4)
  252. - (3, 5)
  253. - (4, 3)
  254. - (4, 4)
  255. - (4, 5)
  256. ArrangeBy keys=[[]] // { types: "()" }
  257. Project () // { types: "()" }
  258. ReadStorage materialize.public.t // { types: "(integer?, integer?)" }
  259. Source materialize.public.t
  260. Target cluster: quickstart
  261. EOF
  262. # Permute the literals around the columns added by FlatMap
  263. query T multiline
  264. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select * from (select 1 as a from t), generate_series(a+1, 4);
  265. ----
  266. Explained Query:
  267. Project (#1, #0) // { arity: 2 }
  268. Map (1) // { arity: 2 }
  269. FlatMap generate_series(2, 4, 1) // { arity: 1 }
  270. Project () // { arity: 0 }
  271. ReadStorage materialize.public.t // { arity: 2 }
  272. Source materialize.public.t
  273. Target cluster: quickstart
  274. EOF
  275. # Make sure that grouping/distinct is handled correctly in the face of derived tables
  276. # We want the proper interleaving between Map and Distinct to be preserved
  277. # With literals only
  278. query T multiline
  279. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select 123 from (select 234 from t);
  280. ----
  281. Explained Query:
  282. Project (#2) // { arity: 1 }
  283. Map (123) // { arity: 3 }
  284. ReadStorage materialize.public.t // { arity: 2 }
  285. Source materialize.public.t
  286. Target cluster: quickstart
  287. EOF
  288. query T multiline
  289. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select 123 from (select distinct 234 from t);
  290. ----
  291. Explained Query:
  292. Map (123) // { arity: 1 }
  293. Distinct project=[] // { arity: 0 }
  294. Project () // { arity: 0 }
  295. ReadStorage materialize.public.t // { arity: 2 }
  296. Source materialize.public.t
  297. Target cluster: quickstart
  298. EOF
  299. query T multiline
  300. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct 123 from (select 234 from t);
  301. ----
  302. Explained Query:
  303. Map (123) // { arity: 1 }
  304. Distinct project=[] // { arity: 0 }
  305. Project () // { arity: 0 }
  306. ReadStorage materialize.public.t // { arity: 2 }
  307. Source materialize.public.t
  308. Target cluster: quickstart
  309. EOF
  310. query T multiline
  311. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct 123 from (select distinct 234 from t);
  312. ----
  313. Explained Query:
  314. Map (123) // { arity: 1 }
  315. Distinct project=[] // { arity: 0 }
  316. Project () // { arity: 0 }
  317. ReadStorage materialize.public.t // { arity: 2 }
  318. Source materialize.public.t
  319. Target cluster: quickstart
  320. EOF
  321. # With a single literal
  322. query T multiline
  323. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select * from (select distinct 123 from t);
  324. ----
  325. Explained Query:
  326. Map (123) // { arity: 1 }
  327. Distinct project=[] // { arity: 0 }
  328. Project () // { arity: 0 }
  329. ReadStorage materialize.public.t // { arity: 2 }
  330. Source materialize.public.t
  331. Target cluster: quickstart
  332. EOF
  333. query T multiline
  334. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct * from (select 123 from t);
  335. ----
  336. Explained Query:
  337. Map (123) // { arity: 1 }
  338. Distinct project=[] // { arity: 0 }
  339. Project () // { arity: 0 }
  340. ReadStorage materialize.public.t // { arity: 2 }
  341. Source materialize.public.t
  342. Target cluster: quickstart
  343. EOF
  344. # With a literal in the outer query and a column in the derived table
  345. query T multiline
  346. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select 123 from (select a from t);
  347. ----
  348. Explained Query:
  349. Project (#2) // { arity: 1 }
  350. Map (123) // { arity: 3 }
  351. ReadStorage materialize.public.t // { arity: 2 }
  352. Source materialize.public.t
  353. Target cluster: quickstart
  354. EOF
  355. query T multiline
  356. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select 123 from (select distinct a from t);
  357. ----
  358. Explained Query:
  359. Project (#1) // { arity: 1 }
  360. Map (123) // { arity: 2 }
  361. Distinct project=[#0{a}] // { arity: 1 }
  362. Project (#0{a}) // { arity: 1 }
  363. ReadStorage materialize.public.t // { arity: 2 }
  364. Source materialize.public.t
  365. Target cluster: quickstart
  366. EOF
  367. query T multiline
  368. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct 123 from (select a from t);
  369. ----
  370. Explained Query:
  371. Map (123) // { arity: 1 }
  372. Distinct project=[] // { arity: 0 }
  373. Project () // { arity: 0 }
  374. ReadStorage materialize.public.t // { arity: 2 }
  375. Source materialize.public.t
  376. Target cluster: quickstart
  377. EOF
  378. query T multiline
  379. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct 123 from (select distinct a from t);
  380. ----
  381. Explained Query:
  382. Map (123) // { arity: 1 }
  383. Distinct project=[] // { arity: 0 }
  384. Project () // { arity: 0 }
  385. ReadStorage materialize.public.t // { arity: 2 }
  386. Source materialize.public.t
  387. Target cluster: quickstart
  388. EOF
  389. # With a literal and a column in the derived table
  390. query T multiline
  391. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.a, a1.literal from (select a, 123 as literal from t) as a1;
  392. ----
  393. Explained Query:
  394. Map (123) // { arity: 2 }
  395. Distinct project=[#0{a}] // { arity: 1 }
  396. Project (#0{a}) // { arity: 1 }
  397. ReadStorage materialize.public.t // { arity: 2 }
  398. Source materialize.public.t
  399. Target cluster: quickstart
  400. EOF
  401. query T multiline
  402. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select a1.a, a1.literal from (select distinct a, 123 as literal from t) as a1;
  403. ----
  404. Explained Query:
  405. Map (123) // { arity: 2 }
  406. Distinct project=[#0{a}] // { arity: 1 }
  407. Project (#0{a}) // { arity: 1 }
  408. ReadStorage materialize.public.t // { arity: 2 }
  409. Source materialize.public.t
  410. Target cluster: quickstart
  411. EOF
  412. query T multiline
  413. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select a1.a, a1.literal from (select distinct a, 123 as literal from t) as a1;
  414. ----
  415. Explained Query:
  416. Map (123) // { arity: 2 }
  417. Distinct project=[#0{a}] // { arity: 1 }
  418. Project (#0{a}) // { arity: 1 }
  419. ReadStorage materialize.public.t // { arity: 2 }
  420. Source materialize.public.t
  421. Target cluster: quickstart
  422. EOF
  423. query T multiline
  424. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.a, a1.literal from (select distinct a, 123 as literal from t) as a1;
  425. ----
  426. Explained Query:
  427. Map (123) // { arity: 2 }
  428. Distinct project=[#0{a}] // { arity: 1 }
  429. Project (#0{a}) // { arity: 1 }
  430. ReadStorage materialize.public.t // { arity: 2 }
  431. Source materialize.public.t
  432. Target cluster: quickstart
  433. EOF
  434. # With a literal and a column in the outer query
  435. query T multiline
  436. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.a, 123 from (select a from t) as a1;
  437. ----
  438. Explained Query:
  439. Map (123) // { arity: 2 }
  440. Distinct project=[#0{a}] // { arity: 1 }
  441. Project (#0{a}) // { arity: 1 }
  442. ReadStorage materialize.public.t // { arity: 2 }
  443. Source materialize.public.t
  444. Target cluster: quickstart
  445. EOF
  446. query T multiline
  447. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.a, 123 from (select distinct a from t) as a1;
  448. ----
  449. Explained Query:
  450. Map (123) // { arity: 2 }
  451. Distinct project=[#0{a}] // { arity: 1 }
  452. Project (#0{a}) // { arity: 1 }
  453. ReadStorage materialize.public.t // { arity: 2 }
  454. Source materialize.public.t
  455. Target cluster: quickstart
  456. EOF
  457. # With expressions
  458. query T multiline
  459. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.a+2 from (select distinct a+1 as a, 123 as literal from t) as a1;
  460. ----
  461. Explained Query:
  462. Distinct project=[((#0{a} + 1) + 2)] // { arity: 1 }
  463. Project (#0{a}) // { arity: 1 }
  464. ReadStorage materialize.public.t // { arity: 2 }
  465. Source materialize.public.t
  466. Target cluster: quickstart
  467. EOF
  468. query T multiline
  469. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.a, 123 from (select distinct a+1 as a, 234 as literal from t) as a1;
  470. ----
  471. Explained Query:
  472. Map (123) // { arity: 2 }
  473. Distinct project=[(#0{a} + 1)] // { arity: 1 }
  474. Project (#0{a}) // { arity: 1 }
  475. ReadStorage materialize.public.t // { arity: 2 }
  476. Source materialize.public.t
  477. Target cluster: quickstart
  478. EOF
  479. query T multiline
  480. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.a+2, a1.literal from (select distinct a+1 as a, 123 as literal from t) as a1;
  481. ----
  482. Explained Query:
  483. Map (123) // { arity: 2 }
  484. Distinct project=[((#0{a} + 1) + 2)] // { arity: 1 }
  485. Project (#0{a}) // { arity: 1 }
  486. ReadStorage materialize.public.t // { arity: 2 }
  487. Source materialize.public.t
  488. Target cluster: quickstart
  489. EOF
  490. query T multiline
  491. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.a, a1.literal + 1 from (select distinct a, 123 as literal from t) as a1;
  492. ----
  493. Explained Query:
  494. Map (124) // { arity: 2 }
  495. Distinct project=[#0{a}] // { arity: 1 }
  496. Project (#0{a}) // { arity: 1 }
  497. ReadStorage materialize.public.t // { arity: 2 }
  498. Source materialize.public.t
  499. Target cluster: quickstart
  500. EOF
  501. # Check that literals do not interfere with table elimination
  502. statement ok
  503. create table t_pk (
  504. a int primary key,
  505. b int
  506. )
  507. query T multiline
  508. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select a1.*, 123 from t_pk as a1, t_pk as a2 WHERE a1.a = a2.a;
  509. ----
  510. Explained Query:
  511. Map (123) // { arity: 3 }
  512. ReadStorage materialize.public.t_pk // { arity: 2 }
  513. Source materialize.public.t_pk
  514. Target cluster: quickstart
  515. EOF
  516. query T multiline
  517. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select distinct a1.*, 123 from t_pk as a1, t_pk as a2 WHERE a1.a = a2.a;
  518. ----
  519. Explained Query:
  520. Map (123) // { arity: 3 }
  521. ReadStorage materialize.public.t_pk // { arity: 2 }
  522. Source materialize.public.t_pk
  523. Target cluster: quickstart
  524. EOF
  525. # Reduce
  526. query T multiline
  527. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  528. select a, b, max(2), count(*) from t where b = 1 group by a, b;
  529. ----
  530. Explained Query:
  531. Project (#0{a}, #2, #3, #1{count}) // { arity: 4 }
  532. Map (1, 2) // { arity: 4 }
  533. Reduce group_by=[#0{a}] aggregates=[count(*)] // { arity: 2 }
  534. Project (#0{a}) // { arity: 1 }
  535. Filter (#1{b} = 1) // { arity: 2 }
  536. ReadStorage materialize.public.t // { arity: 2 }
  537. Source materialize.public.t
  538. filter=((#1{b} = 1))
  539. Target cluster: quickstart
  540. EOF
  541. query T multiline
  542. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  543. select a, b, count(*), max(2) from t where b = 1 group by a, b;
  544. ----
  545. Explained Query:
  546. Project (#0{a}, #2, #1{count}, #3) // { arity: 4 }
  547. Map (1, 2) // { arity: 4 }
  548. Reduce group_by=[#0{a}] aggregates=[count(*)] // { arity: 2 }
  549. Project (#0{a}) // { arity: 1 }
  550. Filter (#1{b} = 1) // { arity: 2 }
  551. ReadStorage materialize.public.t // { arity: 2 }
  552. Source materialize.public.t
  553. filter=((#1{b} = 1))
  554. Target cluster: quickstart
  555. EOF
  556. query T multiline
  557. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  558. select a, b, min(2), max(3) from t where b = 1 group by a, b;
  559. ----
  560. Explained Query:
  561. Map (1, 2, 3) // { arity: 4 }
  562. Distinct project=[#0{a}] // { arity: 1 }
  563. Project (#0{a}) // { arity: 1 }
  564. Filter (#1{b} = 1) // { arity: 2 }
  565. ReadStorage materialize.public.t // { arity: 2 }
  566. Source materialize.public.t
  567. filter=((#1{b} = 1))
  568. Target cluster: quickstart
  569. EOF
  570. # regression tests for database-issues#2159
  571. query T multiline
  572. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select min(1/x) from (select a as y, 0 as x from t);
  573. ----
  574. Explained Query:
  575. With
  576. cte l0 =
  577. Distinct project=[] // { arity: 0 }
  578. Project () // { arity: 0 }
  579. ReadStorage materialize.public.t // { arity: 2 }
  580. Return // { arity: 1 }
  581. Union // { arity: 1 }
  582. Map (error("division by zero")) // { arity: 1 }
  583. Get l0 // { arity: 0 }
  584. Map (null) // { arity: 1 }
  585. Union // { arity: 0 }
  586. Negate // { arity: 0 }
  587. Get l0 // { arity: 0 }
  588. Constant // { arity: 0 }
  589. - ()
  590. Source materialize.public.t
  591. Target cluster: quickstart
  592. EOF
  593. query T multiline
  594. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select sum(1/x) from (select a as y, 0 as x from t);
  595. ----
  596. Explained Query:
  597. With
  598. cte l0 =
  599. Distinct project=[] // { arity: 0 }
  600. Project () // { arity: 0 }
  601. ReadStorage materialize.public.t // { arity: 2 }
  602. Return // { arity: 1 }
  603. Union // { arity: 1 }
  604. Map (error("division by zero")) // { arity: 1 }
  605. Get l0 // { arity: 0 }
  606. Map (null) // { arity: 1 }
  607. Union // { arity: 0 }
  608. Negate // { arity: 0 }
  609. Get l0 // { arity: 0 }
  610. Constant // { arity: 0 }
  611. - ()
  612. Source materialize.public.t
  613. Target cluster: quickstart
  614. EOF
  615. query T multiline
  616. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select min(a) from t_pk where a between 38 and 195 and a = (select a from t where a = 1308);
  617. ----
  618. Explained Query:
  619. With
  620. cte l0 =
  621. Reduce aggregates=[min(#0{a})] // { arity: 1 }
  622. Project (#0{a}) // { arity: 1 }
  623. Join on=(#0{a} = #1) type=differential // { arity: 2 }
  624. implementation
  625. %0:t_pk[#0{a}]UKiif » %1[#0]Kiif
  626. ArrangeBy keys=[[#0{a}]] // { arity: 1 }
  627. Project (#0{a}) // { arity: 1 }
  628. Filter (#0{a} <= 195) AND (#0{a} >= 38) // { arity: 2 }
  629. ReadStorage materialize.public.t_pk // { arity: 2 }
  630. ArrangeBy keys=[[#0]] // { arity: 1 }
  631. Project (#1) // { arity: 1 }
  632. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  633. Reduce aggregates=[count(*)] // { arity: 1 }
  634. Project () // { arity: 0 }
  635. Filter (#0{a} = 1308) // { arity: 2 }
  636. ReadStorage materialize.public.t // { arity: 2 }
  637. Return // { arity: 1 }
  638. Union // { arity: 1 }
  639. Get l0 // { arity: 1 }
  640. Map (null) // { arity: 1 }
  641. Union // { arity: 0 }
  642. Negate // { arity: 0 }
  643. Project () // { arity: 0 }
  644. Get l0 // { arity: 1 }
  645. Constant // { arity: 0 }
  646. - ()
  647. Source materialize.public.t
  648. filter=((#0{a} = 1308))
  649. Source materialize.public.t_pk
  650. filter=((#0{a} <= 195) AND (#0{a} >= 38))
  651. Target cluster: quickstart
  652. EOF
  653. query T multiline
  654. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select min(a) from t where a between 38 and 195 and a = (select a from t where a = 1308);
  655. ----
  656. Explained Query:
  657. With
  658. cte l0 =
  659. Reduce aggregates=[min(#0{a})] // { arity: 1 }
  660. Project (#0{a}) // { arity: 1 }
  661. Join on=(#0{a} = #1) type=differential // { arity: 2 }
  662. implementation
  663. %0:t[#0{a}]Kiif » %1[#0]Kiif
  664. ArrangeBy keys=[[#0{a}]] // { arity: 1 }
  665. Project (#0{a}) // { arity: 1 }
  666. Filter (#0{a} <= 195) AND (#0{a} >= 38) // { arity: 2 }
  667. ReadStorage materialize.public.t // { arity: 2 }
  668. ArrangeBy keys=[[#0]] // { arity: 1 }
  669. Project (#1) // { arity: 1 }
  670. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  671. Reduce aggregates=[count(*)] // { arity: 1 }
  672. Project () // { arity: 0 }
  673. Filter (#0{a} = 1308) // { arity: 2 }
  674. ReadStorage materialize.public.t // { arity: 2 }
  675. Return // { arity: 1 }
  676. Union // { arity: 1 }
  677. Get l0 // { arity: 1 }
  678. Map (null) // { arity: 1 }
  679. Union // { arity: 0 }
  680. Negate // { arity: 0 }
  681. Project () // { arity: 0 }
  682. Get l0 // { arity: 1 }
  683. Constant // { arity: 0 }
  684. - ()
  685. Source materialize.public.t
  686. Target cluster: quickstart
  687. EOF
  688. statement ok
  689. insert into t_pk values (40);
  690. # check no error is returned, but a NULL result
  691. query I
  692. select min(a) from t_pk where a between 38 and 195 and a = (select a from t where a = 1308);
  693. ----
  694. NULL
  695. statement ok
  696. insert into t values (1308);
  697. query I
  698. select min(a) from t_pk where a between 38 and 195 and a = (select a from t where a = 1308);
  699. ----
  700. NULL
  701. statement ok
  702. insert into t_pk values (1308);
  703. query I
  704. select min(a) from t_pk where a between 38 and 195 and a = (select a from t where a = 1308);
  705. ----
  706. NULL
  707. statement ok
  708. insert into t values (1308);
  709. statement error more than one record produced in subquery
  710. select min(a) from t_pk where a between 38 and 195 and a = (select a from t where a = 1308);
  711. # check that literal errors are not lifted beyond Joins, but also that that doesn't lead to
  712. # a stack overflow due to LiteralLifting and JoinImplementation fighting against each other
  713. statement ok
  714. create table t1 (f1 double precision, f2 double precision not null);
  715. query T multiline
  716. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select * from t1 as a1 join t1 as a2 on (a2.f2 = (select 6 from t1)) where a2.f2 = 9;
  717. ----
  718. Explained Query:
  719. CrossJoin type=delta // { arity: 4 }
  720. implementation
  721. %0:t1 » %1:t1[×]ef » %2[×]ef
  722. %1:t1 » %2[×]ef » %0:t1[×]
  723. %2 » %1:t1[×]ef » %0:t1[×]
  724. ArrangeBy keys=[[]] // { arity: 2 }
  725. ReadStorage materialize.public.t1 // { arity: 2 }
  726. ArrangeBy keys=[[]] // { arity: 2 }
  727. Filter (#1{f2} = 9) // { arity: 2 }
  728. ReadStorage materialize.public.t1 // { arity: 2 }
  729. ArrangeBy keys=[[]] // { arity: 0 }
  730. Project () // { arity: 0 }
  731. Filter (9 = integer_to_double(#1)) // { arity: 2 }
  732. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  733. Reduce aggregates=[count(*)] // { arity: 1 }
  734. Project () // { arity: 0 }
  735. ReadStorage materialize.public.t1 // { arity: 2 }
  736. Source materialize.public.t1
  737. Target cluster: quickstart
  738. EOF
  739. # count(null) is always 0
  740. query T multiline
  741. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR select count(null) from t;
  742. ----
  743. Explained Query (fast path):
  744. Constant
  745. - (0)
  746. Target cluster: quickstart
  747. EOF
  748. statement ok
  749. DROP TABLE t1;
  750. statement ok
  751. CREATE TABLE t1 (f1 INTEGER, f2 INTEGER);
  752. # WITH MUTUALLY RECURSIVE support
  753. # -------------------------------
  754. # Single non-recursive binding under `WITH MUTUALLY RECURSIVE`
  755. query T multiline
  756. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  757. WITH MUTUALLY RECURSIVE
  758. c0(x INT, y INT) AS (
  759. SELECT * FROM c0
  760. UNION
  761. SELECT f1, 42 FROM t1
  762. UNION
  763. SELECT f2, 42 FROM t1
  764. )
  765. SELECT * FROM c0
  766. ----
  767. Explained Query:
  768. With Mutually Recursive
  769. cte l0 =
  770. Map (42) // { arity: 2 }
  771. Distinct project=[#0{f1}] // { arity: 1 }
  772. Union // { arity: 1 }
  773. Project (#0{f1}) // { arity: 1 }
  774. Get l0 // { arity: 2 }
  775. Project (#0{f1}) // { arity: 1 }
  776. ReadStorage materialize.public.t1 // { arity: 2 }
  777. Project (#1{f2}) // { arity: 1 }
  778. ReadStorage materialize.public.t1 // { arity: 2 }
  779. Return // { arity: 2 }
  780. Get l0 // { arity: 2 }
  781. Source materialize.public.t1
  782. Target cluster: quickstart
  783. EOF
  784. # Multiple bindings under `WITH MUTUALLY RECURSIVE`
  785. query T multiline
  786. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  787. WITH MUTUALLY RECURSIVE
  788. c0(x INT, y INT) AS (
  789. SELECT f1, 42 FROM t1
  790. UNION
  791. SELECT f2, 42 FROM t1
  792. ),
  793. c1(x INT, y INT) AS (
  794. SELECT * FROM c0
  795. UNION
  796. SELECT * FROM c1
  797. )
  798. SELECT * FROM c0 UNION ALL SELECT * FROM c1
  799. ----
  800. Explained Query:
  801. With
  802. cte l0 =
  803. Distinct project=[#0{f1}] // { arity: 1 }
  804. Union // { arity: 1 }
  805. Project (#0{f1}) // { arity: 1 }
  806. ReadStorage materialize.public.t1 // { arity: 2 }
  807. Project (#1{f2}) // { arity: 1 }
  808. ReadStorage materialize.public.t1 // { arity: 2 }
  809. Return // { arity: 2 }
  810. With Mutually Recursive
  811. cte l1 =
  812. Map (42) // { arity: 2 }
  813. Distinct project=[#0{f1}] // { arity: 1 }
  814. Union // { arity: 1 }
  815. Get l0 // { arity: 1 }
  816. Project (#0{f1}) // { arity: 1 }
  817. Get l1 // { arity: 2 }
  818. Return // { arity: 2 }
  819. Union // { arity: 2 }
  820. Map (42) // { arity: 2 }
  821. Get l0 // { arity: 1 }
  822. Get l1 // { arity: 2 }
  823. Source materialize.public.t1
  824. Target cluster: quickstart
  825. EOF