-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32083][SQL] Apply CoalesceShufflePartitions when input RDD has 0 partitions with AQE #28954
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ package org.apache.spark.sql.execution.adaptive | |
|
|
||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
| import org.apache.spark.sql.execution.{ShufflePartitionSpec, SparkPlan} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| /** | ||
|
|
@@ -34,7 +34,7 @@ case class CoalesceShufflePartitions(session: SparkSession) extends Rule[SparkPl | |
| return plan | ||
| } | ||
| if (!plan.collectLeaves().forall(_.isInstanceOf[QueryStageExec]) | ||
| || plan.find(_.isInstanceOf[CustomShuffleReaderExec]).isDefined) { | ||
| || plan.find(_.isInstanceOf[CustomShuffleReaderExec]).isDefined) { | ||
| // If not all leaf nodes are query stages, it's not safe to reduce the number of | ||
| // shuffle partitions, because we may break the assumption that all children of a spark plan | ||
| // have same number of output partitions. | ||
|
|
@@ -56,20 +56,7 @@ case class CoalesceShufflePartitions(session: SparkSession) extends Rule[SparkPl | |
| // we should skip it when calculating the `partitionStartIndices`. | ||
| val validMetrics = shuffleStages.flatMap(_.mapStats) | ||
|
|
||
| // We may have different pre-shuffle partition numbers, don't reduce shuffle partition number | ||
| // in that case. For example when we union fully aggregated data (data is arranged to a single | ||
| // partition) and a result of a SortMergeJoin (multiple partitions). | ||
| val distinctNumPreShufflePartitions = | ||
| validMetrics.map(stats => stats.bytesByPartitionId.length).distinct | ||
| if (validMetrics.nonEmpty && distinctNumPreShufflePartitions.length == 1) { | ||
| // We fall back to Spark default parallelism if the minimum number of coalesced partitions | ||
| // is not set, so to avoid perf regressions compared to no coalescing. | ||
| val minPartitionNum = conf.getConf(SQLConf.COALESCE_PARTITIONS_MIN_PARTITION_NUM) | ||
| .getOrElse(session.sparkContext.defaultParallelism) | ||
| val partitionSpecs = ShufflePartitionsUtil.coalescePartitions( | ||
| validMetrics.toArray, | ||
| advisoryTargetSize = conf.getConf(SQLConf.ADVISORY_PARTITION_SIZE_IN_BYTES), | ||
| minNumPartitions = minPartitionNum) | ||
| def updatePlan(partitionSpecs: Seq[ShufflePartitionSpec]): SparkPlan = { | ||
| // This transformation adds new nodes, so we must use `transformUp` here. | ||
| val stageIds = shuffleStages.map(_.id).toSet | ||
| plan.transformUp { | ||
|
|
@@ -79,8 +66,29 @@ case class CoalesceShufflePartitions(session: SparkSession) extends Rule[SparkPl | |
| case stage: ShuffleQueryStageExec if stageIds.contains(stage.id) => | ||
| CustomShuffleReaderExec(stage, partitionSpecs) | ||
| } | ||
| } | ||
|
|
||
| if (validMetrics.isEmpty) { | ||
| updatePlan(Nil) | ||
|
Member
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. Can you add a comment for the case of 0-partition? |
||
| } else { | ||
| plan | ||
| // We may have different pre-shuffle partition numbers, don't reduce shuffle partition | ||
| // number in that case. For example when we union fully aggregated data (data is arranged | ||
| // to a single partition) and a result of a SortMergeJoin (multiple partitions). | ||
| val distinctNumPreShufflePartitions = | ||
| validMetrics.map(stats => stats.bytesByPartitionId.length).distinct | ||
| if (distinctNumPreShufflePartitions.length == 1) { | ||
| // We fall back to Spark default parallelism if the minimum number of coalesced partitions | ||
| // is not set, so to avoid perf regressions compared to no coalescing. | ||
| val minPartitionNum = conf.getConf(SQLConf.COALESCE_PARTITIONS_MIN_PARTITION_NUM) | ||
| .getOrElse(session.sparkContext.defaultParallelism) | ||
| val partitionSpecs = ShufflePartitionsUtil.coalescePartitions( | ||
| validMetrics.toArray, | ||
| advisoryTargetSize = conf.getConf(SQLConf.ADVISORY_PARTITION_SIZE_IN_BYTES), | ||
| minNumPartitions = minPartitionNum) | ||
| updatePlan(partitionSpecs) | ||
| } else { | ||
| plan | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
if a query stage has multiple leaf shuffles, and only one of them has 0-partition input RDD. What shall we do?
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.
I think it's like coalescing one less shuffles and handled by the
nonEmptycodes.