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

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


The following commit(s) were added to refs/heads/master by this push:
     new 030cedcd3e6b [SPARK-61557][SQL] Fix CALL failing with 
IllegalArgumentException when an argument is not foldable
030cedcd3e6b is described below

commit 030cedcd3e6b656448d07d2a55fd825e124e98e5
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Thu Nov 12 16:37:26 2026 -0800

    [SPARK-61557][SQL] Fix CALL failing with IllegalArgumentException when an 
argument is not foldable
    
    ### What changes were proposed in this pull request?
    
    This PR aims to fix `CALL` to fail with an `AnalysisException` instead of 
`IllegalArgumentException` when an argument does not fold to a constant even 
after optimization.
    
    `InvokeProcedures` evaluates every argument once before it calls the 
procedure, and it guarded this with `require(args.forall(_.foldable), "args 
must be foldable")`. However, an argument can pass binding, type coercion and 
`checkArgTypes`, and still remain non-foldable after optimization, e.g. a 
nondeterministic expression like `rand()` or `uuid()`, or a scalar subquery 
that the optimizer cannot inline.
    
    To fix it, `InvokeProcedures` now throws the existing 
`NON_FOLDABLE_ARGUMENT` error (SQLSTATE `42K08`) with the name and type of the 
parameter of the first non-foldable argument.
    
    ### Why are the changes needed?
    
    Since Apache Spark 4.0.0 
([v4.0.0](https://github.com/apache/spark/releases/tag/v4.0.0), 2025-05-19), 
such a `CALL` fails with an `IllegalArgumentException` without an error 
condition. `QueryExecution.isInternalError` does not wrap 
`IllegalArgumentException`, so users see the raw exception.
    
    ```sql
    -- `sum(in1 INT, in2 INT)` procedure of `ProcedureSuite`
    CALL cat.ns.sum(1, CAST(rand() * 10 AS INT));
    ```
    
    **BEFORE**
    
    ```
    java.lang.IllegalArgumentException: requirement failed: args must be 
foldable
    ```
    
    **AFTER**
    
    ```
    org.apache.spark.sql.AnalysisException: [NON_FOLDABLE_ARGUMENT] The 
function `sum` requires the parameter `in2` to be a foldable expression of the 
type "INT", but the actual argument is a non-foldable. SQLSTATE: 42K08
    ```
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. A `CALL` with a non-foldable argument now fails with 
`AnalysisException` (`NON_FOLDABLE_ARGUMENT`) instead of 
`IllegalArgumentException`. A `CALL` with foldable arguments is not affected.
    
    ### How was this patch tested?
    
    Pass the CIs with the newly added test case, which fails without this fix.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    Closes #60400 from dongjoon-hyun/SPARK-61557.
    
    Authored-by: Dongjoon Hyun <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    
    skip-checks: true
---
 .../spark/sql/catalyst/analysis/InvokeProcedures.scala     | 14 +++++++++++---
 .../org/apache/spark/sql/connector/ProcedureSuite.scala    | 13 +++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/InvokeProcedures.scala
 
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/InvokeProcedures.scala
index bec08f4e5e77..0837b3cac2b3 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/InvokeProcedures.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/InvokeProcedures.scala
@@ -28,6 +28,7 @@ import org.apache.spark.sql.catalyst.rules.Rule
 import org.apache.spark.sql.catalyst.types.DataTypeUtils
 import org.apache.spark.sql.connector.catalog.procedures.BoundProcedure
 import org.apache.spark.sql.connector.read.{LocalScan, Scan}
+import org.apache.spark.sql.errors.QueryCompilationErrors
 import org.apache.spark.sql.execution.LocalTableScanExec
 import org.apache.spark.util.ArrayImplicits._
 
@@ -48,7 +49,7 @@ class InvokeProcedures(session: SparkSession) extends 
Rule[LogicalPlan] {
   }
 
   private def invoke(procedure: BoundProcedure, args: Seq[Expression], call: 
Call): LogicalPlan = {
-    val input = toInternalRow(args)
+    val input = toInternalRow(procedure, args)
     val scanIterator = procedure.call(input)
     val relations = scanIterator.asScala.map(toRelation).toSeq
     // Wrap the result in a CommandResult so SparkConnect
@@ -81,8 +82,15 @@ class InvokeProcedures(session: SparkSession) extends 
Rule[LogicalPlan] {
         s"Only local scans are temporarily supported as procedure output: 
${scan.getClass.getName}")
   }
 
-  private def toInternalRow(args: Seq[Expression]): InternalRow = {
-    require(args.forall(_.foldable), "args must be foldable")
+  private def toInternalRow(procedure: BoundProcedure, args: Seq[Expression]): 
InternalRow = {
+    // An argument that does not fold even after optimization, such as a 
nondeterministic
+    // expression or a scalar subquery the optimizer cannot inline, cannot be 
evaluated here.
+    args.zip(procedure.parameters).foreach { case (arg, param) =>
+      if (!arg.foldable) {
+        throw QueryCompilationErrors.nonFoldableArgumentError(
+          procedure.name, param.name, param.dataType)
+      }
+    }
     val values = args.map(_.eval()).toArray
     new GenericInternalRow(values)
   }
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 2dd43184e772..5ca66a0d8feb 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
@@ -98,6 +98,19 @@ class ProcedureSuite extends SharedSparkSession with 
BeforeAndAfter {
     checkAnswer(sql("CALL cat.ns.sum((1 + 1) * 2, in2 => (2 + 1) / 3)"), 
Row(5) :: Nil)
   }
 
+  test("SPARK-61557: non-foldable arguments") {
+    catalog.createProcedure(Identifier.of(Array("ns"), "sum"), UnboundSum)
+    checkError(
+      exception = intercept[AnalysisException] {
+        sql("CALL cat.ns.sum(1, CAST(rand() * 10 AS INT))")
+      },
+      condition = "NON_FOLDABLE_ARGUMENT",
+      parameters = Map(
+        "funcName" -> toSQLId("sum"),
+        "paramName" -> toSQLId("in2"),
+        "paramType" -> toSQLType("INT")))
+  }
+
   test("SPARK-59962: an argument reading a value twice") {
     // NULLIF reads the scalar subquery twice. Pre-evaluating it in a 
`Project` over the
     // `ResolvedProcedure` would leave a plan `InvokeProcedures` does not 
expect.


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

Reply via email to