Skip to content

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

Backlog

Description

@tlfua

Describe the bug

An EXISTS subquery that unnests the result of a correlated count(*) without GROUP BY drops the outer rows that have no match. The count of such a row is 0, so the unnest still gives rows and the EXISTS should be true.

To Reproduce

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;

DataFusion main (25da21b) returns:

1
2

Expected behavior

1
2
4

DuckDB 2.0.1 returns 1, 2, 4 (with list_value in place of make_array).

Additional context

It looks like PullUpCorrelatedExpr adds i.a to the GROUP BY of the aggregate and pulls the filter up through the Unnest. After that the aggregate gives no row for a = 4, where it gave one row with 0. The EXISTS path retries the pull up without count bug handling, so nothing restores that row.

Found while reviewing #27783 . That PR keeps this behavior unchanged for Unnest.

Related: this is the same class of bug as #24960 and #27690 (an outer row with no match is treated as if a count(*) without GROUP BY gave no row). The plain EXISTS repro in #24960 returns the correct rows on main, but this shape with an Unnest between the aggregate and the EXISTS still drops them.

Activity

  1. Faizaanp commented on Nov 29, 2026

    @Faizaanp

    take

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions