-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21417][SQL] Infer join conditions using propagated constraints #18692
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e0e6ad3
[SPARK-21417][SQL] Infer join conditions using propagated constraints
b69185c
[SPARK-21417][SQL] Added a comment
3e090f9
Consider only CROSS joins without conditions & switch to a map-based …
0e5a9f2
separate EquivalentExpressionMap
9ab91a1
Fix nonascii.message
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
66 changes: 66 additions & 0 deletions
66
...st/src/main/scala/org/apache/spark/sql/catalyst/expressions/EquivalentExpressionMap.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,66 @@ | ||
| /* | ||
| * 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.expressions | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.EquivalentExpressionMap.SemanticallyEqualExpr | ||
|
|
||
| /** | ||
| * A class that allows you to map an expression into a set of equivalent expressions. The keys are | ||
| * handled based on their semantic meaning and ignoring cosmetic differences. The values are | ||
| * represented as [[ExpressionSet]]s. | ||
| * | ||
| * The underlying representation of keys depends on the [[Expression.semanticHash]] and | ||
| * [[Expression.semanticEquals]] methods. | ||
| * | ||
| * {{{ | ||
| * val map = new EquivalentExpressionMap() | ||
| * | ||
| * map.put(1 + 2, a) | ||
| * map.put(rand(), b) | ||
| * | ||
| * map.get(2 + 1) => Set(a) // 1 + 2 and 2 + 1 are semantically equivalent | ||
| * map.get(1 + 2) => Set(a) // 1 + 2 and 2 + 1 are semantically equivalent | ||
| * map.get(rand()) => Set() // non-deterministic expressions are not equivalent | ||
| * }}} | ||
| */ | ||
| class EquivalentExpressionMap { | ||
|
|
||
| private val equivalenceMap = mutable.HashMap.empty[SemanticallyEqualExpr, ExpressionSet] | ||
|
|
||
| def put(expression: Expression, equivalentExpression: Expression): Unit = { | ||
| val equivalentExpressions = equivalenceMap.getOrElseUpdate(expression, ExpressionSet.empty) | ||
| equivalenceMap(expression) = equivalentExpressions + equivalentExpression | ||
| } | ||
|
|
||
| def get(expression: Expression): Set[Expression] = | ||
| equivalenceMap.getOrElse(expression, ExpressionSet.empty) | ||
| } | ||
|
|
||
| object EquivalentExpressionMap { | ||
|
|
||
| private implicit class SemanticallyEqualExpr(val expr: Expression) { | ||
| override def equals(obj: Any): Boolean = obj match { | ||
| case other: SemanticallyEqualExpr => expr.semanticEquals(other.expr) | ||
| case _ => false | ||
| } | ||
|
|
||
| override def hashCode: Int = expr.semanticHash() | ||
| } | ||
| } |
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
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
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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import scala.annotation.tailrec | ||
| import scala.collection.mutable | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.planning.ExtractFiltersAndInnerJoins | ||
|
|
@@ -152,3 +153,62 @@ object EliminateOuterJoin extends Rule[LogicalPlan] with PredicateHelper { | |
| if (j.joinType == newJoinType) f else Filter(condition, j.copy(joinType = newJoinType)) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A rule that eliminates CROSS joins by inferring join conditions from propagated constraints. | ||
| * | ||
| * The optimization is applicable only to CROSS joins. For other join types, adding inferred join | ||
|
Contributor
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. can we apply this optimization to all joins after #19054?
Member
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. It sounds promising. |
||
| * conditions would potentially shuffle children as child node's partitioning won't satisfy the JOIN | ||
| * node's requirements which otherwise could have. | ||
| * | ||
| * For instance, given a CROSS join with the constraint 'a = 1' from the left child and the | ||
| * constraint 'b = 1' from the right child, this rule infers a new join predicate 'a = b' and | ||
| * converts it to an Inner join. | ||
| */ | ||
| object EliminateCrossJoin extends Rule[LogicalPlan] with PredicateHelper { | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = { | ||
| if (SQLConf.get.constraintPropagationEnabled) { | ||
| eliminateCrossJoin(plan) | ||
| } else { | ||
| plan | ||
| } | ||
| } | ||
|
|
||
| private def eliminateCrossJoin(plan: LogicalPlan): LogicalPlan = plan transform { | ||
| case join @ Join(leftPlan, rightPlan, Cross, None) => | ||
| val leftConstraints = join.constraints.filter(_.references.subsetOf(leftPlan.outputSet)) | ||
| val rightConstraints = join.constraints.filter(_.references.subsetOf(rightPlan.outputSet)) | ||
| val inferredJoinPredicates = inferJoinPredicates(leftConstraints, rightConstraints) | ||
| val joinConditionOpt = inferredJoinPredicates.reduceOption(And) | ||
| if (joinConditionOpt.isDefined) Join(leftPlan, rightPlan, Inner, joinConditionOpt) else join | ||
| } | ||
|
|
||
| private def inferJoinPredicates( | ||
| leftConstraints: Set[Expression], | ||
| rightConstraints: Set[Expression]): mutable.Set[EqualTo] = { | ||
|
|
||
| val equivalentExpressionMap = new EquivalentExpressionMap() | ||
|
|
||
| leftConstraints.foreach { | ||
| case EqualTo(attr: Attribute, expr: Expression) => | ||
| equivalentExpressionMap.put(expr, attr) | ||
| case EqualTo(expr: Expression, attr: Attribute) => | ||
| equivalentExpressionMap.put(expr, attr) | ||
| case _ => | ||
| } | ||
|
|
||
| val joinConditions = mutable.Set.empty[EqualTo] | ||
|
|
||
| rightConstraints.foreach { | ||
| case EqualTo(attr: Attribute, expr: Expression) => | ||
| joinConditions ++= equivalentExpressionMap.get(expr).map(EqualTo(attr, _)) | ||
| case EqualTo(expr: Expression, attr: Attribute) => | ||
| joinConditions ++= equivalentExpressionMap.get(expr).map(EqualTo(attr, _)) | ||
| case _ => | ||
| } | ||
|
|
||
| joinConditions | ||
| } | ||
|
|
||
| } | ||
56 changes: 56 additions & 0 deletions
56
...c/test/scala/org/apache/spark/sql/catalyst/expressions/EquivalentExpressionMapSuite.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,56 @@ | ||
| /* | ||
| * 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.expressions | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
|
|
||
| class EquivalentExpressionMapSuite extends SparkFunSuite { | ||
|
|
||
| private val onePlusTwo = Literal(1) + Literal(2) | ||
| private val twoPlusOne = Literal(2) + Literal(1) | ||
| private val rand = Rand(10) | ||
|
|
||
| test("behaviour of the equivalent expression map") { | ||
| val equivalentExpressionMap = new EquivalentExpressionMap() | ||
| equivalentExpressionMap.put(onePlusTwo, 'a) | ||
| equivalentExpressionMap.put(Literal(1) + Literal(3), 'b) | ||
| equivalentExpressionMap.put(rand, 'c) | ||
|
|
||
| // 1 + 2 should be equivalent to 2 + 1 | ||
| assertResult(ExpressionSet(Seq('a)))(equivalentExpressionMap.get(twoPlusOne)) | ||
| // non-deterministic expressions should not be equivalent | ||
| assertResult(ExpressionSet.empty)(equivalentExpressionMap.get(rand)) | ||
|
|
||
| // if the same (key, value) is added several times, the map still returns only one entry | ||
| equivalentExpressionMap.put(onePlusTwo, 'a) | ||
| equivalentExpressionMap.put(twoPlusOne, 'a) | ||
| assertResult(ExpressionSet(Seq('a)))(equivalentExpressionMap.get(twoPlusOne)) | ||
|
|
||
| // get several equivalent attributes | ||
| equivalentExpressionMap.put(onePlusTwo, 'e) | ||
| assertResult(ExpressionSet(Seq('a, 'e)))(equivalentExpressionMap.get(onePlusTwo)) | ||
| assertResult(2)(equivalentExpressionMap.get(onePlusTwo).size) | ||
|
|
||
| // several non-deterministic expressions should not be equivalent | ||
| equivalentExpressionMap.put(rand, 'd) | ||
| assertResult(ExpressionSet.empty)(equivalentExpressionMap.get(rand)) | ||
| assertResult(0)(equivalentExpressionMap.get(rand).size) | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
I thought that writing
ExpressionSet.emptywould be more readable thanExpressionSet(Nil). Usually, mutable collections havedef empty()and immutable ones have separate objects that represent empty collections (e.g.,Nil,Stream.Empty). I definedval emptysinceExpressionSetis immutable.