Github user mgaido91 commented on a diff in the pull request:
https://github.com/apache/spark/pull/21156#discussion_r200933998
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeJoinExec.scala
---
@@ -76,8 +76,36 @@ case class SortMergeJoinExec(
s"${getClass.getSimpleName} should not take $x as the JoinType")
}
- override def requiredChildDistribution: Seq[Distribution] =
- HashClusteredDistribution(leftKeys) ::
HashClusteredDistribution(rightKeys) :: Nil
+ private def avoidShuffleIfPossible(
+ joinKeys: Seq[Expression],
+ expressions: Seq[Expression]): Seq[Distribution] = {
+ val indices = expressions.map(x =>
joinKeys.indexWhere(_.semanticEquals(x)))
+ HashClusteredDistribution(indices.map(leftKeys(_))) ::
+ HashClusteredDistribution(indices.map(rightKeys(_))) :: Nil
+ }
+
+ override def requiredChildDistribution: Seq[Distribution] = {
+ if (!conf.sortMergeJoinExecChildrenPartitioningDetection) {
+ return HashClusteredDistribution(leftKeys) ::
HashClusteredDistribution(rightKeys) :: Nil
+ }
+
+ val leftPartitioning = left.outputPartitioning
+ val rightPartitioning = right.outputPartitioning
+ leftPartitioning match {
+ case HashPartitioning(leftExpressions, _)
+ if leftPartitioning.satisfies(ClusteredDistribution(leftKeys)) =>
+ avoidShuffleIfPossible(leftKeys, leftExpressions)
+
+ case _ => rightPartitioning match {
--- End diff --
IIUC if either `left` or `right` are not `HashPartitioning` we are sure we
won't meet the required distribution, so I guess this is useless, isn't it?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]