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

オープン 初心者向け
#27,094 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust, sql
領域
databases, testing

調査の方向性

apache/datafusion-sqlparser-rs#2819 の修正後の対応は DataFusion で行います。まず、どの sqlparser リリースにその修正が含まれるか、また DataFusion が sqlparser のバージョンをどのように固定または更新しているかを確認します。次に、PostgreSQL 方言の解析に関する既存の sqllogictest ケースを探します。依存関係に修正が含まれ、テストで -1 ^ 0 と -2 ^ 2 をカバーし、結果が PostgreSQL と一致すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Rust
スター
9.6k
フォーク
2.6k
平均マージ
3日 21時間
マージ済み PR(30日)
331

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/datafusion のほかの issue

apache/datafusion の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。