cloud-fan commented on a change in pull request #32883: URL: https://github.com/apache/spark/pull/32883#discussion_r659840060
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/ExpandShufflePartitions.scala ########## @@ -0,0 +1,98 @@ +/* + * 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.catalyst.plans.physical.SinglePartition +import org.apache.spark.sql.execution.SparkPlan +import org.apache.spark.sql.execution.exchange.{EnsureRequirements, REBALANCE_PARTITIONS_BY_COL, REBALANCE_PARTITIONS_BY_NONE, ShuffleExchangeLike, ShuffleOrigin} +import org.apache.spark.sql.internal.SQLConf + +/** + * A rule to expand the 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 expanded. + * 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] + */ +object ExpandShufflePartitions extends CustomShuffleReaderRule { + override def supportedShuffleOrigins: Seq[ShuffleOrigin] = + Seq(REBALANCE_PARTITIONS_BY_NONE, REBALANCE_PARTITIONS_BY_COL) + + private def expandPartitions(plan: SparkPlan): SparkPlan = { + def collectShuffleStageInfos(plan: SparkPlan): Seq[ShuffleStageInfo] = plan match { + case ShuffleStageInfo(stage, specs) => Seq(new ShuffleStageInfo(stage, specs)) + case _ => plan.children.flatMap(collectShuffleStageInfos) + } + val shuffleStageInfos = collectShuffleStageInfos(plan) + assert(shuffleStageInfos.size == 1) + val shuffleStageInfo = shuffleStageInfos.head + if (!supportCoalesce(shuffleStageInfo.shuffleStage.shuffle)) { + return plan + } + + val advisorySize = conf.getConf(SQLConf.ADVISORY_PARTITION_SIZE_IN_BYTES) + val mapStats = shuffleStageInfo.shuffleStage.mapStats + if (mapStats.isEmpty || + mapStats.get.bytesByPartitionId.forall(_ <= advisorySize)) { + return plan + } + + val newPartitionsSpec = ShufflePartitionsUtil.expandPartitions( + mapStats.get, shuffleStageInfo.partitionSpecs, advisorySize) + def updateShuffleReaders(p: SparkPlan): SparkPlan = p match { + case ShuffleStageInfo(stage, _) => + CustomShuffleReaderExec(stage, newPartitionsSpec) + case _ => p.mapChildren(updateShuffleReaders) + } + updateShuffleReaders(plan) + } + + override def apply(plan: SparkPlan): SparkPlan = { Review comment: If you want to expand the use cases of Rebalance, please mention it explicitly and discuss the relevant changes we need to make. Otherwise, let's assume Rebalance is only used to optimize file writing and do no introduce complexity for non-existing use cases. ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala ########## @@ -642,6 +642,15 @@ object SQLConf { .bytesConf(ByteUnit.BYTE) .createWithDefault(0L) + val ADAPTIVE_EXPAND_PARTITIONS_ENABLED = Review comment: OK, then we need to think about the config name. How about `spark.sql.adaptive.optimizeSkewsInRebalacePartitions`? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/ShufflePartitionsUtil.scala ########## @@ -296,4 +296,63 @@ object ShufflePartitionsUtil extends Logging { tryMergePartitions() partitionStartIndices.toArray } + + /** Review comment: Are these new methods exactly the same as the removed ones in `OptimizeSkewedJoin`? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/ShufflePartitionsUtil.scala ########## @@ -296,4 +296,63 @@ object ShufflePartitionsUtil extends Logging { tryMergePartitions() partitionStartIndices.toArray } + + /** Review comment: and do all the methods need to be public? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/OptimizeSkewedPartitions.scala ########## @@ -0,0 +1,73 @@ +/* + * 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.catalyst.plans.physical.SinglePartition +import org.apache.spark.sql.execution.SparkPlan +import org.apache.spark.sql.execution.exchange.{REBALANCE_PARTITIONS_BY_COL, REBALANCE_PARTITIONS_BY_NONE, ShuffleExchangeLike, ShuffleOrigin} +import org.apache.spark.sql.internal.SQLConf + +/** + * A rule to expand the shuffle partitions based on the map output statistics, which can Review comment: Please revisit all the docs and comments in this PR. I think "expand" is a weird name. What we do is similar to skew join handling and we are optimizing skews in rebalance partitions. -- 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]
