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


##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/InvokeProcedures.scala:
##########
@@ -39,10 +39,12 @@ class InvokeProcedures(session: SparkSession) extends 
Rule[LogicalPlan] {
   }
 
   private def invokeProcedures(plan: LogicalPlan): LogicalPlan = plan 
resolveOperators {
-    // A CALL with an IN or correlated EXISTS subquery in an argument is left 
for CheckAnalysis to
-    // reject. The optimizer would rewrite the subquery into a join under the 
`Call`.
+    // A CALL with an IN or correlated EXISTS subquery, or an aggregate, 
window function or
+    // generator, in an argument is left for CheckAnalysis to reject. The 
optimizer would rewrite
+    // the subquery into a join under the `Call`, or could fold away the other 
expressions.
     case c: Call if c.resolved && c.bound && c.execute && 
c.checkArgTypes().isSuccess &&
-        !c.args.exists(SubqueryExpression.hasInOrCorrelatedExistsSubquery) =>
+        !c.args.exists(SubqueryExpression.hasInOrCorrelatedExistsSubquery) &&
+        PlanHelper.specialExpressionsInUnsupportedOperator(c).isEmpty =>

Review Comment:
   **Non-blocking (P2):** `PlanHelper.specialExpressionsInUnsupportedOperator` 
collects only `WindowExpression`, `AggregateExpression` and `Generator` nodes, 
so this condition misses two argument forms that `EXPLAIN CALL` rejects:
   
   - A window function written without `OVER`, used as an operand of another 
expression. `FunctionResolution.validateFunction` leaves it unwrapped as a bare 
`RowNumber`, `Rank`, `NTile`, `Lag` and so on 
(FunctionResolution.scala:686-713), and CheckAnalysis rejects it with 
`WINDOW_FUNCTION_WITHOUT_OVER_CLAUSE` (CheckAnalysis.scala:691-696).
   - `grouping(...)` and `grouping_id(...)`, which resolve to `Grouping` and 
`GroupingID`. CheckAnalysis rejects them with `UNSUPPORTED_GROUPING_EXPRESSION` 
(CheckAnalysis.scala:684-689).
   
   From reading the code, `CALL cat.ns.sum(IF(true OR row_number() > 0, 1, 2), 
2)` passes this guard, the optimizer folds the argument to `1`, and the 
procedure runs. This also happens with `spark.sql.planChangeValidation` on, 
because the bare `RowNumber` has no references, so the new test's 
`UnboundNonExecutableSum` would be invoked. `CALL cat.ns.sum(IF(true OR 
grouping(1) > 0, 1, 2), 2)` also runs the procedure with plan-change validation 
off, its production default; in tests it fails earlier with 
`PLAN_VALIDATION_FAILED_RULE_EXECUTOR` because of the dangling 
`spark_grouping_id` reference.
   
   The PR description says such CALLs now fail like `EXPLAIN CALL`, but 
`IF(true OR row_number() OVER (ORDER BY 1) > 0, 1, 2)` now fails while `IF(true 
OR row_number() > 0, 1, 2)` still runs the procedure. The SPARK-61889 test 
covers only the `OVER` form and no grouping function. A window function that is 
a whole argument, such as `CALL cat.ns.sum(row_number(), 2)`, is not affected: 
`EXPLAIN CALL` accepts it, and `CALL` keeps failing with 
`NON_FOLDABLE_ARGUMENT`.
   
   **Recommended change:** In InvokeProcedures.scala, extend the guard at lines 
45-47 so that it also leaves a Call unexecuted when an argument contains a 
grouping or grouping_id call (a Grouping or GroupingID node), or a window 
function written without OVER that is an operand of another expression (a 
WindowFunction child of an expression other than WindowExpression). These are 
the conditions under which CheckAnalysis raises UNSUPPORTED_GROUPING_EXPRESSION 
and WINDOW_FUNCTION_WITHOUT_OVER_CLAUSE (CheckAnalysis.scala:684-696). Keep the 
new check in InvokeProcedures, for example as a small private predicate next to 
the guard, and update the comment at lines 42-44 to describe these forms. 
Extend the SPARK-61889 test in the procedure tests with folded-away cases for a 
window function without OVER and for a grouping function.
   
   **Why this works:** Every Call that the widened guard leaves in the plan is 
one that analysis rejects: CheckAnalysis rejects the grouping and window forms 
this repair adds, as it does aggregates and window expressions, and 
ExtractGenerator rejects a generator. The statement therefore fails analysis 
with the same error as EXPLAIN CALL, before the optimizer can fold the 
expression away and run the procedure. Because the new condition matches 
CheckAnalysis's own conditions exactly, the guard leaves no Call that analysis 
accepts, so no CALL ends up unexecuted and planned as an explain-only plan.
   
   **Compatibility:** Valid CALL statements keep their behavior and still 
invoke the procedure with the folded arguments. A CALL whose argument fails to 
fold for a reason outside these forms, such as a nondeterministic expression or 
a Python UDF, also keeps its behavior and still fails with 
NON_FOLDABLE_ARGUMENT.
   
   **Risks:** A condition broader than CheckAnalysis's, such as rejecting any 
window function node, would also leave CALL cat.ns.sum(row_number(), 2) in the 
plan. CheckAnalysis accepts a window function that is a whole argument, so that 
Call would pass analysis unexecuted and be planned as an explain-only plan. The 
procedure would never run, instead of the statement failing with 
NON_FOLDABLE_ARGUMENT as it does today. Arguments with these forms that do not 
fold change their error from NON_FOLDABLE_ARGUMENT to 
UNSUPPORTED_GROUPING_EXPRESSION or WINDOW_FUNCTION_WITHOUT_OVER_CLAUSE, the 
errors EXPLAIN CALL reports for them. Examples are CALL 
cat.ns.sum(CAST(grouping(1) AS INT), 2), CALL cat.ns.sum(row_number() + 0, 2), 
and a whole-argument window function such as row_number() passed to a BIGINT 
parameter, which ProcedureArgumentCoercion wraps in a cast. The PR already 
makes that change for aggregates, window expressions and generators, and 
NON_FOLDABLE_ARGUMENT for CALL is in no release
 d version.
   
   **Constraints:** The new condition must match CheckAnalysis's conditions for 
these forms exactly: a Grouping or GroupingID node anywhere in an argument, and 
a window function only when it is a direct child of an expression other than 
WindowExpression.
   
   **Exclusions:**
   - Subquery arguments, including a grouping or window function inside a 
subquery's own plan, which the PR leaves to apache/spark#60628.
   - Other checks that CheckAnalysis applies only to a Call it sees, such as 
the rejection of nondeterministic arguments, which the base branch already 
handles by failing a non-foldable argument with NON_FOLDABLE_ARGUMENT, and 
which the PR does not set out to change.
   
   **Verification:**
   - **Regression:** Using a procedure that fails if it is invoked, CALL 
statements whose folded-away argument holds a window function written without 
OVER, such as row_number() or lag(1), or a grouping function, such as 
grouping(1) or grouping_id(), fail analysis with the error EXPLAIN CALL reports 
for the same statement (WINDOW_FUNCTION_WITHOUT_OVER_CLAUSE or 
UNSUPPORTED_GROUPING_EXPRESSION). The procedure is never invoked, including 
with spark.sql.planChangeValidation off, its production default.
   - **Compatibility:** A CALL whose whole argument is a window function 
written without OVER and already has the parameter's type, such as CALL 
cat.ns.sum(row_number(), 2), still fails with NON_FOLDABLE_ARGUMENT rather than 
passing analysis unexecuted. When ProcedureArgumentCoercion casts such an 
argument to a wider parameter type, the CALL fails analysis with 
WINDOW_FUNCTION_WITHOUT_OVER_CLAUSE, as EXPLAIN CALL does, and is never left 
unexecuted.



##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/InvokeProcedures.scala:
##########
@@ -39,10 +39,12 @@ class InvokeProcedures(session: SparkSession) extends 
Rule[LogicalPlan] {
   }
 
   private def invokeProcedures(plan: LogicalPlan): LogicalPlan = plan 
resolveOperators {
-    // A CALL with an IN or correlated EXISTS subquery in an argument is left 
for CheckAnalysis to
-    // reject. The optimizer would rewrite the subquery into a join under the 
`Call`.
+    // A CALL with an IN or correlated EXISTS subquery, or an aggregate, 
window function or
+    // generator, in an argument is left for CheckAnalysis to reject. The 
optimizer would rewrite

Review Comment:
   **Nit (P3):** A generator argument is not rejected by CheckAnalysis. When 
the guard below leaves such a Call, the `ExtractGenerator` rule reaches its 
catch-all `case p if p.expressions.exists(hasGenerator)` on the next Resolution 
iteration and throws `UNSUPPORTED_GENERATOR.OUTSIDE_SELECT` 
(Analyzer.scala:3553-3554), which is the error the new test expects. 
CheckAnalysis never runs for that input; if it did, it would report 
`UNSUPPORTED_EXPR_FOR_OPERATOR` (CheckAnalysis.scala:1210-1217). A maintainer 
tracing the generator error from this comment would look in the wrong component.
   
   ```suggestion
       // generator, in an argument is left for the analyzer to reject. The 
optimizer would rewrite
   ```
   
   **Verification:**
   - **Inspection:** The comment above the CALL invocation guard in 
InvokeProcedures.scala attributes the rejection of a generator argument to 
analysis in general or to ExtractGenerator, matching the 
UNSUPPORTED_GENERATOR.OUTSIDE_SELECT error the procedure test expects, and 
still describes the subquery, aggregate and window cases accurately.



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