Skip to content

Commit d60bdc0

Browse files
committed
Allow LIMIT/OFFSET/FETCH without FROM
Postgres allows it, as does ANSI SQL per the <query expression> definition: https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#_7_13_query_expression
1 parent c1509b3 commit d60bdc0

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/dialect/keywords.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,16 +420,16 @@ define_keywords!(
420420
/// can be parsed unambiguously without looking ahead.
421421
pub const RESERVED_FOR_TABLE_ALIAS: &[&str] = &[
422422
// Reserved as both a table and a column alias:
423-
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT,
423+
WITH, SELECT, WHERE, GROUP, ORDER, LIMIT, OFFSET, FETCH, UNION, EXCEPT, INTERSECT,
424424
// Reserved only as a table alias in the `FROM`/`JOIN` clauses:
425-
ON, JOIN, INNER, CROSS, FULL, LEFT, RIGHT, NATURAL, USING, LIMIT, OFFSET, FETCH,
425+
ON, JOIN, INNER, CROSS, FULL, LEFT, RIGHT, NATURAL, USING,
426426
];
427427

428428
/// Can't be used as a column alias, so that `SELECT <expr> alias`
429429
/// can be parsed unambiguously without looking ahead.
430430
pub const RESERVED_FOR_COLUMN_ALIAS: &[&str] = &[
431431
// Reserved as both a table and a column alias:
432-
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT,
432+
WITH, SELECT, WHERE, GROUP, ORDER, LIMIT, OFFSET, FETCH, UNION, EXCEPT, INTERSECT,
433433
// Reserved only as a column alias in the `SELECT` clause:
434434
FROM,
435435
];

tests/sqlparser_common.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ fn parse_limit_is_not_an_alias() {
216216
// In dialects supporting LIMIT it shouldn't be parsed as a table alias
217217
let ast = verified_query("SELECT id FROM customer LIMIT 1");
218218
assert_eq!(Some(ASTNode::SQLValue(Value::Long(1))), ast.limit);
219+
220+
let ast = verified_query("SELECT 1 LIMIT 5");
221+
assert_eq!(Some(ASTNode::SQLValue(Value::Long(5))), ast.limit);
219222
}
220223

221224
#[test]
@@ -2194,6 +2197,8 @@ fn parse_offset() {
21942197
},
21952198
_ => panic!("Test broke"),
21962199
}
2200+
let ast = verified_query("SELECT 'foo' OFFSET 0 ROWS");
2201+
assert_eq!(ast.offset, Some(ASTNode::SQLValue(Value::Long(0))));
21972202
}
21982203

21992204
#[test]
@@ -2213,6 +2218,8 @@ fn parse_fetch() {
22132218
};
22142219
let ast = verified_query("SELECT foo FROM bar FETCH FIRST 2 ROWS ONLY");
22152220
assert_eq!(ast.fetch, Some(FETCH_FIRST_TWO_ROWS_ONLY));
2221+
let ast = verified_query("SELECT 'foo' FETCH FIRST 2 ROWS ONLY");
2222+
assert_eq!(ast.fetch, Some(FETCH_FIRST_TWO_ROWS_ONLY));
22162223
let ast = verified_query("SELECT foo FROM bar FETCH FIRST ROWS ONLY");
22172224
assert_eq!(
22182225
ast.fetch,

0 commit comments

Comments
 (0)