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

   ### What changes were proposed in this pull request?
   
   This PR makes `bound` a field of `Call` that only `BindProcedures` sets.
   
   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 and sets `bound = true`.
   - `ProcedureArgumentCoercion` and `InvokeProcedures` wait until the `Call` 
is bound.
   - 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 
resolve later in the same 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'`:
   
   ```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
   ```
   
   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.
   
   #58994 (SPARK-59741) runs CALL at execution time and removes 
`InvokeProcedures`. That fixes the first two errors but not the third, because 
coercion still runs before binding. Whichever of the two PRs merges second 
needs a small update for the new `Call` field.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, it fixes a bug. The statements above now work, and too many arguments 
fail 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`.
   
   For Spark extensions, `Call` has a new field. Code that pattern-matches 
`Call(procedure, args, execute)` needs a fourth pattern. Code that builds a 
`Call` over an already bound procedure must pass `bound = true`.
   
   ### How was this patch tested?
   
   Two new tests in `ProcedureSuite`. Both fail without the fix.
   - `SPARK-61506: simple procedure with a late-resolving argument` runs a 
positional call that needs a default, named arguments in a different order, and 
a call with too many arguments.
   - `SPARK-61506: procedure that is both unbound and bound is used only after 
binding` uses a procedure whose `bind()` returns a new object. It checks that 
Spark only calls that object and binds once, also for EXPLAIN.
   
   ### 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