Skip to content

Commit dfc8bb7

Browse files
authored
fix(sql): handle GROUP BY ALL with aliased aggregates (#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. -->
1 parent 293a880 commit dfc8bb7

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

datafusion/sql/src/select.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use datafusion_common::error::DataFusionErrorBuilder;
3131
use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion};
3232
use datafusion_common::{Column, DFSchema, Result, not_impl_err, plan_err};
3333
use datafusion_common::{RecursionUnnestOption, UnnestOptions};
34-
use datafusion_expr::expr::{Alias, PlannedReplaceSelectItem, WildcardOptions};
34+
use datafusion_expr::expr::{PlannedReplaceSelectItem, WildcardOptions};
3535
use datafusion_expr::expr_rewriter::{
3636
normalize_col, normalize_col_with_schemas_and_ambiguity_check, normalize_sorts,
3737
};
@@ -193,15 +193,11 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
193193
.collect::<Result<Vec<Expr>>>()?
194194
} else {
195195
// 'group by all' groups wrt. all select expressions except 'AggregateFunction's.
196-
// Filter and collect non-aggregate select expressions
196+
// Filter and collect non-aggregate select expressions.
197197
select_exprs
198198
.iter()
199-
.filter(|select_expr| match select_expr {
200-
Expr::AggregateFunction(_) => false,
201-
Expr::Alias(Alias { expr, name: _, .. }) => {
202-
!matches!(**expr, Expr::AggregateFunction(_))
203-
}
204-
_ => true,
199+
.filter(|select_expr| {
200+
find_aggregate_exprs(std::iter::once(*select_expr)).is_empty()
205201
})
206202
.cloned()
207203
.collect()

datafusion/sqllogictest/test_files/group_by.slt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,11 @@ SELECT col0, col1, COUNT(col2), SUM(col3) FROM tab3 GROUP BY ALL
19791979
0 2 2 -3
19801980
1 NULL 1 -2
19811981

1982+
query I
1983+
SELECT COUNT(*) AS c FROM tab3 GROUP BY ALL
1984+
----
1985+
5
1986+
19821987
# query below should work in multi partition, successfully.
19831988
query II
19841989
SELECT l.col0, LAST_VALUE(r.col1 ORDER BY r.col0) as last_col1

0 commit comments

Comments
 (0)