-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-30876][SQL] Optimizer fails to infer constraints within join #29170
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Note: This rule was separated because #19149 |
||
Batch("Operator Optimization after Inferring Filters", fixedPoint, | ||
rulesWithoutInferFiltersFromConstraints: _*) :: | ||
|
63 changes: 63 additions & 0 deletions
63
...st/scala/org/apache/spark/sql/catalyst/optimizer/InferFiltersPredicatePushdownSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.catalyst.optimizer | ||
|
||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.dsl.plans._ | ||
import org.apache.spark.sql.catalyst.expressions.{IsNotNull, Literal} | ||
import org.apache.spark.sql.catalyst.expressions.aggregate.Count | ||
import org.apache.spark.sql.catalyst.plans.{Inner, PlanTest} | ||
import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} | ||
import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
|
||
class InferFiltersPredicatePushdownSuite extends PlanTest { | ||
|
||
object Optimize extends RuleExecutor[LogicalPlan] { | ||
val b = Batch("InferAndPushDownFilters", FixedPoint(100), | ||
PushPredicateThroughJoin, | ||
ColumnPruning | ||
) | ||
val batches = | ||
b :: | ||
Batch("infer filter from constraints", FixedPoint(100), | ||
PushDownPredicates, | ||
InferFiltersFromConstraints) :: | ||
Nil | ||
} | ||
val testRelation = LocalRelation('a.int, 'b.int, 'c.int) | ||
|
||
test("SPARK-30876: optimize constraints in 3-way join") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
val z = testRelation.subquery('z) | ||
val originalQuery = x.join(y).join(z) | ||
.where(("x.a".attr === "y.b".attr) && ("y.b".attr === "z.c".attr) && ("z.c".attr === 1)) | ||
.groupBy()(Count(Literal("*"))).analyze | ||
val optimized = Optimize.execute(originalQuery) | ||
val correctAnswer = x.where('a === 1 && IsNotNull('a)).select('a) | ||
.join(y.where('b === 1 && IsNotNull('b)) | ||
.select('b), Inner, Some("x.a".attr === "y.b".attr)) | ||
.select('b) | ||
.join(z.where('c === 1 && IsNotNull('c)) | ||
.select('c), Inner, Some('b === "z.c".attr)) | ||
.select() | ||
.groupBy()(Count(Literal("*"))).analyze | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 becauseInferFiltersFromConstraints
can create a new constraint that a subsequent rule removes. The example in https://issues.apache.org/jira/browse/SPARK-21652 required runningConstantPropagation
,ConstantFolding
and thenBooleanSimplification
to remove such an inferred constraint/filter.I think if we combine
InferFiltersFromConstraints
with other rules that doesn't reduce constraints (likePushDownPredicates
) 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 withInferFiltersFromConstraints
.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.
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 makeInferFiltersFromConstraints
deal with this kind of case?