Skip to content

Outdated operation precedence. #814

Open
@michael-2956

Description

@michael-2956

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 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, 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions