spark-test commented on code in PR #60628:
URL: https://github.com/apache/spark/pull/60628#discussion_r4607821148


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala:
##########
@@ -192,6 +192,40 @@ class ProcedureSuite extends SharedSparkSession with 
BeforeAndAfter {
     }
   }
 
+  test("SPARK-61885: an argument with an invalid subquery") {
+    // `CheckAnalysis` rejects these subqueries, as for `EXPLAIN CALL`. The 
procedure must not be
+    // invoked before that, and `NonExecutableSum` throws if it is.
+    catalog.createProcedure(Identifier.of(Array("ns"), "sum"), 
UnboundNonExecutableSum)
+    checkError(
+      exception = intercept[AnalysisException] {
+        sql("CALL cat.ns.sum((SELECT 1, 2), 3)")

Review Comment:
   **Nit (P3):** Each invalid case in this test holds a single subquery, always 
in `in1` (this one and the two `$arg, 2` cases below), while `checkSubqueries` 
(InvokeProcedures.scala:74) collects the subqueries of every argument. Nothing 
pins that. If the check were narrowed to the first argument, to the first 
collected subquery, or to each argument's first subquery (`collectFirst`), the 
suite would still pass. The only other case whose subquery reaches the check 
through `in2`, `(SELECT CAST(1 IN (SELECT 1) AS INT))` in the SPARK-61669 test, 
is valid and ends with NON_FOLDABLE_ARGUMENT either way. Under such a 
narrowing, a CALL whose invalid subquery comes later would again fail with 
INTERNAL_ERROR, or invoke the procedure when the optimizer folds the invalid 
part away with `spark.sql.planChangeValidation` off.
   
   A case such as `CALL cat.ns.sum((SELECT 1), (SELECT 1) + (SELECT 1, 2))` 
catches all three narrowings. From the code, its expected context is the 
`(SELECT 1, 2)` fragment at start 41, stop 53.
   
   **Verification:**
   - **Regression:** A CALL whose first argument is a valid subquery and whose 
second argument adds a valid subquery to a scalar subquery returning two 
columns fails analysis with SCALAR_SUBQUERY_RETURN_MORE_THAN_ONE_OUTPUT_COLUMN, 
with the two-column subquery as its query context, and does not invoke the 
procedure; the case fails if the check is narrowed to the first argument, to 
the first subquery it collects, or to the first subquery of each argument.



##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/InvokeProcedures.scala:
##########
@@ -62,6 +63,21 @@ class InvokeProcedures(session: SparkSession) extends 
Rule[LogicalPlan] {
       }
   }
 
+  /**
+   * Checks the subqueries in the arguments as `CheckAnalysis` checks them in 
a SELECT. It never
+   * checks them in a CALL, as this rule replaces the `Call` with its result. 
Unchecked, an invalid
+   * subquery can make the optimizer fail, or lose its invalid part and let 
the procedure run.
+   * A SELECT, unlike a `Call`, allows an uncorrelated EXISTS subquery, which 
works in a CALL as
+   * the optimizer rewrites it into a scalar subquery.
+   */
+  private def checkSubqueries(call: Call): Unit = {
+    val subqueries = call.args.flatMap(_.collect { case s: SubqueryExpression 
=> s })

Review Comment:
   **Nit (P3):** Only `SubqueryExpression`s are collected here, so the rest of 
each argument still reaches `optimizer.execute(c)` without a `CheckAnalysis` 
pass. From the code, a CALL whose argument has an aggregate or window function, 
or a nondeterministic expression, in a branch that BooleanSimplification or 
SimplifyConditionals removes still invokes the procedure, although `EXPLAIN 
CALL` rejects the statement. Examples are `IF(true OR max(1) > 0, 1, 2)` and 
`CASE WHEN true THEN (SELECT 1) ELSE max(1) END`, where only the valid `(SELECT 
1)` is checked.
   
   Keeping that for a separate JIRA and PR, as you said in the thread on line 
45, is fine, but nothing in the code records it. The doc comment above 
describes only the subquery check, and the guard comment on lines 42-43 covers 
only IN and correlated EXISTS, so a reader can take this check for the complete 
fix. Could you add a TODO to the doc comment that names these deferred 
arguments, with the follow-up JIRA once it is filed?
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/60628#discussion_r4604741345).
   
   **Verification:**
   - **Inspection:** The doc comment of the subquery check in the rule that 
invokes procedures has a TODO saying that invalid argument expressions outside 
a subquery, such as an aggregate or window function the optimizer folds away, 
still invoke the procedure and are deferred to a follow-up, and the change 
touches no code.



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