-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
[SPARK-30876][SQL] Optimizer fails to infer constraints within join #29170
Conversation
What changes were proposed in this pull request? For a 3-way join of the kind described below, the optimizer fails to infer the constraint a=1. This is because of the interaction between the following rules: ColumnPruning, InferFiltersFromConstraints, and PredicatePushdown. For the following SQL query: ``` create table t1(a int, b int, c int); create table t2(a int, b int, c int); create table t3(a int, b int, c int); select count(*) from t1 join t2 join t3 on (t1.a = t2.b and t2.b = t3.c and t3.c = 1); ``` The optimized logical plan produced is: ``` == Optimized Logical Plan == Aggregate [count(1) AS count(1)#66L] +- Project +- Join Inner, (b#61 = c#65) :- Project [b#61] : +- Join Inner, (a#57 = b#61) : :- Project [a#57] : : +- Filter isnotnull(a#57) : : +- HiveTableRelation `default`.`t1`, org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, [a#57, b#58, c#59], Statistics(sizeInBytes=8.0 EiB) : +- Project [b#61] : +- Filter (isnotnull(b#61) AND (b#61 = 1)) : +- HiveTableRelation `default`.`t2`, org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, [a#60, b#61, c#62], Statistics(sizeInBytes=8.0 EiB) +- Project [c#65] +- Filter (isnotnull(c#65) AND (c#65 = 1)) +- HiveTableRelation `default`.`t3`, org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, [a#63, b#64, c#65], Statistics(sizeInBytes=8.0 EiB) ``` The constraint gets dropped because after column pruning, the InferFiltersFromConstraints rule on the outer join operator only infers b=1, since the project operator drops the constraint that refers to 'a'. Later, when PushdownPredicates runs again, the b=1 constraint gets pushed down to relation 'y'. In order to resolve this, this patch proposes running InferFiltersFromConstraints and PredicatePushdown in a batch to fixed point, in place of running InferFiltersFromConstraints once in a batch. With this change, the constraint b=1 will be inferred and pushed down first. Then, the constraint a=1 will be inferred for the inner join operator since that can be inferred because b=1 is now available to infer it. Running PredicatePushdown again will push a=1 to its correct position. Why are the changes needed? Improves performance of optimization. Also this worked correctly earlier in 2.3. Does this PR introduce any user-facing change? No How was this patch tested? Added a unit test. The test should fail if InferFiltersFromConstraints is run only once.
@@ -116,7 +116,8 @@ abstract class Optimizer(catalogManager: CatalogManager) | |||
operatorOptimizationRuleSet.filterNot(_ == InferFiltersFromConstraints) | |||
Batch("Operator Optimization before Inferring Filters", fixedPoint, | |||
rulesWithoutInferFiltersFromConstraints: _*) :: | |||
Batch("Infer Filters", Once, | |||
Batch("Infer Filters", fixedPoint, | |||
PushDownPredicates, | |||
InferFiltersFromConstraints) :: |
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.
Note: This rule was separated because #19149
ok to test |
Thanks for the contribution, @navinvishy |
Test build #126223 has finished for PR 29170 at commit
|
retest this please |
Test build #126233 has finished for PR 29170 at commit
|
retest this please |
Test build #126252 has finished for PR 29170 at commit
|
retest this please |
Test build #126285 has finished for PR 29170 at commit
|
Thanks for looking into this @maropu ! Do you know anything about this test failure? Looks unrelated to the change. |
Test build #126448 has finished for PR 29170 at commit
|
Test build #126450 has finished for PR 29170 at commit
|
Yea, the failure is not related to this. |
retest this please |
Test build #126675 has finished for PR 29170 at commit
|
@maropu wanted to circle back on this. Do you think this can be merged? |
Hmm, I've run into this issue many times recently. Spark 2.3+ runs |
Revisiting this looks fine to me. Are you still here? @navinvishy |
@@ -116,7 +116,8 @@ abstract class Optimizer(catalogManager: CatalogManager) | |||
operatorOptimizationRuleSet.filterNot(_ == InferFiltersFromConstraints) | |||
Batch("Operator Optimization before Inferring Filters", fixedPoint, | |||
rulesWithoutInferFiltersFromConstraints: _*) :: | |||
Batch("Infer Filters", Once, | |||
Batch("Infer Filters", fixedPoint, | |||
PushDownPredicates, |
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.
My concern is that the change above might cause the issue described in #19149. Instead, (just a suggestion), we couldn't improve InferFiltersFromConstraints
to cover the case you pointed out in the PR description?
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.
Unfortunately, there is no UT in the commit to see what was the exact issue and why InferFiltersFromConstraints
needs to be separated entirely from other rules.
If I get the issue described in https://issues.apache.org/jira/browse/SPARK-21652 right, if we combine InferFiltersFromConstraints
with other rules into a batch then we can end up in an infinite loop in that batch. But this is only because InferFiltersFromConstraints
can create a new constraint that a subsequent rule removes. The example in https://issues.apache.org/jira/browse/SPARK-21652 required running ConstantPropagation
, ConstantFolding
and then BooleanSimplification
to remove such an inferred constraint/filter.
I think if we combine InferFiltersFromConstraints
with other rules that doesn't reduce constraints (like PushDownPredicates
) then we are still good. But I might be wrong so please share your thoughts on this.
Sidebar:
I think the source of the above mentioned loop is that ConstantPropagation
doesn't propagate constants into a join condition (it handles filters only). I had an old PR to enhance that rule where I also commented why it is not so simple to propagate constants into a join: https://github.com/apache/spark/pull/24553/files#diff-d43484d56a4d9991066b5c00d12ec2465c75131e055fc02ee7fb6dfd45b5006fR76-R79 but it is doable if we fix https://issues.apache.org/jira/browse/SPARK-30598 (#27309).
But I'm not saying that ConstantPropagation
is the only reduction rule that can collide with InferFiltersFromConstraints
.
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 if we combine InferFiltersFromConstraints with other rules that doesn't reduce constraints (like PushDownPredicates) then we are still good. But I might be wrong so please share your thoughts on this.
Yea, I don't have a specific query to deny your thought and I also think it is true, but, in my current feeling, I a bit hesitate to change Once
-> fixedPoint
for this batch without adding no logic to avoid the infinite loop case (and without any evidence that it will not affect users queries...). WDTY, @cloud-fan @viirya ?
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 initially looked at trying to see if InferFiltersFromConstraints could itself handle this, but I think I could not figure out a way to solve the case in the unit test. I also don't think that this can cause an infinite loop, but let me take another look at this to see if we can either be sure that it cannot result in an infinite loop or if we can push this into InferFiltersFromConstraints so that we don't need the fixedPoint.
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.
Not look at InferFiltersFromConstraints
, but I tend to agree with @maropu's initial comment. Can we possibly make InferFiltersFromConstraints
deal with this kind of case?
I have proposed a PR for the bug SPARK-33152, which proposes a new logic for maintaining constraint propagation which I have seen is able to infer filters / prune filters more rigorously. I think that PR should solve this issue too. |
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?
For a 3-way join of the kind described below, the optimizer fails to infer the constraint a=1. This appears to be because of the interaction between the following rules: ColumnPruning, InferFiltersFromConstraints, and PredicatePushdown.
For the following SQL query:
The optimized logical plan produced is:
The constraint
a=1
does not get inferred because after column pruning, the InferFiltersFromConstraints rule on the outer join operator only infersb=1
, since the project operator around the join operator drops the constraint that refers to 'a'. Later, when PushdownPredicates runs again, theb=1
constraint gets pushed down to relation 'y'.In order to resolve this, this patch proposes replacing the InferFiltersFromConstraints once batch with a batch of InferFiltersFromConstraints and PredicatePushdown to fixed point. With this change, the constraint
b=1
will be inferred and pushed down first. The constrainta=1
can then be inferred for the inner join operator becauseb=1
is now available in order to infer it. Running PredicatePushdown again will pusha=1
to its correct position.Why are the changes needed?
Improves performance of optimization. Also this worked correctly earlier in 2.3.
Does this PR introduce any user-facing change?
No
How was this patch tested?
Added a unit test. The test should fail if InferFiltersFromConstraints is run only once.