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

   ### What changes were proposed in this pull request?
   
   This PR aims to backport #60392 to `branch-4.0`.
   
   This PR records in the plan whether a `Call` is bound: `BindProcedures` 
replaces the `ResolvedProcedure` child of a `Call` with a new 
`ResolvedBoundProcedure`, and `Call.bound` checks for that node.
   - `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()`.
   
   Note that this backport differs from the original patch in the following 
`branch-4.0`-specific points:
   - `branch-4.0` has no `SimpleProcedure` (SPARK-54834), so the 
`SimpleProcedure` Javadoc hunk is dropped, and the tests that use 
`SimpleProcedure` fixtures use a test-only trait `SelfBindingProcedure` 
instead. It implements both `UnboundProcedure` and `BoundProcedure`, and `bind` 
returns the procedure itself, as Iceberg's procedures do. The test 
`SPARK-61506: simple procedure with a late-resolving argument` is named 
`SPARK-61506: self-binding procedure with a late-resolving argument`.
   - `branch-4.0` has no SPARK-61553, which on master treats a null from 
`ProcedureCatalog.loadProcedure` as a procedure that is not found. On 
`branch-4.0` the null reaches `BindProcedures`, where `other.getClass` would 
throw a `NullPointerException`, so the "Unsupported procedure" error names the 
class null-safely. A ninth test, `SPARK-61506: CALL over a null procedure fails 
analysis`, covers it.
   - `branch-4.0` has no SPARK-58001, so its `UNSUPPORTED_EXPR_FOR_OPERATOR` 
error has no `operator` parameter. CheckAnalysis passes only `invalidExprSqls`, 
as the other call sites of that error on `branch-4.0` do.
   - `branch-4.0` has no `V2TableUtil.toQualifiedName`, so `Call.simpleString` 
builds the qualified name inline, as `branch-4.0` does today.
   - `branch-4.0` has no SPARK-59962, so the hunks that edit its test and its 
`RewriteWithExpression` comment are dropped.
   - `branch-4.0` has no DESCRIBE PROCEDURE, so the scaladoc of 
`ProcedureReference` names only CALL.
   - On `branch-4.0`, `NamedParametersSupport.defaultRearrange` takes no 
resolver, and `Sum` in `ProcedureSuite` is an object, which the tests use where 
master creates a `Sum`.
   
   The hunks for `InvokeProcedures` and the `AddProjectUnderCall` test rule 
apply to the code that SPARK-61669 added on `branch-4.0`, and are the same as 
on master.
   
   ### Why are the changes needed?
   
   `Call.bound` was true as soon as the procedure was a `BoundProcedure`. So 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. `BindProcedures` waits until all 
arguments are resolved. Some arguments become resolved only after it 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 
`branch-4.0`:
   
   ```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)
   -- [NON_FOLDABLE_ARGUMENT] The function `p` requires the parameter `ts` to 
be a foldable expression ...
   
   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
   ```
   
   The 4.0.5 release does not have SPARK-61557, so there the second statement 
fails with `IllegalArgumentException: requirement failed: args must be 
foldable` instead.
   
   If `bind()` returns a new object, it gets worse. Spark reads the parameters 
of, and calls `call()` on, the object that was never bound, and `EXPLAIN CALL` 
binds the procedure again in every analyzer iteration until it reaches the 
iteration limit (100 by default), and prints a "Max iterations (100) reached" 
error instead of a plan.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, it fixes a bug that exists since 4.0.0 for procedures that implement 
both interfaces. The first two statements above now work, and the third fails 
with `WRONG_NUM_ARGS`. For a procedure whose `bind()` returns a new object, 
Spark uses only that object, and `EXPLAIN CALL` binds it once and prints the 
plan.
   
   Two CALLs that pass analysis on `branch-4.0` and fail only on `collect()` 
with `UnsupportedOperationException` now fail analysis: one with a 
`window(...)` or `session_window(...)` argument fails with 
`UNSUPPORTED_EXPR_FOR_OPERATOR`, and 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`.
   
   A CALL over a null procedure, which a catalog's `loadProcedure` can return 
for a procedure that does not exist, now fails analysis with `INTERNAL_ERROR` 
("Unsupported procedure: null"), and its EXPLAIN prints that error instead of a 
plan. On `branch-4.0` it passes analysis, EXPLAIN prints a plan, and 
`collect()` fails with `UnsupportedOperationException`.
   
   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 new tests of #60392 are added to `ProcedureSuite`, adapted as 
described above, with the null procedure test. With this PR, `ProcedureSuite` 
(46 tests) and `StaticProcedureSuite` pass on `branch-4.0`. With the main code 
changes reverted to `branch-4.0`, all nine new tests fail.
   
   Each new check in `BindProcedures`, `Call.bound`, `Call.simpleString`, 
`PullOutNondeterministic`, the new CheckAnalysis branch, 
`ProcedureArgumentCoercion` and `InvokeProcedures` was mutated, including 
naming the class of a null procedure with `other.getClass`. Each mutant fails 
at least one new test, or, for the `InvokeProcedures` match, the SPARK-61669 
test.
   
   ### 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