dongjoon-hyun opened a new pull request, #60473:
URL: https://github.com/apache/spark/pull/60473
### What changes were proposed in this pull request?
This PR aims to fix `CALL` to fail with `NON_FOLDABLE_ARGUMENT` instead of
`INTERNAL_ERROR` when an optimizer rule adds an operator under the `Call` to
evaluate an argument, e.g. for a Python UDF or for an IN subquery inside a
scalar subquery.
`InvokeProcedures` runs the optimizer on the `Call` to fold the arguments,
and expects `Call(ResolvedProcedure(_, _, procedure: BoundProcedure), args, _)`
back. However, `Call` is a `UnaryNode` whose child is the procedure, and some
optimizer rules evaluate an expression of a `UnaryNode` in a new operator under
the node.
- `ExtractPythonUDFs` puts a `BatchEvalPython` under the `Call` for a Python
UDF argument.
- `OptimizeOneRowRelationSubquery` inlines a scalar subquery like `(SELECT
CAST(1 IN (SELECT 1) AS INT))`, which moves its IN subquery into the argument,
and `RewritePredicateSubquery` then adds an existence join under the `Call`.
```
Call [1, cast(exists#106 as int)], false
+- Join ExistenceJoin(exists#106)
:- ResolvedProcedure ...
+- Filter true
+- OneRowRelation
```
Such an argument reads the output of the new operator and does not fold, but
the optimized plan no longer matches, so `InvokeProcedures` throws
`INTERNAL_ERROR`. `EXPLAIN CALL` does not invoke the procedure, so it prints a
plan for the same statement.
To fix it, when the child of the optimized `Call` is not the procedure,
`InvokeProcedures` now throws `NON_FOLDABLE_ARGUMENT` for the first
non-foldable argument, as it already does for any other argument that does not
fold. This covers any rule that adds such an operator, instead of adding an
exception for `Call` to each rule. If every argument still folds,
`INTERNAL_ERROR` remains the fallback, as for any other unexpected plan.
Note that SPARK-61656 (apache/spark#60460) changed the guard of the `case c:
Call` above, so that a `CALL` with an IN subquery directly in an argument is
left for `CheckAnalysis` to reject with `UNSUPPORTED_IN_EXISTS_SUBQUERY`, like
`EXPLAIN CALL`. An IN subquery inside a scalar subquery still reaches the new
code, because the guard does not look into the plan of the scalar subquery and
`CheckAnalysis` accepts it.
Also note that apache/spark#60392 (SPARK-61506) replaces
`ResolvedProcedure(_, _, procedure: BoundProcedure)` with
`ResolvedBoundProcedure(_, _, procedure)` in the first `case`. It merges
without conflicts too, but the new `case` matches the same pattern on the
original `Call`, so whichever PR lands later needs to update it. The new test
case fails otherwise.
### 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 `INTERNAL_ERROR`.
```sql
-- `sum(in1 INT, in2 INT)` procedure of `ProcedureSuite`
CALL cat.ns.sum(1, (SELECT CAST(1 IN (SELECT 1) AS INT)));
```
**BEFORE**
```
org.apache.spark.SparkException: [INTERNAL_ERROR] Unexpected plan for
optimized CALL statement SQLSTATE: XX000
```
**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
```
A Python UDF argument, e.g. `CALL cat.ns.sum(1, pyudf(1))`, fails and is
fixed in the same way.
### Does this PR introduce _any_ user-facing change?
Yes. A `CALL` with an argument that the optimizer evaluates in an operator
under the `Call`, e.g. a Python UDF or an IN subquery inside a scalar subquery,
now fails with `AnalysisException` (`NON_FOLDABLE_ARGUMENT`, SQLSTATE `42K08`)
instead of `INTERNAL_ERROR`. 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.
The new test case uses SQL only, so that it runs without Python. I also
checked manually that a Python UDF argument
(`IntegratedUDFTestUtils.TestPythonUDF`) fails with `NON_FOLDABLE_ARGUMENT`
after this fix, and that `CALL cat.ns.sum(1, 2)` still fails with
`INTERNAL_ERROR` when an extra optimizer rule
(`spark.experimental.extraOptimizations`) puts a `Project` under the `Call`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
--
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]