PostgreSQL dialect: unary minus binds more loosely than `^`, so `-1 ^ 0` returns -1
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
Research direction
The follow-up is in DataFusion after the fix in apache/datafusion-sqlparser-rs#2819. First check which sqlparser release includes that fix and how DataFusion pins or upgrades sqlparser; then find the existing sqllogictest cases for PostgreSQL dialect parsing. Done when the dependency includes the fix and tests cover -1 ^ 0 and -2 ^ 2 with results matching PostgreSQL.
Written by the indexing model from the issue text.
Description
This issue tracks the parser fix in apache/datafusion-sqlparser-rs#2819. The cause is in sqlparser, and DataFusion needs follow-up work after the fix lands.
Describe the bug
With the PostgreSQL dialect, ^ is the power operator. Unary minus binds more loosely than ^, so -1 ^ 0 is planned as -(1 ^ 0) and returns -1.
In PostgreSQL, unary minus binds more tightly than ^ (operator precedence table). PostgreSQL computes (-1) ^ 0 and returns 1.
To Reproduce
SET datafusion.sql_parser.dialect = 'PostgreSQL';
SELECT -1 ^ 0;
SELECT -2 ^ 2;
EXPLAIN SELECT -2 ^ 2;
On main (c120619de), datafusion-cli returns:
+------------------------------+
| (- power(Int64(1),Int64(0))) |
+------------------------------+
| -1.0 |
+------------------------------+
+------------------------------+
| (- power(Int64(2),Int64(2))) |
+------------------------------+
| -4.0 |
+------------------------------+
The plan shows (- power(Int64(2),Int64(2))), so the minus is applied after the power.
Expected behavior
The same results as PostgreSQL 17:
postgres=# SELECT -1 ^ 0, -2 ^ 2;
?column? | ?column?
----------+----------
1 | 4
DuckDB gives the same results (1 and 4).
Additional context
The cause is in sqlparser: the operand of a unary - or + is parsed at Precedence::MulDivModOp, but PostgreSqlDialect puts ^ above that level. The details are in apache/datafusion-sqlparser-rs#2819.
Work in DataFusion after the parser fix:
- Upgrade to a
sqlparserrelease that has the fix. - Add a
sqllogictestcase for-1 ^ 0and-2 ^ 2with the PostgreSQL dialect.
Related: #22461 (binary operator precedence against PostgreSQL).
- Dominant language
- Rust
- Stars
- 9.6k
- Forks
- 2.6k
- Avg merge
- 3d 5h
- Merged PRs (30d)
- 352
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/datafusion
-
`verify-release-candidate.sh` version check doesn't catch `-dev` versions and is skipped on macOSPossibly taken @vinycalves claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
apache/datafusion#27563 · 1 comment ·
Maintainers usually reply within 1 day
-
`MoveTowardsLeafNodes` placement implies speculative evaluation, but the docs don't say soPossibly taken @Inf1mum claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
apache/datafusion#27487 · 3 comments ·
Maintainers usually reply within 1 day
-
`lpad` and `rpad` with a literal length above `i32::MAX` error on a NULL string or fillPossibly taken @TechWizard9999 claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
apache/datafusion#27482 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
-
Window constructors panic when the expression list is emptyPossibly taken @abp002 claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/datafusion#27315 · 3 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
Substrait consumer doesn't recognise `shift_left` / `shift_right` from functions_arithmetic.yamlPossibly taken @jonasHanhan claimed this 15 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
apache/datafusion#27059 ·
Maintainers usually reply within 1 day
All issues in apache/datafusion
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
tinystream-dev/tinystream#132 ·
-
contract good first issue testing
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Emmyt24/nova-launch#2436 ·
Maintainers usually reply within 1 day