subquery.slt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. mode cockroach
  10. simple conn=mz_system,user=mz_system
  11. ALTER SYSTEM SET unsafe_enable_table_keys = true
  12. ----
  13. COMPLETE 0
  14. statement ok
  15. CREATE TABLE nullary ()
  16. statement ok
  17. CREATE TABLE peeps (
  18. peep text
  19. )
  20. statement ok
  21. INSERT INTO peeps VALUES ('alice'), ('bob'), ('eve')
  22. statement ok
  23. CREATE TABLE likes (
  24. liker text,
  25. likee text
  26. )
  27. statement ok
  28. INSERT INTO likes VALUES ('alice', 'bob'), ('bob', 'eve'), ('alice', 'eve')
  29. query TB rowsort
  30. SELECT peep, EXISTS(
  31. SELECT * FROM likes WHERE peep = liker
  32. ) FROM peeps
  33. ----
  34. alice true
  35. bob true
  36. eve false
  37. query TTB rowsort
  38. SELECT *, EXISTS(
  39. SELECT * FROM likes as likes2 WHERE likes.likee = likes2.liker
  40. ) FROM likes
  41. ----
  42. alice bob true
  43. alice eve false
  44. bob eve false
  45. # inner scope shadows outer scope, so `likee` without qualification refers to `likes2.likee`
  46. query TTB rowsort
  47. SELECT *, EXISTS(
  48. SELECT * FROM likes as likes2 WHERE likee = likes2.liker
  49. ) FROM likes
  50. ----
  51. alice bob false
  52. alice eve false
  53. bob eve false
  54. # similarly, without `as likes2`, `likes.liker` refers to the inner scope
  55. query TTB rowsort
  56. SELECT *, EXISTS(
  57. SELECT * FROM likes WHERE likee = likes.liker
  58. ) FROM likes
  59. ----
  60. alice bob false
  61. alice eve false
  62. bob eve false
  63. statement error more than one record produced in subquery
  64. SELECT peep, (
  65. SELECT likee FROM likes WHERE liker = peep
  66. ) FROM peeps
  67. statement ok
  68. CREATE TABLE favorites (peep text, other text)
  69. statement ok
  70. INSERT INTO favorites VALUES ('alice', 'eve'), ('alice', 'alice'), ('bob', 'bob')
  71. query TTB rowsort
  72. SELECT
  73. liker,
  74. likee,
  75. likee IN (SELECT other FROM favorites WHERE peep = liker) AS is_favorite
  76. FROM likes
  77. ----
  78. alice bob false
  79. bob eve false
  80. alice eve true
  81. query TTB rowsort
  82. SELECT
  83. liker,
  84. likee,
  85. likee NOT IN (SELECT other FROM favorites WHERE peep = liker) AS is_favorite
  86. FROM likes
  87. ----
  88. alice bob true
  89. bob eve true
  90. alice eve false
  91. statement ok
  92. CREATE TABLE age (peep text, age int)
  93. statement ok
  94. INSERT INTO age VALUES ('alice', 103), ('bob', 100), ('eve', 104)
  95. statement error more than one record produced in subquery
  96. SELECT peeps.peep, (
  97. SELECT age FROM likes, age WHERE peeps.peep = liker AND likee = age.peep
  98. ) FROM peeps
  99. query TB rowsort
  100. SELECT peeps.peep, age < ANY (
  101. SELECT age FROM likes, age WHERE peeps.peep = liker AND likee = age.peep
  102. ) FROM peeps, age
  103. WHERE peeps.peep = age.peep
  104. ----
  105. alice true
  106. bob true
  107. eve false
  108. query TB rowsort
  109. SELECT peeps.peep, age < ALL (
  110. SELECT age FROM likes, age WHERE peeps.peep = liker AND likee = age.peep
  111. ) FROM peeps, age
  112. WHERE peeps.peep = age.peep
  113. ----
  114. alice false
  115. bob true
  116. eve true
  117. # ANY/ALL semantics
  118. query BBBBBBBBBBBBBBB
  119. (VALUES (
  120. 1 < ANY(SELECT * FROM (VALUES (1)) WHERE false),
  121. 1 < ANY(VALUES (0)),
  122. 1 < ANY(VALUES (1)),
  123. 1 < ANY(VALUES (2)),
  124. 1 < ANY(VALUES (0), (NULL)),
  125. 1 < ANY(VALUES (1), (NULL)),
  126. 1 < ANY(VALUES (2), (NULL)),
  127. NULL < ANY(SELECT * FROM (VALUES (1)) WHERE false),
  128. NULL < ANY(VALUES (0)),
  129. NULL < ANY(VALUES (1)),
  130. NULL < ANY(VALUES (2)),
  131. NULL < ANY(VALUES (NULL)),
  132. NULL < ANY(VALUES (0), (NULL)),
  133. NULL < ANY(VALUES (1), (NULL)),
  134. NULL < ANY(VALUES (2), (NULL))
  135. ))
  136. ----
  137. false false false true NULL NULL true false NULL NULL NULL NULL NULL NULL NULL
  138. query BBBBBBBBBBBBBBB
  139. (VALUES (
  140. 1 < ALL(SELECT * FROM (VALUES (1)) WHERE false),
  141. 1 < ALL(VALUES (0)),
  142. 1 < ALL(VALUES (1)),
  143. 1 < ALL(VALUES (2)),
  144. 1 < ALL(VALUES (0), (NULL)),
  145. 1 < ALL(VALUES (1), (NULL)),
  146. 1 < ALL(VALUES (2), (NULL)),
  147. NULL < ALL(SELECT * FROM (VALUES (1)) WHERE false),
  148. NULL < ALL(VALUES (0)),
  149. NULL < ALL(VALUES (1)),
  150. NULL < ALL(VALUES (2)),
  151. NULL < ALL(VALUES (NULL)),
  152. NULL < ALL(VALUES (0), (NULL)),
  153. NULL < ALL(VALUES (1), (NULL)),
  154. NULL < ALL(VALUES (2), (NULL))
  155. ))
  156. ----
  157. true false false true false false NULL true NULL NULL NULL NULL NULL NULL NULL
  158. query error db error: ERROR: subquery1 has 0 columns available but 1 columns specified
  159. SELECT 1 < ALL(SELECT * FROM nullary)
  160. query error Expected subselect to return 1 column, got 0 columns
  161. SELECT (SELECT);
  162. ----
  163. query
  164. SELECT * FROM (SELECT);
  165. ----
  166. query error db error: ERROR: subquery1 has 2 columns available but 1 columns specified
  167. SELECT 1 < ALL(SELECT 1, 2)
  168. statement ok
  169. CREATE TABLE s1 (a int NOT NULL)
  170. statement ok
  171. CREATE TABLE s2 (a int NOT NULL)
  172. statement ok
  173. CREATE TABLE s3 (b int NOT NULL)
  174. statement ok
  175. INSERT INTO s1 VALUES (0)
  176. statement ok
  177. INSERT INTO s2 VALUES (1)
  178. statement ok
  179. INSERT INTO s3 VALUES (1)
  180. # Verify that scope resolution prefers the closer scope when a name occurs in
  181. # multiple outer levels.
  182. query B
  183. SELECT true FROM s1 WHERE EXISTS (SELECT true FROM s2 WHERE EXISTS (SELECT true FROM s3 WHERE a = s3.b))
  184. ----
  185. true
  186. statement ok
  187. CREATE TABLE t1 (a int NOT NULL)
  188. statement ok
  189. CREATE TABLE t2 (b int NOT NULL)
  190. statement ok
  191. CREATE TABLE t3 (a int NOT NULL, b int NOT NULL)
  192. statement ok
  193. INSERT INTO t1 VALUES (1), (2), (3)
  194. statement ok
  195. INSERT INTO t2 VALUES (1), (2), (3)
  196. statement ok
  197. INSERT INTO t3 VALUES (2, 3), (1, 4)
  198. # Test a constraint that spans multiple levels of nesting.
  199. query I
  200. SELECT a FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE EXISTS (SELECT 1 FROM t3 WHERE t1.a = t3.a AND t2.b = t3.b))
  201. ----
  202. 2
  203. mode standard
  204. # Verify that the plans for some simple non-correlated subqueries are sane.
  205. query T multiline
  206. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2)
  207. ----
  208. Explained Query:
  209. CrossJoin type=differential // { arity: 1 }
  210. implementation
  211. %1[×]UA » %0:t1[×]
  212. ArrangeBy keys=[[]] // { arity: 1 }
  213. ReadStorage materialize.public.t1 // { arity: 1 }
  214. ArrangeBy keys=[[]] // { arity: 0 }
  215. Distinct project=[] // { arity: 0 }
  216. Project () // { arity: 0 }
  217. ReadStorage materialize.public.t2 // { arity: 1 }
  218. Source materialize.public.t1
  219. Source materialize.public.t2
  220. Target cluster: quickstart
  221. EOF
  222. query T multiline
  223. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1, t3 WHERE t1.a = t3.a AND EXISTS (SELECT * FROM t2)
  224. ----
  225. Explained Query:
  226. Project (#0{a}, #0{a}, #2{b}) // { arity: 3 }
  227. Join on=(#0{a} = #1{a}) type=delta // { arity: 3 }
  228. implementation
  229. %0:t1 » %2[×]UA » %1:t3[#0{a}]K
  230. %1:t3 » %2[×]UA » %0:t1[#0{a}]K
  231. %2 » %0:t1[×] » %1:t3[#0{a}]K
  232. ArrangeBy keys=[[], [#0{a}]] // { arity: 1 }
  233. ReadStorage materialize.public.t1 // { arity: 1 }
  234. ArrangeBy keys=[[#0{a}]] // { arity: 2 }
  235. ReadStorage materialize.public.t3 // { arity: 2 }
  236. ArrangeBy keys=[[]] // { arity: 0 }
  237. Distinct project=[] // { arity: 0 }
  238. Project () // { arity: 0 }
  239. ReadStorage materialize.public.t2 // { arity: 1 }
  240. Source materialize.public.t1
  241. Source materialize.public.t2
  242. Source materialize.public.t3
  243. Target cluster: quickstart
  244. EOF
  245. query T multiline
  246. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1, t3 WHERE t1.a = t3.a AND EXISTS (SELECT * FROM t2 WHERE t3.b = t2.b)
  247. ----
  248. Explained Query:
  249. Project (#0{a}, #0{a}, #2{b}) // { arity: 3 }
  250. Join on=(#0{a} = #1{a} AND #2{b} = #3{b}) type=delta // { arity: 4 }
  251. implementation
  252. %0:t1 » %1:t3[#0{a}]K » %2[#0]UKA
  253. %1:t3 » %2[#0]UKA » %0:t1[#0{a}]K
  254. %2 » %1:t3[#1]K » %0:t1[#0{a}]K
  255. ArrangeBy keys=[[#0{a}]] // { arity: 1 }
  256. ReadStorage materialize.public.t1 // { arity: 1 }
  257. ArrangeBy keys=[[#0{a}], [#1{b}]] // { arity: 2 }
  258. ReadStorage materialize.public.t3 // { arity: 2 }
  259. ArrangeBy keys=[[#0{b}]] // { arity: 1 }
  260. Distinct project=[#0{b}] // { arity: 1 }
  261. ReadStorage materialize.public.t2 // { arity: 1 }
  262. Source materialize.public.t1
  263. Source materialize.public.t2
  264. Source materialize.public.t3
  265. Target cluster: quickstart
  266. EOF
  267. # Regression test for database-issues#396
  268. # The following subquery currently generates a plan with a map with
  269. # 4 scalars that refer to other scalars in the map. If query planning optimizes away
  270. # this particular case, replace with another query that generates such a plan
  271. query T multiline
  272. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  273. SELECT age, ascii_num * 2 as result FROM (
  274. SELECT age, ascii(letter) AS ascii_num FROM (
  275. SELECT age, substr(replaced, 2, 1) AS letter FROM (
  276. SELECT age, replace(likee, 'o', 'i') AS replaced FROM (
  277. SELECT likee, age FROM likes, age WHERE liker=peep
  278. )
  279. )
  280. )
  281. )
  282. ----
  283. Explained Query:
  284. Project (#3{age}, #4) // { arity: 2 }
  285. Map ((ascii(substr(replace(#1{likee}, "o", "i"), 2, 1)) * 2)) // { arity: 5 }
  286. Join on=(#0{liker} = #2{peep}) type=differential // { arity: 4 }
  287. implementation
  288. %0:likes[#0{liker}]K » %1:age[#0{peep}]K
  289. ArrangeBy keys=[[#0{liker}]] // { arity: 2 }
  290. Filter (#0{liker}) IS NOT NULL // { arity: 2 }
  291. ReadStorage materialize.public.likes // { arity: 2 }
  292. ArrangeBy keys=[[#0{peep}]] // { arity: 2 }
  293. Filter (#0{peep}) IS NOT NULL // { arity: 2 }
  294. ReadStorage materialize.public.age // { arity: 2 }
  295. Source materialize.public.likes
  296. filter=((#0{liker}) IS NOT NULL)
  297. Source materialize.public.age
  298. filter=((#0{peep}) IS NOT NULL)
  299. Target cluster: quickstart
  300. EOF
  301. mode cockroach
  302. query II rowsort
  303. SELECT age, ascii_num * 2 as result FROM (
  304. SELECT age, ascii(letter) AS ascii_num FROM (
  305. SELECT age, substr(replaced, 2, 1) AS letter FROM (
  306. SELECT age, replace(likee, 'o', 'i') AS replaced FROM (
  307. SELECT likee, age FROM likes, age WHERE liker=peep
  308. )
  309. )
  310. )
  311. )
  312. ----
  313. 100 236
  314. 103 210
  315. 103 236
  316. # regression test for https://github.com/MaterializeInc/database-issues/issues/621
  317. statement ok
  318. CREATE TABLE tab0(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col3 INTEGER)
  319. statement ok
  320. INSERT INTO tab0 VALUES(0,1,2,63)
  321. statement ok
  322. INSERT INTO tab0 VALUES(87,63,3,4)
  323. query I rowsort
  324. SELECT pk FROM tab0 WHERE
  325. (
  326. col3 IN (
  327. SELECT col0 FROM tab0
  328. )
  329. )
  330. ----
  331. 0
  332. query I rowsort
  333. SELECT pk FROM tab0 WHERE
  334. (
  335. (
  336. col0 IN (
  337. SELECT col3 FROM tab0 WHERE col3 IN (
  338. SELECT col0 FROM tab0
  339. )
  340. )
  341. )
  342. )
  343. ----
  344. 87
  345. query I rowsort
  346. SELECT pk FROM tab0 WHERE
  347. (
  348. col3 IN (
  349. SELECT col0 FROM tab0
  350. )
  351. OR
  352. (
  353. col0 IN (
  354. SELECT col3 FROM tab0 WHERE col3 IN (
  355. SELECT col0 FROM tab0
  356. )
  357. )
  358. )
  359. )
  360. ----
  361. 0
  362. 87
  363. query I
  364. SELECT col FROM (SELECT 1 AS col) t WHERE col = ANY(VALUES (1))
  365. ----
  366. 1
  367. query I
  368. SELECT col FROM (SELECT 1 AS col) t WHERE col = ANY((VALUES (1)))
  369. ----
  370. 1
  371. query I
  372. SELECT col FROM (SELECT 1 AS col) t WHERE col IN(VALUES(1))
  373. ----
  374. 1
  375. query I
  376. SELECT col FROM (SELECT 1 AS col) t WHERE col IN(((VALUES(1))))
  377. ----
  378. 1
  379. statement ok
  380. CREATE TABLE x (a int not null)
  381. statement ok
  382. CREATE TABLE y (b int not null)
  383. statement ok
  384. INSERT INTO x VALUES (1), (2), (3);
  385. statement ok
  386. INSERT INTO y VALUES (2), (3), (4);
  387. query B
  388. SELECT b != ALL(SELECT a FROM x) FROM y
  389. ----
  390. false
  391. false
  392. true
  393. query T multiline
  394. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT b IN (SELECT a FROM x) FROM y
  395. ----
  396. Explained Query:
  397. With
  398. cte l0 =
  399. Distinct project=[#0{b}] // { arity: 1 }
  400. ReadStorage materialize.public.y // { arity: 1 }
  401. cte l1 =
  402. Project (#0{b}) // { arity: 1 }
  403. Join on=(#0{b} = #1{a}) type=differential // { arity: 2 }
  404. implementation
  405. %0:l0[#0]UKA » %1[#0]UKA
  406. ArrangeBy keys=[[#0{b}]] // { arity: 1 }
  407. Get l0 // { arity: 1 }
  408. ArrangeBy keys=[[#0{a}]] // { arity: 1 }
  409. Distinct project=[#0{a}] // { arity: 1 }
  410. ReadStorage materialize.public.x // { arity: 1 }
  411. Return // { arity: 1 }
  412. Project (#2) // { arity: 1 }
  413. Join on=(#0{b} = #1{b}) type=differential // { arity: 3 }
  414. implementation
  415. %0:y[#0]K » %1[#0]K
  416. ArrangeBy keys=[[#0{b}]] // { arity: 1 }
  417. ReadStorage materialize.public.y // { arity: 1 }
  418. ArrangeBy keys=[[#0{b}]] // { arity: 2 }
  419. Union // { arity: 2 }
  420. Map (true) // { arity: 2 }
  421. Get l1 // { arity: 1 }
  422. Map (false) // { arity: 2 }
  423. Union // { arity: 1 }
  424. Negate // { arity: 1 }
  425. Get l1 // { arity: 1 }
  426. Get l0 // { arity: 1 }
  427. Source materialize.public.x
  428. Source materialize.public.y
  429. Target cluster: quickstart
  430. EOF
  431. query T multiline
  432. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT b != ALL(SELECT a FROM x) FROM y
  433. ----
  434. Explained Query:
  435. With
  436. cte l0 =
  437. Distinct project=[#0{b}] // { arity: 1 }
  438. ReadStorage materialize.public.y // { arity: 1 }
  439. cte l1 =
  440. Project (#0{b}) // { arity: 1 }
  441. Join on=(#0{b} = #1{a}) type=differential // { arity: 2 }
  442. implementation
  443. %0:l0[#0]UKA » %1[#0]UKA
  444. ArrangeBy keys=[[#0{b}]] // { arity: 1 }
  445. Get l0 // { arity: 1 }
  446. ArrangeBy keys=[[#0{a}]] // { arity: 1 }
  447. Distinct project=[#0{a}] // { arity: 1 }
  448. ReadStorage materialize.public.x // { arity: 1 }
  449. Return // { arity: 1 }
  450. Project (#3) // { arity: 1 }
  451. Map (NOT(#2)) // { arity: 4 }
  452. Join on=(#0{b} = #1{b}) type=differential // { arity: 3 }
  453. implementation
  454. %0:y[#0]K » %1[#0]K
  455. ArrangeBy keys=[[#0{b}]] // { arity: 1 }
  456. ReadStorage materialize.public.y // { arity: 1 }
  457. ArrangeBy keys=[[#0{b}]] // { arity: 2 }
  458. Union // { arity: 2 }
  459. Map (true) // { arity: 2 }
  460. Get l1 // { arity: 1 }
  461. Map (false) // { arity: 2 }
  462. Union // { arity: 1 }
  463. Negate // { arity: 1 }
  464. Get l1 // { arity: 1 }
  465. Get l0 // { arity: 1 }
  466. Source materialize.public.x
  467. Source materialize.public.y
  468. Target cluster: quickstart
  469. EOF
  470. # Check that we correctly invert the filter here via De Morgan.
  471. query T multiline
  472. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT b > ALL(SELECT a FROM x) FROM y
  473. ----
  474. Explained Query:
  475. With
  476. cte l0 =
  477. Distinct project=[#0{b}] // { arity: 1 }
  478. ReadStorage materialize.public.y // { arity: 1 }
  479. cte l1 =
  480. Distinct project=[#0{b}] // { arity: 1 }
  481. Project (#0{b}) // { arity: 1 }
  482. Filter (#0{b} <= #1{a}) // { arity: 2 }
  483. CrossJoin type=differential // { arity: 2 }
  484. implementation
  485. %0:l0[×] » %1:x[×]
  486. ArrangeBy keys=[[]] // { arity: 1 }
  487. Get l0 // { arity: 1 }
  488. ArrangeBy keys=[[]] // { arity: 1 }
  489. ReadStorage materialize.public.x // { arity: 1 }
  490. Return // { arity: 1 }
  491. Project (#3) // { arity: 1 }
  492. Map (NOT(#2)) // { arity: 4 }
  493. Join on=(#0{b} = #1{b}) type=differential // { arity: 3 }
  494. implementation
  495. %0:y[#0]K » %1[#0]K
  496. ArrangeBy keys=[[#0{b}]] // { arity: 1 }
  497. ReadStorage materialize.public.y // { arity: 1 }
  498. ArrangeBy keys=[[#0{b}]] // { arity: 2 }
  499. Union // { arity: 2 }
  500. Map (true) // { arity: 2 }
  501. Get l1 // { arity: 1 }
  502. Map (false) // { arity: 2 }
  503. Union // { arity: 1 }
  504. Negate // { arity: 1 }
  505. Get l1 // { arity: 1 }
  506. Get l0 // { arity: 1 }
  507. Source materialize.public.x
  508. Source materialize.public.y
  509. Target cluster: quickstart
  510. EOF
  511. statement ok
  512. CREATE TABLE xs (x int not null)
  513. statement ok
  514. CREATE TABLE ys (y int not null)
  515. statement ok
  516. INSERT INTO xs VALUES (1), (1), (2);
  517. statement ok
  518. INSERT INTO ys VALUES (0), (1), (2);
  519. # Check that we correctly perform subqueries that do not preserve
  520. # multiplicity of the outer relation.
  521. query II rowsort
  522. select x, (select count(*) from ys where y < x) from xs
  523. ----
  524. 1 1
  525. 1 1
  526. 2 2
  527. # Tests that conditional subqueries should not error.
  528. query RR rowsort
  529. select y, (
  530. case when (select count(*) from xs where x < y) != 0
  531. then (select 1.0 / count(*) from xs where x < y)
  532. else (select 1.0 / (count(*) - 1) from xs where x < y)
  533. end
  534. )
  535. from ys
  536. ----
  537. 0 -1
  538. 1 -1
  539. 2 0.5
  540. # Regression test for database-issues#1221, in which quantified comparision simplification was
  541. # not correctly handling LATERAL joins.
  542. query II
  543. SELECT * FROM (VALUES (1)), LATERAL (SELECT * FROM (SELECT column1) WHERE true)
  544. ----
  545. 1 1
  546. # Regression test for database-issues#1222, in which the query planner mishandled the outer
  547. # scope of a nested LATERAL join.
  548. query IIII
  549. SELECT * FROM
  550. (SELECT 1, 1 AS col2),
  551. LATERAL (SELECT * FROM (SELECT col2) LEFT JOIN LATERAL (SELECT col2) ON true)
  552. ----
  553. 1 1 1 1
  554. # Regression test for database-issues#1287, in which quantified expression simplification
  555. # failed to handle map expressions which depended upon a column introduced by
  556. # an earlier expression in the same map node.
  557. query I
  558. SELECT (SELECT 1 FROM ((SELECT col1) UNION (SELECT 1)))
  559. FROM (SELECT 1 col1)
  560. ----
  561. 1
  562. query error aggregate functions that refer exclusively to outer columns not yet supported
  563. SELECT (SELECT count(likes.likee)) FROM likes
  564. # Regression test for database-issues#2209, where the multiplicity of the outer query was not
  565. # preserved when an EXISTS subquery only involves constants/mpf/flatmaps.
  566. query I
  567. SELECT x FROM xs WHERE EXISTS (SELECT y FROM (SELECT 1 as y) WHERE x = y)
  568. ----
  569. 1
  570. 1
  571. # Regression test for database-issues#2994, in which the presence of a SELECT subquery inside
  572. # of another subexpression produced a degenerate join that was not elided before
  573. # join implementation planning.
  574. query I
  575. SELECT 1 FROM x RIGHT JOIN (SELECT 2 FROM y) ON NULL
  576. WHERE a IN (9, 0)
  577. OR 0 < ALL (
  578. SELECT agg1 NULL
  579. FROM (SELECT MAX(3) agg1 FROM (SELECT FROM x)
  580. GROUP BY TRUE)
  581. )
  582. GROUP BY TRUE;
  583. ----
  584. 1
  585. query T multiline
  586. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT 1 FROM x RIGHT JOIN (SELECT 2 FROM y) ON NULL
  587. WHERE a IN (9, 0)
  588. OR 0 < ALL (
  589. SELECT agg1 NULL
  590. FROM (SELECT MAX(3) agg1 FROM (SELECT FROM x)
  591. GROUP BY TRUE)
  592. )
  593. GROUP BY TRUE;
  594. ----
  595. Explained Query:
  596. Map (1) // { arity: 1 }
  597. Distinct project=[] // { arity: 0 }
  598. Project () // { arity: 0 }
  599. ReadStorage materialize.public.y // { arity: 1 }
  600. Source materialize.public.x
  601. Source materialize.public.y
  602. Target cluster: quickstart
  603. EOF