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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -1126,9 +1126,13 @@ object OptimizeOneRowRelationSubquery extends 
Rule[LogicalPlan] {
    * if there is no nested subqueries in the subquery plan.
    */
   private def rewrite(plan: LogicalPlan): LogicalPlan = 
plan.transformUpWithSubqueries {
+    // A lateral subquery with a Generate over OneRowRelation, with or without 
a Project on top,

Review Comment:
   **Blocking (P1):** Skipping the inlining sends these joins to the 
domain-join decorrelation that the `ON TRUE` form uses, and that path has 
limits the inlined `Generate` does not. Reading the code, `SELECT * FROM tm 
LEFT JOIN LATERAL explode(tm.m)` over a MAP column with no empty map returned 
its rows on the base branch and now fails with 
`UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.UNSUPPORTED_CORRELATED_REFERENCE_DATA_TYPE`,
 because the domain join needs a null-safe equality on `m` and maps have no 
ordering (DecorrelateInnerQuery.scala:539-550). The same holds for VARIANT, 
GEOMETRY and GEOGRAPHY columns, for arrays or structs that hold one, for 
`stack(1, t.m)`, and for a Python UDTF given such an argument.
   
   A streaming query with such a join, even over an orderable array column, 
gains a streaming aggregation under a stream-stream left outer join. 
`StateStoreSaveExec` rejects it in the default Append mode without a watermark, 
the join cannot emit the NULL-padded rows without one, and the Structured 
Streaming guide does not allow the added stateful operators on a restart from a 
checkpoint the base branch wrote; the unsupported-operation check runs only on 
the analyzed plan, so nothing reports this when the query starts. With the 
internal `spark.sql.optimizer.decorrelateInnerQuery.enabled` set to false, the 
legacy decorrelation has no `Generate` case, so the outer reference is left 
inside the `Generate` and the join cannot be planned.
   
   The description's deferral covers the plan's cost, not these failures, and 
the code comment at lines 1129-1131 does not record that deferral either. No 
test covers a non-orderable correlated column, a streaming query or the flag 
set to false on this path.
   
   See **Shared repair plan 1** in the review body.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -1126,9 +1126,13 @@ object OptimizeOneRowRelationSubquery extends 
Rule[LogicalPlan] {
    * if there is no nested subqueries in the subquery plan.
    */
   private def rewrite(plan: LogicalPlan): LogicalPlan = 
plan.transformUpWithSubqueries {
+    // A lateral subquery with a Generate over OneRowRelation, with or without 
a Project on top,
+    // becomes a Generate over the left side. That Generate drops the left 
rows for which the
+    // generator returns no rows, so it can not replace a left outer lateral 
join.
     case LateralJoin(
-      left, right @ LateralSubquery(OneRowSubquery(plan), _, _, _, _), _, None)
-        if !hasCorrelatedSubquery(right.plan) && right.joinCond.isEmpty =>
+      left, right @ LateralSubquery(OneRowSubquery(plan), _, _, _, _), 
joinType, None)
+        if !hasCorrelatedSubquery(right.plan) && right.joinCond.isEmpty &&
+          !(joinType == LeftOuter && plan.exists(_.isInstanceOf[Generate])) =>

Review Comment:
   **Blocking (P1):** `plan.exists(_.isInstanceOf[Generate])` also matches a 
`Generate` with `outer = true`, which `explode_outer`, `posexplode_outer`, 
`inline_outer` and `variant_explode_outer` build, as does a SELECT-list 
`*_outer` generator. An outer `Generate` already emits one NULL-padded row for 
a left row whose input is empty or NULL, so the base branch's inlining was 
exact for these joins, with or without a projection on top, and the comment at 
lines 1129-1131 is not true for them.
   
   Reading the code, these joins now take the domain-join path instead. `SELECT 
* FROM tv LEFT JOIN LATERAL variant_explode_outer(tv.v)`, where `tv.v` is a 
VARIANT holding `[1, 2]` in one row and `[]` in another, returned three rows on 
the base branch, the last one `(2, [], NULL, NULL, NULL)`; it now fails with 
`UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.UNSUPPORTED_CORRELATED_REFERENCE_DATA_TYPE`,
 because the domain join needs a null-safe equality on `v` and VARIANT has no 
ordering. With an orderable column, `SELECT * FROM t3 LEFT JOIN LATERAL 
explode_outer(c2)` returns the same rows, but its plan evaluates `t3` a second 
time, aggregates its distinct `c2` values and joins them back, work that grows 
with the left side, where the base branch ran one `Generate`. `LEFT JOIN 
LATERAL explode_outer(...)` is also the natural workaround users of released 
versions have for the bug this PR fixes. No test covers an outer generator on 
this path.
   
   See **Shared repair plan 1** in the review body.



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