-
Notifications
You must be signed in to change notification settings - Fork 772
Postgres: allow reserved keywords as bare column alias #2491
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: main
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 |
|---|---|---|
|
|
@@ -1422,11 +1422,19 @@ impl<'a> Parser<'a> { | |
| // Parse an optional collation cast operator following `expr`. | ||
| // | ||
| // For example (MSSQL): t1.a COLLATE Latin1_General_CI_AS | ||
| if !self.in_column_definition_state() && self.parse_keyword(Keyword::COLLATE) { | ||
| expr = Expr::Collate { | ||
| expr: Box::new(expr), | ||
| collation: self.parse_object_name(false)?, | ||
| }; | ||
| // | ||
| // `COLLATE` with no collation name following it is not a cast operator; it's a bare | ||
| // (`AS`-less) column alias, e.g. Postgres' `SELECT 1 collate`. | ||
| if !self.in_column_definition_state() && self.peek_keyword(Keyword::COLLATE) { | ||
| if let Some(collation) = self.maybe_parse(|parser| { | ||
| parser.expect_keyword(Keyword::COLLATE)?; | ||
| parser.parse_object_name(false) | ||
| })? { | ||
| expr = Expr::Collate { | ||
| expr: Box::new(expr), | ||
| collation, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| debug!("prefix: {expr:?}"); | ||
|
|
@@ -1444,6 +1452,22 @@ impl<'a> Parser<'a> { | |
| break; | ||
| } | ||
|
Contributor
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. This shared-parser change also enables these aliases for unrelated dialects. I am not sure about how to best handle excessive permissiveness. For example,
Let's try to find a clean way to handle these cases and add negative non-PostgreSQL tests. |
||
|
|
||
| // `AND`/`OR`/`COLLATE` with no right-hand expression following it is not a | ||
| // binary operator or collation cast; it's a bare (`AS`-less) column alias, | ||
| // e.g. Postgres' `SELECT 1 and` or `SELECT 1 collate`. | ||
| if let Token::Word(w) = &self.peek_token_ref().token { | ||
| let kw = w.keyword; | ||
| if matches!(kw, Keyword::AND | Keyword::OR | Keyword::COLLATE) | ||
|
Contributor
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.
Please handle clause boundaries such as |
||
| && matches!( | ||
| self.peek_nth_token_ref(1).token, | ||
| Token::EOF | Token::Comma | Token::RParen | Token::SemiColon | ||
| ) | ||
| && self.dialect.is_column_alias(&kw, self) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| expr = self.parse_infix(expr, next_precedence)?; | ||
| } | ||
| Ok(expr) | ||
|
|
||
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.
I believe at least these other ones are missing, I am not sure which of these others should be included: https://github.com/postgres/postgres/blob/master/src/include/parser/kwlist.h