-
Notifications
You must be signed in to change notification settings - Fork 60
Fix contextual keyword parsing #327
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -580,13 +580,13 @@ func (p *Parser) parseColumnExpr(pos Pos) (Expr, error) { //nolint:funlen | |
|
|
||
| return interval, nil | ||
| case p.matchKeyword(KeywordDate), p.matchKeyword(KeywordTimestamp): | ||
| nextToken, err := p.lexer.peekToken() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if nextToken != nil && nextToken.Kind == TokenKindString { | ||
| return p.parseString(p.Pos()) | ||
| literalPos := p.Pos() | ||
| savedState := p.lexer.saveState() | ||
| _ = p.lexer.consumeToken() | ||
| if p.matchTokenKind(TokenKindString) { | ||
| return p.parseString(literalPos) | ||
| } | ||
| p.lexer.restoreState(savedState) | ||
| return p.parseIdentOrFunction(pos) | ||
| case p.matchKeyword(KeywordCast): | ||
| return p.parseColumnCastExpr(pos) | ||
|
|
@@ -1172,7 +1172,7 @@ func (p *Parser) parseSelectItem() (*SelectItem, error) { | |
|
|
||
| modifiers := make([]*FunctionExpr, 0) | ||
| for { | ||
| if p.matchKeyword(KeywordExcept) || p.matchKeyword(KeywordApply) || p.matchKeyword(KeywordReplace) { | ||
| if (p.matchKeyword(KeywordExcept) && !p.peekKeyword(KeywordSelect)) || p.matchKeyword(KeywordApply) || p.matchKeyword(KeywordReplace) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the right operand is parenthesized, as in Useful? React with 👍 / 👎. |
||
| modifier, err := p.parseFunctionExpr(p.Pos()) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| SELECT DATE '2024-01-01', TIMESTAMP '2024-01-01 00:00:00', date AS timestamp; | ||
| SELECT * EXCEPT (a) FROM t; | ||
| SELECT 1 EXCEPT SELECT 2; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| -- Origin SQL: | ||
| SELECT DATE '2024-01-01', TIMESTAMP '2024-01-01 00:00:00', date AS timestamp; | ||
| SELECT * EXCEPT (a) FROM t; | ||
| SELECT 1 EXCEPT SELECT 2; | ||
|
|
||
|
|
||
| -- Beautify SQL: | ||
| SELECT | ||
| '2024-01-01', | ||
| '2024-01-01 00:00:00', | ||
| date AS timestamp; | ||
| SELECT | ||
| * EXCEPT(a) | ||
| FROM | ||
| t; | ||
| SELECT | ||
| 1 | ||
| EXCEPT | ||
| SELECT | ||
| 2; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| -- Origin SQL: | ||
| SELECT DATE '2024-01-01', TIMESTAMP '2024-01-01 00:00:00', date AS timestamp; | ||
| SELECT * EXCEPT (a) FROM t; | ||
| SELECT 1 EXCEPT SELECT 2; | ||
|
|
||
|
|
||
| -- Format SQL: | ||
| SELECT '2024-01-01', '2024-01-01 00:00:00', date AS timestamp; | ||
| SELECT * EXCEPT(a) FROM t; | ||
| SELECT 1 EXCEPT SELECT 2; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| [ | ||
| { | ||
| "SelectPos": 0, | ||
| "StatementEnd": 76, | ||
| "With": null, | ||
| "Top": null, | ||
| "HasDistinct": false, | ||
| "DistinctOn": null, | ||
| "SelectItems": [ | ||
| { | ||
| "Expr": { | ||
| "LiteralPos": 7, | ||
| "LiteralEnd": 23, | ||
| "Literal": "2024-01-01" | ||
| }, | ||
| "Modifiers": [], | ||
| "Alias": null | ||
| }, | ||
| { | ||
| "Expr": { | ||
| "LiteralPos": 26, | ||
| "LiteralEnd": 56, | ||
| "Literal": "2024-01-01 00:00:00" | ||
| }, | ||
| "Modifiers": [], | ||
| "Alias": null | ||
| }, | ||
| { | ||
| "Expr": { | ||
| "Name": "date", | ||
| "QuoteType": 1, | ||
| "NamePos": 59, | ||
| "NameEnd": 63 | ||
| }, | ||
| "Modifiers": [], | ||
| "Alias": { | ||
| "Name": "timestamp", | ||
| "QuoteType": 1, | ||
| "NamePos": 67, | ||
| "NameEnd": 76 | ||
| } | ||
| } | ||
| ], | ||
| "From": null, | ||
| "Window": null, | ||
| "Prewhere": null, | ||
| "Where": null, | ||
| "GroupBy": null, | ||
| "WithTotal": false, | ||
| "Having": null, | ||
| "OrderBy": null, | ||
| "LimitBy": null, | ||
| "Limit": null, | ||
| "Settings": null, | ||
| "Format": null, | ||
| "UnionAll": null, | ||
| "UnionDistinct": null, | ||
| "Except": null, | ||
| "Intersect": null | ||
| }, | ||
| { | ||
| "SelectPos": 78, | ||
| "StatementEnd": 104, | ||
| "With": null, | ||
| "Top": null, | ||
| "HasDistinct": false, | ||
| "DistinctOn": null, | ||
| "SelectItems": [ | ||
| { | ||
| "Expr": { | ||
| "Name": "*", | ||
| "QuoteType": 0, | ||
| "NamePos": 85, | ||
| "NameEnd": 85 | ||
| }, | ||
| "Modifiers": [ | ||
| { | ||
| "Name": { | ||
| "Name": "EXCEPT", | ||
| "QuoteType": 1, | ||
| "NamePos": 87, | ||
| "NameEnd": 93 | ||
| }, | ||
| "Params": { | ||
| "LeftParenPos": 94, | ||
| "RightParenPos": 96, | ||
| "Items": { | ||
| "ListPos": 95, | ||
| "ListEnd": 96, | ||
| "HasDistinct": false, | ||
| "Items": [ | ||
| { | ||
| "Expr": { | ||
| "Name": "a", | ||
| "QuoteType": 1, | ||
| "NamePos": 95, | ||
| "NameEnd": 96 | ||
| }, | ||
| "Alias": null | ||
| } | ||
| ] | ||
| }, | ||
| "ColumnArgList": null | ||
| } | ||
| } | ||
| ], | ||
| "Alias": null | ||
| } | ||
| ], | ||
| "From": { | ||
| "FromPos": 98, | ||
| "Expr": { | ||
| "Table": { | ||
| "TablePos": 103, | ||
| "TableEnd": 104, | ||
| "Alias": null, | ||
| "Expr": { | ||
| "Database": null, | ||
| "Table": { | ||
| "Name": "t", | ||
| "QuoteType": 1, | ||
| "NamePos": 103, | ||
| "NameEnd": 104 | ||
| } | ||
| }, | ||
| "HasFinal": false | ||
| }, | ||
| "StatementEnd": 104, | ||
| "SampleRatio": null, | ||
| "HasFinal": false | ||
| } | ||
| }, | ||
| "Window": null, | ||
| "Prewhere": null, | ||
| "Where": null, | ||
| "GroupBy": null, | ||
| "WithTotal": false, | ||
| "Having": null, | ||
| "OrderBy": null, | ||
| "LimitBy": null, | ||
| "Limit": null, | ||
| "Settings": null, | ||
| "Format": null, | ||
| "UnionAll": null, | ||
| "UnionDistinct": null, | ||
| "Except": null, | ||
| "Intersect": null | ||
| }, | ||
| { | ||
| "SelectPos": 106, | ||
| "StatementEnd": 114, | ||
| "With": null, | ||
| "Top": null, | ||
| "HasDistinct": false, | ||
| "DistinctOn": null, | ||
| "SelectItems": [ | ||
| { | ||
| "Expr": { | ||
| "NumPos": 113, | ||
| "NumEnd": 114, | ||
| "Literal": "1", | ||
| "Base": 10 | ||
| }, | ||
| "Modifiers": [], | ||
| "Alias": null | ||
| } | ||
| ], | ||
| "From": null, | ||
| "Window": null, | ||
| "Prewhere": null, | ||
| "Where": null, | ||
| "GroupBy": null, | ||
| "WithTotal": false, | ||
| "Having": null, | ||
| "OrderBy": null, | ||
| "LimitBy": null, | ||
| "Limit": null, | ||
| "Settings": null, | ||
| "Format": null, | ||
| "UnionAll": null, | ||
| "UnionDistinct": null, | ||
| "Except": { | ||
| "SelectPos": 122, | ||
| "StatementEnd": 130, | ||
| "With": null, | ||
| "Top": null, | ||
| "HasDistinct": false, | ||
| "DistinctOn": null, | ||
| "SelectItems": [ | ||
| { | ||
| "Expr": { | ||
| "NumPos": 129, | ||
| "NumEnd": 130, | ||
| "Literal": "2", | ||
| "Base": 10 | ||
| }, | ||
| "Modifiers": [], | ||
| "Alias": null | ||
| } | ||
| ], | ||
| "From": null, | ||
| "Window": null, | ||
| "Prewhere": null, | ||
| "Where": null, | ||
| "GroupBy": null, | ||
| "WithTotal": false, | ||
| "Having": null, | ||
| "OrderBy": null, | ||
| "LimitBy": null, | ||
| "Limit": null, | ||
| "Settings": null, | ||
| "Format": null, | ||
| "UnionAll": null, | ||
| "UnionDistinct": null, | ||
| "Except": null, | ||
| "Intersect": null | ||
| }, | ||
| "Intersect": null | ||
| } | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a caller parses and formats a typed literal such as
SELECT DATE '2024-01-01', this returns a plainStringLiteralafter consuming the type keyword, so formatting producesSELECT '2024-01-01'(as the new golden file demonstrates). That changes the expression from a typed Date/Timestamp literal into a String and can alter comparisons, overload resolution, and result schemas; the AST must retain and re-emit the consumed literal type.Useful? React with 👍 / 👎.