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

   ### What changes were proposed in this pull request?
   
   Backport of #60392 to `branch-4.2`.
   
   Differences from #60392:
   - `branch-4.2` does not have SPARK-59962, so the two changes that reword its 
comments are dropped: the comment on the command case in 
`RewriteWithExpression`, and the comment in the `SPARK-59962` test of 
`ProcedureSuite`.
   - `branch-4.2` does not have SPARK-58001, so `UNSUPPORTED_EXPR_FOR_OPERATOR` 
has no `<operator>` parameter. The new CheckAnalysis error for a CALL with a 
time window argument passes only `invalidExprSqls`, as the other uses of that 
condition on `branch-4.2` do, and the time window test expects only that 
parameter.
   
   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 `SimpleProcedure` or 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` and `SimpleProcedure` 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 `SimpleProcedure` with parameters `ts TIMESTAMP, label STRING DEFAULT 
'none'`, on the 4.2.1 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.2` the second statement fails with `NON_FOLDABLE_ARGUMENT` 
("The function `p` requires the parameter `ts` to be a foldable expression 
...") 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.2.2. 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, and since 4.2.0 for `SimpleProcedure`.
   
   Two CALLs that pass analysis on the 4.2.1 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` (as does `EXPLAIN` of it; on `branch-4.2` the 
message of that condition still suggests rewriting the WHERE clause, as it does 
for every 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`.
   
   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 time window test expecting only the `invalidExprSqls` parameter. 
`ProcedureSuite` (53 tests) and `StaticProcedureSuite` pass on `branch-4.2`.
   
   With the main code reverted to `branch-4.2` (keeping only the 
`ResolvedBoundProcedure` class so the tests compile), all eight new tests fail, 
and so does the `SPARK-61669` test, whose `AddProjectUnderCall` rule now 
matches `ResolvedBoundProcedure`. Each of these mutants fails at least one new 
test: dropping the `BoundProcedure` arm, the "Unsupported procedure" arm, or 
the parameter mode check for a procedure that is only bound in 
`BindProcedures`; setting `execute` to true when binding; treating a 
`ResolvedProcedure` over a `BoundProcedure` as bound again; disallowing 
nondeterministic arguments in `Call`; not skipping `Call` in 
`PullOutNondeterministic`; coercing arguments before binding; printing only a 
`ResolvedProcedure` in `Call.simpleString`; and, in CheckAnalysis, removing the 
`UNSUPPORTED_EXPR_FOR_OPERATOR` branch, the unwrapping of named arguments, the 
special expressions, the `distinct`, or the `INTERNAL_ERROR` for a `Call` that 
is not bound.
   
   ### 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