Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ queryOrganization
(DISTRIBUTE BY distributeBy+=expression (',' distributeBy+=expression)*)?
(SORT BY sort+=sortItem (',' sort+=sortItem)*)?
windows?
(LIMIT limit=expression)?
(LIMIT (ALL | limit=expression))?
;

multiInsertQueryBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging {
val withWindow = withOrder.optionalMap(windows)(withWindows)

// LIMIT
// - LIMIT ALL is the same as omitting the LIMIT clause
withWindow.optional(limit) {
Limit(typedVisit(limit), withWindow)
}
Expand Down
24 changes: 14 additions & 10 deletions sql/core/src/test/resources/sql-tests/inputs/limit.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@

-- limit on various data types
select * from testdata limit 2;
select * from arraydata limit 2;
select * from mapdata limit 2;
SELECT * FROM testdata LIMIT 2;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wonder why these should be upper-cased just for curiosity. Is this way preferred?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is preferred. All the SQL keywords are preferred to use the upper case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thank you.

SELECT * FROM arraydata LIMIT 2;
SELECT * FROM mapdata LIMIT 2;

-- foldable non-literal in limit
select * from testdata limit 2 + 1;
SELECT * FROM testdata LIMIT 2 + 1;

select * from testdata limit CAST(1 AS int);
SELECT * FROM testdata LIMIT CAST(1 AS int);

-- limit must be non-negative
select * from testdata limit -1;
SELECT * FROM testdata LIMIT -1;
SELECT * FROM testData TABLESAMPLE (-1 ROWS);

-- limit must be foldable
select * from testdata limit key > 3;
SELECT * FROM testdata LIMIT key > 3;

-- limit must be integer
select * from testdata limit true;
select * from testdata limit 'a';
SELECT * FROM testdata LIMIT true;
SELECT * FROM testdata LIMIT 'a';

-- limit within a subquery
select * from (select * from range(10) limit 5) where id > 3;
SELECT * FROM (SELECT * FROM range(10) LIMIT 5) WHERE id > 3;

-- limit ALL
SELECT * FROM testdata WHERE key < 3 LIMIT ALL;
48 changes: 33 additions & 15 deletions sql/core/src/test/resources/sql-tests/results/limit.sql.out
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 10
-- Number of queries: 12


-- !query 0
select * from testdata limit 2
SELECT * FROM testdata LIMIT 2
-- !query 0 schema
struct<key:int,value:string>
-- !query 0 output
Expand All @@ -12,7 +12,7 @@ struct<key:int,value:string>


-- !query 1
select * from arraydata limit 2
SELECT * FROM arraydata LIMIT 2
-- !query 1 schema
struct<arraycol:array<int>,nestedarraycol:array<array<int>>>
-- !query 1 output
Expand All @@ -21,7 +21,7 @@ struct<arraycol:array<int>,nestedarraycol:array<array<int>>>


-- !query 2
select * from mapdata limit 2
SELECT * FROM mapdata LIMIT 2
-- !query 2 schema
struct<mapcol:map<int,string>>
-- !query 2 output
Expand All @@ -30,7 +30,7 @@ struct<mapcol:map<int,string>>


-- !query 3
select * from testdata limit 2 + 1
SELECT * FROM testdata LIMIT 2 + 1
-- !query 3 schema
struct<key:int,value:string>
-- !query 3 output
Expand All @@ -40,15 +40,15 @@ struct<key:int,value:string>


-- !query 4
select * from testdata limit CAST(1 AS int)
SELECT * FROM testdata LIMIT CAST(1 AS int)
-- !query 4 schema
struct<key:int,value:string>
-- !query 4 output
1 1


-- !query 5
select * from testdata limit -1
SELECT * FROM testdata LIMIT -1
-- !query 5 schema
struct<>
-- !query 5 output
Expand All @@ -57,35 +57,53 @@ The limit expression must be equal to or greater than 0, but got -1;


-- !query 6
select * from testdata limit key > 3
SELECT * FROM testData TABLESAMPLE (-1 ROWS)
-- !query 6 schema
struct<>
-- !query 6 output
org.apache.spark.sql.AnalysisException
The limit expression must evaluate to a constant value, but got (testdata.`key` > 3);
The limit expression must be equal to or greater than 0, but got -1;


-- !query 7
select * from testdata limit true
SELECT * FROM testdata LIMIT key > 3
-- !query 7 schema
struct<>
-- !query 7 output
org.apache.spark.sql.AnalysisException
The limit expression must be integer type, but got boolean;
The limit expression must evaluate to a constant value, but got (testdata.`key` > 3);


-- !query 8
select * from testdata limit 'a'
SELECT * FROM testdata LIMIT true
-- !query 8 schema
struct<>
-- !query 8 output
org.apache.spark.sql.AnalysisException
The limit expression must be integer type, but got string;
The limit expression must be integer type, but got boolean;


-- !query 9
select * from (select * from range(10) limit 5) where id > 3
SELECT * FROM testdata LIMIT 'a'
-- !query 9 schema
struct<id:bigint>
struct<>
-- !query 9 output
org.apache.spark.sql.AnalysisException
The limit expression must be integer type, but got string;


-- !query 10
SELECT * FROM (SELECT * FROM range(10) LIMIT 5) WHERE id > 3
-- !query 10 schema
struct<id:bigint>
-- !query 10 output
4


-- !query 11
SELECT * FROM testdata WHERE key < 3 LIMIT ALL
-- !query 11 schema
struct<key:int,value:string>
-- !query 11 output
1 1
2 2
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,6 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
sortTest()
}

test("negative in LIMIT or TABLESAMPLE") {
val expected = "The limit expression must be equal to or greater than 0, but got -1"
var e = intercept[AnalysisException] {
sql("SELECT * FROM testData TABLESAMPLE (-1 rows)")
}.getMessage
assert(e.contains(expected))
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. This was duplicated.

test("CTE feature") {
checkAnswer(
sql("with q1 as (select * from testData limit 10) select * from q1"),
Expand Down