dongjoon-hyun opened a new pull request, #60628: URL: https://github.com/apache/spark/pull/60628
### What changes were proposed in this pull request? This PR aims to fix `CALL` to fail with the same `AnalysisException` as `EXPLAIN CALL` and `SELECT` when an argument has an invalid subquery, instead of invoking the procedure or failing with `INTERNAL_ERROR`. `InvokeProcedures` runs in the `Resolution` batch of the analyzer. It optimizes the `Call` to fold the arguments, invokes the procedure, and replaces the `Call` with a `CommandResult`. `CheckAnalysis` runs only after that, on the `CommandResult`, so it never checks the subqueries in the arguments. For an invalid subquery, the optimizer either fails, e.g. `OptimizeOneRowRelationSubquery` asserts that a scalar subquery has one column, or removes the invalid part and folds the argument, and then the procedure runs. `EXPLAIN CALL` does not invoke the procedure, so `CheckAnalysis` rejects the same statement. To fix it, `InvokeProcedures` now checks the subqueries in the arguments with `CheckAnalysis.checkAnalysis` before it optimizes the `Call`. They are checked in a `Project` over `OneRowRelation`, as in a SELECT, like `EvalSubqueriesForTimeTravel` does for a scalar subquery. This runs the same checks as `EXPLAIN CALL`, including the CTE inlining before them, except that a SELECT allows an uncorrelated EXISTS subquery. A `CALL` with a valid one keeps working as before, as the optimizer rewrites it into a scalar subquery (SPARK-61656 kept it for the same reason). IN and correlated EXISTS subqueries are still left for `CheckAnalysis` to reject, as SPARK-61656 does. Note that apache/spark#60392 (SPARK-61506) changes two pattern lines in the same `case` of `InvokeProcedures` and adds tests to `ProcedureSuite`. The two PRs merge without conflicts in either order (checked with `git merge-file`). apache/spark#58994 (SPARK-59741) runs `CALL` at execution time, after `CheckAnalysis`, and removes `InvokeProcedures`, which would also avoid this issue. ### 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` or invokes the procedure, while `EXPLAIN CALL` and `SELECT` reject the same subquery with a user-facing error. A procedure can have side effects, and they are applied for a statement that fails analysis. Spark 4.0.5, 4.1.4, 4.2.1 and 4.3.0 behave as below. ```sql -- `sum(in1 INT, in2 INT)` procedure of `ProcedureSuite` CALL cat.ns.sum((SELECT 1, 2), 3); CALL cat.ns.sum((SELECT 1 WHERE true OR max(1) > 0), 2); ``` **BEFORE** ``` org.apache.spark.SparkException: [INTERNAL_ERROR] The Spark SQL phase analysis failed with an internal error. You hit a bug in Spark or the Spark plugins you use. Please, report this bug to the corresponding communities or vendors, and provide the full stack trace. SQLSTATE: XX000 ``` The second statement invokes the procedure, which returns `3`. **AFTER** ``` org.apache.spark.sql.catalyst.ExtendedAnalysisException: [INVALID_SUBQUERY_EXPRESSION.SCALAR_SUBQUERY_RETURN_MORE_THAN_ONE_OUTPUT_COLUMN] Invalid subquery: Scalar subquery must return only one column, but got 2. SQLSTATE: 42823; line 1 pos 16; Project [scalar-subquery#0 [] AS subquery#3] : +- Project [1 AS 1#1, 2 AS 2#2] : +- OneRowRelation +- OneRowRelation ``` ``` org.apache.spark.sql.catalyst.ExtendedAnalysisException: [INVALID_WHERE_CONDITION] The WHERE condition "(true OR (max(1) > 0))" contains invalid expressions: max(1). Rewrite the query to avoid window functions, aggregate functions, and generator functions in the WHERE clause. SQLSTATE: 42903; line 1 pos 17; Project [scalar-subquery#4 [] AS subquery#7] : +- Project [1 AS 1#6] : +- Filter (true OR (max(1) > 0)) : +- OneRowRelation +- OneRowRelation ``` ### Does this PR introduce _any_ user-facing change? Yes. A `CALL` whose argument has a subquery that fails analysis now fails with the same `AnalysisException` as `EXPLAIN CALL`, instead of `INTERNAL_ERROR` or invoking the procedure. A `CALL` with valid subqueries, including an uncorrelated EXISTS subquery, works as before. ### How was this patch tested? Pass the CIs with the newly added test case, which fails without this fix. In tests, `spark.sql.planChangeValidation` is enabled, so without this fix the invalid `WHERE` cases fail with `PLAN_VALIDATION_FAILED_RULE_EXECUTOR` before the procedure runs. ### 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]
