use std::ops::RangeInclusive;
use vortex_buffer::Buffer;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_ensure_eq;
use vortex_mask::Mask;
use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::arrays::DecimalArray;
use crate::arrays::WidenArray;
use crate::arrays::decimal::DecimalArrayExt;
use crate::arrays::decimal::DecimalData;
use crate::arrays::decimal::DecimalDataParts;
use crate::arrays::decimal::copy_to_canonical_width;
use crate::arrays::decimal::valid_range;
use crate::buffer::BufferHandle;
use crate::dtype::DType;
use crate::dtype::DecimalDType;
use crate::dtype::NativeDecimalType;
use crate::dtype::i256;
use crate::expr::stats::Precision;
use crate::expr::stats::Stat;
use crate::match_each_decimal_value_type;
use crate::scalar::DecimalValue;
use crate::scalar::ScalarValue;
use crate::validity::Validity;
impl WidenArray {
pub fn encode_decimal(array: DecimalArray, ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
let logical_decimal_dtype = array.decimal_dtype();
let validity = array.validity()?;
let validity_mask = validity.execute_mask(array.len(), ctx)?;
let child = match_each_decimal_value_type!(array.values_type(), |T| {
let values_buffer = array.buffer::<T>();
let valid_value_range =
ValidRange::resolve(Precision::Absent, values_buffer.as_slice(), &validity_mask);
narrowest_decimal_child(
&values_buffer,
&validity,
&validity_mask,
valid_value_range,
logical_decimal_dtype,
)?
});
let Some(child) = child else {
return Ok(array.into_array());
};
Ok(WidenArray::try_new(child.into_array(), array.dtype().clone())?.into_array())
}
pub fn from_stored_decimal(
parts: DecimalDataParts,
known_range: Precision<RangeInclusive<i256>>,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let DecimalDataParts {
decimal_dtype,
values,
values_type,
validity,
} = parts;
DecimalData::validate(&values, values_type)?;
let len = values.len() / values_type.byte_width();
if let Some(validity_len) = validity.maybe_len() {
vortex_ensure_eq!(
validity_len,
len,
"Decimal validity must have one entry per value"
);
}
if values_type == decimal_dtype.canonical_values_type() {
let array = DecimalArray::try_new_handle(values, values_type, decimal_dtype, validity)?;
return Ok(array.into_array());
}
let values = values.try_into_host_sync()?;
let mask = validity.execute_mask(len, ctx)?;
match_each_decimal_value_type!(values_type, |T| {
let values = Buffer::<T>::from_byte_buffer(values);
let range = ValidRange::resolve(known_range, values.as_slice(), &mask);
widen_or_canonical(values, validity, &mask, range, decimal_dtype)
})
}
}
pub(crate) fn smallest_decimal_dtype(
range: Option<RangeInclusive<i256>>,
parent: DecimalDType,
) -> VortexResult<DecimalDType> {
let minimum_precision = parent.scale().max(1) as u8;
let Some(range) = range else {
return Ok(DecimalDType::new(minimum_precision, parent.scale()));
};
let (min, max) = range.into_inner();
vortex_ensure!(
min <= max,
"Decimal range requires ordered extrema, got {min} to {max}"
);
for precision in minimum_precision..=parent.precision() {
let index = usize::from(precision);
if i256::MIN_BY_PRECISION[index] <= min && max <= i256::MAX_BY_PRECISION[index] {
return Ok(DecimalDType::new(precision, parent.scale()));
}
}
vortex_bail!("Decimal range must fit {parent}, got {min} to {max}")
}
enum ValidRange {
Empty,
Exact(RangeInclusive<i256>),
Bounds(RangeInclusive<i256>),
}
impl ValidRange {
fn resolve<F: NativeDecimalType>(
known_range: Precision<RangeInclusive<i256>>,
values: &[F],
mask: &Mask,
) -> Self {
match known_range {
Precision::Exact(extrema) => return Self::Exact(extrema),
Precision::Inexact(bounds) => return Self::Bounds(bounds),
Precision::Absent => {}
}
let Some((min, max)) = valid_range(values, mask) else {
return Self::Empty;
};
let min = min.to_i256().vortex_expect("every decimal value fits i256");
let max = max.to_i256().vortex_expect("every decimal value fits i256");
Self::Exact(min..=max)
}
fn bounds(&self) -> Option<RangeInclusive<i256>> {
match self {
Self::Empty => None,
Self::Exact(bounds) | Self::Bounds(bounds) => Some(bounds.clone()),
}
}
fn cache_statistics(self, child: &DecimalArray) {
let extrema = match self {
Self::Empty => return,
Self::Exact(bounds) => Precision::Exact(bounds),
Self::Bounds(bounds) => Precision::Inexact(bounds),
};
let child_dtype = child.decimal_dtype();
let min = extrema
.clone()
.map(|bounds| decimal_stat(*bounds.start(), child_dtype));
let max = extrema.map(|bounds| decimal_stat(*bounds.end(), child_dtype));
child.statistics().set(Stat::Min, min);
child.statistics().set(Stat::Max, max);
}
}
fn widen_or_canonical<F: NativeDecimalType>(
values: Buffer<F>,
validity: Validity,
mask: &Mask,
range: ValidRange,
decimal_dtype: DecimalDType,
) -> VortexResult<ArrayRef> {
if let Some(child) = narrowest_decimal_child(&values, &validity, mask, range, decimal_dtype)? {
let dtype = DType::Decimal(decimal_dtype, validity.nullability());
return Ok(WidenArray::try_new(child.into_array(), dtype)?.into_array());
}
let array = build_canonical_decimal(&values, &validity, mask, decimal_dtype)?;
Ok(array.into_array())
}
fn narrowest_decimal_child<F: NativeDecimalType>(
values: &Buffer<F>,
validity: &Validity,
mask: &Mask,
range: ValidRange,
decimal_dtype: DecimalDType,
) -> VortexResult<Option<DecimalArray>> {
let child_dtype = smallest_decimal_dtype(range.bounds(), decimal_dtype)?;
let child_type = child_dtype.canonical_values_type();
if child_type == decimal_dtype.canonical_values_type() {
return Ok(None);
}
let child = build_canonical_decimal(values, validity, mask, child_dtype)?;
range.cache_statistics(&child);
Ok(Some(child))
}
fn build_canonical_decimal<F: NativeDecimalType>(
values: &Buffer<F>,
validity: &Validity,
mask: &Mask,
decimal_dtype: DecimalDType,
) -> VortexResult<DecimalArray> {
let values_type = decimal_dtype.canonical_values_type();
let buffer = if F::DECIMAL_TYPE == values_type {
BufferHandle::new_host(values.clone().into_byte_buffer())
} else {
copy_to_canonical_width(values.as_slice(), mask, decimal_dtype)?
};
DecimalArray::try_new_handle(buffer, values_type, decimal_dtype, validity.clone())
}
fn decimal_stat(value: i256, decimal_dtype: DecimalDType) -> ScalarValue {
let value = DecimalValue::try_from_i256(value, decimal_dtype)
.vortex_expect("the selected child precision contains its range bounds");
ScalarValue::Decimal(value)
}