You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Correlated subquery fails when filtering an aliased derived table on an outer column #26504
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 vFROM (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.
Activity
fix(optimizer): preserve correlated columns when removing filter abov…