apache / apache/datafusion-sqlparser-rs

Outdated operation precedence.

Open
#814 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
3.5k
Forks
772
Avg merge
4d 9h
Merged PRs (30d)
17

Description

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?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the parser's referenced PostgreSQL 7.0 precedence table and the current PostgreSQL precedence documentation linked in the issue. Compare the parser's AST for `SELECT ~1 + 2` with the documented behavior and the three example queries. Done means the parser applies the intended precedence and produces the corresponding AST.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, rust, sql
Domain
compilers, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.