wangyum commented on code in PR #40312:
URL: https://github.com/apache/spark/pull/40312#discussion_r1205132132


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/OptimizeSkewedJoin.scala:
##########
@@ -215,6 +216,32 @@ case class OptimizeSkewedJoin(ensureRequirements: 
EnsureRequirements)
         case (newLeft, newRight) =>
           shj.copy(left = newLeft, right = newRight, isSkewJoin = true)
       }.getOrElse(shj)
+
+    case bhj @ BroadcastHashJoinExec(_, _, joinType, _, _, ShuffleStage(s: 
ShuffleQueryStageExec),
+    BroadcastQueryStageExec(_, _, _), _, false) if canSplitLeftSide(joinType) 
=>
+      optimizeBroadcastHashJoin(s).map(newLeft => bhj.copy(left = newLeft, 
isSkewJoin = true))
+        .getOrElse(bhj)
+
+    case bhj @ BroadcastHashJoinExec(_, _, joinType, _, _, 
BroadcastQueryStageExec(_, _, _),
+    ShuffleStage(s: ShuffleQueryStageExec), _, false) if 
canSplitRightSide(joinType) =>
+      optimizeBroadcastHashJoin(s).map(newRight => bhj.copy(right = newRight, 
isSkewJoin = true))
+        .getOrElse(bhj)
+  }
+
+  private def optimizeBroadcastHashJoin(shuffleStage: ShuffleQueryStageExec): 
Option[SparkPlan] = {
+
+    def isSkewed(stats: MapOutputStatistics): Boolean = {
+      val medSize = Utils.median(stats.bytesByPartitionId, false)
+      val skewThreshold = getSkewThreshold(medSize)
+      stats.bytesByPartitionId.exists(_ > skewThreshold)
+    }
+
+    if (!shuffleStage.mapStats.forall(isSkewed(_))) {
+      return None
+    }
+
+    Some(SkewJoinChildWrapper(AQEShuffleReadExec(shuffleStage,
+      OptimizeShuffleWithLocalRead.getPartitionSpecs(shuffleStage, None))))

Review Comment:
   There will be no skew if do not put the same join key values together. This 
optimization assumes that the data in the map stage does not have skew values 
(usually it is true). I don’t know if databricks has any other ideas? @cloud-fan



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to