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


##########
datafusion/optimizer/src/decorrelate.rs:
##########
@@ -388,7 +407,43 @@ impl TreeNodeRewriter for PullUpCorrelatedExpr {
         // `column_refs_above` holds only the nodes strictly above `plan`.
         self.column_refs_above.pop();
         self.depends_only_on_input_having_rows_above.pop();
+        let aliases_before = self.aliases_before.pop().unwrap_or(0);
+
+        let transformed = self.pull_up(plan)?;
+        if matches!(transformed.data, LogicalPlan::SubqueryAlias(_)) {

Review Comment:
   This query returns `3` on this branch, but the right answer is `1, 2, 3`; 
55.3.0 and main fail with a schema error instead:
   
   ```sql
   CREATE TABLE t (a INT, b INT) AS VALUES (1, 1), (2, 2), (3, NULL);
   CREATE TABLE t2 (a INT, b INT) AS VALUES (1, 1), (3, 30), (2, NULL);
   SELECT t.a FROM t WHERE EXISTS (
     SELECT 1 FROM (SELECT * FROM t2 AS s WHERE s.a = t.a) x
     CROSS JOIN t2 AS s WHERE s.b = 30
   ) ORDER BY 1;
   ```
   
   Here's how it happens. Once the first optimizer pass keeps the subquery, 
`push_down_filter` moves both filters below their `s`. Now the two `s` differ, 
and the second pass collides on two equal `TableScan: t2` plans instead. The 
join filter then reads the plain `s.a`, not `x.a`.
   
   Dropping the `SubqueryAlias` condition here makes this query error. It also 
turns your two "without a shared alias" tests at lines 4529 and 4541 into 
errors, though both return correct rows in 55.3.0. Could you make this query 
error and add it as a `query error` test?



##########
datafusion/optimizer/src/decorrelate.rs:
##########
@@ -388,7 +407,43 @@ impl TreeNodeRewriter for PullUpCorrelatedExpr {
         // `column_refs_above` holds only the nodes strictly above `plan`.
         self.column_refs_above.pop();
         self.depends_only_on_input_having_rows_above.pop();
+        let aliases_before = self.aliases_before.pop().unwrap_or(0);
+
+        let transformed = self.pull_up(plan)?;
+        if matches!(transformed.data, LogicalPlan::SubqueryAlias(_)) {
+            let has_facts = self.has_facts(&transformed.data);
+            let next_order = self.alias_order.len();
+            match self.alias_order.get_mut(&transformed.data) {
+                Some((order, had_facts)) => {
+                    if *order < aliases_before && (*had_facts || has_facts) {

Review Comment:
   No test covers the `(*had_facts || has_facts)` part. If it is removed, every 
test still passes, while two equal aliases with no correlated columns of their 
own start to error. Could you add this test next to the "same table twice" 
ones? It passes on this branch and fails without that condition:
   
   ```sql
   # Two equal `t26692_2 AS s` without correlated columns of their own still
   # decorrelate.
   query I
   SELECT t.a FROM t26692 t
   WHERE EXISTS (
     SELECT 1 FROM (SELECT * FROM t26692_2 AS s) p
     CROSS JOIN (SELECT * FROM t26692_2 AS s) q
     WHERE p.a = t.a
   )
   ORDER BY 1;
   ----
   1
   2
   3
   ```



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