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


##########
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:
   @tlfua thanks for applying this! My earlier suggestion put the check in the 
shared helper, which was too broad. It also stops the pull up through an 
`Unnest`, so two queries that return correct rows on 56.0.0 now fail with the 
not-implemented error:
   
   ```sql
   SELECT o.a FROM t27691 o WHERE o.a IN (
     SELECT unnest(make_array(m)) FROM (SELECT max(a) m FROM t27691_i WHERE a = 
o.a) e
   ) ORDER BY o.a;
   -- 56.0.0: 1, 2; this PR: This feature is not implemented ... InSubquery
   ```
   
   Checking the flag only in the `Join`/`AsOfJoin` arm keeps every join case in 
this PR correlated (all of them were schema errors on 56.0.0) and leaves 
`Unnest` as it is in 56.0.0. With that change the `Unnest` test returns `1, 2` 
(wrong, the same as 56.0.0), so it should become its own issue rather than a 
test here. Could you open one?
   
   ```diff
                LogicalPlan::Join(_) | LogicalPlan::AsOfJoin(_) => {
   +                // A regrouped aggregate below gives no row for an outer row
   +                // without a match, where the aggregate gave one. `IN` and 
the
   +                // `EXISTS` retry run without count bug handling, so
   +                // `collected_count_expr_map` can be empty here.
   +                if self.regrouped_scalar_agg {
   +                    self.can_pull_up = false;
   +                }
                    self.pass_through_pulled_up_cols(plan)
                }
   @@
   -    /// hold once a Window or an Unnest runs over the aggregate, so the 
pull up
   -    /// stops in that case.
   +    /// hold once a Window, an Unnest or a Join runs over the aggregate, so 
the
   +    /// pull up stops in that case.
        fn pass_through_pulled_up_cols(
            &mut self,
            plan: LogicalPlan,
        ) -> Result<Transformed<LogicalPlan>> {
   -        // `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
   -            || plan
   -                .inputs()
   -                .iter()
   -                .any(|input| 
self.collected_count_expr_map.contains_key(*input))
   +        if plan
   +            .inputs()
   +            .iter()
   +            .any(|input| self.collected_count_expr_map.contains_key(*input))
            {
   ```
   
   <details>
   <summary>subquery.slt: replace the Unnest error test with the two queries 
that work on 56.0.0</summary>
   
   ```diff
   -# The same holds for an Unnest. Expected: 1, 2, 4.
   -query error DataFusion error: This feature is not implemented: Physical 
plan does not support logical expression Exists
   -SELECT o.a FROM t27691 o WHERE EXISTS (
   -  SELECT unnest(make_array(c, c))
   -  FROM (SELECT count(*) c FROM t27691_i WHERE a = o.a) e
   -) ORDER BY o.a;
   +# Below an Unnest, the aggregate is still pulled up. These queries give no
   +# row for an outer row without a match either way.
   +query I
   +SELECT o.a FROM t27691 o WHERE o.a IN (
   +  SELECT unnest(make_array(m)) FROM (SELECT max(a) m FROM t27691_i WHERE a 
= o.a) e
   +) ORDER BY o.a;
   +----
   +1
   +2
   +
   +query I
   +SELECT o.a FROM t27691 o WHERE EXISTS (
   +  SELECT 1 FROM (
   +    SELECT unnest(make_array(c)) v
   +    FROM (SELECT count(*) c FROM t27691_i WHERE a = o.a) e
   +  ) y WHERE v > 0
   +) ORDER BY o.a;
   +----
   +1
   +2
   ```
   
   With this change the full sqllogictest suite, `cargo test -p 
datafusion-optimizer` and clippy pass. Removing the `Join`-arm check fails 4 
tests, and moving it back into the helper fails the 2 new `Unnest` tests.
   </details>



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