Skip to content

Commit

Permalink
Introduce new Keyword FETCH FIRST N ROWS ONLY in Presto Query
Browse files Browse the repository at this point in the history
Issue details : #18935

We are introducing a new keyword FETCH FIRST N ROWS ONLY which can be used in presto queries. The keyword will work in sql queries just like LIMIT keyword to limit the number of rows returned from an sql select query.

For Example : select * from table FETCH FIRST 3 ROWS ONLY will return only 3 rows as a result of the sql query.

== RELEASE NOTES ==

General Changes

Introducing a new Keyword FETCH FIRST N ROWS ONLY to the Presto Query
  • Loading branch information
skairali committed Jan 24, 2023
2 parents 1387be7 + ea64fb6 commit 8c40406
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ externalRoutineName
queryNoWith:
queryTerm
(ORDER BY sortItem (',' sortItem)*)?
(FETCH FIRST fetchFirstNRows=INTEGER_VALUE ROWS ONLY)?
(OFFSET offset=INTEGER_VALUE (ROW | ROWS)?)?
(LIMIT limit=(INTEGER_VALUE | ALL))?
;
Expand Down Expand Up @@ -566,7 +567,7 @@ nonReserved
| CALL | CALLED | CASCADE | CATALOGS | COLUMN | COLUMNS | COMMENT | COMMIT | COMMITTED | CURRENT | CURRENT_ROLE
| DATA | DATE | DAY | DEFINER | DESC | DETERMINISTIC | DISTRIBUTED
| EXCLUDING | EXPLAIN | EXTERNAL
| FILTER | FIRST | FOLLOWING | FORMAT | FUNCTION | FUNCTIONS
| FETCH | FILTER | FIRST | FOLLOWING | FORMAT | FUNCTION | FUNCTIONS
| GRANT | GRANTED | GRANTS | GRAPHVIZ
| HOUR
| IF | IGNORE | INCLUDING | INPUT | INTERVAL | INVOKER | IO | ISOLATION
Expand Down Expand Up @@ -645,6 +646,7 @@ EXPLAIN: 'EXPLAIN';
EXTRACT: 'EXTRACT';
EXTERNAL: 'EXTERNAL';
FALSE: 'FALSE';
FETCH: 'FETCH';
FILTER: 'FILTER';
FIRST: 'FIRST';
FOLLOWING: 'FOLLOWING';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,14 @@ public Node visitQueryNoWith(SqlBaseParser.QueryNoWithContext context)
offset = Optional.of(new Offset(Optional.of(getLocation(context.OFFSET())), getTextIfPresent(context.offset).orElseThrow(() -> new IllegalStateException("Missing OFFSET row count"))));
}

Optional<String> limit = Optional.empty();
if (context.LIMIT() != null) {
limit = getTextIfPresent(context.limit);
}
else if (context.FETCH() != null) {
limit = getTextIfPresent(context.fetchFirstNRows);
}

if (term instanceof QuerySpecification) {
// When we have a simple query specification
// followed by order by, offset, limit, fold the order by and limit
Expand All @@ -754,7 +762,7 @@ public Node visitQueryNoWith(SqlBaseParser.QueryNoWithContext context)
query.getHaving(),
orderBy,
offset,
getTextIfPresent(context.limit)),
limit),
Optional.empty(),
Optional.empty(),
Optional.empty());
Expand All @@ -766,7 +774,7 @@ public Node visitQueryNoWith(SqlBaseParser.QueryNoWithContext context)
term,
orderBy,
offset,
getTextIfPresent(context.limit));
limit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,22 @@ public void testSelectWithOffset()
Optional.empty(),
Optional.empty()));

assertStatement("SELECT * FROM table1 order by x FETCH FIRST 10 ROWS ONLY",
new Query(
Optional.empty(),
new QuerySpecification(
selectList(new AllColumns()),
Optional.of(new Table(QualifiedName.of("table1"))),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("x"), ASCENDING, UNDEFINED)))),
Optional.empty(),
Optional.of("10")),
Optional.empty(),
Optional.empty(),
Optional.empty()));

Query valuesQuery = query(values(
row(new LongLiteral("1"), new StringLiteral("1")),
row(new LongLiteral("2"), new StringLiteral("2"))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Object[][] getStatements()
{"select * from 'oops",
"line 1:15: mismatched input '''. Expecting: '(', 'LATERAL', 'UNNEST', <identifier>"},
{"select *\nfrom x\nfrom",
"line 3:1: mismatched input 'from'. Expecting: ',', '.', 'AS', 'CROSS', 'EXCEPT', 'FULL', 'GROUP', 'HAVING', 'INNER', 'INTERSECT', 'JOIN', 'LEFT', 'LIMIT', 'NATURAL', 'OFFSET', " +
"line 3:1: mismatched input 'from'. Expecting: ',', '.', 'AS', 'CROSS', 'EXCEPT', 'FETCH', 'FULL', 'GROUP', 'HAVING', 'INNER', 'INTERSECT', 'JOIN', 'LEFT', 'LIMIT', 'NATURAL', 'OFFSET', " +
"'ORDER', 'RIGHT', 'TABLESAMPLE', 'UNION', 'WHERE', <EOF>, <identifier>"},
{"select *\nfrom x\nwhere from",
"line 3:7: mismatched input 'from'. Expecting: <expression>"},
Expand Down Expand Up @@ -112,7 +112,7 @@ public Object[][] getStatements()
{"SELECT foo(*) filter (",
"line 1:23: mismatched input '<EOF>'. Expecting: 'WHERE'"},
{"SELECT * FROM t t x",
"line 1:19: mismatched input 'x'. Expecting: '(', ',', 'CROSS', 'EXCEPT', 'FULL', 'GROUP', 'HAVING', 'INNER', 'INTERSECT', 'JOIN', 'LEFT', 'LIMIT', 'NATURAL', 'OFFSET', 'ORDER', " +
"line 1:19: mismatched input 'x'. Expecting: '(', ',', 'CROSS', 'EXCEPT', 'FETCH', 'FULL', 'GROUP', 'HAVING', 'INNER', 'INTERSECT', 'JOIN', 'LEFT', 'LIMIT', 'NATURAL', 'OFFSET', 'ORDER', " +
"'RIGHT', 'TABLESAMPLE', 'UNION', 'WHERE', <EOF>"},
{"SELECT * FROM t WHERE EXISTS (",
"line 1:31: mismatched input '<EOF>'. Expecting: <query>"},
Expand Down

0 comments on commit 8c40406

Please sign in to comment.