Skip to content

[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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member

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?

Copy link
Contributor

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.

Copy link
Member

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 ?

Copy link
Contributor Author

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.

Copy link
Member

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?

InferFiltersFromConstraints) ::
Copy link
Member

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

Batch("Operator Optimization after Inferring Filters", fixedPoint,
rulesWithoutInferFiltersFromConstraints: _*) ::
Expand Down
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)
}

}