query I
SELECT number FROM table(numbers(100)) ORDER BY number LIMIT 5
----
0
1
2
3
4

query I
SELECT number FROM table(numbers(100)) ORDER BY number limit 5
----
0
1
2
3
4

query I
SELECT number FROM table(numbers(100)) ORDER BY number limit 1
----
0

query I
SELECT number FROM table(numbers(100)) ORDER BY number offset 1 limit 3
----
1
2
3

query I
SELECT number FROM table(numbers(100)) ORDER BY number limit 1 OFFSET 3
----
3

# is order limit and offset matters?
# query I
# SELECT number FROM table(numbers(100)) ORDER BY number OFFSET 3 limit 1

# ----
# 3

query I
SELECT number FROM table(numbers(100)) ORDER BY number limit 2
----
0
1

statement ok
drop table if exists t

statement ok
CREATE TABLE t (k INT PRIMARY KEY, v INT, w INT)

statement ok
INSERT INTO t VALUES (1, 1, 1), (2, -4, 8), (3, 9, 27), (4, -16, 94), (5, 25, 125), (6, -36, 216)

query III
SELECT * FROM t WHERE v > -20 AND w > 30 ORDER BY v LIMIT 2
----
4 -16 94
5 25 125

query II
SELECT k, v FROM t ORDER BY k LIMIT 5
----
1 1
2 -4
3 9
4 -16
5 25

query II
SELECT k, v FROM t ORDER BY k OFFSET 5
----
6 -36

query II rowsort
SELECT k, v FROM t ORDER BY v LIMIT 5 OFFSET 1
----
1 1
2 -4
3 9
4 -16
5 25

query II rowsort
SELECT k, v FROM t ORDER BY v DESC LIMIT 5 OFFSET 1
----
1 1
2 -4
3 9
4 -16
6 -36

query I
SELECT k, v, sum(w) FROM t GROUP BY k, v ORDER BY v DESC LIMIT 10
----
5 25 125
3 9 27
1 1 1
2 -4 8
4 -16 94
6 -36 216

query I rowsort
SELECT k FROM (SELECT k, v FROM t ORDER BY v LIMIT 4)
----
1
2
4
6

query I rowsort
SELECT k FROM (SELECT k, v, w FROM t ORDER BY v LIMIT 4)
----
1
2
4
6

query II
SELECT k, v FROM t ORDER BY k LIMIT 6
----
1 1
2 -4
3 9
4 -16
5 25
6 -36

query II
SELECT k, v FROM t ORDER BY k LIMIT 2
----
1 1
2 -4

query II rowsort
SELECT k, v FROM t ORDER BY k OFFSET 3
----
4 -16
5 25
6 -36

query II
SELECT k, v FROM t ORDER BY k LIMIT 3 OFFSET 3
----
4 -16
5 25
6 -36

query I
SELECT * FROM (select * from table(numbers(10)) a ORDER BY number LIMIT 5) OFFSET 3
----
3
4

statement ok
SELECT * FROM (select * from table(numbers(10)) a LIMIT 5) OFFSET 6

statement ok
drop table if exists t_47283

statement ok
CREATE TABLE t_47283(k INT PRIMARY KEY, a INT)

statement ok
INSERT INTO t_47283 VALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)

# This should return no results if it does, we incorrectly removed the hard
statement ok
SELECT * FROM (SELECT * FROM t_47283 ORDER BY k LIMIT 4) WHERE a > 5 LIMIT 1

# order by expr | limit expr is not support
# SELECT a FROM probe ORDER BY a LIMIT (SELECT v FROM vals WHERE k = 'maxint64') OFFSET (SELECT v FROM vals WHERE k = 'large')

statement ok
drop table if exists t65171

statement ok
CREATE TABLE t65171 (id INT PRIMARY KEY, x INT, y INT)

statement ok
INSERT INTO t65171 VALUES (0, 1, 2), (1, 1, 2), (2, 2, 3)

query II
SELECT * FROM t65171 WHERE x = 1 OR x = 2 ORDER BY y LIMIT 2
----
0 1 2
1 1 2

query III rowsort
SELECT * FROM t ORDER BY v, w LIMIT 3
----
2 -4 8
4 -16 94
6 -36 216