order_by.slt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/order_by
  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 t (
  26. a INT PRIMARY KEY,
  27. b INT,
  28. c BOOLEAN
  29. )
  30. statement ok
  31. INSERT INTO t VALUES (1, 9, true), (2, 8, false), (3, 7, NULL)
  32. # Note: Result differs from Cockroach but matches Postgres.
  33. query B
  34. SELECT c FROM t ORDER BY c
  35. ----
  36. false
  37. true
  38. NULL
  39. # The following test ensures that the "rowsort" directive
  40. # in TestLogic does its work properly.
  41. query B rowsort
  42. SELECT c FROM t ORDER BY c
  43. ----
  44. false
  45. NULL
  46. true
  47. # Note: Result differs from Cockroach but matches Postgres.
  48. query B
  49. SELECT c FROM t ORDER BY c DESC
  50. ----
  51. NULL
  52. true
  53. false
  54. query II
  55. SELECT a, b FROM t ORDER BY b
  56. ----
  57. 3 7
  58. 2 8
  59. 1 9
  60. query II
  61. SELECT a, b FROM t ORDER BY b DESC
  62. ----
  63. 1 9
  64. 2 8
  65. 3 7
  66. query I
  67. SELECT a FROM t ORDER BY 1 DESC
  68. ----
  69. 3
  70. 2
  71. 1
  72. query II
  73. SELECT a, b FROM t ORDER BY b DESC LIMIT 2
  74. ----
  75. 1 9
  76. 2 8
  77. query BI
  78. SELECT DISTINCT c, b FROM t ORDER BY b DESC LIMIT 2
  79. ----
  80. true 9
  81. false 8
  82. query II
  83. SELECT a AS foo, b FROM t ORDER BY foo DESC
  84. ----
  85. 3 7
  86. 2 8
  87. 1 9
  88. # Check that ambiguous references to renders are properly reported.
  89. query error column reference "foo" is ambiguous
  90. SELECT a AS foo, b AS foo FROM t ORDER BY foo
  91. # Check that no ambiguity is reported if the ORDER BY name refers
  92. # to two or more equivalent renders (special case in SQL92).
  93. query II
  94. SELECT a AS foo, (a) AS foo FROM t ORDER BY foo LIMIT 1
  95. ----
  96. 1 1
  97. query II
  98. SELECT a AS "foo.bar", b FROM t ORDER BY "foo.bar" DESC
  99. ----
  100. 3 7
  101. 2 8
  102. 1 9
  103. query II
  104. SELECT a AS foo, b FROM t ORDER BY a DESC
  105. ----
  106. 3 7
  107. 2 8
  108. 1 9
  109. query I
  110. SELECT b FROM t ORDER BY a DESC
  111. ----
  112. 7
  113. 8
  114. 9
  115. statement ok
  116. INSERT INTO t VALUES (4, 7), (5, 7)
  117. query II
  118. SELECT a, b FROM t WHERE b = 7 ORDER BY b, a
  119. ----
  120. 3 7
  121. 4 7
  122. 5 7
  123. query II
  124. SELECT a, b FROM t ORDER BY b, a DESC
  125. ----
  126. 5 7
  127. 4 7
  128. 3 7
  129. 2 8
  130. 1 9
  131. query III
  132. SELECT a, b, a+b AS ab FROM t WHERE b = 7 ORDER BY ab DESC, a
  133. ----
  134. 5 7 12
  135. 4 7 11
  136. 3 7 10
  137. query I
  138. SELECT a FROM t ORDER BY a+b DESC, a
  139. ----
  140. 5
  141. 4
  142. 1
  143. 2
  144. 3
  145. query I
  146. SELECT a FROM t ORDER BY (((a)))
  147. ----
  148. 1
  149. 2
  150. 3
  151. 4
  152. 5
  153. query I
  154. (((SELECT a FROM t))) ORDER BY a DESC LIMIT 4
  155. ----
  156. 5
  157. 4
  158. 3
  159. 2
  160. query I
  161. (((SELECT a FROM t ORDER BY a DESC LIMIT 4)))
  162. ----
  163. 5
  164. 4
  165. 3
  166. 2
  167. query error pgcode 42601 multiple ORDER BY clauses not allowed
  168. ((SELECT a FROM t ORDER BY a)) ORDER BY a
  169. query error CASE types integer and boolean cannot be matched
  170. SELECT CASE a WHEN 1 THEN b ELSE c END as val FROM t ORDER BY val
  171. query error pgcode 42P10 column reference 0 in ORDER BY clause is out of range \(1 - 3\)
  172. SELECT * FROM t ORDER BY 0
  173. query error column "foo" does not exist
  174. SELECT * FROM t ORDER BY foo
  175. query error column "a.b" does not exist
  176. SELECT a FROM t ORDER BY a.b
  177. query IT
  178. SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY 1
  179. ----
  180. 1 {1}
  181. query IT
  182. SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY generate_series
  183. ----
  184. 1 {1}
  185. query IT
  186. SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY -generate_series
  187. ----
  188. 1 {1}
  189. statement ok
  190. CREATE TABLE abc (
  191. a INT,
  192. b INT,
  193. c INT,
  194. d VARCHAR,
  195. PRIMARY KEY (a, b, c)
  196. )
  197. statement ok
  198. INSERT INTO abc VALUES (1, 2, 3, 'one'), (4, 5, 6, 'Two')
  199. query T
  200. SELECT d FROM abc ORDER BY lower(d)
  201. ----
  202. one
  203. Two
  204. query I
  205. SELECT a FROM abc ORDER BY a DESC
  206. ----
  207. 4
  208. 1
  209. query I
  210. SELECT a FROM abc ORDER BY a DESC LIMIT 1
  211. ----
  212. 4
  213. query I
  214. SELECT a FROM abc ORDER BY a DESC OFFSET 1
  215. ----
  216. 1
  217. statement ok
  218. CREATE TABLE bar (id INT PRIMARY KEY, baz STRING)
  219. statement ok
  220. INSERT INTO bar VALUES (0, NULL), (1, NULL)
  221. # Here rowsort is needed because the ORDER BY clause does not guarantee any
  222. # relative ordering between rows where baz is NULL. As we see above, because
  223. # this is a unique index, the ordering `+baz,+id` is deemed equivalent to just
  224. # `+baz`.
  225. query IT rowsort
  226. SELECT * FROM bar ORDER BY baz, id
  227. ----
  228. 0 NULL
  229. 1 NULL
  230. statement ok
  231. CREATE TABLE abcd (
  232. a INT PRIMARY KEY,
  233. b INT,
  234. c INT,
  235. d INT
  236. )
  237. statement ok
  238. INSERT INTO abcd VALUES (1, 4, 2, 3), (2, 3, 4, 1), (3, 2, 1, 2), (4, 4, 1, 1)
  239. # Verify that render expressions after sorts perform correctly. We need the
  240. # rowsort as we're attempting to force a RENDER expression after the first
  241. # ORDER BY, to ensure it renders correctly, but the outer query doesn't
  242. # guarantee that it will preserve the order.
  243. query I rowsort
  244. SELECT a+b FROM (SELECT * FROM abcd ORDER BY d)
  245. ----
  246. 5
  247. 5
  248. 5
  249. 8
  250. query I rowsort
  251. SELECT b+d FROM (SELECT * FROM abcd ORDER BY a,d)
  252. ----
  253. 7
  254. 4
  255. 4
  256. 5
  257. statement ok
  258. CREATE TABLE nan (id INT PRIMARY KEY, x REAL)
  259. statement ok
  260. INSERT INTO nan VALUES (1, 'NaN'), (2, -1), (3, 1), (4, 'NaN')
  261. # TODO(benesch): NaN sorts backwards in Materialize, it seems.
  262. skipif postgresql
  263. query R
  264. SELECT x FROM nan ORDER BY x
  265. ----
  266. NaN
  267. NaN
  268. -1
  269. 1
  270. statement ok
  271. CREATE TABLE blocks (
  272. block_id INT,
  273. writer_id STRING,
  274. block_num INT,
  275. raw_bytes BYTES,
  276. PRIMARY KEY (block_id, writer_id, block_num)
  277. )
  278. # Test ORDER BY with STORING column.
  279. statement ok
  280. CREATE TABLE store (
  281. id INT PRIMARY KEY,
  282. baz STRING,
  283. extra INT
  284. )
  285. statement ok
  286. INSERT INTO store VALUES (0, NULL, 10), (1, NULL, 5)
  287. # Here rowsort is needed because a unique index still allows duplicate NULL
  288. # values. It's not correct to sort on baz alone, even though it is "unique".
  289. query ITI
  290. SELECT * FROM store ORDER BY baz, extra
  291. ----
  292. 1 NULL 5
  293. 0 NULL 10
  294. # ------------------------------------------------------------------------------
  295. # ORDER BY INDEX test cases.
  296. # ------------------------------------------------------------------------------
  297. subtest order_by_index
  298. # NOTE(benesch): this is Cockroach-specific syntax we're unlikely to support.
  299. halt
  300. statement ok
  301. CREATE TABLE kv(k INT PRIMARY KEY, v INT)
  302. statement ok
  303. CREATE INDEX foo ON kv(v DESC)
  304. # Check the extended syntax cannot be used in case of renames.
  305. statement error no data source matches prefix: test.public.kv
  306. SELECT * FROM kv AS a, kv AS b ORDER BY PRIMARY KEY kv
  307. # The INDEX/PRIMARY syntax can only be used when the data source
  308. # is a real table, not an alias.
  309. #
  310. statement error no data source matches prefix: test.public.kv
  311. SELECT k FROM (SELECT @1, @1 FROM generate_series(1,10)) AS kv(k,v) ORDER BY PRIMARY KEY kv
  312. statement error no data source matches prefix: test.public.kv
  313. CREATE TABLE unrelated(x INT); SELECT * FROM unrelated ORDER BY PRIMARY KEY kv
  314. # Check that prepare doesn't crash on ORDER BY PK clauses materialize#17312
  315. statement ok
  316. PREPARE a AS (TABLE kv) ORDER BY PRIMARY KEY kv
  317. statement error ORDER BY INDEX in window definition is not supported
  318. SELECT avg(k) OVER (ORDER BY PRIMARY KEY kv) FROM kv
  319. statement ok
  320. INSERT INTO kv VALUES (1, 1), (2, 1), (3, 1), (4, 1), (5, 1)
  321. query I
  322. SELECT k FROM kv ORDER BY INDEX kv@foo
  323. ----
  324. 1
  325. 2
  326. 3
  327. 4
  328. 5
  329. statement ok
  330. CREATE TABLE abc2 (
  331. a INT,
  332. b INT,
  333. c INT,
  334. PRIMARY KEY (a, b),
  335. UNIQUE INDEX bc (b, c),
  336. INDEX ba (b, a)
  337. )
  338. statement ok
  339. INSERT INTO abc2 VALUES (2, 30, 400), (1, 30, 500), (3, 30, 300)
  340. query III
  341. SELECT a, b, c FROM abc2 ORDER BY PRIMARY KEY abc2
  342. ----
  343. 1 30 500
  344. 2 30 400
  345. 3 30 300
  346. query III
  347. SELECT a, b, c FROM abc2 ORDER BY PRIMARY KEY abc2 DESC
  348. ----
  349. 3 30 300
  350. 2 30 400
  351. 1 30 500
  352. query III
  353. SELECT a, b, c FROM abc2 ORDER BY INDEX abc2@bc
  354. ----
  355. 3 30 300
  356. 2 30 400
  357. 1 30 500
  358. query III
  359. SELECT a, b, c FROM abc2 ORDER BY INDEX abc2@bc DESC
  360. ----
  361. 1 30 500
  362. 2 30 400
  363. 3 30 300
  364. query III
  365. SELECT a, b, c FROM abc2 ORDER BY INDEX abc2@ba
  366. ----
  367. 1 30 500
  368. 2 30 400
  369. 3 30 300
  370. query III
  371. SELECT a, b, c FROM abc2 ORDER BY INDEX abc2@ba DESC
  372. ----
  373. 3 30 300
  374. 2 30 400
  375. 1 30 500
  376. statement error relation \"x\" does not exist
  377. SELECT a, b, c FROM abc2 AS x ORDER BY INDEX x@bc
  378. statement error no data source matches prefix: test.public.abc2
  379. SELECT a, b, c FROM abc2 AS x ORDER BY INDEX abc2@bc