Honour operator precedence in IS [NOT] DISTINCT FROM - #2459
Closed
adriangb wants to merge 1 commit into
Closed
Conversation
`parse_infix` parsed the right operand of `IS [NOT] DISTINCT FROM` with `parse_expr` (precedence 0), so it swallowed every following operator, including `AND` and `OR`. Use the caller's precedence instead. `Precedence::PgOther` (`->`, `@>`, custom operators) also has to move above `Is` in the default table, matching PostgreSQL's `%left Op OPERATOR RIGHT_ARROW`, or the parser fix regresses `a IS DISTINCT FROM b -> 'k'` outside PostgreSQL. Based on apache#2436 by @zvonimir-dd, with review feedback applied. Co-Authored-By: zvonimir.rakamaric <zvonimir.rakamaric@datadoghq.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Continues #2436 by @zvonimir-dd (kept as co-author on the commit), with the review feedback there applied. Opening this since that PR has been idle — happy to close it in favour of #2436 if @zvonimir-dd picks it back up.
Two hunks:
src/parser/mod.rs— the right operand ofIS [NOT] DISTINCT FROMwas parsed withparse_expr(), i.e. precedence 0, so it swallowed everything following it, includingANDandOR.a IS DISTINCT FROM 1 AND b = 2parsed asa IS DISTINCT FROM (1 AND b = 2). Using the caller'sprecedencefixes it.src/dialect/mod.rs—Precedence::PgOther(the "any other operator" row:->,->>,@>, custom operators) sat at 16, belowIs(17). Without moving it, the parser hunk is a regression:a IS DISTINCT FROM b -> 'k'would regroup to(a IS DISTINCT FROM b) -> 'k'in every dialect except PostgreSQL. PostgreSQL'sgram.yplaces%left Op OPERATOR RIGHT_ARROW '|'aboveIS/comparison/LIKE/BETWEENand below+/-, andsrc/dialect/postgresql.rsalready encodes that (PG_OTHER_PREC70); only the default table was inverted.Displayadds no parentheses, so the wrong tree reprinted as the original text — which is why round trips never caught this, and why the tests assert on the tree.Found via apache/datafusion#23692, where it made multi-column
IS NOT DISTINCT FROMjoins fail to plan. Verified end to end: with this patched into DataFusionmain, those cases plan correctly and sqllogictest stays at 502/502 files.cargo test,cargo fmt --checkandcargo clippy --all-targets --all-features -- -D warningsall pass.Out of scope: @LucaCappelletti94 noted on #2436 that MySQL/Spark
DIVhas the same defect (7 DIV 2 + 1). Left for a follow-up.