use arrow::datatypes::{DataType, Field};
use std::any::Any;
use std::fmt::Debug;
use std::sync::Arc;
use arrow::array::{Array, ArrayRef};
use std::collections::HashSet;
use crate::aggregate::utils::down_cast_any_ref;
use crate::expressions::format_state_name;
use crate::{AggregateExpr, PhysicalExpr};
use datafusion_common::{internal_err, DataFusionError, Result, ScalarValue};
use datafusion_expr::Accumulator;
#[derive(Debug)]
pub struct DistinctArrayAgg {
name: String,
input_data_type: DataType,
expr: Arc<dyn PhysicalExpr>,
}
impl DistinctArrayAgg {
pub fn new(
expr: Arc<dyn PhysicalExpr>,
name: impl Into<String>,
input_data_type: DataType,
) -> Self {
let name = name.into();
Self {
name,
expr,
input_data_type,
}
}
}
impl AggregateExpr for DistinctArrayAgg {
fn as_any(&self) -> &dyn Any {
self
}
fn field(&self) -> Result<Field> {
Ok(Field::new_list(
&self.name,
Field::new("item", self.input_data_type.clone(), true),
false,
))
}
fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(DistinctArrayAggAccumulator::try_new(
&self.input_data_type,
)?))
}
fn state_fields(&self) -> Result<Vec<Field>> {
Ok(vec![Field::new_list(
format_state_name(&self.name, "distinct_array_agg"),
Field::new("item", self.input_data_type.clone(), true),
false,
)])
}
fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> {
vec![self.expr.clone()]
}
fn name(&self) -> &str {
&self.name
}
}
impl PartialEq<dyn Any> for DistinctArrayAgg {
fn eq(&self, other: &dyn Any) -> bool {
down_cast_any_ref(other)
.downcast_ref::<Self>()
.map(|x| {
self.name == x.name
&& self.input_data_type == x.input_data_type
&& self.expr.eq(&x.expr)
})
.unwrap_or(false)
}
}
#[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(&self) -> Result<Vec<ScalarValue>> {
Ok(vec![ScalarValue::new_list(
Some(self.values.clone().into_iter().collect()),
self.datatype.clone(),
)])
}
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
assert_eq!(values.len(), 1, "batch input should only include 1 column!");
let array = &values[0];
(0..array.len()).try_for_each(|i| {
if !array.is_null(i) {
self.values.insert(ScalarValue::try_from_array(array, i)?);
}
Ok(())
})
}
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
if states.is_empty() {
return Ok(());
}
assert_eq!(
states.len(),
1,
"array_agg_distinct states must contain single array"
);
let array = &states[0];
(0..array.len()).try_for_each(|i| {
let scalar = ScalarValue::try_from_array(array, i)?;
if let ScalarValue::List(Some(values), _) = scalar {
self.values.extend(values);
Ok(())
} else {
internal_err!("array_agg_distinct state must be list")
}
})?;
Ok(())
}
fn evaluate(&self) -> Result<ScalarValue> {
Ok(ScalarValue::new_list(
Some(self.values.clone().into_iter().collect()),
self.datatype.clone(),
))
}
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)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregate::utils::get_accum_scalar_values_as_arrays;
use crate::expressions::col;
use crate::expressions::tests::aggregate;
use arrow::array::{ArrayRef, Int32Array};
use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
fn compare_list_contents(expected: ScalarValue, actual: ScalarValue) -> Result<()> {
match (expected, actual) {
(ScalarValue::List(Some(mut e), _), ScalarValue::List(Some(mut a), _)) => {
let cmp = |a: &ScalarValue, b: &ScalarValue| {
a.partial_cmp(b).expect("Can compare ScalarValues")
};
e.sort_by(cmp);
a.sort_by(cmp);
assert_eq!(e, a);
}
_ => {
return Err(DataFusionError::Internal(
"Expected scalar lists as inputs".to_string(),
));
}
}
Ok(())
}
fn check_distinct_array_agg(
input: ArrayRef,
expected: ScalarValue,
datatype: DataType,
) -> Result<()> {
let schema = Schema::new(vec![Field::new("a", datatype.clone(), false)]);
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![input])?;
let agg = Arc::new(DistinctArrayAgg::new(
col("a", &schema)?,
"bla".to_string(),
datatype,
));
let actual = aggregate(&batch, agg)?;
compare_list_contents(expected, actual)
}
fn check_merge_distinct_array_agg(
input1: ArrayRef,
input2: ArrayRef,
expected: ScalarValue,
datatype: DataType,
) -> Result<()> {
let schema = Schema::new(vec![Field::new("a", datatype.clone(), false)]);
let agg = Arc::new(DistinctArrayAgg::new(
col("a", &schema)?,
"bla".to_string(),
datatype,
));
let mut accum1 = agg.create_accumulator()?;
let mut accum2 = agg.create_accumulator()?;
accum1.update_batch(&[input1])?;
accum2.update_batch(&[input2])?;
let state = get_accum_scalar_values_as_arrays(accum2.as_ref())?;
accum1.merge_batch(&state)?;
let actual = accum1.evaluate()?;
compare_list_contents(expected, actual)
}
#[test]
fn distinct_array_agg_i32() -> Result<()> {
let col: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 7, 4, 5, 2]));
let out = ScalarValue::new_list(
Some(vec![
ScalarValue::Int32(Some(1)),
ScalarValue::Int32(Some(2)),
ScalarValue::Int32(Some(7)),
ScalarValue::Int32(Some(4)),
ScalarValue::Int32(Some(5)),
]),
DataType::Int32,
);
check_distinct_array_agg(col, out, DataType::Int32)
}
#[test]
fn merge_distinct_array_agg_i32() -> Result<()> {
let col1: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 7, 4, 5, 2]));
let col2: ArrayRef = Arc::new(Int32Array::from(vec![1, 3, 7, 8, 4]));
let out = ScalarValue::new_list(
Some(vec![
ScalarValue::Int32(Some(1)),
ScalarValue::Int32(Some(2)),
ScalarValue::Int32(Some(3)),
ScalarValue::Int32(Some(4)),
ScalarValue::Int32(Some(5)),
ScalarValue::Int32(Some(7)),
ScalarValue::Int32(Some(8)),
]),
DataType::Int32,
);
check_merge_distinct_array_agg(col1, col2, out, DataType::Int32)
}
#[test]
fn distinct_array_agg_nested() -> Result<()> {
let l1 = ScalarValue::new_list(
Some(vec![
ScalarValue::new_list(
Some(vec![
ScalarValue::from(1i32),
ScalarValue::from(2i32),
ScalarValue::from(3i32),
]),
DataType::Int32,
),
ScalarValue::new_list(
Some(vec![ScalarValue::from(4i32), ScalarValue::from(5i32)]),
DataType::Int32,
),
]),
DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
);
let l2 = ScalarValue::new_list(
Some(vec![
ScalarValue::new_list(
Some(vec![ScalarValue::from(6i32)]),
DataType::Int32,
),
ScalarValue::new_list(
Some(vec![ScalarValue::from(7i32), ScalarValue::from(8i32)]),
DataType::Int32,
),
]),
DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
);
let l3 = ScalarValue::new_list(
Some(vec![ScalarValue::new_list(
Some(vec![ScalarValue::from(9i32)]),
DataType::Int32,
)]),
DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
);
let list = ScalarValue::new_list(
Some(vec![l1.clone(), l2.clone(), l3.clone()]),
DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
);
let array = ScalarValue::iter_to_array(vec![l1.clone(), l2, l3, l1]).unwrap();
check_distinct_array_agg(
array,
list,
DataType::List(Arc::new(Field::new_list(
"item",
Field::new("item", DataType::Int32, true),
true,
))),
)
}
#[test]
fn merge_distinct_array_agg_nested() -> Result<()> {
let l1 = ScalarValue::new_list(
Some(vec![
ScalarValue::new_list(
Some(vec![ScalarValue::from(1i32), ScalarValue::from(2i32)]),
DataType::Int32,
),
ScalarValue::new_list(
Some(vec![ScalarValue::from(3i32), ScalarValue::from(4i32)]),
DataType::Int32,
),
]),
DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
);
let l2 = ScalarValue::new_list(
Some(vec![ScalarValue::new_list(
Some(vec![ScalarValue::from(5i32)]),
DataType::Int32,
)]),
DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
);
let l3 = ScalarValue::new_list(
Some(vec![
ScalarValue::new_list(
Some(vec![ScalarValue::from(6i32), ScalarValue::from(7i32)]),
DataType::Int32,
),
ScalarValue::new_list(
Some(vec![ScalarValue::from(8i32)]),
DataType::Int32,
),
]),
DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
);
let expected = ScalarValue::new_list(
Some(vec![l1.clone(), l2.clone(), l3.clone()]),
DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
);
let input1 = ScalarValue::iter_to_array(vec![l1.clone(), l2]).unwrap();
let input2 = ScalarValue::iter_to_array(vec![l1, l3]).unwrap();
check_merge_distinct_array_agg(
input1,
input2,
expected,
DataType::List(Arc::new(Field::new_list(
"item",
Field::new("item", DataType::Int32, true),
true,
))),
)
}
}