tlfua opened a new issue, #27800:
URL: https://github.com/apache/datafusion/issues/27800

   ### Describe the bug
   
   A `LATERAL` subquery that joins a correlated derived table with an 
uncorrelated aggregate without `GROUP BY` returns an extra row of NULLs for an 
outer row that has no match. A plain (inner) lateral join should drop that 
outer row.
   
   ### To Reproduce
   
   ```sql
   CREATE TABLE t (a INT) AS VALUES (1), (2), (4);
   CREATE TABLE i (a INT) AS VALUES (1), (2), (2), (3);
   CREATE TABLE u (b INT) AS VALUES (10), (20);
   
   SELECT t.a, s.a, s.c FROM t, LATERAL (
     SELECT * FROM (SELECT a FROM i WHERE a = t.a), (SELECT count(*) c FROM u) z
   ) s ORDER BY 1, 2;
   ```
   
   DataFusion main returns:
   
   ```text
   1 1 2
   2 2 2
   2 2 2
   4 NULL NULL
   ```
   
   ### Expected behavior
   
   No row for `t.a = 4` (DuckDB 2.0.1):
   
   ```text
   1 1 2
   2 2 2
   2 2 2
   ```
   
   An error would also be better than the extra row.
   
   ### Additional context
   
   The uncorrelated `count(*)` sets 
`PullUpCorrelatedExpr::pulled_up_scalar_agg`, and `decorrelate_lateral_join` 
then turns the inner lateral join into a left join, which keeps the unmatched 
outer row.
   
   Found in the review of https://github.com/apache/datafusion/pull/27783 . 
With a derived table that renames its column (`SELECT a * 10 AS y FROM i WHERE 
a = t.a`), main fails with a schema error (similar to 
https://github.com/apache/datafusion/issues/25837 ), and with that PR it 
returns the same extra row.
   
   ### Willingness to contribute
   
   I cannot contribute a fix for this bug at this time
   


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