cloudspannerecosystem / memefish

memefish is the foundation to analyze Spanner SQL
https://cloudspannerecosystem.dev/memefish/
MIT License
76 stars 19 forks source link

fix(parser): adjust `AND` and `OR` precedence #37

Closed S-YOU closed 2 years ago

S-YOU commented 2 years ago

Following returns true in spanner query.

SELECT TRUE OR FALSE AND FALSE

But current AST returns following

       Expr: &ast.BinaryExpr{
          Op:   "AND",
          Left: &ast.BinaryExpr{
            Op:   "OR",
            Left: &ast.BoolLiteral{
              ValuePos: 7,
              Value:    true,
            },
            Right: &ast.BoolLiteral{
              ValuePos: 15,
              Value:    false,
            },
          },
          Right: &ast.BoolLiteral{
            ValuePos: 25,
            Value:    false,
          },
        },
      },

Which will evaluate to false, which is not correct I think and spec says AND has higher precedence.

https://cloud.google.com/spanner/docs/reference/standard-sql/operators

Order of Precedence Operator Input Data Types Name Operator Arity
11 AND BOOL Logical AND Binary
12 OR BOOL Logical OR Binary
makenowjust commented 2 years ago

@S-YOU Thank you!