ulysses-you commented on a change in pull request #32084:
URL: https://github.com/apache/spark/pull/32084#discussion_r610677829



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/CoalesceShufflePartitions.scala
##########
@@ -35,14 +35,25 @@ case class CoalesceShufflePartitions(session: SparkSession) 
extends CustomShuffl
     if (!conf.coalesceShufflePartitionsEnabled) {
       return plan
     }
-    if (!plan.collectLeaves().forall(_.isInstanceOf[QueryStageExec])
-        || 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.
-      return plan
+
+    if (canCoalescePartitions(plan)) {
+      coalescePartitions(plan)
+    } else {
+      plan.transformUp {
+        case u: UnionExec =>
+          u.withNewChildren(u.children.map { child =>
+            if (canCoalescePartitions(child) &&
+              child.find(_.isInstanceOf[UnionExec]).isEmpty) {

Review comment:
       We should add the coalesce if it's children don't have `Union` to avoid 
adding duplicate `CustomShufflerReader`.
   
   Without `CombineUnions`, the plan can be
   ````
   Union
     Union
        HashAggregate
         ShuffleQueryStage
        FileScan
     FileScan
   ````




-- 
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.

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