kumarUjjawal commented on code in PR #24808:
URL: https://github.com/apache/datafusion/pull/24808#discussion_r3891533233
##########
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:
The new scope rewrite changes only the `SELECT` items. An outer `Sort` is
rendered before this branch and keeps the old qualifier.
For example, `SELECT j1_id FROM (...) ORDER BY j1_id` can still emit `ORDER
BY ta.j1_id`. Only `derived_projection` is visible there.
The same problem applies to an outer filter rendered before this branch.
Please rebase all outer clauses and add an `ORDER BY` regression.
##########
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:
These methods are public because `ast` is a public module. Only the unparser
uses this scope state.
Please use `pub(super)` for these three methods.
--
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]