Skip to content

fix: handle count bug through Unnest in correlated predicate subqueries - #27839

Open
Faizaanp wants to merge 4 commits into
apache:mainfrom
Faizaanp:fix/unnest-count-bug-27829
Open

Faizaanp wants to merge 4 commits into
apache:mainfrom
Faizaanp:fix/unnest-count-bug-27829

Conversation

@Faizaanp

Copy link
Copy Markdown

Which issue does this PR close?

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:

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 fix: update join schema when pulling up correlated columns #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.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Nov 29, 2026
@Faizaanp

Faizaanp commented Dec 3, 2026

Copy link
Copy Markdown
Author

Hi @jayzhan211, this is my first contribution to DataFusion so the CI workflows are waiting for approval. Could you please approve them when you have a chance? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong results: EXISTS over unnest of a correlated count(*) without GROUP BY drops outer rows with no match

1 participant