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


##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveExecuteImmediate.scala:
##########
@@ -43,23 +46,45 @@ case class ResolveExecuteImmediate(sparkSession: 
SparkSession, catalogManager: C
     plan.resolveOperatorsWithPruning(_.containsPattern(EXECUTE_IMMEDIATE), 
ruleId) {
       case node @ UnresolvedExecuteImmediate(sqlStmtStr, args, 
targetVariables) =>
         if (sqlStmtStr.resolved && targetVariables.forall(_.resolved) && 
args.forall(_.resolved)) {
-          // All resolved - execute immediately and handle INTO clause if 
present
-          if (targetVariables.nonEmpty) {
-            // EXECUTE IMMEDIATE ... INTO should generate SetVariable plan 
with eagerly executed
-            // source
-            val finalTargetVars = extractTargetVariables(targetVariables)
-            val executedSource = executeImmediateQuery(sqlStmtStr, args, 
hasIntoClause = true)
-            SetVariable(finalTargetVars, executedSource)
-          } else {
-            // Regular EXECUTE IMMEDIATE without INTO - execute and return 
result directly
-            executeImmediateQuery(sqlStmtStr, args, hasIntoClause = false)
-          }
+          ResolveExecuteImmediate.resolveExecuteImmediate(
+            sparkSession, sqlStmtStr, args, targetVariables)
         } else {
-          // Not all resolved yet - wait for next iteration
           node
         }
     }
   }
+}
+
+/**
+ * Companion object containing shared resolution logic for `EXECUTE IMMEDIATE` 
statements.
+ */
+object ResolveExecuteImmediate {
+
+  /**
+   * Resolves an [[UnresolvedExecuteImmediate]] node into an executable plan.
+   *
+   * All expressions (`sqlStmtStr`, `args`, `targetVariables`) must already be 
resolved
+   * before calling this method.
+   *
+   * When an `INTO` clause is present, the dynamic SQL is executed eagerly and 
the result
+   * is wrapped in a [[SetVariable]] plan that assigns output columns to the 
target
+   * variables. Without `INTO`, the dynamic SQL is executed and its plan is 
returned
+   * directly.

Review Comment:
   "Executed eagerly" overstates what happens here: only parsing and analysis 
are eager. Queries come back as `queryExecution.analyzed` precisely to avoid 
eager evaluation (per the comment inside `executeImmediateQuery`), and the 
`INTO` source in particular is never eagerly executed since commands are 
rejected for `INTO` — execution happens when the `SetVariable` command runs.
   ```suggestion
      * When an `INTO` clause is present, the dynamic SQL is eagerly parsed and 
analyzed, and
      * the analyzed plan is wrapped in a [[SetVariable]] plan that assigns 
output columns to
      * the target variables. Without `INTO`, the resulting plan is returned 
directly
      * (commands are executed eagerly; queries are returned analyzed but 
unexecuted).
   ```



-- 
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: [email protected]

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