sgrebnov commented on code in PR #12331:
URL: https://github.com/apache/datafusion/pull/12331#discussion_r1744899415


##########
datafusion/sql/src/unparser/rewrite.rs:
##########
@@ -257,6 +257,37 @@ pub(super) fn subquery_alias_inner_query_and_columns(
     (outer_projections.input.as_ref(), columns)
 }
 
+/// Injects column aliases into the projection of a logical plan by wrapping 
`Expr::Column` expressions
+/// with `Expr::Alias` using the provided list of aliases. Non-column 
expressions are left unchanged.
+///
+/// Example:
+/// - `SELECT col1, col2 FROM table` with aliases `["alias_1", 
"some_alias_2"]` will be transformed to
+/// - `SELECT col1 AS alias_1, col2 AS some_alias_2 FROM table`
+pub(super) fn inject_column_aliases(
+    projection: &datafusion_expr::Projection,
+    aliases: &Vec<Ident>,
+) -> LogicalPlan {
+    let mut updated_projection = projection.clone();
+
+    let new_exprs = projection
+        .expr
+        .iter()
+        .zip(aliases)
+        .map(|(expr, col_alias)| match expr {
+            Expr::Column(col) => Expr::Alias(Alias {
+                expr: Box::new(expr.clone()),
+                relation: col.relation.clone(),
+                name: col_alias.to_string(),
+            }),
+            _ => expr.clone(),
+        })
+        .collect::<Vec<_>>();
+
+    updated_projection.expr.clone_from(&new_exprs);

Review Comment:
   Updated as suggested - thank you 👍



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