Skip to content

Duplicate subqueries produce incorrect results #21210

Closed

Description

@neilconway

Describe the bug

  CREATE TABLE t1(id INTEGER) AS VALUES (1), (2), (3);
  CREATE TABLE t2(id INTEGER, outer_id INTEGER) AS VALUES (10, 1), (20, 1), (30, 2);

  SELECT
    (SELECT count(*) AS c FROM t2 WHERE t2.outer_id = t1.id) + 1 AS a,
    (SELECT count(*) AS c FROM t2 WHERE t2.outer_id = t1.id) + 2 AS b
  FROM t1
  ORDER BY t1.id;

Expected:

  +---+---+
  | a | b |
  +---+---+
  | 3 | 4 |
  | 2 | 3 |
  | 1 | 2 |
  +---+---+

Actual:

  +------+---+
  | a    | b |
  +------+---+
  | 3    | 4 |
  | 2    | 3 |
  | NULL | 2 |
  +------+---+

Presumably because we don't do the count-compensation trick for both subqueries.

To Reproduce

No response

Expected behavior

No response

Additional context

No response

Activity

  1. added theissue type on Mar 28, 2026
  2. neilconway commented on Mar 28, 2026

    @neilconway
    ContributorAuthor

    take

  3. added a commit that references this issue on May 19, 2026
    f962686
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions