use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::Columnar;
use crate::ExecutionCtx;
use crate::aggregate_fn::AggregateDTypes;
use crate::aggregate_fn::AggregateFnRef;
use crate::aggregate_fn::AggregateFnVTable;
use crate::aggregate_fn::NumericalAggregateOpts;
use crate::aggregate_fn::fns::is_constant::IsConstant;
use crate::aggregate_fn::fns::is_sorted::IsSorted;
use crate::aggregate_fn::fns::max::Max;
use crate::aggregate_fn::fns::min::Min;
use crate::aggregate_fn::fns::min_max::MinMax;
use crate::aggregate_fn::fns::min_max::MinMaxResult;
use crate::aggregate_fn::fns::min_max::min_max;
use crate::aggregate_fn::fns::sum::Sum;
use crate::aggregate_fn::kernels::DynAggregateKernel;
use crate::arrays::Widen;
use crate::arrays::widen::WidenArraySlotsExt;
use crate::dtype::DType;
use crate::expr::stats::Stat;
use crate::scalar::Scalar;
#[derive(Debug)]
pub(crate) struct WidenArrayAggregate;
impl DynAggregateKernel for WidenArrayAggregate {
fn aggregate(
&self,
aggregate_fn: &AggregateFnRef,
batch: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<Scalar>> {
let Some(widen) = batch.as_opt::<Widen>() else {
return Ok(None);
};
let values = widen.values();
let logical_dtype = batch.dtype();
if logical_dtype.is_decimal() {
return aggregate_decimal(aggregate_fn, values, logical_dtype, ctx);
}
if !logical_dtype.is_int() {
return Ok(None);
}
if aggregate_fn.is::<Sum>() && !is_exact_sum(aggregate_fn, batch, values) {
return Ok(None);
}
let partial = aggregate_values(aggregate_fn, values, logical_dtype, ctx)?;
Ok(Some(partial))
}
}
fn aggregate_decimal(
aggregate_fn: &AggregateFnRef,
values: &ArrayRef,
logical_dtype: &DType,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<Scalar>> {
if let Some(options) = aggregate_fn.as_opt::<Sum>() {
if Stat::from_aggregate_fn(aggregate_fn) != Some(Stat::Sum) {
return Ok(None);
}
let partial = decimal_sum_partial(values, options, logical_dtype, ctx)?;
return Ok(Some(partial));
}
if !(aggregate_fn.is::<MinMax>()
|| aggregate_fn.is::<Min>()
|| aggregate_fn.is::<Max>()
|| aggregate_fn.is::<IsConstant>()
|| aggregate_fn.is::<IsSorted>())
{
return Ok(None);
}
let partial = aggregate_values(aggregate_fn, values, logical_dtype, ctx)?;
Ok(Some(partial))
}
fn aggregate_values(
aggregate_fn: &AggregateFnRef,
values: &ArrayRef,
logical_dtype: &DType,
ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
let partial_dtype = aggregate_fn.state_dtype(logical_dtype).vortex_expect(
"the served aggregate accepts the logical dtype when it accepts the narrower values",
);
if let Some(options) = aggregate_fn.as_opt::<MinMax>() {
return min_max_partial(values, *options, logical_dtype, partial_dtype, ctx);
}
let mut accumulator = aggregate_fn.accumulator(values.dtype())?;
accumulator.accumulate(values, ctx)?;
accumulator.flush()?.cast(&partial_dtype)
}
fn decimal_sum_partial(
values: &ArrayRef,
options: &NumericalAggregateOpts,
logical_dtype: &DType,
ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
let dtypes = AggregateDTypes::try_new(&Sum, options, logical_dtype.clone())?;
let args = dtypes.args(options);
let values = values.clone().execute::<Columnar>(ctx)?;
let mut partial = Sum.empty_partial(args)?;
Sum.accumulate(args, &mut partial, &values, ctx)?;
Sum.to_scalar(args, &partial)
}
fn min_max_partial(
values: &ArrayRef,
options: NumericalAggregateOpts,
logical_dtype: &DType,
partial_dtype: DType,
ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
let Some(MinMaxResult { min, max }) = min_max(values, ctx, options)? else {
return Ok(Scalar::null(partial_dtype));
};
let element_dtype = logical_dtype.as_nonnullable();
let extrema = vec![min.cast(&element_dtype)?, max.cast(&element_dtype)?];
Ok(Scalar::struct_(partial_dtype, extrema))
}
fn is_exact_sum(aggregate_fn: &AggregateFnRef, batch: &ArrayRef, values: &ArrayRef) -> bool {
let DType::Primitive(values_ptype, _) = values.dtype() else {
return false;
};
let max_len = 1u64 << (64 - values_ptype.bit_width());
Stat::from_aggregate_fn(aggregate_fn) == Some(Stat::Sum) && batch.len() as u64 <= max_len
}