peter-toth commented on code in PR #60392:
URL: https://github.com/apache/spark/pull/60392#discussion_r4554246110


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala:
##########
@@ -511,6 +513,45 @@ class ProcedureSuite extends SharedSparkSession with 
BeforeAndAfter {
     checkAnswer(sql("CALL cat.ns.simple_sum(in2 => 4, in1 => 6)"), Row(10) :: 
Nil)
   }
 
+  test("SPARK-61506: simple procedure with a late-resolving argument") {

Review Comment:
   **Finding 2.** The description names a scalar subquery and `CAST('...' AS 
TIMESTAMP)` as other late-resolving arguments, and no test covers them. A late 
argument that needs an implicit cast is not covered either. Coercion now waits 
for binding, and both new tests pass arguments of the parameter's exact type.
   
   I ran these cases on master and here. On master each one fails with 
`requirement failed: args must be foldable`, and here all three pass:
   
   ```scala
     test("SPARK-61506: other late-resolving arguments") {
       catalog.createProcedure(Identifier.of(Array("ns"), "simple_ts"), 
SimpleTimestampProcedure)
       val expected = Row(Timestamp.valueOf("2026-01-01 00:00:00"), "x") :: Nil
       Seq(
         "(SELECT TIMESTAMP '2026-01-01 00:00:00')",
         "CAST('2026-01-01 00:00:00' AS TIMESTAMP)",
         // A DATE, which needs an implicit cast to the TIMESTAMP parameter 
once bound.
         "DATE '2026-01-02' - INTERVAL '1' DAY").foreach { ts =>
         checkAnswer(sql(s"CALL cat.ns.simple_ts(label => 'x', ts => $ts)"), 
expected)
       }
     }
   ```
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/v2Commands.scala:
##########
@@ -2368,27 +2368,27 @@ case class CloseCursor(cursor: Expression) extends 
LeafCommand
 
 /**
  * The logical plan of the CALL statement.
+ *
+ * @param bound whether `BindProcedures` has bound the procedure and 
rearranged the arguments to
+ *              match its parameters. This can't be derived from the procedure 
type, as a
+ *              procedure may implement both `UnboundProcedure` and 
`BoundProcedure`.
  */
 case class Call(
     procedure: LogicalPlan,
     args: Seq[Expression],
-    execute: Boolean = true)
+    execute: Boolean = true,
+    bound: Boolean = false)

Review Comment:
   **Finding 1.** On master this is fine. SPARK-61506 also lists 4.0.0, 4.1.0 
and 4.2.0, though. In a patch release, a new field changes the constructor, 
`copy` and `unapply` of `Call`. So an extension compiled against that release 
that builds or matches a `Call` fails with `NoSuchMethodError`. MiMa does not 
check catalyst, so CI would not show it.
   
   For the backports, the flag could live outside the constructor: a mutable 
field that the node hands over when it is copied, as `InMemoryRelation` does 
with `statsOfPlanToCache`. The description of each backport could then say why 
its shape differs from master's.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala:
##########
@@ -511,6 +513,45 @@ class ProcedureSuite extends SharedSparkSession with 
BeforeAndAfter {
     checkAnswer(sql("CALL cat.ns.simple_sum(in2 => 4, in1 => 6)"), Row(10) :: 
Nil)
   }
 
+  test("SPARK-61506: simple procedure with a late-resolving argument") {
+    // `ResolveBinaryArithmetic` resolves the timestamp subtraction after 
`BindProcedures` in the
+    // same analyzer iteration.

Review Comment:
   **Finding 3.** `ResolveBinaryArithmetic` rewrites this subtraction into a 
`TimestampAddInterval` under a `Cast` 
(`BinaryArithmeticWithDatetimeResolver.scala:99`). Both need a time zone, so 
the argument is still unresolved after the rewrite. `ResolveTimeZone` runs 
before `ResolveBinaryArithmetic` in the batch, so it sets the zone in the next 
iteration, which is still after `BindProcedures`. The test catches the bug 
either way, but someone who reorders these rules would go by this comment.
   
   ```suggestion
       // `ResolveBinaryArithmetic` rewrites the timestamp subtraction into an 
interval addition that
       // still needs a time zone. `ResolveTimeZone` sets it in the next 
analyzer iteration, after
       // `BindProcedures` has run in that iteration.
   ```
   



-- 
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