peter-toth commented on code in PR #60628:
URL: https://github.com/apache/spark/pull/60628#discussion_r4604741345
##########
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:
**Finding 1.** The same bypass works without a subquery. `CheckAnalysis`
never sees any argument of an invoked `CALL`, so anything the optimizer folds
away skips the check too. I ran these with
`spark.sql.planChangeValidation=false` (the default outside tests), on base and
on this head:
- `CALL cat.ns.sum(IF(true OR max(1) > 0, 1, 2), 2)` invokes the procedure
and returns `3`.
- `CALL cat.ns.sum(IF(true OR row_number() OVER (ORDER BY 1) > 0, 1, 2), 2)`
does the same.
- `EXPLAIN CALL` of the first one fails with `UNSUPPORTED_EXPR_FOR_OPERATOR`
("The query operator Call contains one or more unsupported expressions").
With the test conf, both statements fail with
`PLAN_VALIDATION_FAILED_RULE_EXECUTOR` instead, so the suite doesn't show it.
The IN subquery exclusion above has the same shape. If such a `Call` is left
for `CheckAnalysis`, it fails with exactly the `EXPLAIN CALL` error:
```scala
case c: Call if c.resolved && c.bound && c.execute &&
c.checkArgTypes().isSuccess &&
!c.args.exists(SubqueryExpression.hasInOrCorrelatedExistsSubquery) &&
PlanHelper.specialExpressionsInUnsupportedOperator(c).isEmpty =>
```
It also needs `PlanHelper` in the `plans.logical` import. I applied it
locally:
- Both statements above fail with `UNSUPPORTED_EXPR_FOR_OPERATOR`, with and
without the test conf.
- `ProcedureSuite` still passes, including the new test.
- `CALL cat.ns.sum(max(1), 2)` changes from `NON_FOLDABLE_ARGUMENT` to
`UNSUPPORTED_EXPR_FOR_OPERATOR`, the `EXPLAIN CALL` error. That would be worth
a line in the description.
I found no JIRA for it besides SPARK-59741, which removes this rule. Could
you fold it in here, or file a ticket if you prefer to keep this PR to
subqueries?
##########
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:
**Finding 2.** The `Project` wrapper is what keeps a valid uncorrelated
EXISTS working, and the description promises that it does. Nothing tests it
though. I swapped the check to
`session.sessionState.analyzer.checkAnalysis(call)` locally. `CALL
cat.ns.sum(CAST(EXISTS (SELECT 1) AS INT), 2)` then fails with
`UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.UNSUPPORTED_IN_EXISTS_SUBQUERY`, and
all of `ProcedureSuite` still passes. A small test next to this one would pin
it. It returns `3` on this head:
```scala
test("SPARK-61885: an argument with a valid uncorrelated EXISTS subquery")
{
catalog.createProcedure(Identifier.of(Array("ns"), "sum"), UnboundSum)
checkAnswer(sql("CALL cat.ns.sum(CAST(EXISTS (SELECT 1) AS INT), 2)"),
Row(3) :: Nil)
}
```
--
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]