Skip to content

Postgres: allow reserved keywords as bare column alias - #2491

Open
BenSatori wants to merge 9 commits into
apache:mainfrom
BenSatori:postgres-bare-keyword-column-alias
Open

BenSatori wants to merge 9 commits into
apache:mainfrom
BenSatori:postgres-bare-keyword-column-alias

Conversation

@BenSatori

Copy link
Copy Markdown
Contributor

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

Comment thread src/dialect/postgresql.rs Outdated
Comment thread src/parser/mod.rs
Comment thread src/parser/mod.rs
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.
@BenSatori
BenSatori force-pushed the postgres-bare-keyword-column-alias branch from 907398c to 0de43ef Compare September 14, 2026 10:21
@BenSatori

Copy link
Copy Markdown
Contributor Author

I also tested directly on pg the aliases in the tests @LucaCappelletti94 .

@codecov-commenter

codecov-commenter commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.10%. Comparing base (560a13c) to head (e2f3873).

Files with missing lines Patch % Lines
src/parser/mod.rs 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2491   +/-   ##
=======================================
  Coverage   81.09%   81.10%           
=======================================
  Files          42       42           
  Lines       33684    33700   +16     
  Branches    33684    33700   +16     
=======================================
+ Hits        27316    27332   +16     
  Misses       2798     2798           
  Partials     3570     3570           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94 LucaCappelletti94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/dialect/postgresql.rs
Comment thread tests/sqlparser_postgres.rs
Comment thread tests/sqlparser_common.rs
@LucaCappelletti94 LucaCappelletti94 added the waiting on contributor The review needs further refinements by its author label Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PostgreSQL waiting on contributor The review needs further refinements by its author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants