union.slt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. # Copyright 1994, Regents of the University of California.
  2. # Copyright 1996-2022 PostgreSQL Global Development Group.
  3. # Copyright Materialize, Inc. and contributors. All rights reserved.
  4. #
  5. # Use of this software is governed by the Business Source License
  6. # included in the LICENSE file at the root of this repository.
  7. #
  8. # As of the Change Date specified in that file, in accordance with
  9. # the Business Source License, use of this software will be governed
  10. # by the Apache License, Version 2.0.
  11. #
  12. # This file is derived from the regression test suite in PostgreSQL.
  13. # The original file was retrieved on February 2, 2022 from:
  14. #
  15. # https://github.com/postgres/postgres/blob/d33a81203e95d31e62157c4ae0e00e2198841208/src/test/regress/expected/union.out
  16. #
  17. # The original source code is subject to the terms of the PostgreSQL
  18. # license, a copy of which can be found in the LICENSE file at the
  19. # root of this repository.
  20. # UNION (also INTERSECT, EXCEPT)
  21. mode cockroach
  22. statement ok
  23. CREATE TABLE tenk1 (unique1 int, unique2 int, fivethous int)
  24. statement ok
  25. INSERT INTO tenk1 SELECT * FROM ROWS FROM (generate_series(0, 4999), generate_series(4999, 0, -1), generate_series(0, 4999))
  26. statement ok
  27. INSERT INTO tenk1 SELECT * FROM ROWS FROM (generate_series(5000, 9999), generate_series(9999, 5000, -1), generate_series(0, 4999))
  28. statement ok
  29. CREATE TABLE char_tbl (f1 char(4))
  30. statement ok
  31. INSERT INTO char_tbl VALUES ('a'), ('ab'), ('abcd'), ('abcd ')
  32. statement ok
  33. CREATE TABLE varchar_tbl (f1 varchar(4))
  34. statement ok
  35. INSERT INTO varchar_tbl VALUES ('a'), ('ab'), ('abcd'), ('abcd ')
  36. statement ok
  37. CREATE TABLE TEXT_TBL (f1 text)
  38. statement ok
  39. INSERT INTO TEXT_TBL VALUES ('doh!'), ('hi de ho neighbor')
  40. statement ok
  41. CREATE TABLE int4_tbl (f1 int4)
  42. statement ok
  43. INSERT INTO int4_tbl VALUES ('0'), ('123456'), ('-123456'), ('2147483647'), ('-2147483647')
  44. statement ok
  45. CREATE TABLE int8_tbl (q1 int8, q2 int8)
  46. statement ok
  47. INSERT INTO int8_tbl VALUES ('123', '456'), ('123', '4567890123456789'), ('4567890123456789', '123'), ('4567890123456789', '4567890123456789'), ('4567890123456789','-4567890123456789')
  48. statement ok
  49. CREATE TABLE float8_tbl (f1 float8)
  50. statement ok
  51. INSERT INTO float8_tbl VALUES ('0.0'), ('-34.84'), ('-1004.30'), ('-123456789012340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), ('-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345678901234')
  52. query I colnames
  53. SELECT 1 AS two UNION SELECT 2 ORDER BY 1
  54. ----
  55. two
  56. 1
  57. 2
  58. query I colnames
  59. SELECT 1 AS one UNION SELECT 1 ORDER BY 1
  60. ----
  61. one
  62. 1
  63. query I colnames
  64. SELECT 1 AS two UNION ALL SELECT 2
  65. ----
  66. two
  67. 1
  68. 2
  69. query I colnames
  70. SELECT 1 AS two UNION ALL SELECT 1
  71. ----
  72. two
  73. 1
  74. 1
  75. query I colnames
  76. SELECT 1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1
  77. ----
  78. three
  79. 1
  80. 2
  81. 3
  82. query I colnames
  83. SELECT 1 AS two UNION SELECT 2 UNION SELECT 2 ORDER BY 1
  84. ----
  85. two
  86. 1
  87. 2
  88. query I colnames
  89. SELECT 1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1
  90. ----
  91. three
  92. 1
  93. 2
  94. 2
  95. query R colnames
  96. SELECT 1.1 AS two UNION SELECT 2.2 ORDER BY 1
  97. ----
  98. two
  99. 1.1
  100. 2.2
  101. # Mixed types
  102. query R colnames
  103. SELECT 1.1 AS two UNION SELECT 2 ORDER BY 1
  104. ----
  105. two
  106. 1.1
  107. 2
  108. query R colnames
  109. SELECT 1 AS two UNION SELECT 2.2 ORDER BY 1
  110. ----
  111. two
  112. 1
  113. 2.2
  114. query R colnames
  115. SELECT 1 AS one UNION SELECT 1.0::float8 ORDER BY 1
  116. ----
  117. one
  118. 1
  119. query R colnames
  120. SELECT 1.1 AS two UNION ALL SELECT 2 ORDER BY 1
  121. ----
  122. two
  123. 1.1
  124. 2
  125. query R colnames
  126. SELECT 1.0::float8 AS two UNION ALL SELECT 1 ORDER BY 1
  127. ----
  128. two
  129. 1
  130. 1
  131. query R colnames
  132. SELECT 1.1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1
  133. ----
  134. three
  135. 1.1
  136. 2
  137. 3
  138. query R colnames
  139. SELECT 1.1::float8 AS two UNION SELECT 2 UNION SELECT 2.0::float8 ORDER BY 1
  140. ----
  141. two
  142. 1.1
  143. 2
  144. query R colnames
  145. SELECT 1.1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1
  146. ----
  147. three
  148. 1.1
  149. 2
  150. 2
  151. query R colnames
  152. SELECT 1.1 AS two UNION (SELECT 2 UNION ALL SELECT 2) ORDER BY 1
  153. ----
  154. two
  155. 1.1
  156. 2
  157. # Try testing from tables...
  158. query R colnames
  159. SELECT f1 AS five FROM FLOAT8_TBL
  160. UNION
  161. SELECT f1 FROM FLOAT8_TBL
  162. ORDER BY 1
  163. ----
  164. five
  165. -123456789012340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  166. -1004.3
  167. -34.84
  168. -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345678901234
  169. 0
  170. query R colnames
  171. SELECT f1 AS ten FROM FLOAT8_TBL
  172. UNION ALL
  173. SELECT f1 FROM FLOAT8_TBL
  174. ORDER BY 1
  175. ----
  176. ten
  177. -123456789012340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  178. -123456789012340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  179. -1004.3
  180. -1004.3
  181. -34.84
  182. -34.84
  183. -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345678901234
  184. -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345678901234
  185. 0
  186. 0
  187. query R colnames
  188. SELECT f1 AS nine FROM FLOAT8_TBL
  189. UNION
  190. SELECT f1 FROM INT4_TBL
  191. ORDER BY 1
  192. ----
  193. nine
  194. -123456789012340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  195. -2147483647
  196. -123456
  197. -1004.3
  198. -34.84
  199. -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345678901234
  200. 0
  201. 123456
  202. 2147483647
  203. query R colnames
  204. SELECT f1 AS ten FROM FLOAT8_TBL
  205. UNION ALL
  206. SELECT f1 FROM INT4_TBL
  207. ORDER BY 1
  208. ----
  209. ten
  210. -123456789012340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  211. -2147483647
  212. -123456
  213. -1004.3
  214. -34.84
  215. -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345678901234
  216. 0
  217. 0
  218. 123456
  219. 2147483647
  220. query R colnames
  221. SELECT f1 AS five FROM FLOAT8_TBL
  222. WHERE f1 BETWEEN -1e6 AND 1e6
  223. UNION
  224. SELECT f1 FROM INT4_TBL
  225. WHERE f1 BETWEEN 0 AND 1000000
  226. ORDER BY 1
  227. ----
  228. five
  229. -1004.3
  230. -34.84
  231. -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345678901234
  232. 0
  233. 123456
  234. query T colnames
  235. SELECT CAST(f1 AS char(4)) AS three FROM VARCHAR_TBL
  236. UNION
  237. SELECT f1 FROM CHAR_TBL
  238. ORDER BY 1
  239. ----
  240. three
  241. a␠␠␠
  242. ab␠␠
  243. abcd
  244. query T colnames
  245. SELECT f1 AS three FROM VARCHAR_TBL
  246. UNION
  247. SELECT CAST(f1 AS varchar) FROM CHAR_TBL
  248. ORDER BY 1
  249. ----
  250. three
  251. a
  252. ab
  253. abcd
  254. query T colnames
  255. SELECT f1 AS eight FROM VARCHAR_TBL
  256. UNION ALL
  257. SELECT f1 FROM CHAR_TBL
  258. ORDER BY 1
  259. ----
  260. eight
  261. a
  262. a
  263. ab
  264. ab
  265. abcd
  266. abcd
  267. abcd
  268. abcd
  269. query T colnames
  270. SELECT f1 AS five FROM TEXT_TBL
  271. UNION
  272. SELECT f1 FROM VARCHAR_TBL
  273. UNION
  274. SELECT TRIM(TRAILING FROM f1) FROM CHAR_TBL
  275. ORDER BY 1
  276. ----
  277. five
  278. a
  279. ab
  280. abcd
  281. doh!
  282. hi de ho neighbor
  283. # INTERSECT and EXCEPT
  284. query T colnames
  285. SELECT q2 FROM int8_tbl INTERSECT SELECT q1 FROM int8_tbl ORDER BY 1
  286. ----
  287. q2
  288. 123
  289. 4567890123456789
  290. query T colnames
  291. SELECT q2 FROM int8_tbl INTERSECT ALL SELECT q1 FROM int8_tbl ORDER BY 1
  292. ----
  293. q2
  294. 123
  295. 4567890123456789
  296. 4567890123456789
  297. query T colnames
  298. SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1
  299. ----
  300. q2
  301. -4567890123456789
  302. 456
  303. query T colnames
  304. SELECT q2 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl ORDER BY 1
  305. ----
  306. q2
  307. -4567890123456789
  308. 456
  309. query T colnames
  310. SELECT q2 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q1 FROM int8_tbl ORDER BY 1
  311. ----
  312. q2
  313. -4567890123456789
  314. 456
  315. 4567890123456789
  316. query T colnames
  317. SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY 1
  318. ----
  319. q1
  320. query T colnames
  321. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q2 FROM int8_tbl ORDER BY 1
  322. ----
  323. q1
  324. 123
  325. 4567890123456789
  326. query T colnames
  327. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl ORDER BY 1
  328. ----
  329. q1
  330. 123
  331. 4567890123456789
  332. 4567890123456789
  333. # nested cases
  334. query TTT colnames
  335. (SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6
  336. ----
  337. ?column? ?column? ?column?
  338. 4 5 6
  339. query TTT colnames
  340. (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6
  341. ----
  342. ?column? ?column? ?column?
  343. 4 5 6
  344. query TTT colnames
  345. (SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6
  346. ----
  347. ?column? ?column? ?column?
  348. 1 2 3
  349. query TTT colnames
  350. (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6
  351. ----
  352. ?column? ?column? ?column?
  353. 1 2 3
  354. query I colnames
  355. select count(*) from
  356. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss
  357. ----
  358. count
  359. 10000
  360. query I colnames
  361. select count(*) from
  362. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss
  363. ----
  364. count
  365. 5000
  366. query I colnames
  367. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10
  368. ----
  369. unique1
  370. 10
  371. query I colnames
  372. select count(*) from
  373. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss
  374. ----
  375. count
  376. 10000
  377. query I colnames
  378. select count(*) from
  379. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss
  380. ----
  381. count
  382. 5000
  383. query I colnames
  384. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10
  385. ----
  386. unique1
  387. 10
  388. query T colnames,rowsort
  389. select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x)
  390. ----
  391. x
  392. {1,4}
  393. {1,2}
  394. {1,3}
  395. query T colnames
  396. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x)
  397. ----
  398. x
  399. {1,2}
  400. query T colnames
  401. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x)
  402. ----
  403. x
  404. {1,3}
  405. query T colnames
  406. select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x)
  407. ----
  408. x
  409. {1,2}
  410. {1,3}
  411. {1,4}
  412. query T colnames
  413. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x)
  414. ----
  415. x
  416. {1,2}
  417. query T colnames
  418. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x)
  419. ----
  420. x
  421. {1,3}
  422. query T colnames
  423. select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x)
  424. ----
  425. x
  426. (1,2)
  427. (1,3)
  428. (1,4)
  429. query T colnames
  430. select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x)
  431. ----
  432. x
  433. (1,2)
  434. query T colnames
  435. select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x)
  436. ----
  437. x
  438. (1,3)
  439. query T colnames
  440. select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x)
  441. ----
  442. x
  443. (1,2)
  444. (1,3)
  445. (1,4)
  446. query T colnames
  447. select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x)
  448. ----
  449. x
  450. (1,2)
  451. query T colnames
  452. select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x)
  453. ----
  454. x
  455. (1,3)
  456. # Mixed types
  457. query R colnames
  458. SELECT f1 FROM float8_tbl INTERSECT SELECT f1 FROM int4_tbl ORDER BY 1
  459. ----
  460. f1
  461. 0
  462. query R colnames,rowsort
  463. SELECT f1 FROM float8_tbl EXCEPT SELECT f1 FROM int4_tbl ORDER BY 1
  464. ----
  465. f1
  466. -123456789012340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  467. -1004.3
  468. -34.84
  469. -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345678901234
  470. # Operator precedence and (((((extra))))) parentheses
  471. query I colnames,rowsort
  472. SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl ORDER BY 1
  473. ----
  474. q1
  475. -4567890123456789
  476. 123
  477. 123
  478. 456
  479. 4567890123456789
  480. 4567890123456789
  481. 4567890123456789
  482. query I colnames,rowsort
  483. SELECT q1 FROM int8_tbl INTERSECT (((SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) ORDER BY 1
  484. ----
  485. q1
  486. 123
  487. 4567890123456789
  488. query I colnames,rowsort
  489. (((SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl ORDER BY 1))) UNION ALL SELECT q2 FROM int8_tbl
  490. ----
  491. q1
  492. 123
  493. 4567890123456789
  494. 456
  495. 4567890123456789
  496. 123
  497. 4567890123456789
  498. -4567890123456789
  499. query I colnames
  500. SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1
  501. ----
  502. q1
  503. -4567890123456789
  504. 456
  505. query I colnames,rowsort
  506. SELECT q1 FROM int8_tbl UNION ALL (((SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1)))
  507. ----
  508. q1
  509. 123
  510. 123
  511. 456
  512. 4567890123456789
  513. 4567890123456789
  514. 4567890123456789
  515. -4567890123456789
  516. query I colnames
  517. (((SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1
  518. ----
  519. q1
  520. -4567890123456789
  521. 456
  522. # Subqueries with ORDER BY & LIMIT clauses
  523. #
  524. # In this syntax, ORDER BY/LIMIT apply to the result of the EXCEPT
  525. query II colnames
  526. SELECT q1,q2 FROM int8_tbl EXCEPT SELECT q2,q1 FROM int8_tbl
  527. ORDER BY q2,q1
  528. ----
  529. q1 q2
  530. 4567890123456789 -4567890123456789
  531. 123 456
  532. # This should fail, because q2 isn't a name of an EXCEPT output column
  533. query error column "q2" does not exist
  534. SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1
  535. # But this should work:
  536. query T colnames
  537. SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1
  538. ----
  539. q1
  540. 123
  541. 4567890123456789
  542. # New syntaxes (7.1) permit new tests
  543. query II colnames,rowsort
  544. (((((select * from int8_tbl)))))
  545. ----
  546. q1 q2
  547. 123 456
  548. 123 4567890123456789
  549. 4567890123456789 -4567890123456789
  550. 4567890123456789 123
  551. 4567890123456789 4567890123456789
  552. # Check behavior with empty select list (allowed since 9.4)
  553. query
  554. select union select
  555. ----
  556. query
  557. select intersect select
  558. ----
  559. query
  560. select except select
  561. ----
  562. query
  563. select from generate_series(1,5) union select from generate_series(1,3)
  564. ----
  565. query
  566. select from generate_series(1,5) union all select from generate_series(1,3)
  567. ----
  568. query
  569. select from generate_series(1,5) intersect select from generate_series(1,3)
  570. ----
  571. query
  572. select from generate_series(1,5) intersect all select from generate_series(1,3)
  573. ----
  574. query
  575. select from generate_series(1,5) except select from generate_series(1,3)
  576. ----
  577. query
  578. select from generate_series(1,5) except all select from generate_series(1,3)
  579. ----
  580. query
  581. select from generate_series(1,5) union select from generate_series(1,3)
  582. ----
  583. query
  584. select from generate_series(1,5) union all select from generate_series(1,3)
  585. ----
  586. query
  587. select from generate_series(1,5) intersect select from generate_series(1,3)
  588. ----
  589. query
  590. select from generate_series(1,5) intersect all select from generate_series(1,3)
  591. ----
  592. query
  593. select from generate_series(1,5) except select from generate_series(1,3)
  594. ----
  595. query
  596. select from generate_series(1,5) except all select from generate_series(1,3)
  597. ----
  598. # Check handling of a case with unknown constants. We don't guarantee
  599. # an undecorated constant will work in all cases, but historically this
  600. # usage has worked, so test we don't break it.
  601. query T colnames
  602. SELECT a.f1 FROM (SELECT 'test' AS f1 FROM varchar_tbl) a
  603. UNION
  604. SELECT b.f1 FROM (SELECT f1 FROM varchar_tbl) b
  605. ORDER BY 1
  606. ----
  607. f1
  608. a
  609. ab
  610. abcd
  611. test
  612. # TODO(benesch): this should have the following error instead.
  613. # query error invalid input syntax for type numeric: "foo"
  614. query error UNION types numeric and text cannot be matched
  615. SELECT '3.4'::numeric UNION SELECT 'foo'
  616. query TT colnames
  617. SELECT * FROM
  618. (SELECT 1 AS t, 2 AS x
  619. UNION
  620. SELECT 2 AS t, 4 AS x) ss
  621. WHERE x < 4
  622. ORDER BY x
  623. ----
  624. t x
  625. 1 2
  626. query TT colnames
  627. SELECT * FROM
  628. (SELECT 1 AS t, generate_series(1,10) AS x
  629. UNION
  630. SELECT 2 AS t, 4 AS x) ss
  631. WHERE x < 4
  632. ORDER BY x
  633. ----
  634. t x
  635. 1 1
  636. 1 2
  637. 1 3
  638. query I colnames
  639. select distinct q1 from
  640. (select distinct * from int8_tbl i81
  641. union all
  642. select distinct * from int8_tbl i82) ss
  643. where q2 = q2
  644. order by 1
  645. ----
  646. q1
  647. 123
  648. 4567890123456789
  649. query I colnames
  650. select distinct q1 from
  651. (select distinct * from int8_tbl i81
  652. union all
  653. select distinct * from int8_tbl i82) ss
  654. where -q1 = q2
  655. ----
  656. q1
  657. 4567890123456789
  658. query III colnames
  659. select * from
  660. (select *, 0 as x from int8_tbl a
  661. union all
  662. select *, 1 as x from int8_tbl b) ss
  663. where (x = 0) or (q1 >= q2 and q1 <= q2)
  664. order by 1, 2, 3
  665. ----
  666. q1 q2 x
  667. 123 456 0
  668. 123 4567890123456789 0
  669. 4567890123456789 -4567890123456789 0
  670. 4567890123456789 123 0
  671. 4567890123456789 4567890123456789 0
  672. 4567890123456789 4567890123456789 1