| 1 | #include "catch.hpp" | 
|---|
| 2 | #include "test_helpers.hpp" | 
|---|
| 3 |  | 
|---|
| 4 | using namespace duckdb; | 
|---|
| 5 | using namespace std; | 
|---|
| 6 |  | 
|---|
| 7 | TEST_CASE( "Test LIMIT keyword", "[limit]") { | 
|---|
| 8 | unique_ptr<QueryResult> result; | 
|---|
| 9 | DuckDB db(nullptr); | 
|---|
| 10 | Connection con(db); | 
|---|
| 11 | con.EnableQueryVerification(); | 
|---|
| 12 |  | 
|---|
| 13 | REQUIRE_NO_FAIL(con.Query( "CREATE TABLE test (a INTEGER, b INTEGER);")); | 
|---|
| 14 | REQUIRE_NO_FAIL(con.Query( "INSERT INTO test VALUES (11, 22), (12, 21), (13, 22)")); | 
|---|
| 15 |  | 
|---|
| 16 | // constant limit | 
|---|
| 17 | result = con.Query( "SELECT a FROM test LIMIT 1"); | 
|---|
| 18 | REQUIRE(CHECK_COLUMN(result, 0, {11})); | 
|---|
| 19 |  | 
|---|
| 20 | // decimal limit | 
|---|
| 21 | result = con.Query( "SELECT a FROM test LIMIT 1.5"); | 
|---|
| 22 | REQUIRE(CHECK_COLUMN(result, 0, {11})); | 
|---|
| 23 |  | 
|---|
| 24 | // LIMIT with operation | 
|---|
| 25 | result = con.Query( "SELECT a FROM test LIMIT 2-1"); | 
|---|
| 26 | REQUIRE(CHECK_COLUMN(result, 0, {11})); | 
|---|
| 27 |  | 
|---|
| 28 | // LIMIT with non-scalar should fail | 
|---|
| 29 | REQUIRE_FAIL(con.Query( "SELECT a FROM test LIMIT a")); | 
|---|
| 30 | // LIMIT with non-scalar operation should also fail | 
|---|
| 31 | REQUIRE_FAIL(con.Query( "SELECT a FROM test LIMIT a+1")); | 
|---|
| 32 |  | 
|---|
| 33 | // aggregate in limit | 
|---|
| 34 | REQUIRE_FAIL(con.Query( "SELECT a FROM test LIMIT SUM(42)")); | 
|---|
| 35 | // window function in limit | 
|---|
| 36 | REQUIRE_FAIL(con.Query( "SELECT a FROM test LIMIT row_number() OVER ()")); | 
|---|
| 37 | // subquery in limit | 
|---|
| 38 | REQUIRE_FAIL(con.Query( "SELECT a FROM test LIMIT (SELECT MIN(a) FROM test)")); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | TEST_CASE( "LIMIT Bug #321 Crazy Result", "[limit]") { | 
|---|
| 42 | unique_ptr<QueryResult> result; | 
|---|
| 43 | DuckDB db(nullptr); | 
|---|
| 44 | Connection con(db); | 
|---|
| 45 | con.EnableQueryVerification(); | 
|---|
| 46 |  | 
|---|
| 47 | REQUIRE_NO_FAIL(con.Query( "CREATE TABLE test (a STRING);")); | 
|---|
| 48 | REQUIRE_NO_FAIL(con.Query( "INSERT INTO test VALUES ('Hello World')")); | 
|---|
| 49 |  | 
|---|
| 50 | auto prep = con.Prepare( "SELECT * FROM test LIMIT 3"); | 
|---|
| 51 | vector<Value> params; | 
|---|
| 52 | params.clear(); | 
|---|
| 53 | result = prep->Execute(params); | 
|---|
| 54 | REQUIRE(CHECK_COLUMN(result, 0, { "Hello World"})); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|