blinding-pixels commented on code in PR #24808:
URL: https://github.com/apache/datafusion/pull/24808#discussion_r3892770274


##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -829,6 +847,61 @@ impl Unparser<'_> {
                         columns,
                     );
                 }
+
+                let qualified_projection = p.expr.iter().try_fold(false, 
|found, expr| {
+                    if found {
+                        Ok(true)
+                    } else {
+                        expr.exists(|expr| {
+                            Ok(matches!(expr, Expr::Column(column) if 
column.relation.is_some()))
+                        })
+                    }
+                })?;
+                let mut input_names = HashSet::new();
+                let unique_input_names = p
+                    .input
+                    .schema()
+                    .fields()
+                    .iter()
+                    .all(|field| input_names.insert(field.name()));
+                if let Some(input_alias) = 
Self::derived_input_alias(p.input.as_ref())
+                    && qualified_projection
+                    && unique_input_names
+                    && find_unnest_node_within_select(plan).is_none()
+                    && !select.inside_subquery_alias()
+                {
+                    // The input is about to enter a new SQL scope. Preserve 
that
+                    // boundary explicitly and make the outer expressions 
resolve
+                    // against the relation that will actually be visible 
there.
+                    let requires_alias = 
self.dialect.requires_derived_table_alias();
+                    let alias = requires_alias
+                        .then(|| self.new_table_alias(input_alias.to_string(), 
vec![]));
+                    self.derive(p.input.as_ref(), relation, alias, false)?;

Review Comment:
   Fixed. The shared derived-input scope now rebases projection, 
Filter/HAVING/QUALIFY, and Sort expressions. I added Generic and MySQL ORDER BY 
regressions plus a manually layered outer-Filter case so the filter path cannot 
pass through planner pushdown.



##########
datafusion/sql/src/unparser/ast.rs:
##########
@@ -195,6 +197,19 @@ impl SelectBuilder {
         self.flatten_table_aliases.iter().any(|a| a == alias)
     }
 
+    pub fn enter_subquery_alias(&mut self) {

Review Comment:
   Fixed. These three scope helpers are now `pub(super)`.



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