dongjoon-hyun opened a new pull request, #60460:
URL: https://github.com/apache/spark/pull/60460

   ### What changes were proposed in this pull request?
   
   This PR aims to fix `CALL` to fail with the same `AnalysisException` as 
`EXPLAIN CALL` instead of `INTERNAL_ERROR` when an argument has an IN subquery.
   
   `InvokeProcedures` runs in the `Resolution` batch of the analyzer, before 
`CheckAnalysis`. It 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 
`RewritePredicateSubquery` rewrites an IN (or correlated EXISTS) subquery in a 
`UnaryNode` into an existence join under the node. The optimized plan no longer 
matches, so `InvokeProcedures` throws `INTERNAL_ERROR`. `EXPLAIN CALL` does not 
invoke the procedure, so `CheckAnalysis` rejects the same statement with 
`UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.UNSUPPORTED_IN_EXISTS_SUBQUERY`.
   
   To fix it, `InvokeProcedures` no longer invokes a `CALL` when an argument 
has an IN or correlated EXISTS subquery, and leaves it for `CheckAnalysis` to 
reject. It uses `SubqueryExpression.hasInOrCorrelatedExistsSubquery`, the same 
condition under which `RewritePredicateSubquery` rewrites a `UnaryNode`. A 
`CALL` with an uncorrelated EXISTS subquery is handled as before, because the 
optimizer rewrites it into a scalar subquery, not a join.
   
   Note that apache/spark#60392 (SPARK-61506) changes the pattern on the next 
line to `Call(ResolvedBoundProcedure(_, _, procedure), args, _)`. The two 
changes are on different lines and merge without conflicts in either order.
   
   ### 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`, while `EXPLAIN CALL` reports a 
user-facing error for the same statement.
   
   ```sql
   -- `sum(in1 INT, in2 INT)` procedure of `ProcedureSuite`
   CALL cat.ns.sum(1, 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.catalyst.ExtendedAnalysisException: 
[UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.UNSUPPORTED_IN_EXISTS_SUBQUERY] 
Unsupported subquery expression: IN/EXISTS predicate subqueries can only be 
used in filters, joins, aggregations, window functions, projections, and 
UPDATE/MERGE/DELETE commands:
   ...
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A `CALL` with an IN subquery in an argument now fails with 
`AnalysisException` 
(`UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.UNSUPPORTED_IN_EXISTS_SUBQUERY`, 
SQLSTATE `0A000`) instead of `INTERNAL_ERROR`, the same as `EXPLAIN CALL`. A 
`CALL` that used to succeed only because the optimizer removed the IN subquery, 
e.g. with `CAST(false AND 1 IN (SELECT 1) AS INT)`, now fails with the same 
error, as `EXPLAIN CALL` already did.
   
   ### 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
   


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