union.slt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/union
  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. query I rowsort
  25. VALUES (1), (1), (1), (2), (2) UNION VALUES (1), (3), (1)
  26. ----
  27. 1
  28. 2
  29. 3
  30. query I rowsort
  31. VALUES (1), (1), (1), (2), (2) UNION ALL VALUES (1), (3), (1)
  32. ----
  33. 1
  34. 1
  35. 1
  36. 1
  37. 1
  38. 2
  39. 2
  40. 3
  41. query I rowsort
  42. VALUES (1), (1), (1), (2), (2) INTERSECT VALUES (1), (3), (1)
  43. ----
  44. 1
  45. query I rowsort
  46. VALUES (1), (1), (1), (2), (2) INTERSECT ALL VALUES (1), (3), (1)
  47. ----
  48. 1
  49. 1
  50. query I rowsort
  51. VALUES (1), (1), (1), (2), (2) EXCEPT VALUES (1), (3), (1)
  52. ----
  53. 2
  54. query I rowsort
  55. VALUES (1), (1), (1), (2), (2) EXCEPT ALL VALUES (1), (3), (1)
  56. ----
  57. 1
  58. 2
  59. 2
  60. query II rowsort
  61. VALUES (1, 2), (1, 1), (1, 2), (2, 1), (2, 1) UNION VALUES (1, 3), (3, 4), (1, 1)
  62. ----
  63. 1 1
  64. 1 2
  65. 1 3
  66. 2 1
  67. 3 4
  68. # The ORDER BY and LIMIT apply to the UNION, not the last VALUES.
  69. query I
  70. VALUES (1), (1), (1), (2), (2) UNION ALL VALUES (1), (3), (1) ORDER BY 1 DESC LIMIT 2
  71. ----
  72. 3
  73. 2
  74. # TODO(benesch): uncomment if we improve UNION type matching. PostgreSQL doesn't
  75. # support these, so it's not high priority.
  76. #
  77. # # UNION with NULL columns in operands works.
  78. # query I
  79. # VALUES (1) UNION ALL VALUES (NULL) ORDER BY 1
  80. # ----
  81. # NULL
  82. # 1
  83. #
  84. # query I
  85. # VALUES (NULL) UNION ALL VALUES (1) ORDER BY 1
  86. # ----
  87. # NULL
  88. # 1
  89. #
  90. # query I
  91. # VALUES (NULL) UNION ALL VALUES (NULL)
  92. # ----
  93. # NULL
  94. # NULL
  95. # TODO(benesch): uncomment when we have support for pg_typeof and column
  96. # aliases.
  97. #
  98. # query IT rowsort
  99. # SELECT x, pg_typeof(y) FROM (SELECT 1, NULL UNION ALL SELECT 2, 4) AS t(x, y)
  100. # ----
  101. # 1 unknown
  102. # 2 int
  103. #
  104. # query IT rowsort
  105. # SELECT x, pg_typeof(y) FROM (SELECT 1, 3 UNION ALL SELECT 2, NULL) AS t(x, y)
  106. # ----
  107. # 1 int
  108. # 2 unknown
  109. # TODO(benesch): uncomment if we improve UNION type matching. PostgreSQL doesn't
  110. # support these, so it's not high priority.
  111. #
  112. # INTERSECT with NULL columns in operands works.
  113. # query I
  114. # VALUES (1) INTERSECT VALUES (NULL) ORDER BY 1
  115. # ----
  116. #
  117. # query I
  118. # VALUES (NULL) INTERSECT VALUES (1) ORDER BY 1
  119. # ----
  120. #
  121. # query I
  122. # VALUES (NULL) INTERSECT VALUES (NULL)
  123. # ----
  124. # NULL
  125. #
  126. # # EXCEPT with NULL columns in operands works.
  127. # query I
  128. # VALUES (1) EXCEPT VALUES (NULL) ORDER BY 1
  129. # ----
  130. # 1
  131. #
  132. # query I
  133. # VALUES (NULL) EXCEPT VALUES (1) ORDER BY 1
  134. # ----
  135. # NULL
  136. #
  137. # query I
  138. # VALUES (NULL) EXCEPT VALUES (NULL)
  139. # ----
  140. #
  141. statement ok
  142. CREATE TABLE uniontest (
  143. k INT,
  144. v INT
  145. )
  146. statement OK
  147. INSERT INTO uniontest VALUES
  148. (1, 1),
  149. (1, 1),
  150. (1, 1),
  151. (1, 2),
  152. (1, 2),
  153. (2, 1),
  154. (2, 3),
  155. (2, 1)
  156. query I rowsort
  157. SELECT v FROM uniontest WHERE k = 1 UNION SELECT v FROM uniontest WHERE k = 2
  158. ----
  159. 1
  160. 2
  161. 3
  162. query I rowsort
  163. SELECT v FROM uniontest WHERE k = 1 UNION ALL SELECT v FROM uniontest WHERE k = 2
  164. ----
  165. 1
  166. 1
  167. 1
  168. 1
  169. 1
  170. 2
  171. 2
  172. 3
  173. query I rowsort
  174. SELECT v FROM uniontest WHERE k = 1 INTERSECT SELECT v FROM uniontest WHERE k = 2
  175. ----
  176. 1
  177. query I rowsort
  178. SELECT v FROM uniontest WHERE k = 1 INTERSECT ALL SELECT v FROM uniontest WHERE k = 2
  179. ----
  180. 1
  181. 1
  182. query I rowsort
  183. SELECT v FROM uniontest WHERE k = 1 EXCEPT SELECT v FROM uniontest WHERE k = 2
  184. ----
  185. 2
  186. query I rowsort
  187. SELECT v FROM uniontest WHERE k = 1 EXCEPT ALL SELECT v FROM uniontest WHERE k = 2
  188. ----
  189. 1
  190. 2
  191. 2
  192. query I
  193. (SELECT v FROM uniontest WHERE k = 1 UNION ALL SELECT v FROM uniontest WHERE k = 2) ORDER BY 1 DESC LIMIT 2
  194. ----
  195. 3
  196. 2
  197. # The ORDER BY and LIMIT apply to the UNION, not the last SELECT.
  198. query I
  199. SELECT v FROM uniontest WHERE k = 1 UNION ALL SELECT v FROM uniontest WHERE k = 2 ORDER BY 1 DESC LIMIT 2
  200. ----
  201. 3
  202. 2
  203. query II
  204. SELECT * FROM (SELECT * FROM (VALUES (1)) a LEFT JOIN (VALUES (1) UNION VALUES (2)) b on a.column1 = b.column1);
  205. ----
  206. 1 1
  207. query II
  208. SELECT * FROM (VALUES (1)) a LEFT JOIN (VALUES (1) UNION VALUES (2)) b on a.column1 = b.column1;
  209. ----
  210. 1 1
  211. query error pgcode 42601 each UNION query must have the same number of columns: 2 vs 1
  212. SELECT 1, 2 UNION SELECT 3
  213. query error pgcode 42601 each INTERSECT query must have the same number of columns: 2 vs 1
  214. SELECT 1, 2 INTERSECT SELECT 3
  215. query error pgcode 42601 each EXCEPT query must have the same number of columns: 2 vs 1
  216. SELECT 1, 2 EXCEPT SELECT 3
  217. query error pgcode 42804 UNION types integer and text cannot be matched
  218. SELECT 1 UNION SELECT '3'
  219. query error pgcode 42804 INTERSECT types integer and text cannot be matched
  220. SELECT 1 INTERSECT SELECT '3'
  221. query error pgcode 42804 EXCEPT types integer and text cannot be matched
  222. SELECT 1 EXCEPT SELECT '3'
  223. query error pgcode 42703 column "z" does not exist
  224. SELECT 1 UNION SELECT 3 ORDER BY z
  225. query error UNION types integer\[] and text\[] cannot be matched
  226. SELECT ARRAY[1] UNION ALL SELECT ARRAY['foo']
  227. # Check that UNION permits columns of different visible types
  228. statement ok
  229. CREATE TABLE a (a INT PRIMARY KEY)
  230. statement ok
  231. CREATE TABLE b (a INTEGER PRIMARY KEY)
  232. query I
  233. SELECT * FROM a UNION ALL SELECT * FROM b
  234. ----
  235. # Make sure that UNION ALL doesn't crash when its two children have different
  236. # post-processing stages.
  237. statement ok
  238. CREATE TABLE c (a INT PRIMARY KEY, b INT)
  239. query I
  240. SELECT a FROM a WHERE a > 2 UNION ALL (SELECT a FROM c WHERE b > 2) LIMIT 1;
  241. ----
  242. query III
  243. select *,1 from (values(1,2) union all select 2,2 from c);
  244. ----
  245. 1 2 1
  246. statement ok
  247. INSERT INTO a VALUES (1)
  248. statement ok
  249. INSERT INTO c VALUES (1,2)
  250. statement ok
  251. INSERT INTO c VALUES (3,4)
  252. # Check that UNION ALL columns are mapped correctly - even if one side gets optimized out
  253. query I
  254. SELECT a FROM (SELECT a FROM a UNION ALL SELECT a FROM c) ORDER BY a
  255. ----
  256. 1
  257. 1
  258. 3
  259. query I
  260. SELECT a FROM (SELECT a FROM a WHERE a > 3 AND a < 1 UNION ALL SELECT a FROM c) ORDER BY a
  261. ----
  262. 1
  263. 3
  264. query I
  265. SELECT a FROM (SELECT a FROM c UNION ALL SELECT a FROM a WHERE a > 3 AND a < 1) ORDER BY a
  266. ----
  267. 1
  268. 3
  269. query I
  270. SELECT a FROM (SELECT a FROM c UNION ALL SELECT a FROM a) WHERE a > 0 AND a < 3
  271. ----
  272. 1
  273. 1