-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add input_nullable to UDAF args StateField/AccumulatorArgs This follows how it done for input_type and only provide a single value. But might need to be changed into a Vec in the future. This is need when we are moving `arrag_agg` to udaf where one of the states nullability will depend on the nullability of the input. * Make ArragAgg (not ordered or distinct) into a UDAF * Add roundtrip_expr_api test case * Address PR comments * Propegate input nullability for aggregates * Remove from accumulator args * first draft Signed-off-by: jayzhan211 <[email protected]> * cleanup Signed-off-by: jayzhan211 <[email protected]> * fix test Signed-off-by: jayzhan211 <[email protected]> * distinct Signed-off-by: jayzhan211 <[email protected]> * fix Signed-off-by: jayzhan211 <[email protected]> * address comment Signed-off-by: jayzhan211 <[email protected]> --------- Signed-off-by: jayzhan211 <[email protected]> Co-authored-by: Emil Ejbyfeldt <[email protected]>
- Loading branch information
1 parent
723a595
commit 12d82c4
Showing
16 changed files
with
328 additions
and
892 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//! `ARRAY_AGG` aggregate implementation: [`ArrayAgg`] | ||
use arrow::array::{Array, ArrayRef, AsArray}; | ||
use arrow::datatypes::DataType; | ||
use arrow_schema::Field; | ||
|
||
use datafusion_common::cast::as_list_array; | ||
use datafusion_common::utils::array_into_list_array_nullable; | ||
use datafusion_common::ScalarValue; | ||
use datafusion_common::{internal_err, Result}; | ||
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; | ||
use datafusion_expr::utils::format_state_name; | ||
use datafusion_expr::AggregateUDFImpl; | ||
use datafusion_expr::{Accumulator, Signature, Volatility}; | ||
use std::collections::HashSet; | ||
use std::sync::Arc; | ||
|
||
make_udaf_expr_and_func!( | ||
ArrayAgg, | ||
array_agg, | ||
expression, | ||
"input values, including nulls, concatenated into an array", | ||
array_agg_udaf | ||
); | ||
|
||
#[derive(Debug)] | ||
/// ARRAY_AGG aggregate expression | ||
pub struct ArrayAgg { | ||
signature: Signature, | ||
alias: Vec<String>, | ||
} | ||
|
||
impl Default for ArrayAgg { | ||
fn default() -> Self { | ||
Self { | ||
signature: Signature::any(1, Volatility::Immutable), | ||
alias: vec!["array_agg".to_string()], | ||
} | ||
} | ||
} | ||
|
||
impl AggregateUDFImpl for ArrayAgg { | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
// TODO: change name to lowercase | ||
fn name(&self) -> &str { | ||
"ARRAY_AGG" | ||
} | ||
|
||
fn aliases(&self) -> &[String] { | ||
&self.alias | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
Ok(DataType::List(Arc::new(Field::new( | ||
"item", | ||
arg_types[0].clone(), | ||
true, | ||
)))) | ||
} | ||
|
||
fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<Field>> { | ||
if args.is_distinct { | ||
return Ok(vec![Field::new_list( | ||
format_state_name(args.name, "distinct_array_agg"), | ||
Field::new("item", args.input_type.clone(), true), | ||
true, | ||
)]); | ||
} | ||
|
||
Ok(vec![Field::new_list( | ||
format_state_name(args.name, "array_agg"), | ||
Field::new("item", args.input_type.clone(), true), | ||
true, | ||
)]) | ||
} | ||
|
||
fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> { | ||
if acc_args.is_distinct { | ||
return Ok(Box::new(DistinctArrayAggAccumulator::try_new( | ||
acc_args.input_type, | ||
)?)); | ||
} | ||
|
||
Ok(Box::new(ArrayAggAccumulator::try_new(acc_args.input_type)?)) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ArrayAggAccumulator { | ||
values: Vec<ArrayRef>, | ||
datatype: DataType, | ||
} | ||
|
||
impl ArrayAggAccumulator { | ||
/// new array_agg accumulator based on given item data type | ||
pub fn try_new(datatype: &DataType) -> Result<Self> { | ||
Ok(Self { | ||
values: vec![], | ||
datatype: datatype.clone(), | ||
}) | ||
} | ||
} | ||
|
||
impl Accumulator for ArrayAggAccumulator { | ||
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> { | ||
// Append value like Int64Array(1,2,3) | ||
if values.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
if values.len() != 1 { | ||
return internal_err!("expects single batch"); | ||
} | ||
|
||
let val = Arc::clone(&values[0]); | ||
if val.len() > 0 { | ||
self.values.push(val); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> { | ||
// Append value like ListArray(Int64Array(1,2,3), Int64Array(4,5,6)) | ||
if states.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
if states.len() != 1 { | ||
return internal_err!("expects single state"); | ||
} | ||
|
||
let list_arr = as_list_array(&states[0])?; | ||
for arr in list_arr.iter().flatten() { | ||
self.values.push(arr); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn state(&mut self) -> Result<Vec<ScalarValue>> { | ||
Ok(vec![self.evaluate()?]) | ||
} | ||
|
||
fn evaluate(&mut self) -> Result<ScalarValue> { | ||
// Transform Vec<ListArr> to ListArr | ||
let element_arrays: Vec<&dyn Array> = | ||
self.values.iter().map(|a| a.as_ref()).collect(); | ||
|
||
if element_arrays.is_empty() { | ||
return Ok(ScalarValue::new_null_list(self.datatype.clone(), true, 1)); | ||
} | ||
|
||
let concated_array = arrow::compute::concat(&element_arrays)?; | ||
let list_array = array_into_list_array_nullable(concated_array); | ||
|
||
Ok(ScalarValue::List(Arc::new(list_array))) | ||
} | ||
|
||
fn size(&self) -> usize { | ||
std::mem::size_of_val(self) | ||
+ (std::mem::size_of::<ArrayRef>() * self.values.capacity()) | ||
+ self | ||
.values | ||
.iter() | ||
.map(|arr| arr.get_array_memory_size()) | ||
.sum::<usize>() | ||
+ self.datatype.size() | ||
- std::mem::size_of_val(&self.datatype) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct DistinctArrayAggAccumulator { | ||
values: HashSet<ScalarValue>, | ||
datatype: DataType, | ||
} | ||
|
||
impl DistinctArrayAggAccumulator { | ||
pub fn try_new(datatype: &DataType) -> Result<Self> { | ||
Ok(Self { | ||
values: HashSet::new(), | ||
datatype: datatype.clone(), | ||
}) | ||
} | ||
} | ||
|
||
impl Accumulator for DistinctArrayAggAccumulator { | ||
fn state(&mut self) -> Result<Vec<ScalarValue>> { | ||
Ok(vec![self.evaluate()?]) | ||
} | ||
|
||
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> { | ||
if values.len() != 1 { | ||
return internal_err!("expects single batch"); | ||
} | ||
|
||
let array = &values[0]; | ||
|
||
for i in 0..array.len() { | ||
let scalar = ScalarValue::try_from_array(&array, i)?; | ||
self.values.insert(scalar); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> { | ||
if states.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
if states.len() != 1 { | ||
return internal_err!("expects single state"); | ||
} | ||
|
||
states[0] | ||
.as_list::<i32>() | ||
.iter() | ||
.flatten() | ||
.try_for_each(|val| self.update_batch(&[val])) | ||
} | ||
|
||
fn evaluate(&mut self) -> Result<ScalarValue> { | ||
let values: Vec<ScalarValue> = self.values.iter().cloned().collect(); | ||
if values.is_empty() { | ||
return Ok(ScalarValue::new_null_list(self.datatype.clone(), true, 1)); | ||
} | ||
let arr = ScalarValue::new_list(&values, &self.datatype, true); | ||
Ok(ScalarValue::List(arr)) | ||
} | ||
|
||
fn size(&self) -> usize { | ||
std::mem::size_of_val(self) + ScalarValue::size_of_hashset(&self.values) | ||
- std::mem::size_of_val(&self.values) | ||
+ self.datatype.size() | ||
- std::mem::size_of_val(&self.datatype) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.