Skip to content

PostgreSQL: Fix unary +/- precedence before exponentiation - #2829

Open
Surajsuthar wants to merge 10 commits into
apache:mainfrom
Surajsuthar:fix/postgresql-unary-operator-precedence
Open

Surajsuthar wants to merge 10 commits into
apache:mainfrom
Surajsuthar:fix/postgresql-unary-operator-precedence

Conversation

@Surajsuthar

@Surajsuthar Surajsuthar commented Nov 5, 2026 •

Copy link
Copy Markdown

Add dialect-specific precedence for unary + and - so PostgreSQL
binds them more tightly than ^, while preserving generic-dialect behavior.

#2819

@LucaCappelletti94 LucaCappelletti94 added the bug Something isn't working label Nov 5, 2026
@LucaCappelletti94
LucaCappelletti94 self-requested a review November 5, 2026 18:01
@codecov-commenter

codecov-commenter commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.20%. Comparing base (bac1468) to head (e1774c6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2829   +/-   ##
=======================================
  Coverage   83.20%   83.20%           
=======================================
  Files          42       42           
  Lines       36471    36472    +1     
  Branches    36471    36472    +1     
=======================================
+ Hits        30345    30346    +1     
  Misses       2632     2632           
  Partials     3494     3494           

☔ 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.

SQLiteDialect::prec_value matches every variant, so it needs the new one too.

-            Precedence::MulDivModOp => 40,
+            Precedence::MulDivModOp | Precedence::UnaryPlusMinus => 40,

I have looked into whether this precedence bug applies also to the other dialects, and it does in some, so it will need to be addressed. DuckDB, MySQL and MariaDB all bind ^ tighter than * (2 * 3 ^ 2 is 18 in DuckDB 1.4.4, and 2 * 3 ^ 1 is 4 in MySQL 8.4 and MariaDB 11.8), while the DuckDB and MySQL dialects parse (2 * 3) ^ n. The DuckDB dialect also parses ^ as BitwiseXor, although it is exponentiation there. Those fixes are outside this PR and can follow separately once the new level lands, since their tables need UnaryPlusMinus above Caret. If you happen to have time, such PRs would be welcomed.

Comment thread src/dialect/mod.rs Resolved
Comment thread src/dialect/mod.rs Resolved
Comment thread src/dialect/mod.rs Resolved
Comment thread src/dialect/postgresql.rs Resolved
Comment thread src/dialect/postgresql.rs Resolved
Comment thread src/dialect/postgresql.rs Resolved
Comment thread src/parser/mod.rs Resolved
Comment thread tests/sqlparser_postgres.rs Resolved

@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.

In a little while I will open a PR adding a fuzzer exactly aimed at finding these issues, do use it before the next review round as I think it could be useful. It unfortunately finds still several expression priority issues.

Comment thread src/parser/mod.rs Resolved
Comment thread src/parser/mod.rs Resolved
Comment thread src/parser/mod.rs Resolved
Comment thread tests/sqlparser_postgres.rs Resolved

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.

You could rename this seed to postgres_2829_unary_minus_caret.sql, since every other fuzz_seeds/<dialect>_<n>_*.sql name carries the number of the sqlparser PR that added it, and 27094 is the DataFusion issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working PostgreSQL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants