use std::ops::RangeInclusive;
use itertools::Itertools;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::Canonical;
use crate::Columnar;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::aggregate_fn::NumericalAggregateOpts;
use crate::aggregate_fn::fns::min_max::min_max;
use crate::array::ArrayView;
use crate::arrays::Constant;
use crate::arrays::ConstantArray;
use crate::arrays::ScalarFn;
use crate::arrays::Widen;
use crate::arrays::scalar_fn::ExactScalarFn;
use crate::arrays::scalar_fn::ScalarFnArrayExt;
use crate::arrays::scalar_fn::ScalarFnArrayView;
use crate::arrays::widen::WidenArraySlotsExt;
use crate::arrays::widen::compute::cast_keeping_nullability;
use crate::arrays::widen::compute::wrap_values;
use crate::arrays::widen::encode::integer_extrema;
use crate::arrays::widen::encode::smallest_integer_ptype;
use crate::dtype::DType;
use crate::dtype::PType;
use crate::kernel::ExecuteParentKernel;
use crate::scalar::NumericOperator;
use crate::scalar::Scalar;
use crate::scalar_fn::fns::binary::Binary;
use crate::scalar_fn::fns::binary::execute_numeric;
use crate::scalar_fn::fns::operators::Operator;
#[derive(Debug)]
pub(crate) struct WidenNumericKernel;
impl ExecuteParentKernel<Widen> for WidenNumericKernel {
type Parent = ExactScalarFn<Binary>;
fn execute_parent(
&self,
array: ArrayView<'_, Widen>,
parent: ScalarFnArrayView<'_, Binary>,
child_idx: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
let operator = match parent.options {
Operator::Add => NumericOperator::Add,
Operator::Sub => NumericOperator::Sub,
Operator::Mul => NumericOperator::Mul,
Operator::Div => NumericOperator::Div,
_ => return Ok(None),
};
let DType::Primitive(logical_ptype, _) = array.dtype() else {
return Ok(None);
};
let binary = parent
.as_opt::<ScalarFn>()
.vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
let other = match child_idx {
0 => binary.get_child(1),
1 => binary.get_child(0),
_ => return Ok(None),
};
let other_values = if let Some(other) = other.as_opt::<Widen>() {
other.values().clone()
} else if other.is::<Constant>() {
other.clone()
} else {
return Ok(None);
};
let dtype = parent.dtype();
if parent.is_empty() {
return Ok(Some(Canonical::empty(dtype).into_array()));
}
let values = array
.values()
.clone()
.execute::<Columnar>(ctx)?
.into_array();
let other_values = other_values.execute::<Columnar>(ctx)?.into_array();
let (lhs, rhs) = if child_idx == 0 {
(values, other_values)
} else {
(other_values, values)
};
let (Some(lhs_interval), Some(rhs_interval)) = (
valid_value_interval(&lhs, ctx)?,
valid_value_interval(&rhs, ctx)?,
) else {
let null = Scalar::null(dtype.clone());
return Ok(Some(ConstantArray::new(null, parent.len()).into_array()));
};
let ptype =
smallest_arithmetic_ptype(&lhs_interval, &rhs_interval, operator, *logical_ptype);
let lhs = cast_keeping_nullability(&lhs, ptype)?;
let rhs = cast_keeping_nullability(&rhs, ptype)?;
let result = execute_numeric(&lhs, &rhs, operator, ctx)?;
wrap_values(result, dtype.clone()).map(Some)
}
}
fn smallest_arithmetic_ptype(
lhs: &RangeInclusive<i128>,
rhs: &RangeInclusive<i128>,
operator: NumericOperator,
logical: PType,
) -> PType {
result_interval(lhs, rhs, operator)
.and_then(|result| {
let min = (*lhs.start()).min(*rhs.start()).min(*result.start());
let max = (*lhs.end()).max(*rhs.end()).max(*result.end());
smallest_integer_ptype(min..=max, logical.is_signed_int())
})
.filter(|ptype| ptype.byte_width() < logical.byte_width())
.unwrap_or(logical)
}
fn valid_value_interval(
array: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<RangeInclusive<i128>>> {
let Some(bounds) = min_max(array, ctx, NumericalAggregateOpts::default())? else {
return Ok(None);
};
Ok(Some(integer_extrema(bounds)))
}
pub(crate) fn result_interval(
lhs: &RangeInclusive<i128>,
rhs: &RangeInclusive<i128>,
operator: NumericOperator,
) -> Option<RangeInclusive<i128>> {
let (lhs_min, lhs_max) = (*lhs.start(), *lhs.end());
let (rhs_min, rhs_max) = (*rhs.start(), *rhs.end());
let (min, max) = match operator {
NumericOperator::Add => (lhs_min.checked_add(rhs_min)?, lhs_max.checked_add(rhs_max)?),
NumericOperator::Sub => (lhs_min.checked_sub(rhs_max)?, lhs_max.checked_sub(rhs_min)?),
NumericOperator::Mul => [
lhs_min.checked_mul(rhs_min)?,
lhs_min.checked_mul(rhs_max)?,
lhs_max.checked_mul(rhs_min)?,
lhs_max.checked_mul(rhs_max)?,
]
.into_iter()
.minmax()
.into_option()?,
NumericOperator::Div => [rhs_min, rhs_max, -1, 1]
.into_iter()
.filter(|divisor| *divisor != 0 && rhs.contains(divisor))
.map(|divisor| Some([lhs_min.checked_div(divisor)?, lhs_max.checked_div(divisor)?]))
.collect::<Option<Vec<_>>>()?
.into_iter()
.flatten()
.minmax()
.into_option()?,
};
Some(min..=max)
}