-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-39475][SQL] Pull out complex join keys for shuffled join #36874
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
3 commits
Select commit
Hold shift + click to select a range
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
118 changes: 118 additions & 0 deletions
118
...alyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PullOutComplexJoinKeys.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,118 @@ | ||
| /* | ||
| * 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 scala.collection.mutable | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Alias, And, EqualTo, Expression, NamedExpression} | ||
| import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Join, LogicalPlan, Project} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.catalyst.trees.TreePattern.JOIN | ||
|
|
||
| /** | ||
| * This rule pulls out the complex join keys expression if can not broadcast. | ||
| * Example: | ||
| * | ||
| * +- Join Inner, ((c1 % 2) = c2)) - Project [c1, c2] | ||
| * :- Relation default.t1[c1] parquet +- Join Inner, (_complexjoinkey_0 = c2)) | ||
| * +- Relation default.t2[c2] parquet => :- Project [c1, (c1 % 2) AS _complexjoinkey_0] | ||
| * : +- Relation default.t1[c1] parquet | ||
| * +- Relation default.t2[c2] parquet | ||
| * | ||
| * For shuffle based join, we may evaluate the join keys for several times: | ||
| * - SMJ: always evaluate the join keys during join, and probably evaluate if has shuffle or sort | ||
| * - SHJ: always evaluate the join keys during join, and probably evaluate if has shuffle | ||
| * So this rule can reduce the cost of repetitive evaluation. | ||
| */ | ||
| object PullOutComplexJoinKeys extends Rule[LogicalPlan] with JoinSelectionHelper { | ||
|
|
||
| private def isComplexExpression(e: Expression): Boolean = | ||
| e.deterministic && !e.foldable && e.children.nonEmpty | ||
|
|
||
| private def hasComplexExpression(joinKeys: Seq[Expression]): Boolean = | ||
| joinKeys.exists(isComplexExpression) | ||
|
|
||
| private def extractComplexExpression( | ||
| joinKeys: Seq[Expression], | ||
| startIndex: Int): mutable.LinkedHashMap[Expression, NamedExpression] = { | ||
| val map = new mutable.LinkedHashMap[Expression, NamedExpression]() | ||
| var i = startIndex | ||
| joinKeys.foreach { | ||
| case e: Expression if isComplexExpression(e) => | ||
| map.put(e.canonicalized, Alias(e, s"_complexjoinkey_$i")()) | ||
| i += 1 | ||
| case _ => | ||
| } | ||
| map | ||
| } | ||
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = { | ||
| plan.transformWithPruning(_.containsPattern(JOIN), ruleId) { | ||
| case j @ ExtractEquiJoinKeys(joinType, leftKeys, rightKeys, other, _, left, right, joinHint) | ||
| if hasComplexExpression(leftKeys) || hasComplexExpression(rightKeys) => | ||
| val leftComplexExprs = extractComplexExpression(leftKeys, 0) | ||
| val (newLeftKeys, newLeft) = | ||
| if ((!canBuildBroadcastLeft(joinType) || !canBroadcastBySize(left, conf)) && | ||
| leftComplexExprs.nonEmpty) { | ||
| ( | ||
| leftKeys.map { e => | ||
| if (leftComplexExprs.contains(e.canonicalized)) { | ||
| leftComplexExprs(e.canonicalized).toAttribute | ||
| } else { | ||
| e | ||
| } | ||
| }, | ||
| Project(left.output ++ leftComplexExprs.values.toSeq, left) | ||
| ) | ||
| } else { | ||
| (leftKeys, left) | ||
| } | ||
|
|
||
| val rightComplexExprs = extractComplexExpression(rightKeys, leftComplexExprs.size) | ||
| val (newRightKeys, newRight) = | ||
| if ((!canBuildBroadcastRight(joinType) || !canBroadcastBySize(right, conf)) && | ||
| rightComplexExprs.nonEmpty) { | ||
| ( | ||
| rightKeys.map { e => | ||
| if (rightComplexExprs.contains(e.canonicalized)) { | ||
| rightComplexExprs(e.canonicalized).toAttribute | ||
| } else { | ||
| e | ||
| } | ||
| }, | ||
| Project(right.output ++ rightComplexExprs.values.toSeq, right) | ||
| ) | ||
| } else { | ||
| (rightKeys, right) | ||
| } | ||
|
|
||
| if (left.eq(newLeft) && right.eq(newRight)) { | ||
| j | ||
| } else { | ||
| val newConditions = newLeftKeys.zip(newRightKeys).map { | ||
| case (l, r) => EqualTo(l, r) | ||
| } ++ other | ||
|
|
||
| Project( | ||
| j.output, | ||
| Join(newLeft, newRight, joinType, newConditions.reduceOption(And), joinHint)) | ||
| } | ||
| } | ||
| } | ||
| } |
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
111 changes: 111 additions & 0 deletions
111
.../src/test/scala/org/apache/spark/sql/catalyst/optimizer/PullOutComplexJoinKeysSuite.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,111 @@ | ||
| /* | ||
| * 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.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| class PullOutComplexJoinKeysSuite extends PlanTest { | ||
|
|
||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = Batch("PullOutComplexJoinKeys", FixedPoint(1), | ||
| PullOutComplexJoinKeys, | ||
| CollapseProject) :: Nil | ||
| } | ||
|
|
||
| val testRelation1 = LocalRelation($"a".int, $"b".int) | ||
| val testRelation2 = LocalRelation($"x".int, $"y".int) | ||
|
|
||
| test("pull out complex join keys") { | ||
| withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") { | ||
| // join | ||
| // a (complex join key) | ||
| // b | ||
| val plan1 = testRelation1.join(testRelation2, condition = Some($"a" % 2 === $"x")) | ||
| val expected1 = testRelation1.select($"a", $"b", ($"a" % 2) as "_complexjoinkey_0").join( | ||
| testRelation2, condition = Some($"_complexjoinkey_0" === $"x")) | ||
| .select($"a", $"b", $"x", $"y") | ||
| comparePlans(Optimize.execute(plan1.analyze), expected1.analyze) | ||
|
|
||
| // join | ||
| // project | ||
| // a (complex join key) | ||
| // b | ||
| val plan2 = testRelation1.select($"a").join( | ||
| testRelation2, condition = Some($"a" % 2 === $"x")) | ||
| val expected2 = testRelation1.select($"a", ($"a" % 2) as "_complexjoinkey_0") | ||
| .join(testRelation2, condition = Some($"_complexjoinkey_0" === $"x")) | ||
| .select($"a", $"x", $"y") | ||
| comparePlans(Optimize.execute(plan2.analyze), expected2.analyze) | ||
|
|
||
| // join | ||
| // a (two complex join keys) | ||
| // b | ||
| val plan3 = testRelation1.join(testRelation2, | ||
| condition = Some($"a" % 2 === $"x" && $"b" % 3 === $"y")) | ||
| val expected3 = testRelation1.select($"a", $"b", ($"a" % 2) as "_complexjoinkey_0", | ||
| ($"b" % 3) as "_complexjoinkey_1").join(testRelation2, | ||
| condition = Some($"_complexjoinkey_0" === $"x" && $"_complexjoinkey_1" === $"y")) | ||
| .select($"a", $"b", $"x", $"y") | ||
| comparePlans(Optimize.execute(plan3.analyze), expected3.analyze) | ||
|
|
||
| // join | ||
| // a | ||
| // b (complex join key) | ||
| val plan4 = testRelation1.join(testRelation2, condition = Some($"a" === $"x" % 2)) | ||
| val expected4 = testRelation1.join(testRelation2.select($"x", $"y", | ||
| ($"x" % 2) as "_complexjoinkey_0"), condition = Some($"a" === $"_complexjoinkey_0")) | ||
| .select($"a", $"b", $"x", $"y") | ||
| comparePlans(Optimize.execute(plan4.analyze), expected4.analyze) | ||
|
|
||
| // join | ||
| // a (complex join key) | ||
| // b (complex join key) | ||
| val plan5 = testRelation1.join(testRelation2, condition = Some($"a" % 2 === $"x" % 3)) | ||
| val expected5 = testRelation1.select($"a", $"b", ($"a" % 2) as "_complexjoinkey_0").join( | ||
| testRelation2.select($"x", $"y", ($"x" % 3) as "_complexjoinkey_1"), | ||
| condition = Some($"_complexjoinkey_0" === $"_complexjoinkey_1")) | ||
| .select($"a", $"b", $"x", $"y") | ||
| comparePlans(Optimize.execute(plan5.analyze), expected5.analyze) | ||
| } | ||
| } | ||
|
|
||
| test("do not pull out complex join keys") { | ||
| // can broadcast | ||
| withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "100000") { | ||
| val p1 = testRelation1.join(testRelation2, condition = Some($"a" % 2 === $"x")).analyze | ||
| comparePlans(Optimize.execute(p1), p1) | ||
|
|
||
| val p2 = testRelation1.join(testRelation2, condition = Some($"a" === $"x" % 2)).analyze | ||
| comparePlans(Optimize.execute(p2), p2) | ||
| } | ||
|
|
||
| // not contains complex expression | ||
| val p1 = testRelation1.subquery("t1").join( | ||
| testRelation2.subquery("t2"), condition = Some($"a" === $"x")) | ||
| comparePlans(Optimize.execute(p1.analyze), p1.analyze) | ||
|
|
||
| // not a equi-join | ||
| val p2 = testRelation1.subquery("t1").join(testRelation2.subquery("t2")) | ||
| comparePlans(Optimize.execute(p2.analyze), p2.analyze) | ||
| } | ||
| } |
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.
There is an advantage to putting it here:
However, a disadvantage cannot be avoided:
concat(col1, col2, col3, col4 ...).