anuragmantri commented on code in PR #60392:
URL: https://github.com/apache/spark/pull/60392#discussion_r4604981844
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/v2ResolutionPlans.scala:
##########
@@ -219,12 +219,30 @@ case class ResolvedFieldName(path: Seq[String], field:
StructField) extends Fiel
case class ResolvedFieldPosition(position: ColumnPosition) extends
FieldPosition
+/**
+ * A resolved procedure that a CALL refers to, before or after
`BindProcedures` binds it.
Review Comment:
Done.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala:
##########
@@ -635,6 +645,117 @@ 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` 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.
+ catalog.createProcedure(Identifier.of(Array("ns"), "simple_ts"),
SimpleTimestampProcedure)
+ val tsExpr = "TIMESTAMP '2026-01-02 00:00:00' - INTERVAL '1' DAY"
+ val expected = Timestamp.valueOf("2026-01-01 00:00:00")
+ checkAnswer(sql(s"CALL cat.ns.simple_ts($tsExpr)"), Row(expected, "none")
:: Nil)
+ checkAnswer(
+ sql(s"CALL cat.ns.simple_ts(label => 'x', ts => $tsExpr)"),
+ Row(expected, "x") :: Nil)
+ checkError(
+ exception = intercept[AnalysisException](
+ sql(s"CALL cat.ns.simple_ts($tsExpr, 'x', 1)")
+ ),
+ condition = "WRONG_NUM_ARGS.WITHOUT_SUGGESTION",
+ parameters = Map(
+ "functionName" -> toSQLId("simple_ts"),
+ "expectedNum" -> "[1, 2]",
+ "actualNum" -> "3",
+ "docroot" -> SPARK_DOC_ROOT))
+ }
+
+ 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)
+ }
+ }
+
+ test("SPARK-61506: procedure that is both unbound and bound is used only
after binding") {
+ val binds = new AtomicInteger()
+ catalog.createProcedure(
+ Identifier.of(Array("ns"), "rebinding_ts"),
+ new RebindingTimestampProcedure(binds, isBound = false))
+ checkAnswer(
+ sql("CALL cat.ns.rebinding_ts(TIMESTAMP '2026-01-02 00:00:00' - INTERVAL
'1' DAY, 'x')"),
+ Row(Timestamp.valueOf("2026-01-01 00:00:00"), "x") :: Nil)
+ assert(binds.get() == 1)
+ // EXPLAIN does not run the procedure, so later iterations see the bound
`Call` again and must
+ // not bind it again.
+ val plan = explain("CALL cat.ns.rebinding_ts(label => 'x', ts => TIMESTAMP
'2026-01-01')")
+ assert(binds.get() == 2)
+ assert(plan.contains("Call cat.ns.rebinding_ts(2026-01-01 00:00:00, x)"))
+ }
+
+ test("SPARK-61506: EXPLAIN CALL with a nondeterministic argument") {
+ catalog.createProcedure(Identifier.of(Array("ns"), "sum"),
UnboundNonExecutableSum)
+ val plan = explain("CALL cat.ns.sum(rand(0), 1)")
+ assert(plan.contains("Call cat.ns.sum(cast(rand(0) as int), 1)"))
+ }
+
+ test("SPARK-61506: CALL with a nondeterministic argument of the wrong type")
{
+ catalog.createProcedure(Identifier.of(Array("ns"), "simple_date"),
SimpleDateProcedure)
+ val call = "CALL cat.ns.simple_date(rand())"
+ checkError(
+ exception = intercept[AnalysisException](sql(call)),
+ condition = "DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE",
+ parameters = Map(
+ "sqlExpr" -> "CALL",
+ "paramIndex" -> "first",
+ "inputSql" -> "\"rand()\"",
+ "inputType" -> toSQLType("DOUBLE"),
+ "requiredType" -> toSQLType("DATE")),
+ context = ExpectedContext(fragment = call, start = 0, stop = call.length
- 1))
+ }
+
+ test("SPARK-61506: CALL over a procedure that is only bound") {
+ // Spark loads procedures as `UnboundProcedure`s, but `ResolvedProcedure`
takes any procedure.
+ val ident = Identifier.of(Array("ns"), "proc")
+ val sum = Call(ResolvedProcedure(catalog, ident, new Sum), Seq(Literal(1),
Literal(2)))
+ checkAnswer(Dataset.ofRows(spark, sum), Row(3) :: Nil)
+ // `BindProcedures` keeps `execute`, so this CALL is bound but not invoked.
+ val notInvoked = Dataset.ofRows(spark, sum.copy(execute =
false)).queryExecution.analyzed
+ notInvoked match {
+ case c: Call => assert(c.bound)
Review Comment:
Done.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -2649,18 +2649,25 @@ class Analyzer(
}
/**
- * A rule that binds procedures to the input types and rearranges arguments
as needed.
+ * A rule that binds procedures to the input types and rearranges arguments
as needed, and
+ * records the bound procedure in a [[ResolvedBoundProcedure]].
*/
object BindProcedures extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
- case Call(ResolvedProcedure(catalog, ident, unbound: UnboundProcedure),
args, execute)
+ case c @ Call(ResolvedProcedure(catalog, ident, procedure), args, _)
if args.forall(_.resolved) =>
- val inputType = extractInputType(args)
- val bound = unbound.bind(inputType)
- validateParameterModes(bound)
+ val boundProcedure = procedure match {
+ case unbound: UnboundProcedure =>
unbound.bind(extractInputType(args))
+ case bound: BoundProcedure => bound
+ case other =>
+ throw SparkException.internalError(s"Unsupported procedure:
${other.getClass.getName}")
Review Comment:
I left this as is. Since SPARK-61553 no catalog can produce a null procedure
here, and an extension that builds one still gets `INTERNAL_ERROR`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -1039,8 +1039,13 @@ trait CheckAnalysis extends LookupCatalog with
QueryErrorsBase with PlanToString
varName,
c.defaultExpr.originalSQL)
- case c: Call if c.resolved && c.bound && c.checkArgTypes().isFailure
=>
+ case c: Call if c.resolved =>
+ if (!c.bound) {
Review Comment:
Fixed with your suggestion, except that the message lists only the arguments
that read the operator under the `Call`. With `filterNot(_.foldable)` it also
listed a scalar subquery and every named argument. Added a test for `window`, a
sliding `window` and `session_window`.
--
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]