PostgreSQL dialect: unary minus binds more loosely than `^`, so `-1 ^ 0` returns -1

Aperta Adatta ai principianti
#27,094 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust, sql
Ambito
databases, testing

Direzione di ricerca

Il seguito si svolge in DataFusion dopo la correzione in apache/datafusion-sqlparser-rs#2819. Verifica prima quale versione di sqlparser include la correzione e come DataFusion blocca o aggiorna sqlparser; poi individua i casi sqllogictest esistenti per l’analisi del dialetto PostgreSQL. Il lavoro è completato quando la dipendenza include la correzione e i test coprono -1 ^ 0 e -2 ^ 2 con risultati corrispondenti a PostgreSQL.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

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 sqlparser release that has the fix.
  • Add a sqllogictest case for -1 ^ 0 and -2 ^ 2 with the PostgreSQL dialect.

Related: #22461 (binary operator precedence against PostgreSQL).

Lingua principale
Rust
Stelle
9.6k
Fork
2.6k
Merge medio
3g 21h
PR unite (30g)
331

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/datafusion

Tutte le issue di apache/datafusion

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.