anuragmantri opened a new pull request, #60797:
URL: https://github.com/apache/spark/pull/60797

   ### What changes were proposed in this pull request?
   
   Backport of #60392 to `branch-4.1`.
   
   Differences from #60392:
   - `branch-4.1` does not have `SimpleProcedure` (SPARK-54834), so its Javadoc 
change is dropped. The tests that use a `SimpleProcedure` use a test procedure 
that implements both `UnboundProcedure` and `BoundProcedure` and whose `bind()` 
returns itself, as Iceberg's procedures do.
   - `branch-4.1` does not have SPARK-59962, so the two changes that reword its 
comments, in `RewriteWithExpression` and in the `SPARK-59962` test, are dropped.
   - `branch-4.1` does not have SPARK-58001, so `UNSUPPORTED_EXPR_FOR_OPERATOR` 
has no `<operator>` parameter. The new CheckAnalysis error passes only 
`invalidExprSqls`, as the other uses of that condition on `branch-4.1` do.
   - `branch-4.1` does not have SPARK-61553, so a catalog whose `loadProcedure` 
returns null gives a `ResolvedProcedure` over null. The "Unsupported procedure" 
error in `BindProcedures` prints `null` for it instead of throwing a 
`NullPointerException`, and a new test covers it.
   - On `branch-4.1`, DESCRIBE PROCEDURE does not use `ResolvedProcedure`, so 
the doc of the new `ProcedureReference` mentions only CALL.
   
   This PR records in the plan whether a `Call` is bound: `BindProcedures` 
replaces its `ResolvedProcedure` child with a new `ResolvedBoundProcedure`, and 
`Call.bound` checks for that node.
   
   Before, `Call.bound` was true as soon as the procedure was a 
`BoundProcedure`. A procedure that implements both `UnboundProcedure` and 
`BoundProcedure`, like Iceberg's procedures, counted as bound before 
`BindProcedures` had rearranged its arguments and filled in defaults.
   
   Now:
   - `BindProcedures` binds a `Call` once. It also binds a `Call` over a 
procedure that is only a `BoundProcedure`.
   - `ProcedureArgumentCoercion` and `InvokeProcedures` wait until the `Call` 
is bound.
   - A `Call` that is not invoked, such as `EXPLAIN CALL`, keeps its procedure 
child: `PullOutNondeterministic` skips `Call`, and `Call` allows 
nondeterministic arguments. If a rule such as `TimeWindowing` put an operator 
under a `Call` before it was bound, CheckAnalysis fails it with 
`UNSUPPORTED_EXPR_FOR_OPERATOR`. Any other resolved `Call` that is not bound 
fails with `INTERNAL_ERROR`.
   - The Javadoc of `UnboundProcedure.bind` says when Spark calls `bind()`.
   
   ### Why are the changes needed?
   
   `BindProcedures` waits until all arguments are resolved. Some arguments 
become resolved only after `BindProcedures` has run in an analyzer iteration, 
for example a timestamp minus an interval, a scalar subquery, or `CAST('...' AS 
TIMESTAMP)`. Then `ProcedureArgumentCoercion` and `InvokeProcedures` run on the 
arguments as the user wrote them.
   
   For a procedure that implements both interfaces, whose `bind()` returns 
itself, with parameters `ts TIMESTAMP, label STRING DEFAULT 'none'`, on the 
4.1.4 release:
   
   ```sql
   CALL cat.ns.p(TIMESTAMP '2026-01-02 00:00:00' - INTERVAL '1' DAY)
   -- IllegalArgumentException: requirement failed: number of args and params 
must match after binding
   
   CALL cat.ns.p(label => 'x', ts => TIMESTAMP '2026-01-02 00:00:00' - INTERVAL 
'1' DAY)
   -- IllegalArgumentException: requirement failed: args must be foldable
   
   CALL cat.ns.p(TIMESTAMP '2026-01-02 00:00:00' - INTERVAL '1' DAY, 'x', 1)
   -- runs and ignores the third argument instead of failing with WRONG_NUM_ARGS
   ```
   
   On `branch-4.1` the second statement fails with `NON_FOLDABLE_ARGUMENT` 
instead, since SPARK-61557.
   
   If `bind()` returns a new object, it gets worse. Spark calls `call()` on the 
object that was never bound, and `EXPLAIN CALL` keeps binding until it hits the 
analyzer's iteration limit.
   
   Checking the argument count is not enough. The second statement has the 
right count, just in the wrong order.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, it fixes a bug, in 4.1.5. The first two statements above now work, and 
the third fails with `WRONG_NUM_ARGS`. For a procedure whose `bind()` returns a 
new object, `EXPLAIN CALL` now prints a plan instead of "Max iterations (100) 
reached". The bug exists since 4.0.0 for procedures that implement both 
interfaces.
   
   Three kinds of CALL that pass analysis on the 4.1.4 release and fail only on 
`collect()` with `UnsupportedOperationException` now fail analysis: one with a 
`window(...)` or `session_window(...)` argument fails with 
`UNSUPPORTED_EXPR_FOR_OPERATOR`, one with a nondeterministic argument whose 
type cannot be cast to the parameter type, such as `rand()` for a `DATE` 
parameter, fails with `DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE`, and one to a 
procedure that `loadProcedure` returns as null fails with `INTERNAL_ERROR` 
"Unsupported procedure: null".
   
   For Spark extensions, the child of a bound `Call` is now a 
`ResolvedBoundProcedure` instead of a `ResolvedProcedure`. Code that checks 
whether a `Call` is bound should use `Call.bound`. A `Call` built over a 
procedure that is only a `BoundProcedure` now goes through the same checks as 
other procedures, so one with an `OUT` or `INOUT` parameter, or with a required 
parameter after an optional one, fails with `INTERNAL_ERROR`.
   
   ### How was this patch tested?
   
   The eight `SPARK-61506` tests of #60392 are added to `ProcedureSuite`, with 
the procedure described above in place of `SimpleProcedure` and the time window 
test expecting only the `invalidExprSqls` parameter, plus `SPARK-61506: a CALL 
over a null procedure fails analysis`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (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