dongjoon-hyun commented on code in PR #60628:
URL: https://github.com/apache/spark/pull/60628#discussion_r4604868016


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala:
##########
@@ -192,6 +192,35 @@ 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)")
+      },
+      condition = 
"INVALID_SUBQUERY_EXPRESSION.SCALAR_SUBQUERY_RETURN_MORE_THAN_ONE_OUTPUT_COLUMN",
+      parameters = Map("number" -> "2"),
+      context = ExpectedContext(fragment = "(SELECT 1, 2)", start = 16, stop = 
28))
+    // The optimizer can remove the invalid `max(1)` and fold the argument.
+    Seq(
+      "(SELECT 1 WHERE true OR max(1) > 0)" -> 17,
+      "CAST(EXISTS (SELECT 1 WHERE true OR max(1) > 0) AS INT)" -> 29).foreach 
{
+      case (arg, start) =>
+        checkError(
+          exception = intercept[AnalysisException] {
+            sql(s"CALL cat.ns.sum($arg, 2)")
+          },
+          condition = "INVALID_WHERE_CONDITION",
+          parameters = Map(
+            "condition" -> "\"(true OR (max(1) > 0))\"",
+            "expressionList" -> "max(1)"),
+          context = ExpectedContext(
+            fragment = "SELECT 1 WHERE true OR max(1) > 0", start = start, 
stop = start + 32))
+    }
+  }

Review Comment:
   Thanks for the review, @peter-toth! Added `SPARK-61885: an argument with a 
valid uncorrelated EXISTS subquery` in 6e1530df0da, as you suggested.



##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/InvokeProcedures.scala:
##########
@@ -43,6 +43,7 @@ class InvokeProcedures(session: SparkSession) extends 
Rule[LogicalPlan] {
     // reject. The optimizer would rewrite the subquery into a join under the 
`Call`.
     case c: Call if c.resolved && c.bound && c.execute && 
c.checkArgTypes().isSuccess &&
         !c.args.exists(SubqueryExpression.hasInOrCorrelatedExistsSubquery) =>

Review Comment:
   Thanks for finding this, @peter-toth! It is the same bypass, but it does not 
involve a subquery and changes the error for `CALL cat.ns.sum(max(1), 2)`, so I 
would like to keep this PR to subqueries and handle it in a separate JIRA and 
PR.



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