cloud-fan commented on a change in pull request #32513:
URL: https://github.com/apache/spark/pull/32513#discussion_r645568537



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/QueryExecution.scala
##########
@@ -73,23 +76,54 @@ class QueryExecution(
     sparkSession.sessionState.analyzer.executeAndCheck(logical, tracker)
   }
 
+  // SPARK-35378: Commands should be executed eagerly so that `sql("INSERT 
...")` can trigger the
+  // table insertion immediately without a `.collect()`. We also need to 
eagerly execute non-root
+  // commands, because many commands return `GenericInternalRow` and can't be 
put in a query plan
+  // directly, otherwise the query engine may cast `GenericInternalRow` to 
`UnsafeRow` and fail.
+  lazy val commandExecuted: LogicalPlan = mode match {
+    case CommandExecutionMode.EAGERLY => 
analyzed.mapChildren(eagerlyExecuteCommands)
+    case CommandExecutionMode.COMMON => eagerlyExecuteCommands(analyzed)
+    case CommandExecutionMode.SKIP => analyzed
+  }
+
+  private def eagerlyExecuteCommands(p: LogicalPlan) = p transformDown {
+    case c: Command =>
+      val qe = sparkSession.sessionState.executePlan(c, 
CommandExecutionMode.EAGERLY)
+      val result = SQLExecution.withNewExecutionId(qe, 
name)(qe.executedPlan.executeCollect())
+      CommandResult(
+        qe.analyzed.output,
+        qe.commandExecuted,
+        qe.executedPlan,
+        result)
+    case other => other
+  }
+
   lazy val withCachedData: LogicalPlan = sparkSession.withActive {
     assertAnalyzed()
     assertSupported()
     // clone the plan to avoid sharing the plan instance between different 
stages like analyzing,
     // optimizing and planning.
-    sparkSession.sharedState.cacheManager.useCachedData(analyzed.clone())
+    
sparkSession.sharedState.cacheManager.useCachedData(commandExecuted.clone())
   }
 
-  lazy val optimizedPlan: LogicalPlan = 
executePhase(QueryPlanningTracker.OPTIMIZATION) {
-    // clone the plan to avoid sharing the plan instance between different 
stages like analyzing,
-    // optimizing and planning.
-    val plan = 
sparkSession.sessionState.optimizer.executeAndTrack(withCachedData.clone(), 
tracker)
-    // We do not want optimized plans to be re-analyzed as literals that have 
been constant folded
-    // and such can cause issues during analysis. While `clone` should 
maintain the `analyzed` state
-    // of the LogicalPlan, we set the plan as analyzed here as well out of 
paranoia.
-    plan.setAnalyzed()
-    plan
+  private def assertCommandExecuted(): Unit = commandExecuted

Review comment:
       we can mark this public so that other places don't need to do 
`qe.commandExecuted` to trigger command execution, but just call 
`qe.assertCommandExecuted`




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