-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-28356][SHUFFLE][FOLLOWUP] Fix case with different pre-shuffle partition numbers #25479
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,7 +82,12 @@ case class ReduceNumShufflePartitions(conf: SQLConf) extends Rule[SparkPlan] { | |
| // `ShuffleQueryStageExec` gives null mapOutputStatistics when the input RDD has 0 partitions, | ||
| // we should skip it when calculating the `partitionStartIndices`. | ||
| val validMetrics = shuffleMetrics.filter(_ != null) | ||
| if (validMetrics.nonEmpty) { | ||
| // 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) { | ||
|
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. After we have this condition
Contributor
Author
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. Yes, we could remove it, but the assert has been there since the original version of
Contributor
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. I think it is fine to remove it. We can improve the handling of |
||
| val partitionStartIndices = estimatePartitionStartIndices(validMetrics.toArray) | ||
| // This transformation adds new nodes, so we must use `transformUp` here. | ||
| plan.transformUp { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -587,4 +587,22 @@ class ReduceNumShufflePartitionsSuite extends SparkFunSuite with BeforeAndAfterA | |
| } | ||
| withSparkSession(test, 200, None) | ||
| } | ||
|
|
||
| test("Union two datasets with different pre-shuffle partition number") { | ||
| val test: SparkSession => Unit = { spark: SparkSession => | ||
| val df1 = spark.range(3).join(spark.range(3), "id").toDF() | ||
| val df2 = spark.range(3).groupBy().sum() | ||
|
|
||
| val resultDf = df1.union(df2) | ||
|
|
||
| checkAnswer(resultDf, Seq((0), (1), (2), (3)).map(i => Row(i))) | ||
|
Contributor
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. does this fail without the fix?
Contributor
Author
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. It does. The plan is: and the error comes from this assert: https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/ReduceNumShufflePartitions.scala#L136
Contributor
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 fill the
Contributor
Author
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. Oh ok, sure, filled. |
||
|
|
||
| val finalPlan = resultDf.queryExecution.executedPlan | ||
| .asInstanceOf[AdaptiveSparkPlanExec].executedPlan | ||
| // As the pre-shuffle partition number are different, we will skip reducing | ||
| // the shuffle partition numbers. | ||
| assert(finalPlan.collect { case p: CoalescedShuffleReaderExec => p }.length == 0) | ||
| } | ||
| withSparkSession(test, 100, None) | ||
| } | ||
| } | ||
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.
let's also give an example about when we will have different pre-shuffle partition numbers.
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.
Ok, added. Please let me know if it should be more detailed.