spark-test commented on code in PR #60473:
URL: https://github.com/apache/spark/pull/60473#discussion_r4571502141
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala:
##########
@@ -130,6 +134,62 @@ class ProcedureSuite extends SharedSparkSession with
BeforeAndAfter {
queryContext = Array(ExpectedContext(fragment = "IN (SELECT 1)", start =
26, stop = 38)))
}
+ test("SPARK-61669: an argument with an IN subquery inside a scalar
subquery") {
+ // Unlike the case above, `CheckAnalysis` accepts the IN subquery inside
the scalar subquery.
+ // The optimizer inlines the scalar subquery, and
`RewritePredicateSubquery` then evaluates
+ // the IN subquery in a join under the `Call`.
+ catalog.createProcedure(Identifier.of(Array("ns"), "sum"), UnboundSum)
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql("CALL cat.ns.sum(1, (SELECT CAST(1 IN (SELECT 1) AS INT)))")
+ },
+ condition = "NON_FOLDABLE_ARGUMENT",
+ parameters = Map(
+ "funcName" -> toSQLId("sum"),
+ "paramName" -> toSQLId("in2"),
+ "paramType" -> toSQLType("INT")))
+ }
+
+ test("SPARK-61669: an argument with a Python UDF") {
+ assume(shouldTestPythonUDFs)
+ // `ExtractPythonUDFs` evaluates the Python UDF in an operator under the
`Call`.
+ catalog.createProcedure(Identifier.of(Array("ns"), "sum"), UnboundSum)
+ withUserDefinedFunction("pyudf" -> true) {
+ registerTestUDF(TestPythonUDF(name = "pyudf"), spark)
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql("CALL cat.ns.sum(1, CAST(pyudf(1) AS INT))")
+ },
+ condition = "NON_FOLDABLE_ARGUMENT",
+ parameters = Map(
+ "funcName" -> toSQLId("sum"),
+ "paramName" -> toSQLId("in2"),
+ "paramType" -> toSQLType("INT")))
+ }
+ }
+
+ test("SPARK-61669: an operator under the CALL with foldable arguments") {
+ // No built-in rule leads here, but invoking the procedure would ignore
the operator.
Review Comment:
**Nit (P3):** This comment and the fallback comment in `InvokeProcedures`
("Such an argument reads the output of that operator and does not fold.")
together say that only a user-provided rule can reach the `INTERNAL_ERROR`
fallback with every argument folded. Tracing the optimizer source shows that
built-in rules reach it from plain SQL when a later rule folds away an
argument's reference to the operator they added:
- `CALL cat.ns.sum(1, (SELECT CAST((1 IN (SELECT CAST(NULL AS INT))) IS NULL
AS INT)))`: the `InSubquery` is nullable, so the main `NullPropagation` keeps
the `isnull`. In the `RewriteSubquery` batch, `RewritePredicateSubquery` puts
an `ExistenceJoin` under the `Call` with a non-nullable `exists`, and
`NullPropagation`, which runs right after it, folds `isnull(exists)` to
`false`. No later rule removes the join, so `InvokeProcedures` gets
`Call(Join(...), [1, 0])`.
- `CALL cat.ns.sum(1, size(array(pyudf(1))))`: `ExtractPythonUDFs` adds a
`BatchEvalPython` under the `Call`, and `ConstantFolding` in the `Infer window
group limit` batch then folds `size(array(pythonUDF0))` to `1`.
By the same reading, both statements already fail with `INTERNAL_ERROR` on
the base branch, so behavior does not change, and refusing to invoke is still
right, since the IS NULL argument folds to 0 although its SQL value is 1. The
comments, though, make such a report look impossible and make the set of CALLs
that still get `INTERNAL_ERROR` look smaller than it is. Consider rewording
this comment as below, and qualifying the `InvokeProcedures` comment to say
that a later rule can fold the reference away, which the `INTERNAL_ERROR` below
it covers.
```suggestion
// An extra rule puts a `Project` under the `Call` with foldable
arguments. Built-in rules
// also lead here when a later rule folds away every reference to the
operator they add,
// e.g. `(1 IN (SELECT CAST(NULL AS INT))) IS NULL` inside a scalar
subquery. Either way,
// invoking the procedure would ignore the operator.
```
**Verification:**
- **Inspection:** Read the reworded test comment and fallback comment
against the optimizer: neither may say that no built-in rule reaches the
INTERNAL_ERROR fallback with every argument folded, or that an argument
evaluated by an operator under the Call never folds. The test comment must name
a later rule folding the reference away, such as for an IS NULL over a nullable
IN subquery or size(array(...)) over a Python UDF, as the built-in route, and
the extra optimizer rule as the route the test takes.
- **Inspection:** Confirm that the change edits only comments, so that
InvokeProcedures still raises NON_FOLDABLE_ARGUMENT for an argument that does
not fold and still throws INTERNAL_ERROR without invoking the procedure when
every argument folds over an operator under the Call.
--
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]