Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary expression canonical names are incorrect in some cases #3865

Closed
andygrove opened this issue Oct 17, 2022 · 1 comment · Fixed by #3884
Closed

Binary expression canonical names are incorrect in some cases #3865

andygrove opened this issue Oct 17, 2022 · 1 comment · Fixed by #3884
Assignees
Labels
bug Something isn't working

Comments

@andygrove
Copy link
Member

andygrove commented Oct 17, 2022

Describe the bug
Consider the following two expressions, which are not the same:

let expr1 = expr1.and(expr2.or(expr3));
let expr2 = expr1.and(expr2).or(expr3);

They both produce the same string from canonical_name:

a < Int32(1) AND b < Int32(2) OR c < Int32(3)

This is not only confusing when reading plans but could also lead to optimization rules altering plans to have different semantics.

To Reproduce

Expected behavior
They should produce different results:

expr1.and(expr2.or(expr3)) should produce:

a < Int32(1) AND (b < Int32(2) OR c < Int32(3))

expr1.and(expr2).or(expr3) should produce either of these:

a < Int32(1) AND b < Int32(2) OR c < Int32(3)
(a < Int32(1) AND b < Int32(2)) OR c < Int32(3)

There are two ways of resolving this:

  • We could just wrap every binary expression in parentheses (easier to implement but makes expressions harder to read)
  • We could make the logic precedence-aware so that parentheses are only inserted when needed (preferred)

Additional context

@andygrove andygrove added the bug Something isn't working label Oct 17, 2022
@andygrove
Copy link
Member Author

SQL parser precedence logic for reference: https://github.com/sqlparser-rs/sqlparser-rs/blob/main/src/parser.rs#L1552-L1638

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant