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

Additional optimization suggestions for CASE #33957

Open
maumar opened this issue Jun 11, 2024 · 2 comments
Open

Additional optimization suggestions for CASE #33957

maumar opened this issue Jun 11, 2024 · 2 comments

Comments

@maumar
Copy link
Contributor

maumar commented Jun 11, 2024

          Some generalizations/similar simplifications:
  1. CASE WHEN t1 THEN x WHEN t2 THEN x ... WHEN tn THEN x ELSE x END β†’ x
    This should be safe even for impure x πŸŽ‰
  2. CASE WHEN boolExpr THEN TRUE ELSE FALSE END β†’ boolExpr
  3. CASE WHEN NOT (boolExpr) THEN x ELSE y END β†’ CASE WHEN boolExpr THEN y ELSE x END
  4. NOT (CASE WHEN nullOrBoolExpr THEN x ELSE y END) β†’ CASE WHEN nullOrBoolExpr THEN NOT(x) ELSE NOT(y) END
    ⚠️ it can improve or worsen the complexity of the query
  5. NOT (CASE WHEN nullOrBoolExpr THEN TRUE ELSE FALSE END) β†’ CASE WHEN nullOrBoolExpr THEN FALSE ELSE TRUE END
    This is the query in the top comment and is relevant because it is emitted within EFCore; this specialization of the previous case always improves the query.
  6. NOT (COALESCE(nullOrBoolExpr, FALSE)) -> COALESCE(NOT(nullOrBoolExpr), TRUE)
    is the COALESCE version of the previous rule. It does not improve the complexity of the query, but additional simplifications might be possible on NOT(nullOrBoolExpr)

Originally posted by @ranma42 in #33857 (comment)

@maumar
Copy link
Contributor Author

maumar commented Jun 11, 2024

@ranma42 opened a new issue, to track the potential optimizations you suggested, because the original issue is now fixes & closed

@roji roji added this to the Backlog milestone Jun 11, 2024
@ranma42
Copy link
Contributor

ranma42 commented Jul 6, 2024

See #18774 for optimizations related to nested CASE expressions.
The fourth optimization could be generalized to apply to most operations; as before, it is probably effective on constants:
CASE WHEN nullOrBoolExpr THEN 5 ELSE 9 END = 9
β†’ CASE WHEN nullOrBoolExpr THEN 5 = 9 ELSE 9 = 9 END
β†’ CASE WHEN nullOrBoolExpr THEN FALSE ELSE TRUE END

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants