adriangb commented on code in PR #25838:
URL: https://github.com/apache/datafusion/pull/25838#discussion_r4140859898


##########
datafusion/optimizer/src/decorrelate.rs:
##########
@@ -170,18 +176,62 @@ impl TreeNodeRewriter for PullUpCorrelatedExpr {
                 match (self.exists_sub_query, plan_hold_outer) {
                     (false, true) => {
                         // the unsupported case
-                        self.can_pull_up = false;
-                        Ok(Transformed::new(plan, false, 
TreeNodeRecursion::Jump))
+                        Ok(self.stop_pull_up(plan))
                     }
                     _ => Ok(Transformed::no(plan)),
                 }
             }
-            _ if plan.contains_outer_reference() => {
-                // the unsupported cases, the plan expressions contain out 
reference columns(like window expressions)
-                self.can_pull_up = false;
-                Ok(Transformed::new(plan, false, TreeNodeRecursion::Jump))
+            // A correlated filter below these nodes can move above them. 
`f_up`
+            // adds the columns the pulled up filter needs to a Projection or 
an
+            // Aggregate, and checks that the filter can move above an
+            // Aggregate. A `DISTINCT` gives the same rows when a filter runs
+            // before it or after it, and its output takes the columns the node
+            // below it adds. The node itself must not hold an outer reference,
+            // because only a Filter gives its outer references to the join.
+            LogicalPlan::Projection(_)
+            | LogicalPlan::Aggregate(_)
+            | LogicalPlan::Distinct(Distinct::All(_))
+            | LogicalPlan::Join(_)
+            | LogicalPlan::AsOfJoin(_)
+            | LogicalPlan::Repartition(_)
+            | LogicalPlan::SubqueryAlias(_)
+            | LogicalPlan::TableScan(_)
+            | LogicalPlan::EmptyRelation(_)
+            | LogicalPlan::Values(_) => {
+                if plan.contains_outer_reference() {
+                    // the unsupported cases, the plan expressions contain out
+                    // reference columns
+                    Ok(self.stop_pull_up(plan))
+                } else {
+                    Ok(Transformed::no(plan))
+                }
+            }
+            // A correlated filter below these nodes cannot move above them:
+            // - A Window function reads the rows of its partition, so the
+            //   filter changes the rows each function sees.
+            // - `DISTINCT ON` keeps the first row of each group, so the filter
+            //   changes which row is first.
+            // - The pull up does not carry the columns of the pulled up filter
+            //   through an Unnest, and fails with a schema error.
+            // - A RecursiveQuery repeats its recursive term.
+            // - The other nodes are not queries and are not in a subquery.
+            LogicalPlan::Window(_)
+            | LogicalPlan::Distinct(Distinct::On(_))
+            | LogicalPlan::Unnest(_)

Review Comment:
   Recompute_schema in `f_up` worked! The two new `subquery.slt` cases and the 
`SELECT i.id, unnest(i.arr) LATERAL` case now decorrelate with correct results 
(tests added). The one case that still stops is a correlated filter on the 
column being unnested itself. The SQL planner never builds that, but 
`DataFrame::unnest_column` can. I added a unit test to pin this behavior.



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