limit.slt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/limit
  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 enable_expressions_in_limit_syntax TO true;
  26. ----
  27. COMPLETE 0
  28. query I
  29. SELECT generate_series FROM generate_series(1, 100) ORDER BY generate_series LIMIT 5;
  30. ----
  31. 1
  32. 2
  33. 3
  34. 4
  35. 5
  36. query I
  37. SELECT generate_series FROM generate_series(1, 100) ORDER BY generate_series FETCH FIRST 5 ROWS ONLY;
  38. ----
  39. 1
  40. 2
  41. 3
  42. 4
  43. 5
  44. query I
  45. SELECT generate_series FROM generate_series(1, 100) ORDER BY generate_series FETCH FIRST ROW ONLY;
  46. ----
  47. 1
  48. query I
  49. SELECT generate_series FROM generate_series(1, 100) ORDER BY generate_series OFFSET 3 ROWS FETCH NEXT ROW ONLY;
  50. ----
  51. 4
  52. statement error db error: ERROR: multiple LIMIT/FETCH clauses not allowed
  53. SELECT generate_series FROM generate_series(1, 100) FETCH NEXT ROW ONLY LIMIT 3;
  54. statement error db error: ERROR: multiple LIMIT/FETCH clauses not allowed
  55. SELECT generate_series FROM generate_series(1, 100) LIMIT 3 FETCH NEXT ROW ONLY;
  56. query I
  57. SELECT generate_series FROM generate_series(1, 100) FETCH NEXT 1 + 1 ROWS ONLY;
  58. ----
  59. 1
  60. 2
  61. query I
  62. SELECT generate_series FROM generate_series(1, 100) ORDER BY generate_series FETCH FIRST (1 + 1) ROWS ONLY;
  63. ----
  64. 1
  65. 2
  66. statement ok
  67. CREATE TABLE t (k INT PRIMARY KEY, v INT, w INT)
  68. statement ok
  69. INSERT INTO t VALUES (1, 1, 1), (2, -4, 8), (3, 9, 27), (4, -16, 94), (5, 25, 125), (6, -36, 216)
  70. # Verify we don't incorrectly impose a hard limit at the index scan level.
  71. query III
  72. SELECT * FROM t WHERE v > -20 AND w > 30 ORDER BY v LIMIT 2
  73. ----
  74. 4 -16 94
  75. 5 25 125
  76. query II
  77. SELECT k, v FROM t ORDER BY k LIMIT 5
  78. ----
  79. 1 1
  80. 2 -4
  81. 3 9
  82. 4 -16
  83. 5 25
  84. query II
  85. SELECT k, v FROM t ORDER BY k OFFSET 5
  86. ----
  87. 6 -36
  88. # TODO(benesch): support this.
  89. query error Expected end of statement, found number
  90. SELECT k, v FROM t ORDER BY v LIMIT (1+4) OFFSET 1
  91. ----
  92. 4 -16
  93. 2 -4
  94. 1 1
  95. 3 9
  96. 5 25
  97. # TODO(benesch): support this.
  98. query error Expected end of statement, found number
  99. SELECT k, v FROM t ORDER BY v DESC LIMIT (1+4) OFFSET 1
  100. ----
  101. 3 9
  102. 1 1
  103. 2 -4
  104. 4 -16
  105. 6 -36
  106. query R
  107. SELECT sum(w) FROM t GROUP BY k, v ORDER BY v DESC LIMIT 10
  108. ----
  109. 125
  110. 27
  111. 1
  112. 8
  113. 94
  114. 216
  115. skipif postgresql # Cockroach preserves sort ordering when not required to.
  116. query I
  117. SELECT k FROM (SELECT k, v FROM t ORDER BY v LIMIT 4)
  118. ----
  119. 6
  120. 4
  121. 2
  122. 1
  123. skipif postgresql # Cockroach preserves sort ordering when not required to.
  124. query I
  125. SELECT k FROM (SELECT k, v, w FROM t ORDER BY v LIMIT 4)
  126. ----
  127. 6
  128. 4
  129. 2
  130. 1
  131. # Use expression for LIMIT/OFFSET value.
  132. # TODO(benesch): support this.
  133. query error Expected end of statement, found number
  134. SELECT k, v FROM t ORDER BY k LIMIT length(pg_typeof(123))
  135. ----
  136. 1 1
  137. 2 -4
  138. 3 9
  139. query error Expected end of statement, found number
  140. SELECT k, v FROM t ORDER BY k LIMIT length(pg_typeof(123)) OFFSET length(pg_typeof(123))-2
  141. ----
  142. 2 -4
  143. 3 9
  144. 4 -16
  145. query error Expected end of statement, found number
  146. SELECT k, v FROM t ORDER BY k OFFSET (SELECT count(*)-3 FROM t)
  147. ----
  148. 4 -16
  149. 5 25
  150. 6 -36
  151. query error Expected end of statement, found number
  152. SELECT k, v FROM t ORDER BY k LIMIT (SELECT count(*)-3 FROM t) OFFSET (SELECT count(*)-5 FROM t)
  153. ----
  154. 2 -4
  155. 3 9
  156. 4 -16