AnyhowStep / sql-compiler

An experimental SQL compiler
MIT License
0 stars 0 forks source link

Documentation for operator BETWEEN precedence is incorrect #29

Open AnyhowStep opened 3 years ago

AnyhowStep commented 3 years ago

Related: https://github.com/AnyhowStep/sql-compiler/issues/27

https://www.db-fiddle.com/f/eLqMAKQHmfzCVFKMB9raFn/0

https://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html

https://github.com/mysql/mysql-server/blob/5c8c085ba96d30d697d0baa54d67b102c232116b/sql/sql_yacc.yy#L9360

predicate:
    ...
    | bit_expr BETWEEN_SYM bit_expr AND_SYM predicate
    ....
    | bit_expr

Given this,

SELECT a BETWEEN b AND c BETWEEN d AND e

We can have,

SELECT (a BETWEEN b AND c) BETWEEN d AND e

Or,

SELECT a BETWEEN b AND (c BETWEEN d AND e)

According to the documentation, it should parse from left to right. So, the first choice. But the grammar rules are written such that it parses from right to left.

AnyhowStep commented 3 years ago

The same problem for equality operator precedence, https://www.db-fiddle.com/f/9F9ZCGV3QDs6fPysLMQ7Yh/0

SELECT
    1 = 2 BETWEEN 0 AND 1,
    (1 = 2) BETWEEN 0 AND 1,
    1 = (2 BETWEEN 0 AND 1);
1 = 2 BETWEEN 0 AND 1 (1 = 2) BETWEEN 0 AND 1 1 = (2 BETWEEN 0 AND 1)
0 1 0