distinct_on.slt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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/distinct_on
  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. statement ok
  25. CREATE TABLE xyz (
  26. x INT,
  27. y INT,
  28. z INT,
  29. pk1 INT,
  30. pk2 INT,
  31. PRIMARY KEY (pk1, pk2)
  32. )
  33. statement ok
  34. INSERT INTO xyz VALUES
  35. (1, 1, NULL, 1, 1),
  36. (1, 1, 2, 2, 2),
  37. (1, 1, 2, 3, 3),
  38. (1, 2, 1, 4, 4),
  39. (2, 2, 3, 5, 5),
  40. (4, 5, 6, 6, 6),
  41. (4, 1, 6, 7, 7)
  42. statement ok
  43. CREATE TABLE abc (
  44. a STRING,
  45. b STRING,
  46. c STRING,
  47. PRIMARY KEY (a, b, c)
  48. )
  49. statement ok
  50. INSERT INTO abc VALUES
  51. ('1', '1', '1'),
  52. ('1', '1', '2'),
  53. ('1', '2', '2')
  54. ##################
  55. # Simple queries #
  56. ##################
  57. # 3/3 columns
  58. query III rowsort
  59. SELECT DISTINCT ON (x, y, z) x, y, z FROM xyz
  60. ----
  61. 1 1 NULL
  62. 1 1 2
  63. 1 2 1
  64. 2 2 3
  65. 4 5 6
  66. 4 1 6
  67. query I rowsort
  68. SELECT DISTINCT ON (y, x, z) x FROM xyz
  69. ----
  70. 1
  71. 1
  72. 1
  73. 2
  74. 4
  75. 4
  76. query I rowsort
  77. SELECT DISTINCT ON (z, y, x) z FROM xyz
  78. ----
  79. NULL
  80. 2
  81. 1
  82. 3
  83. 6
  84. 6
  85. query TTT rowsort
  86. SELECT DISTINCT ON (b, c, a) a, c, b FROM abc
  87. ----
  88. 1 1 1
  89. 1 2 1
  90. 1 2 2
  91. query T rowsort
  92. SELECT DISTINCT ON (b, c, a) a FROM abc
  93. ----
  94. 1
  95. 1
  96. 1
  97. # We need to rowsort this since the ORDER BY isn't on the entire SELECT columns.
  98. query T rowsort
  99. SELECT DISTINCT ON (c, a, b) b FROM abc ORDER BY b
  100. ----
  101. 1
  102. 1
  103. 2
  104. # 2/3 columns
  105. query II rowsort
  106. SELECT DISTINCT ON (x, y) y, x FROM xyz
  107. ----
  108. 1 1
  109. 2 1
  110. 2 2
  111. 5 4
  112. 1 4
  113. query I rowsort
  114. SELECT DISTINCT ON (y, x) x FROM xyz
  115. ----
  116. 1
  117. 1
  118. 2
  119. 4
  120. 4
  121. query I rowsort
  122. SELECT DISTINCT ON (x, y) y FROM xyz
  123. ----
  124. 1
  125. 2
  126. 2
  127. 5
  128. 1
  129. query TT
  130. SELECT DISTINCT ON (a, c) a, b FROM abc ORDER BY a, c, b
  131. ----
  132. 1 1
  133. 1 1
  134. # We wrap this with an ORDER BY otherwise this would be non-deterministic.
  135. query TTT
  136. SELECT DISTINCT ON (c, a) b, c, a FROM abc ORDER BY c, a, b DESC
  137. ----
  138. 1 1 1
  139. 2 2 1
  140. # 1/3 columns
  141. query I rowsort
  142. SELECT DISTINCT ON (y) y FROM xyz
  143. ----
  144. 1
  145. 2
  146. 5
  147. query T rowsort
  148. SELECT DISTINCT ON (c) a FROM abc
  149. ----
  150. 1
  151. 1
  152. query T rowsort
  153. SELECT DISTINCT ON (b) b FROM abc
  154. ----
  155. 1
  156. 2
  157. # We wrap this with an ORDER BY otherwise this would be non-deterministic.
  158. query TTT
  159. SELECT DISTINCT ON (a) a, b, c FROM abc ORDER BY a, b, c
  160. ----
  161. 1 1 1
  162. query TT
  163. SELECT DISTINCT ON (a) a, c FROM abc ORDER BY a, c DESC, b
  164. ----
  165. 1 2
  166. #################
  167. # With ORDER BY #
  168. #################
  169. statement error SELECT DISTINCT ON expressions must match initial ORDER BY expressions
  170. SELECT DISTINCT ON (x) x, y, z FROM xyz ORDER BY y
  171. statement error SELECT DISTINCT ON expressions must match initial ORDER BY expressions
  172. SELECT DISTINCT ON (y) x, y, z FROM xyz ORDER BY x, y
  173. statement error SELECT DISTINCT ON expressions must match initial ORDER BY expressions
  174. SELECT DISTINCT ON (y, z) x, y, z FROM xyz ORDER BY x
  175. query I
  176. SELECT DISTINCT ON (x) x FROM xyz ORDER BY x DESC
  177. ----
  178. 4
  179. 2
  180. 1
  181. # We add a filter to eliminate one of the rows that may be flakily returned
  182. # depending on parallel execution of DISTINCT ON.
  183. # Note: Result differs from Cockroach but matches Postgres.
  184. query III
  185. SELECT DISTINCT ON (x, z) y, z, x FROM xyz WHERE (x,y,z) != (4, 1, 6) ORDER BY z
  186. ----
  187. 2 1 1
  188. 1 2 1
  189. 2 3 2
  190. 5 6 4
  191. 1 NULL 1
  192. # Note: Result differs from Cockroach but matches Postgres.
  193. query III
  194. SELECT DISTINCT ON (x) y, z, x FROM xyz ORDER BY x ASC, z DESC, y DESC
  195. ----
  196. 1 NULL 1
  197. 2 3 2
  198. 5 6 4
  199. # Regression test for cockroach#35437: Discard extra ordering columns after performing
  200. # DISTINCT operation.
  201. query T
  202. SELECT (SELECT DISTINCT ON (a) a FROM abc ORDER BY a, b||'foo') || 'bar';
  203. ----
  204. 1bar
  205. #####################
  206. # With aggregations #
  207. #####################
  208. statement error column "xyz.y" must appear in the GROUP BY clause or be used in an aggregate function
  209. SELECT DISTINCT ON(max(x)) y FROM xyz
  210. statement error column "xyz.z" must appear in the GROUP BY clause or be used in an aggregate function
  211. SELECT DISTINCT ON(max(x), z) min(y) FROM xyz
  212. query I
  213. SELECT DISTINCT ON (max(x)) min(y) FROM xyz
  214. ----
  215. 1
  216. query I
  217. SELECT DISTINCT ON (min(x)) max(y) FROM xyz
  218. ----
  219. 5
  220. query T
  221. SELECT DISTINCT ON(min(a), max(b), min(c)) max(c) FROM abc
  222. ----
  223. 2
  224. #################
  225. # With GROUP BY #
  226. #################
  227. statement error column "xyz.x" must appear in the GROUP BY clause or be used in an aggregate function
  228. SELECT DISTINCT ON (x) min(x) FROM xyz GROUP BY y
  229. query I rowsort
  230. SELECT DISTINCT ON(y) min(x) FROM xyz GROUP BY y
  231. ----
  232. 1
  233. 1
  234. 4
  235. query I
  236. SELECT DISTINCT ON(min(x)) min(x) FROM xyz GROUP BY y HAVING min(x) = 1
  237. ----
  238. 1
  239. #########################
  240. # With window functions #
  241. #########################
  242. skipif postgresql # TODO(benesch): support row_number
  243. query I rowsort
  244. SELECT DISTINCT ON(row_number() OVER(ORDER BY (pk1, pk2))) y FROM xyz
  245. ----
  246. 1
  247. 1
  248. 1
  249. 2
  250. 2
  251. 5
  252. 1
  253. skipif postgresql # TODO(benesch): support row_number
  254. query I
  255. SELECT DISTINCT ON(row_number() OVER(ORDER BY (pk1, pk2))) y FROM xyz ORDER BY row_number() OVER(ORDER BY (pk1, pk2)) DESC
  256. ----
  257. 1
  258. 5
  259. 2
  260. 2
  261. 1
  262. 1
  263. 1
  264. ###########################
  265. # With ordinal references #
  266. ###########################
  267. statement error column reference 2 in DISTINCT ON clause is out of range \(1 - 1\)
  268. SELECT DISTINCT ON (2) x FROM xyz
  269. query I rowsort
  270. SELECT DISTINCT ON (1) x FROM xyz
  271. ----
  272. 1
  273. 2
  274. 4
  275. query III rowsort
  276. SELECT DISTINCT ON (1,2,3) x, y, z FROM xyz
  277. ----
  278. 1 1 NULL
  279. 1 1 2
  280. 1 2 1
  281. 2 2 3
  282. 4 5 6
  283. 4 1 6
  284. #########################
  285. # With alias references #
  286. #########################
  287. # This should prioritize alias (use 'x' as the key).
  288. # This would be non-deterministic if we don't select y (actually x) from the
  289. # subquery.
  290. query I rowsort
  291. SELECT y FROM (SELECT DISTINCT ON(y) x AS y, y AS x FROM xyz)
  292. ----
  293. 1
  294. 2
  295. 4
  296. # Ignores the alias.
  297. query I rowsort
  298. SELECT DISTINCT ON(x) x AS y FROM xyz
  299. ----
  300. 1
  301. 2
  302. 4
  303. ##################################
  304. # With nested parentheses/tuples #
  305. ##################################
  306. query II rowsort
  307. SELECT DISTINCT ON(((x)), (x, y)) x, y FROM xyz
  308. ----
  309. 1 1
  310. 1 2
  311. 2 2
  312. 4 5
  313. 4 1
  314. ################################
  315. # Hybrid PK and non-PK queries #
  316. ################################
  317. # We need to rowsort this since the ORDER BY isn't on the entire SELECT columns.
  318. query III rowsort
  319. SELECT DISTINCT ON(pk1, pk2, x, y) x, y, z FROM xyz ORDER BY x, y
  320. ----
  321. 1 1 NULL
  322. 1 1 2
  323. 1 1 2
  324. 1 2 1
  325. 2 2 3
  326. 4 1 6
  327. 4 5 6
  328. # Ordering only propagates up until distinctNode.
  329. # pk1 ordering does not propagate at all since it's not explicitly needed.
  330. # We add a filter since there could be multiple valid pk1s otherwise for distinct
  331. # rows.
  332. query I rowsort
  333. SELECT DISTINCT ON (x, y, z) pk1 FROM (SELECT * FROM xyz WHERE x >= 2) ORDER BY x
  334. ----
  335. 5
  336. 6
  337. 7
  338. # Regression tests for cockroach#34112: distinct on constant column.
  339. query II
  340. SELECT DISTINCT ON (x) x, y FROM xyz WHERE x = 1 ORDER BY x, y
  341. ----
  342. 1 1
  343. query I
  344. SELECT count(*) FROM (SELECT DISTINCT ON (x) x, y FROM xyz WHERE x = 1 ORDER BY x, y)
  345. ----
  346. 1