use vortex_array::ArrayRef;
use vortex_array::Canonical;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray as _;
use vortex_array::aggregate_fn::AggregateFnRef;
use vortex_array::aggregate_fn::fns::is_sorted::IsSorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_strict_sorted;
use vortex_array::aggregate_fn::kernels::DynAggregateKernel;
use vortex_array::scalar::Scalar;
use vortex_error::VortexResult;
use crate::RunEnd;
use crate::array::RunEndArrayExt;
#[derive(Debug)]
pub(crate) struct RunEndIsSortedKernel;
impl DynAggregateKernel for RunEndIsSortedKernel {
fn aggregate(
&self,
aggregate_fn: &AggregateFnRef,
batch: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<Scalar>> {
let Some(options) = aggregate_fn.as_opt::<IsSorted>() else {
return Ok(None);
};
let Some(array) = batch.as_opt::<RunEnd>() else {
return Ok(None);
};
let result = if options.strict {
is_strict_sorted(
&array
.array()
.clone()
.execute::<Canonical>(ctx)?
.into_array(),
ctx,
)?
} else {
is_sorted(array.values(), ctx)?
};
Ok(Some(IsSorted::make_partial(
batch,
result,
options.strict,
ctx,
)?))
}
}