LucaCappelletti94 commented on code in PR #2491:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/2491#discussion_r4091635058


##########
tests/sqlparser_postgres.rs:
##########
@@ -9967,6 +9967,30 @@ fn parse_insert_by_name_keywords_as_table_and_alias() {
     }
 }
 
+#[test]
+fn parse_reserved_keyword_as_bare_column_alias() {
+    // PostgreSQL allows (almost) any keyword, reserved or not, to be used as 
a bare
+    // (`AS`-less) column alias; only a small set of keywords require a 
leading `AS`.
+    // See <https://www.postgresql.org/docs/current/sql-keywords-appendix.html>
+    for kw in [
+        "analyze", "cluster", "end", "exclude", "explain", "lateral", 
"select", "values", "view",
+        "and", "or", "collate",
+    ] {
+        pg().one_statement_parses_to(
+            &format!("SELECT a {kw} FROM tbl_name"),
+            &format!("SELECT a AS {kw} FROM tbl_name"),
+        );
+    }
+
+    // `AND`/`OR`/`COLLATE` are still parsed as operators when followed by an 
operand.
+    pg().verified_stmt("SELECT 1 AND 2");
+    pg().verified_stmt("SELECT 1 OR 2");
+    pg().verified_stmt(r#"SELECT 1 COLLATE "de_DE" FROM tbl_name"#);

Review Comment:
   This line fails on the current head.
   
   ```suggestion
       pg().verified_stmt(r#"SELECT 1 COLLATE "de_DE" FROM tbl_name"#);
       pg().verified_stmt("SELECT a AND sort FROM tbl_name WHERE b OR top");
   ```



##########
tests/sqlparser_common.rs:
##########


Review Comment:
   You should add the negative non-PostgreSQL test. On the current head it 
fails because `SnowflakeDialect` parses `SELECT 1 AND` as `SELECT 1 AS AND`.
   
   ```suggestion
   }
   
   #[test]
   fn parse_and_or_not_bare_column_alias() {
       let dialects = all_dialects_but_pg();
       for sql in ["SELECT 1 AND", "SELECT 1 OR", "SELECT 1 AND, 2"] {
           assert!(dialects.parse_sql_statements(sql).is_err(), "{sql}");
       }
   }
   ```



##########
src/dialect/postgresql.rs:
##########
@@ -54,6 +54,25 @@ 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] = &[
+    Keyword::SELECT,
+    Keyword::ANALYZE,
+    Keyword::LATERAL,
+    Keyword::AND,
+    Keyword::OR,
+    Keyword::COLLATE,
+    Keyword::CLUSTER,
+    Keyword::END,
+    Keyword::EXCLUDE,
+    Keyword::EXPLAIN,
+    Keyword::VALUES,
+    Keyword::VIEW,

Review Comment:
   You should also list the keywords PostgreSQL does not know at all. 
`next_token_starts_an_expr` reads `SORT`, `TOP`, `MINUS` and `DISTRIBUTE` as 
clause keywords, so `SELECT a AND sort FROM t WHERE b OR top` now fails with 
`Expected: end of statement, found: sort`. It parses on `main` and runs on 
PostgreSQL 17.
   
   ```suggestion
       Keyword::VIEW,
       // Not PostgreSQL keywords at all.
       Keyword::DISTRIBUTE,
       Keyword::MINUS,
       Keyword::SORT,
       Keyword::TOP,
   ```



-- 
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]

Reply via email to