shivadarshan-devadiga opened a new pull request, #60450:
URL: https://github.com/apache/spark/pull/60450

   ### What changes were proposed in this pull request?
   
   `OptimizeOneRowRelationSubquery` inlines a lateral subquery over a 
`OneRowRelation` into the outer plan. If the subquery is a generator, it 
becomes a `Generate` over the left side. The rule did not check the join type, 
so for a `LEFT OUTER` lateral join it made the same inner `Generate` (`outer = 
false`). The left rows for which the generator returns no rows were dropped:
   
   ```sql
   CREATE VIEW t3(c1, c2) AS VALUES (0, ARRAY(0, 1)), (1, ARRAY(2)), (2, 
ARRAY()), (null, ARRAY(4));
   
   SELECT * FROM t3 LEFT JOIN LATERAL EXPLODE(c2);          -- 4 rows, (2, [], 
NULL) is missing
   SELECT * FROM t3 LEFT JOIN LATERAL EXPLODE(c2) ON TRUE;  -- 5 rows, correct
   ```
   
   This PR makes the rule skip a `LEFT OUTER` lateral join whose subquery has a 
`Generate`. Such a join is then planned the same way as with `ON TRUE`, which 
is correct: decorrelated through a domain join, instead of as a single 
`Generate` over the left side. Inlining a plain projection over 
`OneRowRelation` stays as it is for all join types, because that subquery 
always returns one row. Inner and cross lateral joins over a generator are 
still inlined.
   
   The rule skips the join rather than marking the `Generate` as `outer`, for 
two reasons:
   - With a projection over the generator, as in `LEFT JOIN LATERAL (SELECT 
coalesce(v, 0) FROM EXPLODE(c2) t(v))`, an outer `Generate` would compute the 
projection over the padded NULL row and return `0`, while the left join must 
return `NULL`.
   - `ExtractPythonUDTFs` does not keep `outer`, so Python UDTFs would still 
drop rows.
   
   Restricting the rewrite is the minimal change for a correctness fix. A 
follow-up could restore the single-`Generate` plan for a bare built-in 
generator by marking it `outer`.
   
   ### Why are the changes needed?
   
   `LEFT JOIN LATERAL` with a generator (`explode`, `inline`, `posexplode`, 
`stack`, Python UDTFs) and no `ON` condition returns wrong results: it behaves 
like an inner join. `DataFrame.lateralJoin(tvf, how="left")` has no condition 
by default, so it always hits this.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `LEFT JOIN LATERAL` over a generator without a join condition now keeps 
the left rows for which the generator returns no rows, padded with NULL, as 
with `ON TRUE`.
   
   ### How was this patch tested?
   
   - A new case in `OptimizeOneRowRelationSubquerySuite` checks that the rule 
does not inline a left outer lateral join over `Generate`, or over a projection 
of a `Generate`. It fails without the fix: the plan is `Generate explode(arr), 
false` over the left side.
   - New queries in `join-lateral.sql`: `LEFT JOIN LATERAL EXPLODE(c2)`, with a 
table alias, with a `SELECT EXPLODE(c2)` subquery, and the `ON TRUE` form for 
reference. All four return the same 5 rows, and `SQLQueryTestSuite` fails on 
them without the fix.
   - A new case in `DataFrameTableValuedFunctionsSuite` checks 
`lateralJoin(spark.tvf.explode(...), "left")`, which has no join condition, 
against the expected rows for an empty and a NULL array. It fails without the 
fix.
   - `DataFrameSubquerySuite`, `DataFrameTableValuedFunctionsSuite`, and 
scalastyle for `catalyst` and `sql` pass.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Co-authored by: Claude Sonnet 4.5


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