Conversation
PostgreSQL allows almost any keyword (SELECT, ANALYZE, LATERAL, AND, OR, COLLATE, ...) to be used as a bare (AS-less) column alias unless it is in a small set of keywords that require a leading AS. See https://www.postgresql.org/docs/current/sql-keywords-appendix.html Previously: SELECT (SELECT c FROM tbl_name LIMIT 1) select; failed to parse.
907398c to
0de43ef
Compare
|
I also tested directly on pg the aliases in the tests @LucaCappelletti94 . |
…rd-column-alias # Conflicts: # tests/sqlparser_postgres.rs
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2491 +/- ##
=======================================
Coverage 80.96% 80.97%
=======================================
Files 42 42
Lines 33386 33402 +16
Branches 33386 33402 +16
=======================================
+ Hits 27032 27046 +14
- Misses 2789 2790 +1
- Partials 3565 3566 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
LucaCappelletti94
left a comment
There was a problem hiding this comment.
Snowflake's is_column_alias returns true for any keyword not in its own list, so the new loop break accepts SELECT 1 AND and SELECT 1 OR. Snowflake reserves both (reserved keywords).
Do extend the keyword list, and add the necessary tests for them.
- Keyword::FROM
+ Keyword::AND
+ | Keyword::FROM
| Keyword::GROUP
| Keyword::HAVING
| Keyword::INTERSECT
| Keyword::INTO
| Keyword::MINUS
+ | Keyword::OR
| Keyword::ORDER| Keyword::EXCLUDE, | ||
| Keyword::EXPLAIN, | ||
| Keyword::VALUES, | ||
| Keyword::VIEW, |
There was a problem hiding this 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.
| Keyword::VIEW, | |
| Keyword::VIEW, | |
| // Not PostgreSQL keywords at all. | |
| Keyword::DISTRIBUTE, | |
| Keyword::MINUS, | |
| Keyword::SORT, | |
| Keyword::TOP, |
| // `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"#); |
There was a problem hiding this comment.
This line fails on the current head.
| pg().verified_stmt(r#"SELECT 1 COLLATE "de_DE" FROM tbl_name"#); | |
| 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"); |
There was a problem hiding this 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.
| } | |
| #[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}"); | |
| } | |
| } |
PostgreSQL allows almost any keyword (SELECT, ANALYZE, LATERAL, AND, OR, COLLATE, ...) to be used as a bare (AS-less) column alias unless it is in a small set of keywords that require a leading AS.
See https://www.postgresql.org/docs/current/sql-keywords-appendix.html
Previously:
SELECT (SELECT c FROM tbl_name LIMIT 1) select;(or analyze/lateral/and/or/collate)
failed to parse.
This PR fixes that