Skip to content

Commit e2b765d

Browse files
committed
Add HAVING as a reserved work for column/table aliases
The ANSI spec allows eg `SELECT 1 HAVING true` or `SELECT * FROM foo HAVING true`
1 parent ba13bb2 commit e2b765d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/dialect/keywords.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ define_keywords!(
426426
/// can be parsed unambiguously without looking ahead.
427427
pub const RESERVED_FOR_TABLE_ALIAS: &[&str] = &[
428428
// Reserved as both a table and a column alias:
429-
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT,
429+
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT, HAVING,
430430
// Reserved only as a table alias in the `FROM`/`JOIN` clauses:
431431
ON, JOIN, INNER, CROSS, FULL, LEFT, RIGHT, NATURAL, USING, LIMIT, OFFSET, FETCH,
432432
];
@@ -435,7 +435,7 @@ pub const RESERVED_FOR_TABLE_ALIAS: &[&str] = &[
435435
/// can be parsed unambiguously without looking ahead.
436436
pub const RESERVED_FOR_COLUMN_ALIAS: &[&str] = &[
437437
// Reserved as both a table and a column alias:
438-
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT,
438+
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT, HAVING,
439439
// Reserved only as a column alias in the `SELECT` clause:
440440
FROM,
441441
];

tests/sqlparser_common.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,21 @@ fn parse_aggregate_with_group_by() {
11551155
//TODO: assertions
11561156
}
11571157

1158+
#[test]
1159+
fn parse_having() {
1160+
let sql = "SELECT 1 HAVING true";
1161+
let ast = verified_only_select(sql);
1162+
assert_eq!(&Some(ASTNode::SQLValue(Value::Boolean(true))), &ast.having);
1163+
1164+
let sql = "SELECT a FROM foo HAVING true";
1165+
let ast = verified_only_select(sql);
1166+
assert_eq!(&Some(ASTNode::SQLValue(Value::Boolean(true))), &ast.having);
1167+
1168+
let sql = "SELECT a FROM foo GROUP BY a, b HAVING true";
1169+
let ast = verified_only_select(sql);
1170+
assert_eq!(&Some(ASTNode::SQLValue(Value::Boolean(true))), &ast.having);
1171+
}
1172+
11581173
#[test]
11591174
fn parse_literal_string() {
11601175
let sql = "SELECT 'one', N'national string', X'deadBEEF'";

0 commit comments

Comments
 (0)