-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32806][SQL] SortMergeJoin with partial hash distribution can be optimized to remove shuffle #29655
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
[SPARK-32806][SQL] SortMergeJoin with partial hash distribution can be optimized to remove shuffle #29655
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * 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.execution.exchange | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.Expression | ||
| import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.{SortExec, SparkPlan} | ||
| import org.apache.spark.sql.execution.joins.SortMergeJoinExec | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| /** | ||
| * This rule removes shuffle for the sort merge join if the following conditions are met: | ||
| * - The child of ShuffleExchangeExec has HashPartitioning with the same number of partitions | ||
| * as the other side of join. | ||
| * - The child of ShuffleExchangeExec has output partitioning which has the subset of | ||
| * join keys on the respective join side. | ||
| * | ||
| * If the above conditions are met, shuffle can be eliminated for the sort merge join | ||
| * because rows are sorted before join logic is applied. | ||
| */ | ||
| case class OptimizeSortMergeJoinWithPartialHashDistribution(conf: SQLConf) extends Rule[SparkPlan] { | ||
| def apply(plan: SparkPlan): SparkPlan = { | ||
| if (!conf.optimizeSortMergeJoinWithPartialHashDistribution) { | ||
| return plan | ||
| } | ||
|
|
||
| plan.transformUp { | ||
| case s @ SortMergeJoinExec(_, _, _, _, | ||
| lSort @ SortExec(_, _, | ||
| ExtractShuffleExchangeExecChild( | ||
| lChild, | ||
| lChildOutputPartitioning: HashPartitioning), | ||
| _), | ||
|
Comment on lines
+48
to
+51
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. nit: why we can't just pattern matching |
||
| rSort @ SortExec(_, _, | ||
| ExtractShuffleExchangeExecChild( | ||
| rChild, | ||
| rChildOutputPartitioning: HashPartitioning), | ||
| _), | ||
| false) if isPartialHashDistribution( | ||
| s.leftKeys, lChildOutputPartitioning, s.rightKeys, rChildOutputPartitioning) => | ||
| // Remove ShuffleExchangeExec. | ||
| s.copy(left = lSort.copy(child = lChild), right = rSort.copy(child = rChild)) | ||
| case other => other | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Returns true if both HashPartitioning have the same number of partitions and | ||
| * their partitioning expressions are a subset of their respective join keys. | ||
| */ | ||
| private def isPartialHashDistribution( | ||
| leftKeys: Seq[Expression], | ||
| leftPartitioning: HashPartitioning, | ||
| rightKeys: Seq[Expression], | ||
| rightPartitioning: HashPartitioning): Boolean = { | ||
| val mapping = leftKeyToRightKeyMapping(leftKeys, rightKeys) | ||
| (leftPartitioning.numPartitions == rightPartitioning.numPartitions) && | ||
| leftPartitioning.expressions.zip(rightPartitioning.expressions) | ||
| .forall { | ||
| case (le, re) => mapping.get(le.canonicalized) | ||
| .map(_.exists(_.semanticEquals(re))) | ||
| .getOrElse(false) | ||
| } | ||
|
Comment on lines
+74
to
+81
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. sorry if I miss anything, but I feel this might not be correct. We should make sure the
Contributor
Author
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. Thanks. I agree with your concerns for both cases. But, for the first example, only one side will be shuffled, so the rule should not kick in. For the second example, we have
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. Sorry if I miss anything:
If the number of buckets for
I think it's unsafe if we do not shuffle both sides.
Contributor
Author
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.
You are right. Thanks for the catch!
Yes, I understand they produce different hash values. However, it has the join condition |
||
| } | ||
|
|
||
| /* | ||
| * Returns a mapping from left key to right key if there is a one-to-one mapping. | ||
| * Otherwise, returns None. | ||
| */ | ||
| private def leftKeyToRightKeyMapping( | ||
| leftKeys: Seq[Expression], | ||
| rightKeys: Seq[Expression]): Map[Expression, Seq[Expression]] = { | ||
| assert(leftKeys.length == rightKeys.length) | ||
| val mapping = mutable.Map.empty[Expression, Seq[Expression]] | ||
| leftKeys.zip(rightKeys).foreach { | ||
| case (leftKey, rightKey) => | ||
| val key = leftKey.canonicalized | ||
| mapping.get(key) match { | ||
| case Some(v) => mapping.put(key, v :+ rightKey) | ||
| case None => mapping.put(key, Seq(rightKey)) | ||
| } | ||
| } | ||
| mapping.toMap | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An extractor that extracts the child of ShuffleExchangeExec and the child's | ||
| * output partitioning. | ||
| */ | ||
| object ExtractShuffleExchangeExecChild { | ||
| def unapply(plan: SparkPlan): Option[(SparkPlan, Partitioning)] = { | ||
| plan match { | ||
| case s: ShuffleExchangeExec => Some(s.child, s.child.outputPartitioning) | ||
| case _ => None | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
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.
We cannot implement this optimization in
EnsureRequirementsinstead? Any reason to apply this rule afterEnsureRequirementsinsert shuffles?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.
Also, could you add fine-grained tests for this rule?
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.
To do this inside
EnsureRequirements.ensureDistributionAndOrdering, it would require a newPartitioningandDistributionthat know both sides of join, so I didn't go that route. Doing this outside would be less intrusive, I thought. But please let me know if doing this insideEnsureRequirementsmakes more sense. Thanks.This is done after
EnsureRequirementssince reordering keys may eliminate shuffles in which case this rule is not applied.