AnyhowStep / sql-compiler

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

Documentation for operator = and LIKE precedence is incorrect? #27

Open AnyhowStep opened 3 years ago

AnyhowStep commented 3 years ago

image

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

SELECT
    'a' = 'a' LIKE '0',
    'a' LIKE '0',
    'a' = 0,
    'a' = ('a' LIKE '0'),
    ('a' = 'a') LIKE '0';
'a' = 'a' LIKE '0' 'a' LIKE '0' 'a' = 0 'a' = ('a' LIKE '0') ('a' = 'a') LIKE '0'
1 0 1 1 0

View on DB Fiddle

AnyhowStep commented 3 years ago

According to the documentation, we should see 'a' = 'a' LIKE '0' be evaluated as,

('a' = 'a') LIKE '0'

But it is evaluated,

'a' = ('a' LIKE '0')

AnyhowStep commented 3 years ago

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

predicate:
    ...
    | bit_expr LIKE simple_expr opt_escape
    ...
    | bit_expr

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

bool_pri:
    ...
    | bool_pri comp_op predicate %prec EQ
    ...
    | predicate
AnyhowStep commented 3 years ago

The problem is that when we have 'a' = 'a' LIKE '0', for it to parse as ('a' = 'a') LIKE '0', there needs to be a rule (directly or indirectly),

predicate:
    ...
    | bool_pri LIKE simple_expr opt_escape

because 'a' = 'a' is a bool_pri.

There is no such rule.

AnyhowStep commented 3 years ago

https://forums.mysql.com/read.php?115,697095