Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1600,10 +1600,14 @@ impl ScalarValue {
let mut valid = BooleanBufferBuilder::new(0);
let mut flat_len = 0i32;
for scalar in scalars {
if let ScalarValue::List(values, _) = scalar {
if let ScalarValue::List(values, field) = scalar {
match values {
Some(values) => {
let element_array = ScalarValue::iter_to_array(values)?;
let element_array = if !values.is_empty() {
ScalarValue::iter_to_array(values)?
} else {
arrow::array::new_empty_array(field.data_type())
};

// Add new offset index
flat_len += element_array.len() as i32;
Expand Down
13 changes: 11 additions & 2 deletions datafusion/core/src/physical_plan/aggregates/row_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,17 @@ fn create_batch_from_map(
results[i].push(acc.evaluate(&state_accessor).unwrap());
}
}
for scalars in results {
columns.push(ScalarValue::iter_to_array(scalars)?);
// We skip over the first `columns.len()` elements.
//
// This shouldn't panic if the `output_schema` has enough fields.
let remaining_field_iterator = output_schema.fields()[columns.len()..].iter();

for (scalars, field) in results.into_iter().zip(remaining_field_iterator) {
if !scalars.is_empty() {
columns.push(ScalarValue::iter_to_array(scalars)?);
} else {
columns.push(arrow::array::new_empty_array(field.data_type()))
}
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions datafusion/core/tests/sql/aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2314,3 +2314,37 @@ async fn aggregate_with_alias() -> Result<()> {
);
Ok(())
}

#[tokio::test]
async fn array_agg_zero() -> Result<()> {
let ctx = SessionContext::new();
// 2 different aggregate functions: avg and sum(distinct)
let sql = "SELECT ARRAY_AGG([]);";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+------------------------+",
"| ARRAYAGG(List([NULL])) |",
"+------------------------+",
"| [] |",
"+------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn array_agg_one() -> Result<()> {
let ctx = SessionContext::new();
// 2 different aggregate functions: avg and sum(distinct)
let sql = "SELECT ARRAY_AGG([1]);";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+",
"| ARRAYAGG(List([1])) |",
"+---------------------+",
"| [[1]] |",
"+---------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}