tlfua commented on code in PR #27783:
URL: https://github.com/apache/datafusion/pull/27783#discussion_r4729395617


##########
datafusion/optimizer/src/decorrelate.rs:
##########
@@ -861,10 +870,14 @@ impl PullUpCorrelatedExpr {
         &mut self,
         plan: LogicalPlan,
     ) -> Result<Transformed<LogicalPlan>> {
-        if plan
-            .inputs()
-            .iter()
-            .any(|input| self.collected_count_expr_map.contains_key(*input))
+        // `EXISTS` retries the pull up without count bug handling, so
+        // `collected_count_expr_map` can be empty while such an aggregate is
+        // below `plan`.
+        if self.regrouped_scalar_agg

Review Comment:
   @jayzhan211 thanks, applied in 3605ae0bcdd910c90592b9e596cab29f0ca5bb91: the 
check is now only in the `Join`/`AsOfJoin` arm, and the `Unnest` error test is 
replaced with your two queries (both return `1, 2`, as on main). Removing the 
`Join`-arm check fails 4 tests, and moving it back into the helper fails the 2 
`Unnest` tests.
   
   Opened https://github.com/apache/datafusion/issues/27829 for the 
`make_array(c, c)` case.
   
   I also compared main, this PR and DuckDB 2.0.1 on about 15k generated 
subqueries (`EXISTS`/`NOT EXISTS`/`IN`/scalar/`LATERAL`, with joins, `ASOF`, 
windows, unnest and filters over aggregates, on 3 datasets, two of them with an 
empty table), 5 runs each, plus about 60 hand-written join shapes:
   
   - No query that is correct on main fails or gives different rows on this PR.
   - This PR gives no wrong rows that main does not also give.
   - 3192 cases in this set go from an error on main to the correct rows.
   
   Two things that are not new, for the record:
   
   - With a `LATERAL` subquery that joins a correlated derived table with an 
uncorrelated `count(*)`, some queries change from main's schema error to an 
internal error from the schema check after `decorrelate_lateral_join`. For 
example:
   
     ```sql
     SELECT o.a, s.v FROM t o, LATERAL (
       SELECT e.v FROM (SELECT i.a AS w FROM i WHERE i.a = o.a) q, (SELECT 
count(*) AS v FROM i) e
     ) s;
     ```
   
     This looks related to https://github.com/apache/datafusion/issues/27800 : 
the uncorrelated `count(*)` sets `pulled_up_scalar_agg`, and the new schema has 
an extra `s.a` field and a nullable `v`. In the cases I tested, both main and 
this PR give an error, never rows.
   - An `EXISTS` over two nested filters on a correlated `count(*)` fails with 
a schema error in about half of the runs (10 of 20 on main and on this PR). The 
query has no join: https://github.com/apache/datafusion/issues/27830 .
   



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