kosiew commented on code in PR #27333:
URL: https://github.com/apache/datafusion/pull/27333#discussion_r4783892831


##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -944,6 +944,29 @@ impl Unparser<'_> {
                     );
                 }
 
+                // Without column references nothing in this SELECT carries a
+                // global aggregate, so absorbing it would return one row per
+                // input row instead of exactly one.
+                if p.expr.iter().all(|expr| expr.column_refs().is_empty())
+                    && find_agg_node_within_select(plan, true)
+                        .is_some_and(|agg| agg.group_expr.is_empty())
+                {
+                    self.derive_with_dialect_alias(

Review Comment:
   repro case:
   
       ```rust
        let plan = ctx.table("t").await?
            .aggregate(vec![], vec![count(lit(1))])?
            .limit(0, Some(1))?
            .limit(0, Some(0))?
            .select(vec![lit(7)])?
            .into_unoptimized_plan();
        ```
   The plan returns zero rows. HEAD emits `SELECT 7 FROM (SELECT count(1) FROM 
t LIMIT 1)` and returns one row; the parent emitted `SELECT 7 FROM t LIMIT 0` 
because it dropped the aggregate.



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