fornwall opened a new issue, #26504:
URL: https://github.com/apache/datafusion/issues/26504

   ### 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
   
   ```shell
   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`):
   
   ```shell
   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 7e102d74c.
   
   ### Willingness to contribute
   
   None


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to