Skip to content

Commit 9aa8c01

Browse files
authored
fix: group by two same columns (#84)
1 parent f7356f4 commit 9aa8c01

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

datafusion/core/src/logical_plan/builder.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,18 @@ impl LogicalPlanBuilder {
868868
/// value of the `group_expr`;
869869
pub fn aggregate(
870870
&self,
871-
group_expr: impl IntoIterator<Item = impl Into<Expr>>,
872-
aggr_expr: impl IntoIterator<Item = impl Into<Expr>>,
871+
group_exprs: impl IntoIterator<Item = impl Into<Expr>>,
872+
aggr_exprs: impl IntoIterator<Item = impl Into<Expr>>,
873873
) -> Result<Self> {
874-
let group_expr = normalize_cols(group_expr, &self.plan)?;
875-
let aggr_expr = normalize_cols(aggr_expr, &self.plan)?;
874+
let mut group_expr: Vec<Expr> = vec![];
875+
normalize_cols(group_exprs, &self.plan)?
876+
.iter()
877+
.for_each(|e| {
878+
if !group_expr.contains(e) {
879+
group_expr.push(e.clone());
880+
}
881+
});
882+
let aggr_expr = normalize_cols(aggr_exprs, &self.plan)?;
876883
let all_expr = group_expr.iter().chain(aggr_expr.iter());
877884
validate_unique_names("Aggregations", all_expr.clone(), self.plan.schema())?;
878885
let aggr_schema = DFSchema::new_with_metadata(

datafusion/core/src/sql/planner.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,6 +3279,17 @@ mod tests {
32793279
quick_test(sql, expected);
32803280
}
32813281

3282+
#[test]
3283+
fn select_aggregate_with_group_by_with_two_same_exprs() {
3284+
let sql = "SELECT first_name as name, first_name as nickname, 1 as first_num, 1 as second_num
3285+
FROM person
3286+
GROUP BY 1, 2, 3, 4";
3287+
let expected = "Projection: #person.first_name AS name, #person.first_name AS nickname, #Int64(1) AS first_num, #Int64(1) AS second_num\
3288+
\n Aggregate: groupBy=[[#person.first_name, Int64(1)]], aggr=[[]]\
3289+
\n TableScan: person projection=None";
3290+
quick_test(sql, expected);
3291+
}
3292+
32823293
#[test]
32833294
fn select_binary_expr() {
32843295
let sql = "SELECT age + salary from person";

0 commit comments

Comments
 (0)