Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions vortex-array/benches/binary_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,38 @@ fn add_decimal_i128_nullable(bencher: Bencher) {
bench_decimal(bencher, lhs, rhs, Operator::Add);
}

#[divan::bench]
fn mul_decimal_i64_nonnull(bencher: Bencher) {
let lhs = decimal_i64_nonnull(0).into_array();
let rhs = decimal_i64_nonnull(1_000_000).into_array();

bench_decimal(bencher, lhs, rhs, Operator::Mul);
}

#[divan::bench]
fn mul_decimal_i128_nullable(bencher: Bencher) {
let lhs = decimal_i128_nullable(0, 7).into_array();
let rhs = decimal_i128_nullable(1_000_000, 5).into_array();

bench_decimal(bencher, lhs, rhs, Operator::Mul);
}

#[divan::bench]
fn div_decimal_i64_nonnull(bencher: Bencher) {
let lhs = decimal_i64_nonnull(0).into_array();
let rhs = decimal_i64_nonnull(1_000_000).into_array();

bench_decimal(bencher, lhs, rhs, Operator::Div);
}

#[divan::bench]
fn div_decimal_i128_nullable(bencher: Bencher) {
let lhs = decimal_i128_nullable(0, 7).into_array();
let rhs = decimal_i128_nullable(1_000_000, 5).into_array();

bench_decimal(bencher, lhs, rhs, Operator::Div);
}

#[divan::bench]
fn eq_i64_constant(bencher: Bencher) {
let lhs = primitive_nonnull(0).into_array();
Expand Down
34 changes: 28 additions & 6 deletions vortex-array/src/compute/conformance/binary_numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use std::fmt::Debug;
use itertools::Itertools;
use num_traits::Bounded;
use num_traits::CheckedAdd;
use num_traits::CheckedDiv;
use num_traits::CheckedMul;
use num_traits::CheckedSub;
use num_traits::Float;
use num_traits::Num;
Expand Down Expand Up @@ -303,8 +305,16 @@ fn test_decimal_binary_numeric_with_scalar(

let scalar = Scalar::decimal(value, decimal_dtype, array.dtype().nullability());

// Decimal Mul/Div are not yet implemented.
for operator in [NumericOperator::Add, NumericOperator::Sub] {
let mut operators = vec![
NumericOperator::Add,
NumericOperator::Sub,
NumericOperator::Mul,
];
if !value.is_zero() {
operators.push(NumericOperator::Div);
}

for operator in operators {
for lhs_is_array in [true, false] {
test_decimal_binary_numeric_direction(
array,
Expand All @@ -329,7 +339,7 @@ fn test_decimal_binary_numeric_direction(
ctx: &mut ExecutionCtx,
) {
let result_decimal_dtype = numeric_op_result_decimal_dtype(decimal_dtype, operator)
.vortex_expect("decimal Add/Sub must have a result dtype");
.vortex_expect("decimal arithmetic must have a result dtype");
let result_dtype = DType::Decimal(result_decimal_dtype, array.dtype().nullability());
let expected_results = expected_decimal_results(
original_values,
Expand Down Expand Up @@ -382,10 +392,22 @@ fn expected_decimal_results(
let (Some(lhs), Some(rhs)) = (lhs.decimal_value(), rhs.decimal_value()) else {
return Some(Scalar::null(result_dtype.clone()));
};
let lhs = lhs.as_i256();
let rhs = rhs.as_i256();
let value = match operator {
NumericOperator::Add => lhs.as_i256().checked_add(&rhs.as_i256()),
NumericOperator::Sub => lhs.as_i256().checked_sub(&rhs.as_i256()),
NumericOperator::Mul | NumericOperator::Div => unreachable!(),
NumericOperator::Add => lhs.checked_add(&rhs),
NumericOperator::Sub => lhs.checked_sub(&rhs),
NumericOperator::Mul => lhs.checked_mul(&rhs),
NumericOperator::Div => {
let scale_power = result_decimal_dtype.scale();
let factor =
i256::from_i128(10).checked_pow(scale_power.unsigned_abs() as u32)?;
if scale_power >= 0 {
lhs.checked_mul(&factor)?.checked_div(&rhs)
} else {
lhs.checked_div(&rhs.checked_mul(&factor)?)
}
}
}?;
let value = DecimalValue::try_from_i256(value, result_decimal_dtype).ok()?;
Some(Scalar::decimal(
Expand Down
4 changes: 2 additions & 2 deletions vortex-array/src/scalar/typed_view/decimal/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub(crate) fn checked_decimal_numeric(
result: DecimalDType,
op: NumericOperator,
) -> Option<DecimalValue> {
let work = work_decimal_dtype(input, result, op);
let work = decimal_numeric_work_dtype(input, result, op);
match_each_decimal_value_type!(DecimalType::smallest_decimal_value_type(&work), |W| {
let value = checked_at_width::<W>(lhs.cast::<W>()?, rhs.cast::<W>()?, result.scale(), op)?;
DecimalValue::from(value).normalize(result)
Expand All @@ -120,7 +120,7 @@ pub(crate) fn checked_decimal_numeric(
/// The result precision covers Add, Sub and Mul, whose intermediates are the result itself. Div
/// scales the dividend (or the divisor, for a negative result scale) by `10^result_scale` before
/// dividing, so it needs room for `p + |result_scale|` digits.
fn work_decimal_dtype(
pub(crate) fn decimal_numeric_work_dtype(
input: DecimalDType,
result: DecimalDType,
op: NumericOperator,
Expand Down
1 change: 1 addition & 0 deletions vortex-array/src/scalar/typed_view/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod dvalue;
mod scalar;

pub(crate) use arithmetic::decimal_numeric_result_dtype;
pub(crate) use arithmetic::decimal_numeric_work_dtype;
pub use dvalue::DecimalValue;
pub use scalar::DecimalScalar;

Expand Down
Loading
Loading