pub(crate) mod aggregate;
mod between;
mod cast;
mod compare;
mod fill_null;
mod filter;
mod mask;
pub(crate) mod numeric;
pub(crate) mod rules;
mod slice;
mod take;
mod zip;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::IntoArray;
use crate::array::ArrayView;
use crate::arrays::ConstantArray;
use crate::arrays::Widen;
use crate::arrays::WidenArray;
use crate::arrays::widen::WidenArraySlotsExt;
use crate::arrays::widen::encode::smallest_integer_ptype;
use crate::builtins::ArrayBuiltins;
use crate::dtype::DType;
use crate::dtype::PType;
use crate::scalar::PValue;
use crate::scalar::Scalar;
pub(super) fn wrap_values(values: ArrayRef, dtype: DType) -> VortexResult<ArrayRef> {
if values.dtype() == &dtype {
return Ok(values);
}
Ok(WidenArray::try_new(values, dtype)?.into_array())
}
pub(super) fn ptype_holding_values_and_scalar(
array: ArrayView<'_, Widen>,
scalar: &Scalar,
) -> Option<PType> {
let (DType::Primitive(values_ptype, _), DType::Primitive(logical_ptype, _)) =
(array.values().dtype(), array.dtype())
else {
return None;
};
if !scalar.dtype().eq_ignore_nullability(array.dtype()) {
return None;
}
if scalar.is_null() {
return Some(*values_ptype);
}
let value = scalar
.as_primitive()
.pvalue()
.and_then(PValue::as_i128)
.vortex_expect("the non-null scalar has the Widen's integer dtype");
let scalar_ptype = smallest_integer_ptype(value..=value, logical_ptype.is_signed_int())?;
if scalar_ptype.byte_width() > values_ptype.byte_width() {
Some(scalar_ptype)
} else {
Some(*values_ptype)
}
}
pub(super) fn cast_keeping_nullability(array: &ArrayRef, ptype: PType) -> VortexResult<ArrayRef> {
array.cast(DType::Primitive(ptype, array.dtype().nullability()))
}
pub(super) fn constant_fits_values(constant: &Scalar, values: &DType) -> Option<bool> {
match (values, constant.dtype()) {
(DType::Decimal(values_dtype, _), DType::Decimal(constant_dtype, _))
if values_dtype.scale() == constant_dtype.scale() =>
{
let value = constant.as_decimal().decimal_value()?;
Some(value.fits_in_precision(*values_dtype))
}
(DType::Primitive(values_ptype, _), DType::Primitive(constant_ptype, _))
if values_ptype.is_int()
&& constant_ptype.is_int()
&& values_ptype.is_signed_int() == constant_ptype.is_signed_int() =>
{
let value = constant.as_primitive().pvalue()?;
Some(
PValue::min_value(*values_ptype) <= value
&& value <= PValue::max_value(*values_ptype),
)
}
_ => None,
}
}
pub(super) fn ptype_holding_both_values(
lhs: ArrayView<'_, Widen>,
rhs: ArrayView<'_, Widen>,
) -> Option<PType> {
let (DType::Primitive(lhs_ptype, _), DType::Primitive(rhs_ptype, _)) =
(lhs.values().dtype(), rhs.values().dtype())
else {
return None;
};
if lhs_ptype.is_signed_int() {
Some(lhs_ptype.max_signed_ptype(*rhs_ptype))
} else {
Some(lhs_ptype.max_unsigned_ptype(*rhs_ptype))
}
}
pub(super) fn constant_in_values_dtype(
constant: &Scalar,
values: &ArrayRef,
len: usize,
) -> VortexResult<ArrayRef> {
let dtype = values
.dtype()
.with_nullability(constant.dtype().nullability());
Ok(ConstantArray::new(constant.cast(&dtype)?, len).into_array())
}