use itertools::Itertools;
use itertools::MinMaxResult;
use vortex_buffer::Buffer;
use vortex_buffer::BufferMut;
use vortex_compute::lane_kernels::IndexedSourceExt;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure_eq;
use vortex_mask::AllOr;
use vortex_mask::Mask;
pub use crate::aggregate_fn::fns::min_max::valid_range;
use crate::arrays::DecimalArray;
use crate::arrays::decimal::DecimalArrayExt;
use crate::buffer::BufferHandle;
use crate::dtype::BigCast;
use crate::dtype::DecimalDType;
use crate::dtype::DecimalType;
use crate::dtype::NativeDecimalType;
use crate::dtype::i256;
use crate::match_each_decimal_value_type;
pub(crate) fn widened_buffer<W: NativeDecimalType>(array: &DecimalArray) -> Buffer<W> {
if array.values_type() == W::DECIMAL_TYPE {
return array.buffer::<W>();
}
match_each_decimal_value_type!(array.values_type(), |T| {
if const { (T::DECIMAL_TYPE as u8) < (W::DECIMAL_TYPE as u8) } {
widen_values::<T, W>(&array.buffer::<T>())
} else {
unreachable!(
"{} is not narrower than {}",
T::DECIMAL_TYPE,
W::DECIMAL_TYPE
)
}
})
}
pub fn converted_buffer<W: NativeDecimalType>(
array: &DecimalArray,
validity: &Mask,
) -> VortexResult<Buffer<W>> {
if array.values_type() <= W::DECIMAL_TYPE {
return Ok(widened_buffer(array));
}
match_each_decimal_value_type!(array.values_type(), |T| {
if const { (T::DECIMAL_TYPE as u8) > (W::DECIMAL_TYPE as u8) } {
let src = array.buffer::<T>();
match validity {
Mask::AllTrue(_) => {
let any_overflow = src.iter().fold(false, |acc, v| acc | W::from(*v).is_none());
if any_overflow {
let (i, v) = src
.iter()
.enumerate()
.find(|&(_, v)| W::from(*v).is_none())
.vortex_expect("overflow scan found an overflowing value");
vortex_bail!(
"decimal value {v} at index {i} does not fit {}",
W::DECIMAL_TYPE
);
}
}
Mask::AllFalse(_) => return Ok(Buffer::zeroed(src.len())),
Mask::Values(values) => {
let any_overflow = src.iter().fold(false, |acc, v| acc | W::from(*v).is_none());
if any_overflow {
for (i, v) in src.iter().enumerate() {
if values.value(i) && W::from(*v).is_none() {
vortex_bail!(
"decimal value {v} at index {i} does not fit {}",
W::DECIMAL_TYPE
);
}
}
}
}
}
Ok(src
.iter()
.map(|v| W::from(*v).unwrap_or_default())
.collect())
} else {
unreachable!("{} is not wider than {}", T::DECIMAL_TYPE, W::DECIMAL_TYPE)
}
})
}
pub fn copy_to_canonical_width<F: NativeDecimalType>(
values: &[F],
validity: &Mask,
target: DecimalDType,
) -> VortexResult<BufferHandle> {
vortex_ensure_eq!(validity.len(), values.len());
let canonical_type = target.canonical_values_type();
if F::DECIMAL_TYPE == canonical_type {
return Ok(BufferHandle::new_host(
Buffer::<F>::copy_from(values).into_byte_buffer(),
));
}
let copy = match_each_decimal_value_type!(canonical_type, |W| {
if const { (F::DECIMAL_TYPE as u8) <= (W::DECIMAL_TYPE as u8) } {
widen_values::<F, W>(values).into_byte_buffer()
} else {
check_precision(values, validity, target)?;
narrow_checked_values::<F, W>(values, validity).into_byte_buffer()
}
});
Ok(BufferHandle::new_host(copy))
}
fn widen_values<F: NativeDecimalType, W: NativeDecimalType>(values: &[F]) -> Buffer<W> {
assert!(
(F::DECIMAL_TYPE as u8) <= (W::DECIMAL_TYPE as u8),
"widen_values requires a target at least as wide as the source, got {} into {}",
F::DECIMAL_TYPE,
W::DECIMAL_TYPE
);
Buffer::from_trusted_len_iter(
values
.iter()
.map(|&value| <W as BigCast>::from(value).unwrap_or_default()),
)
}
fn narrow_checked_values<F: NativeDecimalType, W: NativeDecimalType>(
values: &[F],
validity: &Mask,
) -> Buffer<W> {
let len = values.len();
let valid_bits = match validity.bit_buffer() {
AllOr::All => None,
AllOr::None => return Buffer::zeroed(len),
AllOr::Some(valid_bits) => Some(valid_bits),
};
let narrow = |value: F| <W as BigCast>::from(value).unwrap_or_default();
let mut buffer = BufferMut::<W>::with_capacity(len);
let out = &mut buffer.spare_capacity_mut()[..len];
match valid_bits {
None => values.map_into(out, narrow),
Some(valid_bits) => values.map_masked_into(valid_bits, out, narrow),
}
unsafe { buffer.set_len(len) };
buffer.freeze()
}
pub fn check_precision<T: NativeDecimalType>(
values: &[T],
validity: &Mask,
decimal_dtype: DecimalDType,
) -> VortexResult<()> {
vortex_ensure_eq!(validity.len(), values.len());
let precision = decimal_dtype.precision();
if precision > T::MAX_PRECISION {
return Ok(());
}
let min = T::MIN_BY_PRECISION[usize::from(precision)];
let max = T::MAX_BY_PRECISION[usize::from(precision)];
let is_outside = |value: T| (value < min) | (value > max);
let valid_bits = match validity.bit_buffer() {
AllOr::All => None,
AllOr::None => return Ok(()),
AllOr::Some(valid_bits) => Some(valid_bits),
};
let any_outside = values
.iter()
.fold(false, |acc, &value| acc | is_outside(value));
if !any_outside {
return Ok(());
}
let first_outside = match valid_bits {
None => values.iter().position(|&value| is_outside(value)),
Some(valid_bits) => valid_bits
.set_indices()
.find(|&row| is_outside(values[row])),
};
if let Some(row) = first_outside {
vortex_bail!(
"decimal value at row {row} must be within the precision of {decimal_dtype}, got {}",
values[row]
);
}
Ok(())
}
macro_rules! try_downcast {
($array:expr, from: $src:ty, to: $($dst:ty),*) => {{
let minmax = $array.buffer::<$src>().iter().copied().minmax();
match minmax {
MinMaxResult::NoElements => return $array,
MinMaxResult::OneElement(_) => return $array,
MinMaxResult::MinMax(min, max) => {
$(
if <$dst as BigCast>::from(min).is_some() && <$dst as BigCast>::from(max).is_some() {
return DecimalArray::new::<$dst>(
$array
.buffer::<$src>()
.into_iter()
.map(|v| <$dst as BigCast>::from(v).vortex_expect("decimal conversion failure"))
.collect(),
$array.decimal_dtype(),
$array
.validity()
.vortex_expect("decimal validity should be derivable"),
);
}
)*
return $array;
}
}
}};
}
pub fn narrowed_decimal(decimal_array: DecimalArray) -> DecimalArray {
match decimal_array.values_type() {
DecimalType::I8 => decimal_array,
DecimalType::I16 => {
try_downcast!(decimal_array, from: i16, to: i8)
}
DecimalType::I32 => {
try_downcast!(decimal_array, from: i32, to: i8, i16)
}
DecimalType::I64 => {
try_downcast!(decimal_array, from: i64, to: i8, i16, i32)
}
DecimalType::I128 => {
try_downcast!(decimal_array, from: i128, to: i8, i16, i32, i64)
}
DecimalType::I256 => {
try_downcast!(decimal_array, from: i256, to: i8, i16, i32, i64, i128)
}
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use vortex_buffer::Buffer;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_mask::Mask;
use super::check_precision;
use super::converted_buffer;
use super::copy_to_canonical_width;
use super::valid_range;
use super::widen_values;
use crate::arrays::DecimalArray;
use crate::dtype::BigCast;
use crate::dtype::DecimalDType;
use crate::dtype::DecimalType;
use crate::dtype::NativeDecimalType;
use crate::dtype::ToI256;
use crate::dtype::i256;
use crate::match_each_decimal_value_type;
use crate::validity::Validity;
fn copy_as_i256<F: NativeDecimalType>(
values: &[F],
validity: &Mask,
target: DecimalDType,
) -> VortexResult<Vec<i256>> {
let copy = copy_to_canonical_width(values, validity, target)?;
let width = target.canonical_values_type();
assert_eq!(copy.len(), values.len() * width.byte_width());
Ok(match_each_decimal_value_type!(width, |W| {
Buffer::<W>::from_byte_buffer(copy.unwrap_host())
.iter()
.map(|value| value.to_i256().vortex_expect("decimal values fit i256"))
.collect()
}))
}
fn ten_to_the(precision: u8) -> i256 {
i256::from_i128(10)
.checked_pow(precision.into())
.vortex_expect("10^76 fits i256")
}
fn to_native<T: NativeDecimalType>(values: &[i256]) -> Vec<T> {
values
.iter()
.map(|&value| <T as BigCast>::from(value).vortex_expect("test value fits the type"))
.collect()
}
#[rstest]
#[case::all_valid(vec![5, -3, 7], vec![true, true, true], Some((-3, 7)))]
#[case::partly_valid(vec![5, -3, 7], vec![true, true, false], Some((-3, 5)))]
#[case::all_null(vec![5, -3, 7], vec![false, false, false], None)]
#[case::empty(vec![], vec![], None)]
#[case::one_valid(vec![5, -3, 7], vec![false, true, false], Some((-3, -3)))]
#[case::outside_i8_precision(vec![-100, 100], vec![true, true], Some((-100, 100)))]
fn test_valid_range(
#[case] values: Vec<i8>,
#[case] validity: Vec<bool>,
#[case] expected: Option<(i8, i8)>,
#[values(
DecimalType::I8,
DecimalType::I16,
DecimalType::I32,
DecimalType::I64,
DecimalType::I128,
DecimalType::I256
)]
storage: DecimalType,
) -> VortexResult<()> {
let validity = Mask::from_iter(validity);
match_each_decimal_value_type!(storage, |T| {
let values: Vec<T> = values
.iter()
.map(|&value| {
<T as BigCast>::from(value).vortex_expect("i8 values fit every decimal type")
})
.collect();
let expected = expected.map(|(min, max)| {
(
<T as BigCast>::from(min).vortex_expect("i8 values fit every decimal type"),
<T as BigCast>::from(max).vortex_expect("i8 values fit every decimal type"),
)
});
assert_eq!(valid_range(&values, &validity), expected);
});
Ok(())
}
#[rstest]
fn test_valid_range_ignores_null_extremes(
#[values(
DecimalType::I8,
DecimalType::I16,
DecimalType::I32,
DecimalType::I64,
DecimalType::I128,
DecimalType::I256
)]
storage: DecimalType,
) -> VortexResult<()> {
let validity = Mask::from_iter([false, true, true, false]);
match_each_decimal_value_type!(storage, |T| {
let min = <T as BigCast>::from(-3i8).vortex_expect("-3 fits every decimal type");
let max = <T as BigCast>::from(7i8).vortex_expect("7 fits every decimal type");
assert_eq!(
valid_range(&[T::MIN, min, max, T::MAX], &validity),
Some((min, max))
);
assert_eq!(
valid_range(&[T::MIN, T::MAX], &Mask::AllTrue(2)),
Some((T::MIN, T::MAX))
);
});
Ok(())
}
#[test]
#[should_panic(expected = "valid_range requires one validity bit per value")]
fn test_valid_range_rejects_mismatched_mask() {
valid_range(&[1i8, 2], &Mask::AllTrue(3));
}
#[rstest]
fn converted_buffer_every_storage_pair(
#[values(
DecimalType::I8,
DecimalType::I16,
DecimalType::I32,
DecimalType::I64,
DecimalType::I128,
DecimalType::I256
)]
storage: DecimalType,
#[values(
DecimalType::I8,
DecimalType::I16,
DecimalType::I32,
DecimalType::I64,
DecimalType::I128,
DecimalType::I256
)]
target: DecimalType,
) -> VortexResult<()> {
let values = [1i8, -2, 3];
let dtype = DecimalDType::new(2, 0);
let array = match_each_decimal_value_type!(storage, |S| {
let values = values.map(|value| {
<S as BigCast>::from(value).vortex_expect("small values fit every decimal type")
});
DecimalArray::new(Buffer::copy_from(values), dtype, Validity::NonNullable)
});
match_each_decimal_value_type!(target, |W| {
let converted = converted_buffer::<W>(&array, &Mask::new_true(values.len()))?;
let expected = values.map(|value| {
<W as BigCast>::from(value).vortex_expect("small values fit every decimal type")
});
assert_eq!(converted.as_slice(), expected.as_slice());
});
Ok(())
}
#[rstest]
#[case::i8_to_i16(DecimalType::I8, 3)]
#[case::i16_to_i32(DecimalType::I16, 5)]
#[case::i32_to_i64(DecimalType::I32, 10)]
#[case::i64_to_i128(DecimalType::I64, 19)]
#[case::i128_to_i256(DecimalType::I128, 39)]
#[case::i8_to_i256(DecimalType::I8, 76)]
#[case::i64_same_width(DecimalType::I64, 18)]
fn test_copy_to_canonical_width_widens(
#[case] source: DecimalType,
#[case] target_precision: u8,
) -> VortexResult<()> {
let target = DecimalDType::new(target_precision, 2);
match_each_decimal_value_type!(source, |F| {
let limit = ten_to_the(F::MAX_PRECISION) - i256::ONE;
let expected = [-limit, i256::ZERO, limit];
let values = to_native::<F>(&expected);
assert_eq!(copy_as_i256(&values, &Mask::new_true(3), target)?, expected);
});
Ok(())
}
#[rstest]
#[case::i8_to_i16(DecimalType::I8, DecimalType::I16)]
#[case::i8_to_i32(DecimalType::I8, DecimalType::I32)]
#[case::i8_to_i64(DecimalType::I8, DecimalType::I64)]
#[case::i8_to_i128(DecimalType::I8, DecimalType::I128)]
#[case::i8_to_i256(DecimalType::I8, DecimalType::I256)]
#[case::i16_to_i32(DecimalType::I16, DecimalType::I32)]
#[case::i16_to_i64(DecimalType::I16, DecimalType::I64)]
#[case::i16_to_i128(DecimalType::I16, DecimalType::I128)]
#[case::i16_to_i256(DecimalType::I16, DecimalType::I256)]
#[case::i32_to_i64(DecimalType::I32, DecimalType::I64)]
#[case::i32_to_i128(DecimalType::I32, DecimalType::I128)]
#[case::i32_to_i256(DecimalType::I32, DecimalType::I256)]
#[case::i64_to_i128(DecimalType::I64, DecimalType::I128)]
#[case::i64_to_i256(DecimalType::I64, DecimalType::I256)]
#[case::i128_to_i256(DecimalType::I128, DecimalType::I256)]
fn test_widen_values_sign_extends(#[case] source: DecimalType, #[case] target: DecimalType) {
match_each_decimal_value_type!(source, |F| {
let small = |value: i8| {
<F as BigCast>::from(value).vortex_expect("small values fit every decimal type")
};
let pattern = [F::MIN, small(-1), small(0), small(1), F::MAX];
let values: Vec<F> = pattern.into_iter().cycle().take(130).collect();
match_each_decimal_value_type!(target, |W| {
let expected: Vec<W> = values
.iter()
.map(|&value| {
<W as BigCast>::from(value)
.vortex_expect("W is at least as wide as F, so every value fits")
})
.collect();
assert_eq!(
widen_values::<F, W>(&values).as_slice(),
expected.as_slice()
);
});
});
}
#[test]
#[should_panic(expected = "widen_values requires a target at least as wide as the source")]
fn test_widen_values_rejects_narrower_target() {
widen_values::<i64, i32>(&[1, 2, 3]);
}
#[rstest]
#[case::i16_to_i8(DecimalType::I16, 2)]
#[case::i32_to_i16(DecimalType::I32, 4)]
#[case::i64_to_i32(DecimalType::I64, 9)]
#[case::i128_to_i64(DecimalType::I128, 18)]
#[case::i256_to_i128(DecimalType::I256, 38)]
#[case::i256_to_i8(DecimalType::I256, 1)]
fn test_copy_to_canonical_width_narrowing_boundaries(
#[case] source: DecimalType,
#[case] target_precision: u8,
) -> VortexResult<()> {
let target = DecimalDType::new(target_precision, 0);
let limit = ten_to_the(target_precision);
let inside = [-(limit - i256::ONE), limit - i256::ONE];
let outside = [-limit, limit];
match_each_decimal_value_type!(source, |F| {
let values = to_native::<F>(&inside);
assert_eq!(copy_as_i256(&values, &Mask::new_true(2), target)?, inside);
for value in to_native::<F>(&outside) {
assert!(copy_to_canonical_width(&[value], &Mask::new_true(1), target).is_err());
}
});
Ok(())
}
#[test]
fn test_copy_to_canonical_width_zeros_null_slots() -> VortexResult<()> {
let values = [12_345i128, 10i128.pow(30), -99_999, 100_000];
let validity = Mask::from_iter([true, false, true, false]);
let copy = copy_as_i256(&values, &validity, DecimalDType::new(5, 2))?;
assert_eq!(copy, [12_345, 0, -99_999, 0].map(i256::from_i128));
Ok(())
}
#[test]
fn test_copy_to_canonical_width_error_names_row() {
let values = [1i64, 1_000_000, 1_000, 5_000];
let validity = Mask::from_iter([true, false, true, true]);
let error = copy_to_canonical_width(&values, &validity, DecimalDType::new(3, 0))
.expect_err("row 2 is outside the precision")
.to_string();
assert!(error.contains("row 2"), "{error}");
assert!(error.contains("got 1000"), "{error}");
assert!(error.contains("decimal(3,0)"), "{error}");
}
#[test]
fn test_copy_to_canonical_width_all_null() -> VortexResult<()> {
let values = [10i128.pow(30), i128::MIN, 7];
let copy = copy_as_i256(&values, &Mask::new_false(3), DecimalDType::new(5, 2))?;
assert_eq!(copy, [i256::ZERO; 3]);
Ok(())
}
#[rstest]
fn test_copy_to_canonical_width_length_mismatch(#[values(4, 18)] target_precision: u8) {
let target = DecimalDType::new(target_precision, 0);
let result = copy_to_canonical_width(&[1i32, 2, 3], &Mask::new_true(2), target);
assert!(result.is_err());
}
#[rstest]
#[case::i8(DecimalType::I8, 2)]
#[case::i16(DecimalType::I16, 4)]
#[case::i32(DecimalType::I32, 9)]
#[case::i64(DecimalType::I64, 18)]
#[case::i128(DecimalType::I128, 38)]
#[case::i256(DecimalType::I256, 76)]
#[case::i128_below_max_precision(DecimalType::I128, 5)]
fn test_check_precision_boundaries(
#[case] values_type: DecimalType,
#[case] precision: u8,
) -> VortexResult<()> {
let decimal_dtype = DecimalDType::new(precision, 0);
let limit = ten_to_the(precision);
let inside = [-(limit - i256::ONE), limit - i256::ONE];
let outside = [-limit, limit];
match_each_decimal_value_type!(values_type, |T| {
check_precision(&to_native::<T>(&inside), &Mask::new_true(2), decimal_dtype)?;
for value in to_native::<T>(&outside) {
assert!(check_precision(&[value], &Mask::new_true(1), decimal_dtype).is_err());
}
});
Ok(())
}
#[test]
fn test_check_precision_above_max_precision_of_values_type() -> VortexResult<()> {
let all_valid = Mask::new_true(2);
check_precision(&[i8::MIN, i8::MAX], &all_valid, DecimalDType::new(3, 0))?;
check_precision(&[i64::MIN, i64::MAX], &all_valid, DecimalDType::new(19, 0))?;
Ok(())
}
#[test]
fn test_check_precision_ignores_null_slots() -> VortexResult<()> {
let values = [5i32, 1_000_000_000, -7];
let validity = Mask::from_iter([true, false, true]);
let decimal_dtype = DecimalDType::new(4, 0);
check_precision(&values, &validity, decimal_dtype)?;
check_precision(&values, &Mask::new_false(3), decimal_dtype)?;
Ok(())
}
}