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

Fix(optimizer): don't propagate equality constraints from IF/CASE outwards #2396

Merged
merged 1 commit into from
Oct 11, 2023

Conversation

georgesittas
Copy link
Collaborator

@georgesittas georgesittas commented Oct 11, 2023

This PR adds support for a prune callback argument in walk_in_scope, similar to how walk works. It then leverages it to fix the bug demonstrated below:

Before:

>>> from sqlglot import parse_one
>>> from sqlglot.optimizer.simplify import simplify
>>>
>>> simplify(parse_one("x = 1 and case when x = 5 then false else true end"), constant_propagation=True).sql()
'FALSE'

After:

>>> from sqlglot import parse_one
>>> from sqlglot.optimizer.simplify import simplify
>>>
>>> simplify(parse_one("x = 1 and case when x = 5 then false else true end"), constant_propagation=True).sql()
'x = 1 AND CASE WHEN FALSE THEN FALSE ELSE TRUE END'

One obvious improvement that can be made in a followup PR (I plan to do this soon) is to reduce conditional expressions such as IF and CASE when the conditions are statically known to be TRUE or FALSE.

@georgesittas georgesittas merged commit d7021d1 into main Oct 11, 2023
@georgesittas georgesittas deleted the jo/constant_propagation_cond_fix branch October 11, 2023 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants