apache / apache/datafusion-sqlparser-rs

Bitwise `&` and `->` group differently in the PostgreSQL and MySQL/Generic dialects

未关闭 适合新手
#2,461 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Rust
星标
3.5k
派生
772
平均合并
4 天 9 小时
30 天内合并 PR
17

描述

`&` and `->` have the same precedence in PostgreSQL but not in the default precedence table, so the
same expression produces two different trees depending on dialect:

| SQL | `PostgreSqlDialect` | `MySqlDialect` / `GenericDialect` |
|---|---|---|
| `a -> b & c` | `((a -> b) & c)` | `(a -> (b & c))` |
| `a -> b \| c` | `((a -> b) \| c)` | `((a -> b) \| c)` |
| `a -> b ^ c` | `(a -> (b ^ c))` | `(a -> (b ^ c))` |
| `a -> b + c` | `(a -> (b + c))` | `(a -> (b + c))` |

`&` is the only row that disagrees.

### Cause

The default table in `src/dialect/mod.rs` has:

```rust
Precedence::Ampersand => 23,
Precedence::Caret => 22,
Precedence::Pipe => 21,
Precedence::Colon => 21,
Precedence::PgOther => 21,
```

`PostgreSqlDialect::prec_value` instead maps `Ampersand`, `Pipe`, `Colon` and `PgOther` all to
`PG_OTHER_PREC` (`src/dialect/postgresql.rs:176-184`), which matches `gram.y`, where `&` is just a
generic `Op` and shares one left-associative level with `->`:

```
%left Op OPERATOR RIGHT_ARROW '|'
```

So `Pipe` already agrees with `PgOther` in the default table (both 21), and `Caret` is legitimately
above it, but `Ampersand` at 23 is left as the sole outlier.

### Candidate fix

```diff
- Precedence::Ampersand => 23,
+ Precedence::Ampersand => 21,
```

The full suite passes unchanged with that applied, so no existing test pins the current grouping —
which is also why this went unnoticed. `Display` for `Expr::BinaryOp` emits no parentheses, so a
mis-grouped tree round-trips to the original SQL and `verified_expr` / `verified_stmt` cannot catch
it; a test would have to assert on the tree.

### Open question, possibly a separate issue

For MySQL the fix above is necessary but not sufficient. MySQL's `->` / `->>` take a quoted JSON path
on the right-hand side, so there is nothing for MySQL to resolve — `col->'$.a' + 1` can only mean
`(col->'$.a') + 1`. sqlparser parses that right operand as a full expression at `PgOther`, giving
`col -> ('$.a' + 1)`, so `->` under-binds in MySQL against `+`, `*`, `^` and friends, not just `&`.
Making that correct probably means a MySQL-specific precedence for the arrow operators rather than
another adjustment to the shared row, so I've kept it out of scope here — happy to split it out if
a maintainer would prefer it tracked separately.

Surfaced while working on #2436. Related: #2460.

贡献指南

这个仓库没有索引到贡献指南

调研方向

从 src/dialect/mod.rs 中的默认优先级表开始,然后将其与 src/dialect/postgresql.rs:176-184 中的 PostgreSqlDialect::prec_value 以及 issue 中引用的 gram.y PostgreSQL 规则进行比较。为 `a -> b & c` 添加树级回归测试,因为 SQL 往返测试无法检测分组,并运行完整测试套件以验证更改。

由索引模型根据 Issue 内容生成。

评估

技术栈
mysql, postgresql, rust
领域
compilers, databases
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
活跃
描述清晰度
描述清楚
新手友好度
78/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。