Skip to content

Commit 0c7eb0d

Browse files
authored
Fix convert_to_state bug in GroupsAccumulatorAdapter (#12834)
* Fix convert_to_state bug * fmt
1 parent ffe73fe commit 0c7eb0d

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ async fn test_basic_string_aggr_group_by_single_int64() {
254254
let fuzzer = builder
255255
.data_gen_config(data_gen_config)
256256
.data_gen_rounds(8)
257-
// FIXME: Encounter error in min/max
258-
// ArrowError(InvalidArgumentError("number of columns(1) must match number of fields(2) in schema"))
259-
// .add_sql("SELECT b, max(a) FROM fuzz_table GROUP BY b")
260-
// .add_sql("SELECT b, min(a) FROM fuzz_table GROUP BY b")
257+
.add_sql("SELECT b, max(a) FROM fuzz_table GROUP BY b")
258+
.add_sql("SELECT b, min(a) FROM fuzz_table GROUP BY b")
261259
.add_sql("SELECT b, count(a) FROM fuzz_table GROUP BY b")
262260
.add_sql("SELECT b, count(distinct a) FROM fuzz_table GROUP BY b")
263261
.table_name("fuzz_table")
@@ -291,10 +289,8 @@ async fn test_basic_string_aggr_group_by_single_string() {
291289
let fuzzer = builder
292290
.data_gen_config(data_gen_config)
293291
.data_gen_rounds(16)
294-
// FIXME: Encounter error in min/max
295-
// ArrowError(InvalidArgumentError("number of columns(1) must match number of fields(2) in schema"))
296-
// .add_sql("SELECT b, max(a) FROM fuzz_table GROUP BY b")
297-
// .add_sql("SELECT b, min(a) FROM fuzz_table GROUP BY b")
292+
.add_sql("SELECT b, max(a) FROM fuzz_table GROUP BY b")
293+
.add_sql("SELECT b, min(a) FROM fuzz_table GROUP BY b")
298294
.add_sql("SELECT b, count(a) FROM fuzz_table GROUP BY b")
299295
.add_sql("SELECT b, count(distinct a) FROM fuzz_table GROUP BY b")
300296
.table_name("fuzz_table")
@@ -329,10 +325,8 @@ async fn test_basic_string_aggr_group_by_mixed_string_int64() {
329325
let fuzzer = builder
330326
.data_gen_config(data_gen_config)
331327
.data_gen_rounds(16)
332-
// FIXME: Encounter error in min/max
333-
// ArrowError(InvalidArgumentError("number of columns(1) must match number of fields(2) in schema"))
334-
// .add_sql("SELECT b, c, max(a) FROM fuzz_table GROUP BY b, c")
335-
// .add_sql("SELECT b, c, min(a) FROM fuzz_table GROUP BY b, c")
328+
.add_sql("SELECT b, c, max(a) FROM fuzz_table GROUP BY b, c")
329+
.add_sql("SELECT b, c, min(a) FROM fuzz_table GROUP BY b, c")
336330
.add_sql("SELECT b, c, count(a) FROM fuzz_table GROUP BY b, c")
337331
.add_sql("SELECT b, c, count(distinct a) FROM fuzz_table GROUP BY b, c")
338332
.table_name("fuzz_table")

datafusion/functions-aggregate-common/src/aggregate/groups_accumulator.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod bool_op;
2323
pub mod nulls;
2424
pub mod prim_op;
2525

26+
use arrow::array::new_empty_array;
2627
use arrow::{
2728
array::{ArrayRef, AsArray, BooleanArray, PrimitiveArray},
2829
compute,
@@ -405,6 +406,18 @@ impl GroupsAccumulator for GroupsAccumulatorAdapter {
405406
) -> Result<Vec<ArrayRef>> {
406407
let num_rows = values[0].len();
407408

409+
// If there are no rows, return empty arrays
410+
if num_rows == 0 {
411+
// create empty accumulator to get the state types
412+
let empty_state = (self.factory)()?.state()?;
413+
let empty_arrays = empty_state
414+
.into_iter()
415+
.map(|state_val| new_empty_array(&state_val.data_type()))
416+
.collect::<Vec<_>>();
417+
418+
return Ok(empty_arrays);
419+
}
420+
408421
// Each row has its respective group
409422
let mut results = vec![];
410423
for row_idx in 0..num_rows {

0 commit comments

Comments
 (0)