Skip to content

Commit 8d01437

Browse files
committed
Fix grouping sets behavior when data contains nulls
1 parent 3bd41bc commit 8d01437

File tree

5 files changed

+256
-147
lines changed

5 files changed

+256
-147
lines changed

datafusion/core/src/physical_planner.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,6 @@ impl DefaultPhysicalPlanner {
707707
physical_input_schema.clone(),
708708
)?);
709709

710-
// update group column indices based on partial aggregate plan evaluation
711-
let final_group: Vec<Arc<dyn PhysicalExpr>> =
712-
initial_aggr.output_group_expr();
713-
714710
let can_repartition = !groups.is_empty()
715711
&& session_state.config().target_partitions() > 1
716712
&& session_state.config().repartition_aggregations();
@@ -731,13 +727,7 @@ impl DefaultPhysicalPlanner {
731727
AggregateMode::Final
732728
};
733729

734-
let final_grouping_set = PhysicalGroupBy::new_single(
735-
final_group
736-
.iter()
737-
.enumerate()
738-
.map(|(i, expr)| (expr.clone(), groups.expr()[i].1.clone()))
739-
.collect(),
740-
);
730+
let final_grouping_set = initial_aggr.group_expr().as_final();
741731

742732
Arc::new(AggregateExec::try_new(
743733
next_partition_mode,
@@ -2061,7 +2051,7 @@ mod tests {
20612051
&session_state,
20622052
);
20632053

2064-
let expected = r#"Ok(PhysicalGroupBy { expr: [(Column { name: "c1", index: 0 }, "c1"), (Column { name: "c2", index: 1 }, "c2"), (Column { name: "c3", index: 2 }, "c3")], null_expr: [(Literal { value: Utf8(NULL) }, "c1"), (Literal { value: Int64(NULL) }, "c2"), (Literal { value: Int64(NULL) }, "c3")], groups: [[false, false, false], [true, false, false], [false, true, false], [false, false, true], [true, true, false], [true, false, true], [false, true, true], [true, true, true]] })"#;
2054+
let expected = r#"Ok(PhysicalGroupBy { expr: [(Column { name: "c1", index: 0 }, "c1"), (Column { name: "c2", index: 1 }, "c2"), (Column { name: "c3", index: 2 }, "c3")], null_expr: [(Literal { value: Utf8(NULL) }, "c1"), (Literal { value: Int64(NULL) }, "c2"), (Literal { value: Int64(NULL) }, "c3")], groups: [[false, false, false], [true, false, false], [false, true, false], [false, false, true], [true, true, false], [true, false, true], [false, true, true], [true, true, true]], num_internal_exprs: 1 })"#;
20652055

20662056
assert_eq!(format!("{cube:?}"), expected);
20672057

@@ -2088,7 +2078,7 @@ mod tests {
20882078
&session_state,
20892079
);
20902080

2091-
let expected = r#"Ok(PhysicalGroupBy { expr: [(Column { name: "c1", index: 0 }, "c1"), (Column { name: "c2", index: 1 }, "c2"), (Column { name: "c3", index: 2 }, "c3")], null_expr: [(Literal { value: Utf8(NULL) }, "c1"), (Literal { value: Int64(NULL) }, "c2"), (Literal { value: Int64(NULL) }, "c3")], groups: [[true, true, true], [false, true, true], [false, false, true], [false, false, false]] })"#;
2081+
let expected = r#"Ok(PhysicalGroupBy { expr: [(Column { name: "c1", index: 0 }, "c1"), (Column { name: "c2", index: 1 }, "c2"), (Column { name: "c3", index: 2 }, "c3")], null_expr: [(Literal { value: Utf8(NULL) }, "c1"), (Literal { value: Int64(NULL) }, "c2"), (Literal { value: Int64(NULL) }, "c3")], groups: [[true, true, true], [false, true, true], [false, false, true], [false, false, false]], num_internal_exprs: 1 })"#;
20922082

20932083
assert_eq!(format!("{rollup:?}"), expected);
20942084

datafusion/physical-optimizer/src/combine_partial_final_agg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn can_combine(final_agg: GroupExprsRef, partial_agg: GroupExprsRef) -> bool {
135135

136136
// Compare output expressions of the partial, and input expressions of the final operator.
137137
physical_exprs_equal(
138-
&input_group_by.output_exprs(),
138+
&input_group_by.output_exprs(&AggregateMode::Partial),
139139
&final_group_by.input_exprs(),
140140
) && input_group_by.groups() == final_group_by.groups()
141141
&& input_group_by.null_expr().len() == final_group_by.null_expr().len()

0 commit comments

Comments
 (0)