-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-56034][SQL] Push down Join through Union when the right side is broadcastable #54865
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
Draft
LuciferYang
wants to merge
6
commits into
apache:master
Choose a base branch
from
LuciferYang:SPARK-56034
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d9905e6
init
LuciferYang 6451eb8
use canPlanAsBroadcastHashJoin
LuciferYang a87b297
remove used PredicateHelper
LuciferYang 9a76211
Merge branch 'upmaster' into SPARK-56034
LuciferYang 35b2c58
remove config
LuciferYang 43f120a
cleanup unnecessary code comments
LuciferYang 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
96 changes: 96 additions & 0 deletions
96
...yst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PushDownJoinThroughUnion.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,96 @@ | ||
| /* | ||
| * 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.SparkException | ||
| import org.apache.spark.sql.catalyst.analysis.DeduplicateRelations | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.catalyst.trees.TreePattern.{JOIN, UNION} | ||
|
|
||
| /** | ||
| * Pushes down `Join` through `Union` when the right side of the join is small enough | ||
| * to broadcast. | ||
| * | ||
| * This rule transforms the pattern: | ||
| * {{{ | ||
| * Join(Union(c1, c2, ..., cN), right, joinType, cond) | ||
| * }}} | ||
| * into: | ||
| * {{{ | ||
| * Union(Join(c1, right, joinType, cond1), Join(c2, right, joinType, cond2), ...) | ||
| * }}} | ||
| * | ||
| * where each `condK` has the Union output attributes rewritten to the corresponding child's | ||
| * output attributes. | ||
| * | ||
| * This is beneficial when the right side is small enough to broadcast, because it avoids | ||
| * shuffling the (potentially very large) Union result before the Join. Instead, each Union | ||
| * branch joins independently with the broadcasted right side. | ||
| * | ||
| * Applicable join types: Inner, LeftOuter. | ||
| */ | ||
| object PushDownJoinThroughUnion | ||
| extends Rule[LogicalPlan] | ||
| with JoinSelectionHelper { | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan.transformUpWithPruning( | ||
| _.containsAllPatterns(JOIN, UNION), ruleId) { | ||
|
|
||
| case join @ Join(u: Union, right, joinType, joinCond, hint) | ||
| if (joinType == Inner || joinType == LeftOuter) && | ||
| canPlanAsBroadcastHashJoin(join, conf) && | ||
| // Exclude right subtrees containing subqueries, as DeduplicateRelations | ||
| // may not correctly handle correlated references when cloning. | ||
| !right.exists(_.expressions.exists(SubqueryExpression.hasSubquery)) => | ||
|
|
||
| val unionHeadOutput = u.children.head.output | ||
| val newChildren = u.children.zipWithIndex.map { case (child, idx) => | ||
| val newRight = if (idx == 0) right else dedupRight(right) | ||
| val leftRewrites = AttributeMap(unionHeadOutput.zip(child.output)) | ||
| val rightRewrites = if (idx == 0) { | ||
| AttributeMap.empty[Attribute] | ||
| } else { | ||
| AttributeMap(right.output.zip(newRight.output)) | ||
| } | ||
| val newCond = joinCond.map(_.transform { | ||
| case a: Attribute if leftRewrites.contains(a) => leftRewrites(a) | ||
| case a: Attribute if rightRewrites.contains(a) => rightRewrites(a) | ||
| }) | ||
| Join(child, newRight, joinType, newCond, hint) | ||
| } | ||
| u.withNewChildren(newChildren) | ||
| } | ||
|
|
||
| /** | ||
| * Creates a copy of `plan` with fresh ExprIds on all output attributes, | ||
| * using the same "fake self-join + DeduplicateRelations" pattern as InlineCTE. | ||
| */ | ||
| private def dedupRight(plan: LogicalPlan): LogicalPlan = { | ||
| DeduplicateRelations( | ||
| Join(plan, plan, Inner, None, JoinHint.NONE) | ||
| ) match { | ||
| case Join(_, deduped, _, _, _) => deduped | ||
| case other => | ||
| throw SparkException.internalError( | ||
| s"Unexpected plan shape after DeduplicateRelations: ${other.getClass.getName}") | ||
| } | ||
| } | ||
| } | ||
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
259 changes: 259 additions & 0 deletions
259
...rc/test/scala/org/apache/spark/sql/catalyst/optimizer/PushDownJoinThroughUnionSuite.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,259 @@ | ||
| /* | ||
| * 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.Explode | ||
| import org.apache.spark.sql.catalyst.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan, Union} | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.IntegerType | ||
|
|
||
| class PushDownJoinThroughUnionSuite extends PlanTest { | ||
|
|
||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = Batch("PushDownJoinThroughUnion", FixedPoint(10), | ||
| PushDownJoinThroughUnion) :: Nil | ||
| } | ||
|
|
||
| val testRelation1 = LocalRelation($"a".int, $"b".int) | ||
| val testRelation2 = LocalRelation($"c".int, $"d".int) | ||
| val testRelation3 = LocalRelation($"e".int, $"f".int) | ||
| val testRelation4 = LocalRelation($"g".int, $"h".int) | ||
|
|
||
| test("Push down Inner Join through Union when right side is small") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(testRelation3, Inner, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| val expected = Union( | ||
| testRelation1.join(testRelation3, Inner, Some($"a" === $"e")), | ||
| testRelation2.join(testRelation3, Inner, Some($"c" === $"e")) | ||
| ).analyze | ||
|
|
||
| comparePlans(optimized, expected) | ||
| } | ||
| } | ||
|
|
||
| test("Push down Left Outer Join through Union when right side is small") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(testRelation3, LeftOuter, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| val expected = Union( | ||
| testRelation1.join(testRelation3, LeftOuter, Some($"a" === $"e")), | ||
| testRelation2.join(testRelation3, LeftOuter, Some($"c" === $"e")) | ||
| ).analyze | ||
|
|
||
| comparePlans(optimized, expected) | ||
| } | ||
| } | ||
|
|
||
| test("Do not push down when right side is too large (broadcast disabled)") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") { | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(testRelation3, Inner, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| comparePlans(optimized, query.analyze) | ||
| } | ||
| } | ||
|
|
||
| test("Correctly rewrite attributes in join condition") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(testRelation3, Inner, Some($"a" === $"e" && $"b" > 10)) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| val expected = Union( | ||
| testRelation1.join(testRelation3, Inner, Some($"a" === $"e" && $"b" > 10)), | ||
| testRelation2.join(testRelation3, Inner, Some($"c" === $"e" && $"d" > 10)) | ||
| ).analyze | ||
|
|
||
| comparePlans(optimized, expected) | ||
| } | ||
| } | ||
|
|
||
| test("Push down Inner Join through 3-way Union (TPC-DS pattern)") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val union = Union(Seq(testRelation1, testRelation2, testRelation4)) | ||
| val query = union.join(testRelation3, Inner, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| val expected = Union(Seq( | ||
| testRelation1.join(testRelation3, Inner, Some($"a" === $"e")), | ||
| testRelation2.join(testRelation3, Inner, Some($"c" === $"e")), | ||
| testRelation4.join(testRelation3, Inner, Some($"g" === $"e")) | ||
| )).analyze | ||
|
|
||
| comparePlans(optimized, expected) | ||
| } | ||
| } | ||
|
|
||
| test("Do not push down unsupported join types") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val union = Union(testRelation1, testRelation2) | ||
| Seq(RightOuter, FullOuter, LeftSemi, LeftAnti).foreach { joinType => | ||
| val query = union.join(testRelation3, joinType, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
| comparePlans(optimized, query.analyze) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("Do not push down Cross Join (no join condition)") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(testRelation3, Inner, None) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| comparePlans(optimized, query.analyze) | ||
| } | ||
| } | ||
|
|
||
| test("Do not push down when Union is on the right side") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = testRelation3.join(union, Inner, Some($"e" === $"a")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| comparePlans(optimized, query.analyze) | ||
| } | ||
| } | ||
|
|
||
| test("Push down when right side is a complex subplan") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val complexRight = testRelation3 | ||
| .where($"f" > 0) | ||
| .select($"e", ($"f" + 1).as("f_plus_1")) | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(complexRight, Inner, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| // Verify the optimization was applied (Union should be the root) | ||
| assert(optimized.isInstanceOf[Union]) | ||
| // Verify no duplicate ExprIds across Union children's top-level output. | ||
| // Each branch should have independent ExprIds for the right side. | ||
| val childOutputs = optimized.asInstanceOf[Union].children.map(_.output) | ||
| for (i <- childOutputs.indices; j <- (i + 1) until childOutputs.length) { | ||
| val ids_i = childOutputs(i).map(_.exprId).toSet | ||
| val ids_j = childOutputs(j).map(_.exprId).toSet | ||
| assert(ids_i.intersect(ids_j).isEmpty, | ||
| s"Union children $i and $j share ExprIds: ${ids_i.intersect(ids_j)}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("Push down when right side contains Generate (Explode)") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val arrayRelation = LocalRelation($"k".int, $"arr".array(IntegerType)) | ||
| val rightWithGenerate = arrayRelation | ||
| .generate(Explode($"arr"), outputNames = Seq("exploded_val")) | ||
| .select($"k", $"exploded_val") | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(rightWithGenerate, Inner, Some($"a" === $"k")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| // Verify the optimization was applied | ||
| assert(optimized.isInstanceOf[Union]) | ||
| // Verify no duplicate ExprIds across Union children's output | ||
| val childOutputs = optimized.asInstanceOf[Union].children.map(_.output) | ||
| for (i <- childOutputs.indices; j <- (i + 1) until childOutputs.length) { | ||
| val ids_i = childOutputs(i).map(_.exprId).toSet | ||
| val ids_j = childOutputs(j).map(_.exprId).toSet | ||
| assert(ids_i.intersect(ids_j).isEmpty, | ||
| s"Union children $i and $j share ExprIds: ${ids_i.intersect(ids_j)}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("Push down when right side contains SubqueryAlias") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val rightWithAlias = testRelation3.subquery("dim") | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(rightWithAlias, Inner, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| // Verify the optimization was applied | ||
| assert(optimized.isInstanceOf[Union]) | ||
| // Verify no duplicate ExprIds across Union children's output | ||
| val childOutputs = optimized.asInstanceOf[Union].children.map(_.output) | ||
| for (i <- childOutputs.indices; j <- (i + 1) until childOutputs.length) { | ||
| val ids_i = childOutputs(i).map(_.exprId).toSet | ||
| val ids_j = childOutputs(j).map(_.exprId).toSet | ||
| assert(ids_i.intersect(ids_j).isEmpty, | ||
| s"Union children $i and $j share ExprIds: ${ids_i.intersect(ids_j)}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("Push down when right side contains Project with Alias") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val rightWithAlias = testRelation3 | ||
| .select($"e", ($"f" + 1).as("f_plus_1")) | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(rightWithAlias, Inner, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| assert(optimized.isInstanceOf[Union]) | ||
| val childOutputs = optimized.asInstanceOf[Union].children.map(_.output) | ||
| for (i <- childOutputs.indices; j <- (i + 1) until childOutputs.length) { | ||
| val ids_i = childOutputs(i).map(_.exprId).toSet | ||
| val ids_j = childOutputs(j).map(_.exprId).toSet | ||
| assert(ids_i.intersect(ids_j).isEmpty, | ||
| s"Union children $i and $j share ExprIds: ${ids_i.intersect(ids_j)}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("Push down when right side contains Aggregate") { | ||
| withSQLConf( | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1000") { | ||
| val rightWithAgg = testRelation3 | ||
| .groupBy($"e")(count($"f").as("cnt"), $"e") | ||
| val union = Union(testRelation1, testRelation2) | ||
| val query = union.join(rightWithAgg, Inner, Some($"a" === $"e")) | ||
| val optimized = Optimize.execute(query.analyze) | ||
|
|
||
| assert(optimized.isInstanceOf[Union]) | ||
| val childOutputs = optimized.asInstanceOf[Union].children.map(_.output) | ||
| for (i <- childOutputs.indices; j <- (i + 1) until childOutputs.length) { | ||
| val ids_i = childOutputs(i).map(_.exprId).toSet | ||
| val ids_j = childOutputs(j).map(_.exprId).toSet | ||
| assert(ids_i.intersect(ids_j).isEmpty, | ||
| s"Union children $i and $j share ExprIds: ${ids_i.intersect(ids_j)}") | ||
| } | ||
| } | ||
| } | ||
| } |
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.
Any other optimization through bug-like errors?
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.
Thanks @yaooqinn. Yes,
SparkException.internalErroris used in several optimizer rules as a defensive guard for "should-never-happen" plan shapes, for example:NestedColumnAliasing:"Unreasonable plan after optimization: $other"PushExtraPredicateThroughJoin/Optimizer:"Unexpected join type: $other"DecorrelateInnerQuery:"Unexpected domain join type $o"subquery.scala:"Unexpected plan when optimizing one row relation subquery: $o"The
dedupRightmethod here follows the same pattern — it guards against the (theoretically impossible) case whereDeduplicateRelationschanges the Join plan shape.That said,
InlineCTEuses the same "fake self-join +DeduplicateRelations" approach and simply calls.children(1)directly without any defensive check. I can align withInlineCTEand remove the explicit throw if you think that's cleaner. Alternatively, I could keep the pattern match but return the originalplanunchanged in the fallback case (skipping the dedup rather than failing). Which approach would you prefer?