You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kumarUjjawal (via GitHub)" <gi...@apache.org> on 2026/03/14 13:31:07 UTC

[PR] fix(sql): handle GROUP BY ALL with aliased aggregates [datafusion]

kumarUjjawal opened a new pull request, #20943:
URL: https://github.com/apache/datafusion/pull/20943

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Closes #20909
   
   ## Rationale for this change
   
   Issue #20909 reported that:
   
     - SELECT COUNT(*) FROM t GROUP BY ALL works
     - SELECT COUNT(*) AS c FROM t GROUP BY ALL fails at execution
   
     The failure came from an incorrect plan that grouped by an aggregate alias expression.
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   
   This PR fixes GROUP BY ALL when the select list has an aliased aggregate like COUNT(*) AS c.
   
     Changes:
   
     - Update GROUP BY ALL expansion logic to skip any select expression that contains an aggregate, even if it is wrapped in aliases.
     - Add SQL planner tests for:
         - SELECT COUNT(*) FROM ... GROUP BY ALL
         - SELECT COUNT(*) AS c FROM ... GROUP BY ALL
     - Add core execution test for:
         - SELECT COUNT(*) AS c FROM ... GROUP BY ALL
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   
   Yes
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
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-unsubscribe@datafusion.apache.org

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


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


Re: [PR] fix(sql): handle GROUP BY ALL with aliased aggregates [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on PR #20943:
URL: https://github.com/apache/datafusion/pull/20943#issuecomment-4103556659

   @alamb This PR looks ready to me.


-- 
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-unsubscribe@datafusion.apache.org

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


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


Re: [PR] fix(sql): handle GROUP BY ALL with aliased aggregates [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on code in PR #20943:
URL: https://github.com/apache/datafusion/pull/20943#discussion_r2936919043


##########
datafusion/sql/tests/sql_integration.rs:
##########
@@ -1748,6 +1748,32 @@ fn select_simple_aggregate_with_groupby_can_use_alias() {
     );
 }
 
+#[test]
+fn select_count_with_group_by_all() {

Review Comment:
   The two tests in this file pass without the fix. Probably best to remove them?



##########
datafusion/core/tests/sql/aggregates/basic.rs:
##########
@@ -103,6 +103,21 @@ async fn count_aggregated() -> Result<()> {
     Ok(())
 }
 
+#[tokio::test]
+async fn count_aggregated_group_by_all_alias() -> Result<()> {

Review Comment:
   Seems like this would be better as an SLT test?



##########
datafusion/sql/src/select.rs:
##########
@@ -192,16 +192,13 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
                 })
                 .collect::<Result<Vec<Expr>>>()?
         } else {
-            // 'group by all' groups wrt. all select expressions except 'AggregateFunction's.
-            // Filter and collect non-aggregate select expressions
+            // 'group by all' groups wrt. all select expressions except aggregate expressions.
+            // Note: aggregate expressions can be nested under one or more aliases
+            // (e.g. count(1) AS count(*) AS c), so detect aggregates recursively.

Review Comment:
   This comment is a bit misleading, because that SQL statement doesn't parse. The problem seems to actually occur in practice because of a (nested) alias inserted by the analyzer, which AFAICS you can't do from SQL. I'd probably just remove the change to the comment.



-- 
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-unsubscribe@datafusion.apache.org

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


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


Re: [PR] fix(sql): handle GROUP BY ALL with aliased aggregates [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #20943:
URL: https://github.com/apache/datafusion/pull/20943


-- 
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-unsubscribe@datafusion.apache.org

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


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