raghavyadav01 opened a new pull request, #27019:
URL: https://github.com/apache/datafusion/pull/27019

   ## Which issue does this PR close?
   
   - Part of #26692 (the wrong-results shapes).
   
   ## Rationale for this change
   
   `PullUpCorrelatedExpr` keys `correlated_subquery_cols_map` and 
`collected_count_expr_map` by plan value, so two equal `SubqueryAlias` nodes in 
one subquery (e.g. `t2 AS s` twice) share an entry, and one picks up or 
replaces the other's correlated columns. Following #5492, this makes those 
shapes error rather than trying to make them work.
   
   ## What changes are included in this PR?
   
   The rewriter remembers the order in which each `SubqueryAlias` plan is first 
returned by `f_up`. If a node returns one that was first returned outside its 
own subtree, and either has correlated facts, `can_pull_up` is set to false and 
the subquery stays correlated.
   
   ## Are these changes tested?
   
   Yes. sqllogictests for q1 in EXISTS / NOT EXISTS / IN / scalar forms, the 
same with the plain alias first, and the `LIMIT` case, plus shapes that must 
keep working.
   
   I also compared 33 shapes against DuckDB 1.5.6. Every one that changed went 
from wrong rows to an error (or to correct rows), with one exception: repeating 
the same correlated derived table with alias `s` twice now errors, while main 
returns correct rows for its EXISTS form (its scalar count form is wrong on 
main).
   
   Not covered: two equal derived tables without a shared alias still give 
wrong counts in scalar / LATERAL form. That is a different mechanism 
(deduplicated join filters), filed separately.
   
   ## Are there any user-facing changes?
   
   Queries that returned wrong rows now return an error.
   
   AI-assisted. I reviewed every line and ran the tests above.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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