apache / apache/datafusion-sqlparser-rs

Outdated operation precedence.

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

描述

Here's an expression: `SELECT ~1 + 2`. If you plug it into `PostgreSQL` interpreter, here's what the output looks like:
```
postgres=# select ~1 + 2;
?column?
----------
-4
(1 row)

postgres=# select ~(1 + 2);
?column?
----------
-4
(1 row)

postgres=# select (~1) + 2;
?column?
----------
0
(1 row)
```
As you can see here, and according to [operation precedence](https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-PRECEDENCE) listed in documentation, the `+` operation is more powerful than the `~` operation, and thus we have PGBitwiseNot(1 + 2). However, if we plug this into the parser, we get the following AST:
```
...
projection: [
UnnamedExpr(
BinaryOp {
left: UnaryOp {
op: PGBitwiseNot,
expr: Value(
Number(
"1",
false,
),
),
},
op: Plus,
right: Value(
Number(
"2",
false,
),
),
},
),
],
...
```
Which is implying `(~1) + 2` instead of `~(1 + 2)`. This should be fixed.

**UPDATE:**
In code, I found a reference to an old [PostgreSQL precedence table](https://www.postgresql.org/docs/7.0/operators.htm#AEN2026), using which the parser is built. However, this table is for a very old (`7.0`, released `May 8, 2000`) version of PostgreSQL. There latest version is `15.1`. Shouldn't this be updated?

贡献指南

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

调研方向

首先阅读 parser 所引用的 PostgreSQL 7.0 优先级表,以及 issue 中链接的当前 PostgreSQL 优先级文档。将 parser 对 `SELECT ~1 + 2` 生成的 AST 与文档所述行为和三个示例查询进行比较。当 parser 应用预期的优先级并生成相应的 AST 时,即表示完成。

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

评估

技术栈
postgresql, rust, sql
领域
compilers, databases
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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