cloud-fan commented on code in PR #49715:
URL: https://github.com/apache/spark/pull/49715#discussion_r1944369641


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -579,23 +593,52 @@ case class AdaptiveSparkPlanExec(
         allChildStagesMaterialized = false,
         newStages = Seq(newStage))
 
-    case q: QueryStageExec =>
+    case q: QueryStageExec if q ne currentPhysicalPlan =>
       assertStageNotFailed(q)
       CreateStageResult(newPlan = q,
         allChildStagesMaterialized = q.isMaterialized, newStages = Seq.empty)
 
     case _ =>
-      if (plan.children.isEmpty) {
-        CreateStageResult(newPlan = plan, allChildStagesMaterialized = true, 
newStages = Seq.empty)
+      // This includes cases where root node is QueryStageExec
+      val results = plan.children.map(createQueryStages(resultHandler, _, 
firstRun))
+      val allChildStagesMaterialized = 
results.forall(_.allChildStagesMaterialized)
+      val newStages = results.flatMap(_.newStages)
+      if ((plan eq currentPhysicalPlan) && allChildStagesMaterialized && 
newStages.isEmpty) {
+        // Create result stage if the plan is root plan and all children 
stages are materialized
+        // and no new stages are created.
+        val resultStage = createResultQueryStage(resultHandler, plan)
+        stagesToReplace.append(resultStage)
+        CreateStageResult(
+          newPlan = resultStage,
+          allChildStagesMaterialized = false,
+          newStages = Seq(resultStage))
       } else {
-        val results = plan.children.map(createQueryStages)
+        val newPlan = if (results.isEmpty) {
+          plan
+        } else {
+          plan.withNewChildren(results.map(_.newPlan))
+        }
         CreateStageResult(
-          newPlan = plan.withNewChildren(results.map(_.newPlan)),
-          allChildStagesMaterialized = 
results.forall(_.allChildStagesMaterialized),
-          newStages = results.flatMap(_.newStages))
+          newPlan = newPlan,
+          allChildStagesMaterialized = allChildStagesMaterialized,
+          newStages = newStages)
       }
   }
 
+  private def createResultQueryStage(

Review Comment:
   ```suggestion
     private def newResultQueryStage(
   ```
   
   To follow the name of `newQueryStage`



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