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

   ### Describe the bug
   
   A correlated scalar subquery fails to plan when it has a non-equality 
correlated predicate (`<`) and reads a join over a projection that renames a 
column. The `scalar_subquery_to_join` rule then refers to the column by its 
name before the rename.
   
   The same queries succeed when the `<` predicate is removed or replaced with 
`=`.
   
   ### To Reproduce
   
   ```sql
   CREATE TABLE t(ts TIMESTAMP, id VARCHAR, m VARCHAR) AS VALUES (TIMESTAMP 
'2026-01-01', 'a', 'x');
   
   SELECT (SELECT count(*) FROM (SELECT id, ts t2 FROM t) e JOIN t t3 ON t3.id 
= e.id WHERE e.t2 < bk.ts) a FROM t bk;
   ```
   
   ```
   Optimizer rule 'scalar_subquery_to_join' failed
   Schema error: No field named e.ts. Did you mean 't3.ts'?
   ```
   
   A variant with an aggregate in a CTE fails the same way:
   
   ```sql
   WITH runs AS (SELECT m, max(ts) uid FROM t GROUP BY 1),
        l AS (SELECT r.ts, runs.uid FROM t r JOIN runs ON runs.m = r.m)
   SELECT (SELECT count(*) FROM l WHERE l.uid = o.ts AND l.ts < o.ts) x FROM t 
o;
   ```
   
   ```
   Optimizer rule 'scalar_subquery_to_join' failed
   Schema error: No field named runs."max(t.ts)".
   ```
   
   ### Expected behavior
   
   Both queries plan and return one row with the value `0`.
   
   ### Additional context
   
   Reproduced with `datafusion-cli` built from `main` at 
fb20da453cf2779dae7f438ec1f508921c4acb19.
   
   Possibly related: #25837 (a `LATERAL` subquery with a schema error over a 
derived table alias) and #14554.
   


-- 
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