Skip to content

Correlated subquery fails when filtering an aliased derived table on an outer column #26504

Closed

Description

@fornwall

Describe the bug

A correlated subquery fails to plan when the correlation is inside an aliased derived table and the subquery filters that derived table on an outer column.

To Reproduce

datafusion-cli -c "
SELECT * FROM (VALUES (1)) AS x(d)
WHERE EXISTS (SELECT 1 FROM (SELECT * FROM (VALUES (1)) AS y(c) WHERE y.c = x.d) AS t WHERE x.d > 0)"

This gives:

Error: Schema error: No field named __correlated_sq_1.c.
Valid fields are x.d.

Expected behavior

One row with d = 1, which is what PostgreSQL 17 returns.

Additional context

The query succeeds if either:

  • the alias AS t is removed, or
  • x.d > 0 is moved into the derived table (WHERE y.c = x.d AND x.d > 0).

A scalar subquery of the same shape fails the same way (expected result: 1):

datafusion-cli -c "
SELECT (SELECT n FROM (SELECT sum(y.c) AS n FROM (VALUES (1)) AS y(c) WHERE y.c = x.d) AS t WHERE x.d > 0) AS v
FROM (VALUES (1)) AS x(d)"
Error: Schema error: No field named __scalar_sq_1.c.
Valid fields are x.d.

A similar error with LATERAL and a derived-table alias is reported in #25837.

Found with DataFusion CLI v55.1.0, main at 7e102d7.

Willingness to contribute

None

Activity

  1. added theissue type on Oct 19, 2026
  2. added a commit that references this issue on Oct 24, 2026
    836b4dd
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

    bugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions