subquery_correlated.slt 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/subquery_correlated
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. mode cockroach
  20. simple conn=mz_system,user=mz_system
  21. ALTER SYSTEM SET unsafe_enable_table_keys = true
  22. ----
  23. COMPLETE 0
  24. simple conn=mz_system,user=mz_system
  25. ALTER SYSTEM SET unsafe_enable_table_foreign_key = true
  26. ----
  27. COMPLETE 0
  28. # ------------------------------------------------------------------------------
  29. # Create a simple schema that models customers and orders. Each customer has an
  30. # id (c_id), and has zero or more orders that are related via a foreign key of
  31. # the same name. A customer has a billing state and an order has a shipping
  32. # state, either of which could be NULL. This schema, while simple, is rich
  33. # enough to provide many interesting correlated subquery variations.
  34. # ------------------------------------------------------------------------------
  35. statement ok
  36. CREATE TABLE c (c_id INT PRIMARY KEY, bill TEXT)
  37. statement ok
  38. CREATE TABLE o (o_id INT PRIMARY KEY, c_id INT, ship TEXT)
  39. statement ok
  40. INSERT INTO c VALUES
  41. (1, 'CA'),
  42. (2, 'TX'),
  43. (3, 'MA'),
  44. (4, 'TX'),
  45. (5, NULL),
  46. (6, 'FL')
  47. statement ok
  48. INSERT INTO o VALUES
  49. (10, 1, 'CA'), (20, 1, 'CA'), (30, 1, 'CA'),
  50. (40, 2, 'CA'), (50, 2, 'TX'), (60, 2, NULL),
  51. (70, 4, 'WY'), (80, 4, NULL),
  52. (90, 6, 'WA')
  53. # ------------------------------------------------------------------------------
  54. # Subqueries in select filters.
  55. # ------------------------------------------------------------------------------
  56. # Customers with orders.
  57. query IT rowsort
  58. SELECT * FROM c WHERE EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id);
  59. ----
  60. 1 CA
  61. 2 TX
  62. 4 TX
  63. 6 FL
  64. # Customers with no orders.
  65. query IT rowsort
  66. SELECT * FROM c WHERE NOT EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id);
  67. ----
  68. 3 MA
  69. 5 NULL
  70. # Customers with orders or with no orders (should return all customers).
  71. query IT rowsort
  72. SELECT *
  73. FROM c
  74. WHERE
  75. EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id)
  76. OR NOT EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id);
  77. ----
  78. 1 CA
  79. 2 TX
  80. 3 MA
  81. 4 TX
  82. 5 NULL
  83. 6 FL
  84. # Customers with billing address in TX that have orders.
  85. query IT rowsort
  86. SELECT * FROM c WHERE EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND c.bill='TX');
  87. ----
  88. 2 TX
  89. 4 TX
  90. # Customers that have at least one order shipped to WY.
  91. query IT rowsort
  92. SELECT * FROM c WHERE 'WY' IN (SELECT ship FROM o WHERE o.c_id=c.c_id);
  93. ----
  94. 4 TX
  95. # Customers that have at least one order shipped to WY or to WA.
  96. query IT rowsort
  97. SELECT *
  98. FROM c
  99. WHERE
  100. 'WY' IN (SELECT ship FROM o WHERE o.c_id=c.c_id)
  101. OR 'WA' IN (SELECT ship FROM o WHERE o.c_id=c.c_id);
  102. ----
  103. 4 TX
  104. 6 FL
  105. # Customers that have at least one order shipped to CA, but none to TX.
  106. query IT rowsort
  107. SELECT *
  108. FROM c
  109. WHERE
  110. 'CA' IN (SELECT ship FROM o WHERE o.c_id=c.c_id)
  111. AND 'TX' NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id);
  112. ----
  113. 1 CA
  114. # Customers with at least one order with billing addr = shipping addr.
  115. query IT rowsort
  116. SELECT * FROM c WHERE bill IN (SELECT ship FROM o WHERE o.c_id=c.c_id);
  117. ----
  118. 1 CA
  119. 2 TX
  120. # Customers with all orders with billing addr = shipping addr.
  121. query IT rowsort
  122. SELECT * FROM c WHERE bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id);
  123. ----
  124. 1 CA
  125. 3 MA
  126. 5 NULL
  127. # Customers with no order with billing addr = shipping addr (with NULL ship).
  128. query IT rowsort
  129. SELECT * FROM c WHERE bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id);
  130. ----
  131. 3 MA
  132. 5 NULL
  133. 6 FL
  134. # Customers with no order with billing addr = shipping addr (no NULL ship).
  135. query IT rowsort
  136. SELECT * FROM c WHERE bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL);
  137. ----
  138. 3 MA
  139. 4 TX
  140. 5 NULL
  141. 6 FL
  142. # Customers with no order with billing addr = shipping addr (only NULL ship).
  143. query IT rowsort
  144. SELECT * FROM c WHERE bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NULL);
  145. ----
  146. 1 CA
  147. 3 MA
  148. 5 NULL
  149. 6 FL
  150. # Customers with bill state < any ship state.
  151. query IT rowsort
  152. SELECT * FROM c WHERE bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id);
  153. ----
  154. 4 TX
  155. 6 FL
  156. # Customers where bill state < any ship state is null result. This prevents
  157. # normalizing ANY into EXISTS.
  158. query IT rowsort
  159. SELECT * FROM c WHERE (bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL;
  160. ----
  161. 2 TX
  162. # Customers where bill state < any ship state is not null result. This prevents
  163. # normalizing ANY into EXISTS.
  164. query IT rowsort
  165. SELECT * FROM c WHERE (bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL;
  166. ----
  167. 1 CA
  168. 3 MA
  169. 4 TX
  170. 5 NULL
  171. 6 FL
  172. # Customers with bill state > any ship state.
  173. query IT rowsort
  174. SELECT * FROM c WHERE bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id);
  175. ----
  176. 2 TX
  177. # Customers where bill state > any ship state is null result. This prevents
  178. # normalizing ANY into EXISTS.
  179. query IT rowsort
  180. SELECT * FROM c WHERE (bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL;
  181. ----
  182. 4 TX
  183. # Customers where bill state > any ship state is not null result. This prevents
  184. # normalizing ANY into EXISTS.
  185. query IT rowsort
  186. SELECT * FROM c WHERE (bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL;
  187. ----
  188. 1 CA
  189. 2 TX
  190. 3 MA
  191. 5 NULL
  192. 6 FL
  193. # Customers where bill state matches any ship state.
  194. query IT rowsort
  195. SELECT * FROM c WHERE bill = ANY(SELECT ship FROM o);
  196. ----
  197. 1 CA
  198. 2 TX
  199. 4 TX
  200. # Customers where bill state matches any ship state or is null.
  201. query IT rowsort
  202. SELECT * FROM c WHERE bill = ANY(SELECT ship FROM o) OR bill IS NULL;
  203. ----
  204. 1 CA
  205. 2 TX
  206. 4 TX
  207. 5 NULL
  208. # Test NULL IN case. Use IS NOT NULL to prevent normalize ANY into EXISTS.
  209. query IT rowsort
  210. SELECT * FROM c WHERE (NULL::text IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL;
  211. ----
  212. 3 MA
  213. 5 NULL
  214. # Test NULL NOT IN case. Use IS NOT NULL to prevent normalize ANY into EXISTS.
  215. query IT rowsort
  216. SELECT * FROM c WHERE (NULL::text NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL;
  217. ----
  218. 3 MA
  219. 5 NULL
  220. # Customers where it is unknown whether a replaced bill state is one of the ship
  221. # states. This tests a more complex scalar expression as argument to IN.
  222. query IT rowsort
  223. SELECT * FROM c WHERE (replace(bill, 'TX', 'WY') IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL;
  224. ----
  225. 2 TX
  226. # Customers with all orders with billing addr = shipping addr, or with at least
  227. # one order shipped to WY.
  228. query IT rowsort
  229. SELECT *
  230. FROM c
  231. WHERE
  232. bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id)
  233. OR EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND ship='WY');
  234. ----
  235. 1 CA
  236. 3 MA
  237. 4 TX
  238. 5 NULL
  239. # Customers with all orders with billing addr = shipping addr, but with at least
  240. # one order.
  241. query IT rowsort
  242. SELECT *
  243. FROM c
  244. WHERE
  245. bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id)
  246. AND EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id);
  247. ----
  248. 1 CA
  249. # Customers with more than one order.
  250. query IT rowsort
  251. SELECT * FROM c WHERE (SELECT count(*) FROM o WHERE o.c_id=c.c_id) > 1;
  252. ----
  253. 1 CA
  254. 2 TX
  255. 4 TX
  256. # Customers with more than one order shipped to a known state (i.e. NOT NULL).
  257. query IT rowsort
  258. SELECT * FROM c WHERE (SELECT count(ship) FROM o WHERE o.c_id=c.c_id) > 1;
  259. ----
  260. 1 CA
  261. 2 TX
  262. # For each customer, orders shipped to lowest state (alphabetically).
  263. query IIT rowsort
  264. SELECT c.c_id, o.o_id, o.ship
  265. FROM c
  266. INNER JOIN o
  267. ON c.c_id=o.c_id AND o.ship = (SELECT min(o.ship) FROM o WHERE o.c_id=c.c_id);
  268. ----
  269. 1 20 CA
  270. 2 40 CA
  271. 4 70 WY
  272. 1 10 CA
  273. 6 90 WA
  274. 1 30 CA
  275. # Customers who have shipped more orders to a particular state than all other
  276. # customers have shipped to that state, combined.
  277. query ITI rowsort
  278. SELECT c.c_id, o.ship, count(*)
  279. FROM c
  280. INNER JOIN o
  281. ON c.c_id=o.c_id
  282. WHERE
  283. (SELECT count(*) FROM o AS o2 WHERE o2.ship = o.ship AND o2.c_id = o.c_id) >
  284. (SELECT count(*) FROM o AS o2 WHERE o2.ship = o.ship AND o2.c_id <> o.c_id)
  285. GROUP BY c.c_id, o.ship;
  286. ----
  287. 1 CA 3
  288. 2 TX 1
  289. 4 WY 1
  290. 6 WA 1
  291. # Customers with more than one order and with the highest state = 'CA'.
  292. query IT rowsort
  293. SELECT *
  294. FROM c
  295. WHERE
  296. (SELECT count(*) FROM o WHERE o.c_id=c.c_id) > 1
  297. AND (SELECT max(ship) FROM o WHERE o.c_id=c.c_id) = 'CA';
  298. ----
  299. 1 CA
  300. # Customers with more than one order or with an unknown ship state.
  301. query IT rowsort
  302. SELECT *
  303. FROM c
  304. WHERE
  305. (SELECT count(*) FROM o WHERE o.c_id=c.c_id) > 1
  306. OR EXISTS(SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NULL);
  307. ----
  308. 1 CA
  309. 2 TX
  310. 4 TX
  311. # Customers that have a bill state equal to the max ship state of all their
  312. # orders (alphabetically).
  313. query IT rowsort
  314. SELECT c_id, bill
  315. FROM c AS c2
  316. WHERE EXISTS
  317. (
  318. SELECT * FROM c WHERE bill=(SELECT max(ship) FROM o WHERE c_id=c2.c_id AND c_id=c.c_id)
  319. )
  320. ----
  321. 1 CA
  322. 2 TX
  323. # Customers that have at least one order shipped to their billing state (or if
  324. # the ship state is null).
  325. query IT rowsort
  326. SELECT c_id, bill
  327. FROM c AS c2
  328. WHERE EXISTS
  329. (
  330. SELECT *
  331. FROM (SELECT c_id, coalesce(ship, bill) AS state FROM o WHERE c_id=c2.c_id) AS o
  332. WHERE state=bill
  333. )
  334. ----
  335. 1 CA
  336. 2 TX
  337. 4 TX
  338. query II rowsort
  339. SELECT c_id, generate_series(1, (SELECT count(*) FROM o WHERE o.c_id=c.c_id)) FROM c
  340. ----
  341. 1 1
  342. 1 2
  343. 1 3
  344. 2 1
  345. 2 2
  346. 2 3
  347. 4 1
  348. 4 2
  349. 6 1
  350. # Customers that have no orders with a NULL ship state.
  351. # Note: Result differs from Cockroach but matches Postgres.
  352. query IT rowsort
  353. SELECT *
  354. FROM c
  355. WHERE (SELECT ship FROM o WHERE o.c_id=c.c_id ORDER BY ship LIMIT 1) IS NOT NULL
  356. ----
  357. 1 CA
  358. 2 TX
  359. 4 TX
  360. 6 FL
  361. # Customers that have first order shipping to 'CA' or 'WY' (no NULL ship).
  362. query IT
  363. SELECT *
  364. FROM c
  365. WHERE
  366. (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL ORDER BY ship LIMIT 1)='CA'
  367. OR (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL ORDER BY ship LIMIT 1)='WY'
  368. ORDER BY c_id
  369. ----
  370. 1 CA
  371. 2 TX
  372. 4 TX
  373. # Apply
  374. query IT rowsort
  375. SELECT *
  376. FROM c
  377. WHERE (SELECT o_id FROM o WHERE o.c_id=c.c_id AND ship='WY')=4;
  378. ----
  379. # ------------------------------------------------------------------------------
  380. # Subqueries in projection lists.
  381. # Although the queries are similar to those above, they are often compiled
  382. # differently in the context of a projection list, due to different null
  383. # result handling rules.
  384. # ------------------------------------------------------------------------------
  385. # Customers with orders.
  386. query IB
  387. SELECT c_id, EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id;
  388. ----
  389. 1 true
  390. 2 true
  391. 3 false
  392. 4 true
  393. 5 false
  394. 6 true
  395. # Customers with no orders.
  396. query IB
  397. SELECT c_id, NOT EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id;
  398. ----
  399. 1 false
  400. 2 false
  401. 3 true
  402. 4 false
  403. 5 true
  404. 6 false
  405. # Customers with orders or with no orders (should be all customers).
  406. query IB
  407. SELECT
  408. c_id,
  409. EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id)
  410. OR NOT EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id)
  411. FROM c
  412. ORDER BY c_id;
  413. ----
  414. 1 true
  415. 2 true
  416. 3 true
  417. 4 true
  418. 5 true
  419. 6 true
  420. # Customers with billing address in TX that have orders.
  421. query IB
  422. SELECT c_id, EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND c.bill='TX') FROM c ORDER BY c_id;
  423. ----
  424. 1 false
  425. 2 true
  426. 3 false
  427. 4 true
  428. 5 false
  429. 6 false
  430. # Customers that have at least one order shipped to WY.
  431. query IB
  432. SELECT c_id, 'WY' IN (SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id;
  433. ----
  434. 1 false
  435. 2 NULL
  436. 3 false
  437. 4 true
  438. 5 false
  439. 6 false
  440. # Customers that have at least one order shipped to WY or to WA.
  441. query IB
  442. SELECT
  443. c_id,
  444. 'WY' IN (SELECT ship FROM o WHERE o.c_id=c.c_id)
  445. OR 'WA' IN (SELECT ship FROM o WHERE o.c_id=c.c_id)
  446. FROM c
  447. ORDER BY c_id;
  448. ----
  449. 1 false
  450. 2 NULL
  451. 3 false
  452. 4 true
  453. 5 false
  454. 6 true
  455. # Customers that have at least one order shipped to CA, but none to TX.
  456. query IB
  457. SELECT
  458. c_id,
  459. 'CA' IN (SELECT ship FROM o WHERE o.c_id=c.c_id)
  460. AND 'TX' NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id)
  461. FROM c
  462. ORDER BY c_id;
  463. ----
  464. 1 true
  465. 2 false
  466. 3 false
  467. 4 NULL
  468. 5 false
  469. 6 false
  470. # Customers with at least one order with billing addr = shipping addr.
  471. query IB
  472. SELECT c_id, bill IN (SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id;
  473. ----
  474. 1 true
  475. 2 true
  476. 3 false
  477. 4 NULL
  478. 5 false
  479. 6 false
  480. # Customers with all orders with billing addr = shipping addr.
  481. query IB
  482. SELECT c_id, bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id;
  483. ----
  484. 1 true
  485. 2 false
  486. 3 true
  487. 4 false
  488. 5 true
  489. 6 false
  490. # Customers with no order with billing addr = shipping addr (with NULL ship).
  491. query IB
  492. SELECT c_id, bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id;
  493. ----
  494. 1 false
  495. 2 false
  496. 3 true
  497. 4 NULL
  498. 5 true
  499. 6 true
  500. # Customers with no order with billing addr = shipping addr (no NULL ship).
  501. query IB
  502. SELECT c_id, bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL)
  503. FROM c
  504. ORDER BY c_id;
  505. ----
  506. 1 false
  507. 2 false
  508. 3 true
  509. 4 true
  510. 5 true
  511. 6 true
  512. # Customers with no order with billing addr = shipping addr (only NULL ship).
  513. query IB
  514. SELECT c_id, bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NULL)
  515. FROM c
  516. ORDER BY c_id;
  517. ----
  518. 1 true
  519. 2 NULL
  520. 3 true
  521. 4 NULL
  522. 5 true
  523. 6 true
  524. # Customers with bill state < any ship state.
  525. query IB
  526. SELECT c_id, bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id;
  527. ----
  528. 1 false
  529. 2 NULL
  530. 3 false
  531. 4 true
  532. 5 false
  533. 6 true
  534. # Customers where bill state < any ship state is null result.
  535. query IB
  536. SELECT c_id, (bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL FROM c ORDER BY c_id;
  537. ----
  538. 1 false
  539. 2 true
  540. 3 false
  541. 4 false
  542. 5 false
  543. 6 false
  544. # Customers where bill state < any ship state is not null result.
  545. query IB
  546. SELECT c_id, (bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL FROM c ORDER BY c_id;
  547. ----
  548. 1 true
  549. 2 false
  550. 3 true
  551. 4 true
  552. 5 true
  553. 6 true
  554. # Customers with bill state > any ship state.
  555. query IB
  556. SELECT c_id, bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id;
  557. ----
  558. 1 false
  559. 2 true
  560. 3 false
  561. 4 NULL
  562. 5 false
  563. 6 false
  564. # Customers where bill state > any ship state is null result.
  565. query IB
  566. SELECT c_id, (bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL FROM c ORDER BY c_id;
  567. ----
  568. 1 false
  569. 2 false
  570. 3 false
  571. 4 true
  572. 5 false
  573. 6 false
  574. # Customers where bill state > any ship state is not null result.
  575. query IB
  576. SELECT c_id, (bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL FROM c ORDER BY c_id;
  577. ----
  578. 1 true
  579. 2 true
  580. 3 true
  581. 4 false
  582. 5 true
  583. 6 true
  584. # Customers where bill state matches any non-null ship state.
  585. query IB rowsort
  586. SELECT c_id, bill = ANY(SELECT ship FROM o WHERE ship IS NOT NULL) FROM c;
  587. ----
  588. 1 true
  589. 2 true
  590. 3 false
  591. 4 true
  592. 5 NULL
  593. 6 false
  594. # Customers where bill state matches any non-null ship state or is null.
  595. query IB rowsort
  596. SELECT c_id, bill = ANY(SELECT ship FROM o WHERE ship IS NOT NULL) OR bill IS NULL FROM c;
  597. ----
  598. 1 true
  599. 2 true
  600. 3 false
  601. 4 true
  602. 5 true
  603. 6 false
  604. # Test NULL IN case.
  605. query IB
  606. SELECT c_id, (NULL::text IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL
  607. FROM c
  608. ORDER BY c_id;
  609. ----
  610. 1 false
  611. 2 false
  612. 3 true
  613. 4 false
  614. 5 true
  615. 6 false
  616. # Test NULL NOT IN case. Use IS NOT NULL to prevent normalize ANY into EXISTS.
  617. query IB
  618. SELECT c_id, (NULL::text NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL
  619. FROM c
  620. ORDER BY c_id;
  621. ----
  622. 1 false
  623. 2 false
  624. 3 true
  625. 4 false
  626. 5 true
  627. 6 false
  628. # Customers where it is unknown whether a replaced bill state is one of the ship
  629. # states. This tests a more complex scalar expression as argument to IN.
  630. query IB
  631. SELECT c_id, (replace(bill, 'TX', 'WY') IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL
  632. FROM c
  633. ORDER BY c_id;
  634. ----
  635. 1 false
  636. 2 true
  637. 3 false
  638. 4 false
  639. 5 false
  640. 6 false
  641. # Customers with all orders with billing addr = shipping addr, or with at least
  642. # one order shipped to WY.
  643. query IB
  644. SELECT
  645. c_id,
  646. bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id)
  647. OR EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND ship='WY')
  648. FROM c
  649. ORDER BY c_id;
  650. ----
  651. 1 true
  652. 2 false
  653. 3 true
  654. 4 true
  655. 5 true
  656. 6 false
  657. # Customers with all orders with billing addr = shipping addr, but with at least
  658. # one order.
  659. query IB
  660. SELECT
  661. c_id,
  662. bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id)
  663. AND EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id)
  664. FROM c
  665. ORDER BY c_id;
  666. ----
  667. 1 true
  668. 2 false
  669. 3 false
  670. 4 false
  671. 5 false
  672. 6 false
  673. # Apply.
  674. query IT rowsort
  675. SELECT *
  676. FROM c
  677. WHERE (SELECT min(ship) FROM o WHERE o.c_id=c.c_id) IN (SELECT ship FROM o WHERE o.c_id=c.c_id);
  678. ----
  679. 1 CA
  680. 2 TX
  681. 4 TX
  682. 6 FL
  683. # Customers with at least one shipping address = minimum shipping address.
  684. query IB
  685. SELECT
  686. c_id,
  687. (SELECT min(ship) FROM o WHERE o.c_id=c.c_id) IN (SELECT ship FROM o WHERE o.c_id=c.c_id)
  688. FROM c
  689. ORDER BY c_id;
  690. ----
  691. 1 true
  692. 2 true
  693. 3 false
  694. 4 true
  695. 5 false
  696. 6 true
  697. # Maximum number of orders for a customer. Use subquery in aggregate function.
  698. query I
  699. SELECT max((SELECT count(*) FROM o WHERE o.c_id=c.c_id)) FROM c;
  700. ----
  701. 3
  702. # Order count by customer.
  703. query II
  704. SELECT
  705. c_id,
  706. (SELECT count(*) FROM o WHERE o.c_id=c.c_id)
  707. FROM c
  708. ORDER BY c_id;
  709. ----
  710. 1 3
  711. 2 3
  712. 3 0
  713. 4 2
  714. 5 0
  715. 6 1
  716. # Count bill/ship addresses in each state.
  717. # Note: Result differs from Cockroach but matches Postgres.
  718. query TI
  719. SELECT
  720. s.st,
  721. (SELECT count(*) FROM c WHERE c.bill=s.st) + (SELECT count(*) FROM o WHERE o.ship=s.st)
  722. FROM (SELECT c.bill AS st FROM c UNION SELECT o.ship AS st FROM o) s
  723. ORDER BY s.st;
  724. ----
  725. CA 5
  726. FL 1
  727. MA 1
  728. TX 3
  729. WA 1
  730. WY 1
  731. NULL 0
  732. # Customer orders grouped by ship state, compared with count of all orders
  733. # shipped to that state by all other customers combined.
  734. query ITII rowsort
  735. SELECT c.c_id, o.ship, count(*) AS cust,
  736. (SELECT count(*) FROM o AS o2 WHERE o2.ship = o.ship AND o2.c_id <> c.c_id) AS other
  737. FROM c
  738. INNER JOIN o
  739. ON c.c_id=o.c_id
  740. GROUP BY c.c_id, o.ship;
  741. ----
  742. 2 TX 1 0
  743. 4 NULL 1 0
  744. 6 WA 1 0
  745. 2 NULL 1 0
  746. 4 WY 1 0
  747. 1 CA 3 1
  748. 2 CA 1 3
  749. # Customers with their orders (even if no orders), plus max of bill and ship
  750. # states for that customer (alphabetically).
  751. query IIT
  752. SELECT
  753. c.c_id,
  754. o.o_id,
  755. (
  756. SELECT max(CASE WHEN c2.bill > o2.ship THEN c2.bill ELSE o2.ship END)
  757. FROM c AS c2, o AS o2
  758. WHERE c2.c_id=o2.c_id AND c2.c_id=c.c_id
  759. )
  760. FROM c
  761. LEFT JOIN o
  762. ON c.c_id=o.c_id
  763. ORDER BY c.c_id, o.o_id
  764. ----
  765. 1 10 CA
  766. 1 20 CA
  767. 1 30 CA
  768. 2 40 TX
  769. 2 50 TX
  770. 2 60 TX
  771. 3 NULL NULL
  772. 4 70 WY
  773. 4 80 WY
  774. 5 NULL NULL
  775. 6 90 WA
  776. # Customers, with boolean indicating whether they have at least one order with a
  777. # NULL ship state.
  778. # Note: Result differs from Cockroach but matches Postgres.
  779. query IB
  780. SELECT
  781. c.c_id,
  782. (SELECT ship FROM o WHERE o.c_id=c.c_id ORDER BY ship LIMIT 1) IS NOT NULL
  783. FROM c
  784. ORDER BY c.c_id
  785. ----
  786. 1 true
  787. 2 true
  788. 3 false
  789. 4 true
  790. 5 false
  791. 6 true
  792. # Customers, with boolean indicating whether their first order shipped to 'CA'
  793. # or 'WY' (no NULL ship).
  794. query IB
  795. SELECT
  796. c.c_id,
  797. (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL ORDER BY ship LIMIT 1)='CA'
  798. OR (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL ORDER BY ship LIMIT 1)='WY'
  799. FROM c
  800. ORDER BY c_id
  801. ----
  802. 1 true
  803. 2 true
  804. 3 NULL
  805. 4 true
  806. 5 NULL
  807. 6 false
  808. query T
  809. SELECT (SELECT string_agg(ship, ', ')
  810. FROM
  811. (SELECT c_id AS o_c_id, ship FROM o ORDER BY ship)
  812. WHERE o_c_id=c.c_id)
  813. FROM c ORDER BY c_id
  814. ----
  815. CA, CA, CA
  816. CA, TX
  817. NULL
  818. WY
  819. NULL
  820. WA
  821. query T
  822. SELECT (SELECT string_agg(DISTINCT ship, ', ')
  823. FROM
  824. (SELECT c_id AS o_c_id, ship FROM o ORDER BY ship)
  825. WHERE o_c_id=c.c_id)
  826. FROM c ORDER BY c_id
  827. ----
  828. CA
  829. CA, TX
  830. NULL
  831. WY
  832. NULL
  833. WA
  834. query ITI
  835. SELECT
  836. *
  837. FROM
  838. (SELECT c_id AS c_c_id, bill FROM c) s1,
  839. LATERAL (SELECT o_id FROM o WHERE c_id = c_c_id) s2
  840. ORDER BY c_c_id, bill, o_id
  841. ----
  842. 1 CA 10
  843. 1 CA 20
  844. 1 CA 30
  845. 2 TX 40
  846. 2 TX 50
  847. 2 TX 60
  848. 4 TX 70
  849. 4 TX 80
  850. 6 FL 90
  851. query TI
  852. SELECT
  853. *
  854. FROM
  855. (SELECT bill FROM c) s1,
  856. LATERAL (SELECT o_id FROM o WHERE ship = bill) s2
  857. ORDER BY bill, o_id
  858. ----
  859. CA 10
  860. CA 20
  861. CA 30
  862. CA 40
  863. TX 50
  864. TX 50
  865. # ------------------------------------------------------------------------------
  866. # Subqueries in other interesting locations.
  867. # ------------------------------------------------------------------------------
  868. # Group by order count by customer, and order by that order count as well.
  869. query II
  870. SELECT
  871. (SELECT count(*) FROM o WHERE o.c_id=c.c_id) AS order_cnt,
  872. count(*) AS cust_cnt
  873. FROM c
  874. GROUP BY (SELECT count(*) FROM o WHERE o.c_id=c.c_id)
  875. ORDER BY (SELECT count(*) FROM o WHERE o.c_id=c.c_id) DESC
  876. ----
  877. 3 2
  878. 2 1
  879. 1 1
  880. 0 2
  881. # Subquery in VALUES clause.
  882. query III
  883. SELECT c_cnt, o_cnt, c_cnt + o_cnt AS total
  884. FROM (VALUES ((SELECT count(*) FROM c), (SELECT count(*) FROM o))) AS v(c_cnt, o_cnt)
  885. WHERE c_cnt > 0 AND o_cnt > 0;
  886. ----
  887. 6 9 15
  888. # Subquery in JOIN condition.
  889. query II rowsort
  890. SELECT c.c_id, o.o_id
  891. FROM c
  892. INNER JOIN o
  893. ON c.c_id=o.c_id AND EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND ship IS NULL);
  894. ----
  895. 2 40
  896. 2 50
  897. 2 60
  898. 4 70
  899. 4 80
  900. # TODO(justin): Not supported by Materialize.
  901. # query error more than one row returned by a subquery
  902. # SELECT c.c_id, o.o_id
  903. # FROM c
  904. # INNER JOIN o
  905. # ON c.c_id=o.c_id AND o.ship = (SELECT o.ship FROM o WHERE o.c_id=c.c_id);
  906. # TODO(justin): Not supported by Materialize.
  907. # # Subquery in ARRAY(...)
  908. # query ITT
  909. # SELECT
  910. # c_id,
  911. # ARRAY(SELECT o_id FROM o WHERE o.c_id = c.c_id ORDER BY o_id),
  912. # ARRAY(SELECT o_id FROM o WHERE o.ship = c.bill ORDER BY o_id)
  913. # FROM c ORDER BY c_id
  914. # ----
  915. # 1 {10,20,30} {10,20,30,40}
  916. # 2 {40,50,60} {50}
  917. # 3 {} {}
  918. # 4 {70,80} {50}
  919. # 5 {} {}
  920. # 6 {90} {}
  921. #
  922. # query IT
  923. # SELECT
  924. # c_id,
  925. # ARRAY(SELECT o_id FROM o WHERE o.c_id = c.c_id ORDER BY o_id)
  926. # FROM c ORDER BY c_id
  927. # ----
  928. # 1 {10,20,30}
  929. # 2 {40,50,60}
  930. # 3 {}
  931. # 4 {70,80}
  932. # 5 {}
  933. # 6 {90}
  934. # Regression for issue database-issues#7343: missing support for correlated subquery in JSON
  935. # operator.
  936. statement ok
  937. CREATE TABLE groups(
  938. id INT PRIMARY KEY,
  939. data JSONB
  940. )
  941. statement ok
  942. INSERT INTO groups(id, data) VALUES(1, '{"name": "Group 1", "members": [{"name": "admin", "type": "USER"}, {"name": "user", "type": "USER"}]}')
  943. statement ok
  944. INSERT INTO groups(id, data) VALUES(2, '{"name": "Group 2", "members": [{"name": "admin2", "type": "USER"}]}')
  945. # database-issues#544
  946. # query TT
  947. # SELECT
  948. # g.data->>'name' AS group_name,
  949. # jsonb_array_elements( (SELECT gg.data->'members' FROM groups gg WHERE gg.data->>'name' = g.data->>'name') )
  950. # FROM
  951. # groups g
  952. # ----
  953. # Group 1 {"name": "admin", "type": "USER"}
  954. # Group 1 {"name": "user", "type": "USER"}
  955. # Group 2 {"name": "admin2", "type": "USER"}
  956. # query TT
  957. # SELECT
  958. # data->>'name',
  959. # members
  960. # FROM
  961. # groups AS g,
  962. # jsonb_array_elements(
  963. # (
  964. # SELECT
  965. # gg.data->'members' AS members
  966. # FROM
  967. # groups AS gg
  968. # WHERE
  969. # gg.data->>'name' = g.data->>'name'
  970. # )
  971. # ) AS members
  972. # ----
  973. # Group 1 {"name": "admin", "type": "USER"}
  974. # Group 1 {"name": "user", "type": "USER"}
  975. # Group 2 {"name": "admin2", "type": "USER"}
  976. # ------------------------------------------------------------------------------
  977. # Regression test cases.
  978. # ------------------------------------------------------------------------------
  979. # Regression for issue 35437.
  980. statement ok
  981. CREATE TABLE users (
  982. id INT NOT NULL,
  983. name VARCHAR(50),
  984. PRIMARY KEY (id)
  985. )
  986. statement ok
  987. INSERT INTO users(id, name) VALUES (1, 'user1')
  988. statement ok
  989. INSERT INTO users(id, name) VALUES (2, 'user2')
  990. statement ok
  991. INSERT INTO users(id, name) VALUES (3, 'user3')
  992. statement ok
  993. CREATE TABLE stuff (
  994. id INT NOT NULL,
  995. date DATE,
  996. user_id INT,
  997. PRIMARY KEY (id),
  998. FOREIGN KEY (user_id) REFERENCES users (id)
  999. )
  1000. statement ok
  1001. INSERT INTO stuff(id, date, user_id) VALUES (1, '2007-10-15'::DATE, 1)
  1002. statement ok
  1003. INSERT INTO stuff(id, date, user_id) VALUES (2, '2007-12-15'::DATE, 1)
  1004. statement ok
  1005. INSERT INTO stuff(id, date, user_id) VALUES (3, '2007-11-15'::DATE, 1)
  1006. statement ok
  1007. INSERT INTO stuff(id, date, user_id) VALUES (4, '2008-01-15'::DATE, 2)
  1008. statement ok
  1009. INSERT INTO stuff(id, date, user_id) VALUES (5, '2007-06-15'::DATE, 3)
  1010. statement ok
  1011. INSERT INTO stuff(id, date, user_id) VALUES (6, '2007-03-15'::DATE, 3)
  1012. # database-issues#949
  1013. # query ITITI
  1014. # SELECT
  1015. # users.id AS users_id,
  1016. # users.name AS users_name,
  1017. # stuff_1.id AS stuff_1_id,
  1018. # stuff_1.date AS stuff_1_date,
  1019. # stuff_1.user_id AS stuff_1_user_id
  1020. # FROM
  1021. # users
  1022. # LEFT JOIN stuff AS stuff_1
  1023. # ON
  1024. # users.id = stuff_1.user_id
  1025. # AND stuff_1.id
  1026. # = (
  1027. # SELECT
  1028. # stuff_2.id
  1029. # FROM
  1030. # stuff AS stuff_2
  1031. # WHERE
  1032. # stuff_2.user_id = users.id
  1033. # ORDER BY
  1034. # stuff_2.date DESC
  1035. # LIMIT
  1036. # 1
  1037. # )
  1038. # ORDER BY
  1039. # users.name;
  1040. # ----
  1041. # 1 user1 2 2007-12-15 00:00:00 +0000 +0000 1
  1042. # 2 user2 4 2008-01-15 00:00:00 +0000 +0000 2
  1043. # 3 user3 5 2007-06-15 00:00:00 +0000 +0000 3
  1044. statement ok
  1045. DROP TABLE stuff;
  1046. statement ok
  1047. DROP TABLE users;