wangyum commented on a change in pull request #32883:
URL: https://github.com/apache/spark/pull/32883#discussion_r661284044



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/OptimizeSkewedPartitions.scala
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.adaptive
+
+import org.apache.spark.sql.execution.SparkPlan
+import org.apache.spark.sql.execution.exchange.{REBALANCE_PARTITIONS_BY_COL, 
REBALANCE_PARTITIONS_BY_NONE, ShuffleOrigin}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * A rule to optimize the skewed shuffle partitions based on the map output 
statistics, which can
+ * avoid data skew that hurt performance.
+ *
+ * We use ADVISORY_PARTITION_SIZE_IN_BYTES size to decide if a partition 
should be optimized.
+ * Let's say we have 3 maps with 3 shuffle partitions, and assuming r1 has 
data skew issue.
+ * the map side looks like:
+ *   m0:[b0, b1, b2], m1:[b0, b1, b2], m2:[b0, b1, b2]
+ * and the reduce side looks like:
+ *                          (without this rule) r1[m0-b1, m1-b1, m2-b1]
+ *                              /                              \
+ *   r0:[m0-b0, m1-b0, m2-b0], r1:[m0-b1], r2:[m1-b1], r3:[m2-b1], r4[m0-b2, 
m1-b2, m2-b2]
+ *
+ * Note that, this rule is only applied with the SparkPlan whose top-level 
node is
+ * ShuffleQueryStageExec.
+ */
+object OptimizeSkewedPartitions extends CustomShuffleReaderRule {
+  override def supportedShuffleOrigins: Seq[ShuffleOrigin] =
+    Seq(REBALANCE_PARTITIONS_BY_NONE, REBALANCE_PARTITIONS_BY_COL)
+
+  private def optimizeSkewedPartitions(shuffle: ShuffleQueryStageExec): 
SparkPlan = {
+    val advisorySize = conf.getConf(SQLConf.ADVISORY_PARTITION_SIZE_IN_BYTES)
+    val mapStats = shuffle.mapStats
+    if (mapStats.isEmpty ||
+      mapStats.get.bytesByPartitionId.forall(_ <= advisorySize)) {
+      return shuffle
+    }
+
+    val newPartitionsSpec = ShufflePartitionsUtil.optimizeSkewedPartitions(
+      mapStats.get.shuffleId, mapStats.get.bytesByPartitionId, advisorySize)
+    // return origin plan if we can not optimize partitions
+    if (newPartitionsSpec.length == mapStats.get.bytesByPartitionId.length) {
+      shuffle
+    } else {
+      CustomShuffleReaderExec(shuffle, newPartitionsSpec)
+    }
+  }
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    if 
(!conf.getConf(SQLConf.ADAPTIVE_OPTIMIZE_SKEWS_IN_REBALANCE_PARTITIONS_ENABLED))
 {
+      return plan
+    }
+
+    plan match {
+      case shuffle: ShuffleQueryStageExec
+          if supportedShuffleOrigins.contains(shuffle.shuffle.shuffleOrigin) =>
+        optimizeSkewedPartitions(shuffle)
+      case _ => plan
+    }
+  }

Review comment:
       Could we make it only works with `DataWritingCommandExec` and 
`InsertIntoDataSourceExec`? For example:
   ```scala
       if 
(!conf.getConf(SQLConf.ADAPTIVE_OPTIMIZE_SKEWS_IN_REBALANCE_PARTITIONS_ENABLED))
 {
         return plan
       }
   
       plan match {
         case d @ DataWritingCommandExec(_, child) if supportOptimization(d) =>
           handleSkewed(d, child)
         case i @ InsertIntoDataSourceExec(child, _, _, _, _) if 
supportOptimization(i) =>
           handleSkewed(i, child)
         case _ => plan
       }
     }
   ```




-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to