dmitrybugakov commented on code in PR #12482:
URL: https://github.com/apache/datafusion/pull/12482#discussion_r1761175453
##########
datafusion/sql/src/unparser/rewrite.rs:
##########
@@ -258,8 +258,36 @@ 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.
+/// Injects column aliases into a subquery's logical plan. The function
searches for a `Projection`
+/// within the given plan, which may be wrapped by other operators (e.g.,
LIMIT, SORT).
+/// If the top-level plan is a `Projection`, it directly injects the column
aliases.
+/// Otherwise, it iterates through the plan's children to locate and transform
the `Projection`.
+///
+/// Example:
+/// - `SELECT col1, col2 FROM table LIMIT 10` plan with aliases `["alias_1",
"some_alias_2"]` will be transformed to
+/// - `SELECT col1 AS alias_1, col2 AS some_alias_2 FROM table LIMIT 10`
+pub(super) fn inject_column_aliases_into_subquery(
+ plan: LogicalPlan,
+ aliases: Vec<Ident>,
+) -> Result<LogicalPlan> {
+ match &plan {
+ LogicalPlan::Projection(inner_p) => Ok(inject_column_aliases(inner_p,
aliases)),
+ _ => {
+ // projection is wrapped by other operator (LIMIT, SORT, etc),
iterate through the plan to find it
+ plan.map_children(|child| {
+ if let LogicalPlan::Projection(p) = &child {
+ Ok(Transformed::yes(inject_column_aliases(p,
aliases.clone())))
Review Comment:
Is it correct that we clone aliases in every recursive call?
--
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]