Phoenix500526 commented on code in PR #23327:
URL: https://github.com/apache/datafusion/pull/23327#discussion_r3549751496


##########
datafusion/core/tests/sql/unparser.rs:
##########
@@ -399,6 +402,118 @@ async fn 
optimized_duckdb_unparse_qualifies_nested_passthrough_column() -> Resul
     Ok(())
 }
 
+// https://github.com/apache/datafusion/issues/23317
+//
+// `SingleDistinctToGroupBy` can produce two stacked Aggregates where the inner
+// Aggregate defines intermediate fields such as `group_alias_0`, `alias1`, and
+// `alias2`. The unparser must preserve that inner Aggregate as a derived table
+// before the outer Aggregate references those fields.
+//
+// Without `SingleDistinctToGroupBy`, the Aggregate still sits over an unnamed
+// derived Projection. In that SQL scope, base table aliases `cs` and `c` are 
no
+// longer visible, so aggregate expressions must refer to the derived table's
+// output columns unqualified.
+const ISSUE_23317_QUERY: &str = r#"
+WITH cohort AS (
+    SELECT
+        signup_year,
+        sum(customers) AS customers,
+        sum(revenue) AS revenue
+    FROM
+        (
+            SELECT
+                date_part('year', c.signup_date) AS signup_year,
+                count(DISTINCT cs.customer_id) AS customers,
+                round(sum(cs.total_revenue), 2) AS revenue
+            FROM
+                "warehouse"."main"."sales" cs
+                JOIN "warehouse"."main"."customers" c USING (customer_id)
+            GROUP BY

Review Comment:
   Good catch. The sql has already parsed in 
`assert_issue_23317_unparsed_sql_plans` now. 



##########
datafusion/sql/src/unparser/plan.rs:
##########


Review Comment:
   Done in commit b56a339



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