LucaCappelletti94 commented on code in PR #2491:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2491#discussion_r3982258347
##########
src/dialect/postgresql.rs:
##########
@@ -54,6 +54,13 @@ const RESERVED_EXCLUSIONS_FOR_TABLE_ALIAS: &[Keyword] = &[
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PostgreSqlDialect {}
+/// Keywords that PostgreSQL additionally allows (on top of
+/// [keywords::RESERVED_FOR_COLUMN_ALIAS]) to be used as a bare (`AS`-less)
+/// column alias.
+/// See <https://www.postgresql.org/docs/current/sql-keywords-appendix.html>
+const ADDITIONALLY_ALLOWED_BARE_COLUMN_ALIASES: &[Keyword] =
Review Comment:
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
```
&[
Keyword::ANALYZE,
Keyword::CLUSTER,
Keyword::END,
Keyword::EXCLUDE,
Keyword::EXPLAIN,
Keyword::LATERAL,
Keyword::SELECT,
Keyword::VALUES,
Keyword::VIEW,
]
```
##########
src/parser/mod.rs:
##########
@@ -1444,6 +1452,22 @@ impl<'a> Parser<'a> {
break;
}
+ // `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)
Review Comment:
`PostgreSQL` also permits these bare aliases before the following query
clauses. For example, `SELECT 1` and `FROM t` and `SELECT 1 collate FROM t` are
valid, but `FROM` is not among these four lookahead tokens, so the parser still
treats the alias as an operator and fails.
Please handle clause boundaries such as `FROM`, `INTO`, `WHERE`, and `ORDER
BY`, and add tests with aliases followed by a clause. I may have missed some
others, I may not recall all of them.
##########
src/parser/mod.rs:
##########
@@ -1444,6 +1452,22 @@ impl<'a> Parser<'a> {
break;
}
Review Comment:
This shared-parser change also enables these aliases for unrelated dialects.
I am not sure about how to best handle excessive permissiveness.
For example, `BigQueryDialect` considers `AND` a column alias under its
current predicate, so `SELECT 1 AND` now I believe would succeed instead of
reporting the missing right-hand operand.
`GoogleSQL` explicitly requires reserved keywords such as `AND`, `OR`, and
`COLLATE` to be quoted when used as identifiers.
Let's try to find a clean way to handle these cases and add negative
non-PostgreSQL tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]