Faizaanp opened a new pull request, #27839:
URL: https://github.com/apache/datafusion/pull/27839

   ## Which issue does this PR close?
   
   - Closes #27829.
   
   ## Rationale for this change
   
   An `EXISTS` / `NOT EXISTS` subquery that unnests the result of a correlated 
aggregate without `GROUP BY` (such as `count(*)`) drops outer rows that have no 
matching inner rows:
   
   ```sql
   CREATE TABLE t (a INT) AS VALUES (1), (2), (4);
   CREATE TABLE i (a INT) AS VALUES (1), (2), (2), (3);
   
   SELECT o.a FROM t o WHERE EXISTS (
     SELECT unnest(make_array(c, c))
     FROM (SELECT count(*) c FROM i WHERE a = o.a) e
   ) ORDER BY o.a;
   -- main: 1, 2
   -- expected: 1, 2, 4
   ```
   
   `PullUpCorrelatedExpr` adds `i.a` to the `GROUP BY` of the aggregate, so 
unmatched outer rows produce no rows from the aggregate. When 
`LogicalPlan::Unnest` sees `collected_count_expr_map` on its input, it sets 
`can_pull_up = false`; `build_join` then retries `EXISTS` without count-bug 
handling, where `collected_count_expr_map` is empty, allowing the uncompensated 
`LeftSemi` join to proceed and drop unmatched outer rows.
   
   Similarly, for `IN` / `NOT IN` over `Unnest` of a groupless aggregate, 
`count_bug_is_harmless` did not check `pull_up.can_pull_up`, treating shapes 
rejected by `Unnest` as harmless when `collected_count_expr_map` had no entry 
for the top plan.
   
   ## What changes are included in this PR?
   
   - `PullUpCorrelatedExpr::pull_up_unnest` in 
`datafusion/optimizer/src/decorrelate.rs`:
     - Stops the uncompensated `EXISTS` fallback retry from pulling up through 
`Unnest` over a regrouped scalar aggregate.
     - For predicate subqueries (`EXISTS` / `NOT EXISTS` / `IN` / `NOT IN`) 
with count-bug handling enabled, evaluates single-column depth-1 list `Unnest` 
on empty-input aggregate values:
       - If unnesting produces 0 rows on empty input (e.g., `make_array()`), no 
count bug survives above `Unnest`.
       - If unnesting produces one or more identical rows (e.g., 
`make_array(0)`, `make_array(0, 0)`, `make_array(NULL)`), propagates the 
uniform element value into `collected_count_expr_map`. For `EXISTS`, requires 
that `Unnest` cannot drop matched rows (otherwise a matched group yielding 0 
rows would be indistinguishable from an unmatched outer row in a `LEFT JOIN`), 
and tracks when `Unnest` may emit multiple rows per group 
(`unnest_may_multiply_rows`).
       - For shapes that cannot be evaluated uniformly (distinct elements, 
non-literal lists, struct/multi-column/multi-depth unnest), keeps the subquery 
correlated.
     - Leaves scalar subqueries and `LATERAL` joins unchanged.
   - `build_join` / `build_join_with_count_bug` in 
`datafusion/optimizer/src/decorrelate_predicate_subquery.rs`:
     - When `unnest_may_multiply_rows` is set and there is no `HAVING` or outer 
filter above the aggregate, skips the `LEFT JOIN` (avoiding duplicate outer 
rows) and emits the unconditional `EXISTS = true` plan; when combined with a 
filter above `Unnest`, keeps the subquery correlated.
     - Requires `pull_up.can_pull_up` in `count_bug_is_harmless` for `IN` / 
`NOT IN`.
   
   ## What is the testing strategy for this PR?
   
   Added `sqllogictest` cases in 
`datafusion/sqllogictest/test_files/subquery.slt` covering:
   - The `#27829` reproducer (`EXISTS`, `NOT EXISTS`, and projected `EXISTS` 
over `unnest(make_array(c, c))`).
   - Existing `Unnest` queries from #27783 (`IN` over 
`unnest(make_array(max(a)))`, `EXISTS` over `unnest(make_array(c))` with `WHERE 
v > 0`) continuing to return the same rows.
   - Filter above single-element `Unnest` satisfied by `c = 0` (`WHERE v = 0`).
   - Always-empty array unnest (`make_array()`).
   - Safe fallback to not-implemented error (instead of wrong rows) for 
multi-element `Unnest` with a filter or outer filter above, distinct elements 
(`make_array(c, c + 1)`), `CASE` expressions that can produce an empty array 
for matched rows, `NOT IN` over `max(a)`, and `IN` over `count(*)`.
   
   ## Are there any user-facing changes?
   
   `EXISTS` / `NOT EXISTS` subqueries over `Unnest` of a correlated groupless 
aggregate now return correct results (or a not-implemented error for 
unsupported shapes) instead of silently dropping or returning wrong rows. No 
public API changes.


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