This is an automated email from the ASF dual-hosted git repository.

dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 6188783750cc [SPARK-61554][SQL] Fix EXPLAIN invoking procedures when 
EXECUTE IMMEDIATE contains CALL
6188783750cc is described below

commit 6188783750cc374a4591e5e164f1d1229870dabe
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Thu Nov 12 13:36:59 2026 -0800

    [SPARK-61554][SQL] Fix EXPLAIN invoking procedures when EXECUTE IMMEDIATE 
contains CALL
    
    ### What changes were proposed in this pull request?
    
    This PR aims to fix `EXPLAIN` to not invoke a stored procedure when the 
explained statement is `EXECUTE IMMEDIATE` whose SQL string is `CALL`.
    
    `ExplainCommand.run` stages only the `Call` nodes in the explained plan 
(`stageForAnalysis`). However, `EXECUTE IMMEDIATE` parses and analyzes its SQL 
string while the explained plan is analyzed, and the `InvokeProcedures` rule 
invokes the inner `CALL` there. This PR wraps the analysis and planning in 
`ExplainCommand.run` with `InvokeProcedures.withoutInvocation`, which the 
`parse_sql` ANALYZE mode already uses to analyze statements without running 
`CALL`.
    
    ### Why are the changes needed?
    
    `EXPLAIN` should show a plan without running it. `EXPLAIN EXECUTE 
IMMEDIATE` no longer runs command payloads like `DROP TABLE` or `INSERT` 
(SPARK-59689), but it still runs `CALL`.
    
    **BEFORE**
    
    ```
    spark-sql (default)> EXPLAIN EXECUTE IMMEDIATE 'CALL cat.ns.proc(1)';
    == Physical Plan ==
    CommandResult <empty>
       +- LocalTableScan <empty>
    ```
    
    The procedure is invoked once.
    
    **AFTER**
    
    ```
    spark-sql (default)> EXPLAIN EXECUTE IMMEDIATE 'CALL cat.ns.proc(1)';
    == Physical Plan ==
    Call cat.ns.proc(1)
    ```
    
    The procedure is not invoked.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. `EXPLAIN EXECUTE IMMEDIATE 'CALL ...'` no longer invokes the 
procedure. The issue exists since 
[v4.0.0](https://github.com/apache/spark/releases/tag/v4.0.0) (2025-05-19) 
through [v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) 
(2026-10-27).
    
    Since the result columns of a procedure are known only after it runs, 
`EXPLAIN EXECUTE IMMEDIATE 'CALL ...' INTO v` now reports 
`ASSIGNMENT_ARITY_MISMATCH` in its output instead of invoking the procedure.
    
    ### How was this patch tested?
    
    Pass the CIs with the newly added test case in `ProcedureSuite`.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    Closes #60397 from dongjoon-hyun/SPARK-61554.
    
    Authored-by: Dongjoon Hyun <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    (cherry picked from commit 57ba2f2725bf2ec2859c389361f240b488bed25b)
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    
    skip-checks: true
---
 .../org/apache/spark/sql/execution/command/commands.scala      | 10 +++++++---
 .../scala/org/apache/spark/sql/connector/ProcedureSuite.scala  |  6 ++++++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/commands.scala 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/commands.scala
index 0ea52f3dccb8..f64fbdf52788 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/commands.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/commands.scala
@@ -23,6 +23,7 @@ import scala.util.control.NonFatal
 import org.apache.spark.rdd.RDD
 import org.apache.spark.sql.{Row, SparkSession}
 import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow}
+import org.apache.spark.sql.catalyst.analysis.InvokeProcedures
 import org.apache.spark.sql.catalyst.expressions.{Attribute, 
AttributeReference}
 import org.apache.spark.sql.catalyst.plans.QueryPlan
 import org.apache.spark.sql.catalyst.plans.logical.{Command, 
ExecutableDuringAnalysis, LogicalPlan, SupervisingCommand}
@@ -167,9 +168,12 @@ case class ExplainCommand(
 
   // Run through the optimizer to generate the physical plan.
   override def run(sparkSession: SparkSession): Seq[Row] = try {
-    val stagedLogicalPlan = stageForAnalysis(logicalPlan)
-    val qe = sparkSession.sessionState.executePlan(stagedLogicalPlan, 
CommandExecutionMode.SKIP)
-    val outputString = qe.explainString(mode)
+    // stageForAnalysis cannot reach a CALL that EXECUTE IMMEDIATE parses 
during analysis.
+    val outputString = InvokeProcedures.withoutInvocation {
+      val stagedLogicalPlan = stageForAnalysis(logicalPlan)
+      val qe = sparkSession.sessionState.executePlan(stagedLogicalPlan, 
CommandExecutionMode.SKIP)
+      qe.explainString(mode)
+    }
     Seq(Row(outputString))
   } catch { case NonFatal(cause) =>
     ("Error occurred during query planning: \n" + cause.getMessage).split("\n")
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala 
b/sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala
index 76bed5649ffd..2dd43184e772 100644
--- 
a/sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala
+++ 
b/sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala
@@ -370,6 +370,12 @@ class ProcedureSuite extends SharedSparkSession with 
BeforeAndAfter {
     assert(explain2.toString.contains("cat.ns.sum(10, 10)"))
   }
 
+  test("SPARK-61554: EXPLAIN does not run a CALL inside EXECUTE IMMEDIATE") {
+    catalog.createProcedure(Identifier.of(Array("ns"), "sum"), 
UnboundNonExecutableSum)
+    val explain = sql("EXPLAIN EXECUTE IMMEDIATE 'CALL cat.ns.sum(5, 
5)'").head().getString(0)
+    assert(explain.contains("Call cat.ns.sum(5, 5)"), explain)
+  }
+
   test("void procedure") {
     catalog.createProcedure(Identifier.of(Array("ns"), "proc"), 
UnboundVoidProcedure)
     checkAnswer(sql("CALL cat.ns.proc('A', 'B')"), Nil)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to