goldmedal commented on code in PR #13824:
URL: https://github.com/apache/datafusion/pull/13824#discussion_r1894582093


##########
datafusion/sql/src/unparser/rewrite.rs:
##########
@@ -257,6 +267,43 @@ pub(super) fn subquery_alias_inner_query_and_columns(
     (outer_projections.input.as_ref(), columns)
 }
 
+/// Try to find the column alias for UNNEST in the inner projection.
+/// For example:
+/// ```sql
+///     SELECT * FROM t1 CROSS JOIN UNNEST(t1.c1) AS u(c1)
+/// ```
+/// The above query will be parsed into the following plan:
+/// ```text
+/// Projection: *
+///   Cross Join:
+///     SubqueryAlias: t1
+///       TableScan: t
+///     SubqueryAlias: u
+///       Subquery:
+///         Projection: UNNEST(outer_ref(t1.c1)) AS c1
+///           Projection: __unnest_placeholder(outer_ref(t1.c1),depth=1) AS 
UNNEST(outer_ref(t1.c1))
+///             Unnest: lists[__unnest_placeholder(outer_ref(t1.c1))|depth=1] 
structs[]
+///               Projection: outer_ref(t1.c1) AS 
__unnest_placeholder(outer_ref(t1.c1))
+///                 EmptyRelation
+/// ```
+/// The function will return the inner projection and the column alias `c1` if 
the column name
+/// starts with `UNNEST(` (the `Display` result of [Expr::Unnest]) in the 
inner projection.
+pub(super) fn find_unnest_column_alias(
+    plan: &LogicalPlan,
+) -> (&LogicalPlan, Option<String>) {
+    if let LogicalPlan::Projection(projection) = plan {
+        if projection.expr.len() != 1 {
+            return (plan, None);
+        }
+        if let Some(Expr::Alias(alias)) = projection.expr.first() {
+            if alias.expr.schema_name().to_string().starts_with("UNNEST(") {

Review Comment:
   No, it's not an `Expr::Unnest`. The full plan is 
   ```
   ///     SubqueryAlias: u
   ///       Subquery:
   ///         Projection: UNNEST(outer_ref(t1.c1)) AS c1
   ///           Projection: __unnest_placeholder(outer_ref(t1.c1),depth=1) AS 
UNNEST(outer_ref(t1.c1))
   ///             Unnest: 
lists[__unnest_placeholder(outer_ref(t1.c1))|depth=1] structs[]
   ///               Projection: outer_ref(t1.c1) AS 
__unnest_placeholder(outer_ref(t1.c1))
   ///                 EmptyRelation
   ```
   It's a column pointing to an alias of its child. The alias is built from an 
expression `schema_name`.
   
https://github.com/apache/datafusion/blob/ede665b212ec5e3a673986424103645bcb49e3bc/datafusion/sql/src/utils.rs#L400
   
   I think there is a similar issue for 
https://github.com/apache/datafusion/pull/13824#discussion_r1893427907.
   Maybe we can have a constant for the display result of `UNNEST`.



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to