anuragmantri commented on code in PR #60392:
URL: https://github.com/apache/spark/pull/60392#discussion_r4559812826


##########
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:
   Added, thanks.



##########
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:
   With the item 3 design, `Call` keeps its three fields, so the backports have 
no compatibility change. branch-4.0 and branch-4.1 will get a separate PR.



##########
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:
   Done.



##########
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)
   extends UnaryNode with ExecutableDuringAnalysis {
 
   override def output: Seq[Attribute] = Nil
 
   override def child: LogicalPlan = procedure
 
-  def bound: Boolean = procedure match {
-    case ResolvedProcedure(_, _, _: BoundProcedure) => true
-    case _ => false
-  }
-
   def checkArgTypes(): TypeCheckResult = {
     require(resolved && bound, "can check arg types only after resolution and 
binding")
 
     val params = procedure match {
-      case ResolvedProcedure(_, _, bound: BoundProcedure) => bound.parameters
+      case ResolvedProcedure(_, _, boundProcedure: BoundProcedure) => 
boundProcedure.parameters

Review Comment:
   Fixed with the item 3 design. `PullOutNondeterministic` now also skips 
`Call`, and `Call` allows nondeterministic arguments, so EXPLAIN prints the 
plan and the `DATE` case fails with `DATATYPE_MISMATCH`. Added tests for both.



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

Review Comment:
   Done.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -2648,14 +2648,15 @@ class Analyzer(
    */
   object BindProcedures extends Rule[LogicalPlan] {
     def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
-      case Call(ResolvedProcedure(catalog, ident, unbound: UnboundProcedure), 
args, execute)
-          if args.forall(_.resolved) =>
+      case c @ Call(ResolvedProcedure(catalog, ident, unbound: 
UnboundProcedure), args, execute, _)
+          if !c.bound && args.forall(_.resolved) =>
         val inputType = extractInputType(args)
-        val bound = unbound.bind(inputType)
-        validateParameterModes(bound)
+        val boundProcedure = unbound.bind(inputType)
+        validateParameterModes(boundProcedure)
         val rearrangedArgs =
-          NamedParametersSupport.defaultRearrange(bound, args, 
SQLConf.get.resolver)
-        Call(ResolvedProcedure(catalog, ident, bound), rearrangedArgs, execute)
+          NamedParametersSupport.defaultRearrange(boundProcedure, args, 
SQLConf.get.resolver)
+        Call(

Review Comment:
   Done. An `execute = false` test row pins it.



##########
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:
   I took a different approach: `BindProcedures` now also binds a `Call` over a 
plain `BoundProcedure`, as master did, and any other unbound `Call` fails in 
CheckAnalysis with `INTERNAL_ERROR`. Added tests.



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala:
##########
@@ -974,6 +1015,45 @@ class ProcedureSuite extends SharedSparkSession with 
BeforeAndAfter {
     override def description: String = "simple sum integers"
   }
 
+  abstract class TimestampProcedure extends BoundProcedure {
+    override def description: String = "returns a timestamp and an optional 
label"
+
+    override def isDeterministic: Boolean = true
+
+    override def parameters: Array[ProcedureParameter] = Array(
+      ProcedureParameter.in("ts", DataTypes.TimestampType).build(),
+      ProcedureParameter.in("label", 
DataTypes.StringType).defaultValue("'none'").build())
+
+    def outputType: StructType = new StructType()
+      .add("ts", DataTypes.TimestampType)
+      .add("label", DataTypes.StringType)
+
+    override def call(input: InternalRow): java.util.Iterator[Scan] = {
+      val result = Result(outputType, Array(InternalRow(input.getLong(0), 
input.getUTF8String(1))))
+      Collections.singleton[Scan](result).iterator()
+    }
+  }
+
+  object SimpleTimestampProcedure extends TimestampProcedure with 
SimpleProcedure {
+    override def name: String = "simple_ts"
+  }
+
+  class RebindingTimestampProcedure(binds: AtomicInteger, isBound: Boolean)

Review Comment:
   Done.



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