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

   ### Describe the bug
   
   Since 56.0.0, an `IN` / `NOT IN` subquery is no longer decorrelated when one 
of its aggregates reads from an uncorrelated aggregate without `GROUP BY`, or 
from a `UNION` (which plans as an aggregate). The `InSubquery` stays in the 
plan and planning fails with:
   
   ```
   This feature is not implemented: Physical plan does not support logical 
expression InSubquery(InSubquery { ... })
   ```
   
   ### To Reproduce
   
   ```sql
   CREATE TABLE t(k INT) AS VALUES (1), (2), (3);
   CREATE TABLE u(a INT) AS VALUES (1), (5);
   
   SELECT k FROM t WHERE k IN (SELECT max(n) FROM (SELECT count(*) AS n FROM 
u));
   SELECT k FROM t WHERE k IN (SELECT min(a) FROM u UNION SELECT 2);
   ```
   
   Both fail on main (915c5b608) and on 56.0.0. `NOT IN` and the projection 
form (`SELECT k, k IN (...) FROM t`) fail the same way.
   
   ### Expected behavior
   
   `2` for the first query and `1, 2` for the second. DataFusion 50 through 55 
and DuckDB 2.0.1 return these.
   
   ### Additional context
   
   This started with #26544, the fix for #26508. In 
`PullUpCorrelatedExpr::f_up` (`datafusion/optimizer/src/decorrelate.rs`), an 
`Aggregate` now stops the pull up whenever `pulled_up_scalar_agg` is set. That 
flag is set for every aggregate without `GROUP BY` that the rule visits, 
correlated or not, and every aggregate of an `IN` subquery goes through that 
branch. So the uncorrelated `count(*)` sets the flag and the `max` above it 
stops the rewrite.
   
   The `regrouped_scalar_agg` flag added in #27783 is set only when the pull up 
adds a correlated column to the `GROUP BY` of such an aggregate, which is the 
#26508 case. #27800 looks like another effect of the broad flag, on the 
`LATERAL` path.
   


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