-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-42500][SQL] ConstantPropagation supports more cases #40093
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
Conversation
| condition transform { | ||
| case e @ EqualTo(_, _) if !predicates.contains(e) => replaceConstants0(e) | ||
| case e @ EqualNullSafe(_, _) if !predicates.contains(e) => replaceConstants0(e) | ||
| case b: BinaryComparison => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support other binary comparisons. For example: >, <.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we look at the commit history and understand why constant propagation only supports equality predicates before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @peter-toth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can replace constants in other expressions too, #24553 does the same (and more).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
I'm not sure I get the issue. If a complex expression is equal to a constant why we can't use that constant where the complex expression appears? I don't think it matters if some parts of the complex expression can also be replaced to another constat. The replace happens using
transformDownso larger complex expressions are replaced to constants first. E.g.a = 1 AND (a + b) = 1 AND (a + b) > 2->a = 1 AND (1 + b) = 1 AND 1 > 2->falsemakes sense to me. -
Yes we can. The reason why I didn't do those kind of shortcuts in
ConstantPropagationin my PR is because there are other, similar cases where it isn't that easy to do a shortcut (or simplification has been handled in other rules so why doing it here as well) . E.g.a = 1 AND a > 2->a = 1 AND 1 > 2->falsewhere the 2nd step (1 > 2->false) could also be handled inConstantPropagationbut those foldable expressions are already handled inConstantFoldingand then inBooleanSimplificationwhen contant propagation has been done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me open a PR tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cloud-fan This is why conflicting equality predicates are not supported: #17993 (comment)
It is correct if it only optimize on the filter condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sidenote: In my old PR (https://github.com/apache/spark/pull/24553/files#diff-d43484d56a4d9991066b5c00d12ec2465c75131e055fc02ee7fb6dfd45b5006fR76-R79) I proposed #27309 to detect equijoins better and to allow constant propagation in Joins too.
| if (!allPredicates.contains(b)) { | ||
| replaceConstants0(b, allConstantsMap) | ||
| } else { | ||
| val excludedEqualityPredicates = equalityPredicates.filterNot(_._2.semanticEquals(b)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exclude current binary comparison to support the following case:
a = 1 and a = 2 ==> 2 = 1 and 1 = 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't look like constant propagation but a different optimization to simplify predicates:
- a = 1 and a = 2 ==> false
- a > 1 and a > 2 ==> a > 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR does not support a > 1 and a > 2 ==> a > 2. It must contain an equality predicate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we exclude it first? We should add a new optimizer rule later to do more advanced predicate simplification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean only support a = 1 and a = 2 ==> 2 = 1 and 1 = 2 ==> false, and do not support a = 1 and b > a + 2 ==> a = 1 && b > 3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm, the rule name is ConstantPropagation, so both optimizations fit it.
|
cc @cloud-fan |
|
We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable. |

What changes were proposed in this pull request?
This PR enhances
ConstantPropagationto support more cases.false.For example:
Why are the changes needed?
Improve query performance. Denodo also has a similar optimization. For example:
Before this PR:
After this PR:
Does this PR introduce any user-facing change?
No.
How was this patch tested?
Unit test.