Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new Keyword FETCH FIRST N ROWS ONLY in Presto Query #18968

Merged
merged 5 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ queryNoWith:
queryTerm
(ORDER BY sortItem (',' sortItem)*)?
(OFFSET offset=INTEGER_VALUE (ROW | ROWS)?)?
Copy link
Contributor

Choose a reason for hiding this comment

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

offset comes before "FETCH FIRST"

Copy link
Member Author

@skairali skairali Jan 27, 2023

Choose a reason for hiding this comment

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

This is addressed

(LIMIT limit=(INTEGER_VALUE | ALL))?
((LIMIT limit=(INTEGER_VALUE | ALL) | (FETCH FIRST fetchFirstNRows=INTEGER_VALUE ROWS ONLY))?)?
;

queryTerm
Expand Down Expand Up @@ -566,7 +566,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 +645,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