Skip to content

Commit 4534416

Browse files
committed
comments
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
1 parent 3a56ba9 commit 4534416

6 files changed

Lines changed: 214 additions & 174 deletions

File tree

vortex-array/src/compute/conformance/binary_numeric.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::scalar::DecimalValue;
4949
use crate::scalar::NumericOperator;
5050
use crate::scalar::PrimitiveScalar;
5151
use crate::scalar::Scalar;
52-
use crate::scalar_fn::fns::binary::decimal_add_sub_result_dtype;
52+
use crate::scalar_fn::fns::binary::numeric_op_result_decimal_dtype;
5353

5454
fn to_vec_of_scalar(array: &ArrayRef, ctx: &mut ExecutionCtx) -> Vec<Scalar> {
5555
// Not fast, but obviously correct
@@ -300,13 +300,14 @@ fn test_decimal_binary_numeric_with_scalar(
300300
let original_values = to_vec_of_scalar(&canonicalized_array, ctx);
301301

302302
let scalar = Scalar::decimal(value, decimal_dtype, array.dtype().nullability());
303-
let result_decimal_dtype = decimal_add_sub_result_dtype(decimal_dtype);
304-
let result_dtype = DType::Decimal(result_decimal_dtype, array.dtype().nullability());
305303

306304
// Decimal Mul/Div are not yet implemented.
307305
let operators = vec![NumericOperator::Add, NumericOperator::Sub];
308306

309307
for operator in operators {
308+
let result_decimal_dtype = numeric_op_result_decimal_dtype(decimal_dtype, operator)
309+
.vortex_expect("decimal Add/Sub must have a result dtype");
310+
let result_dtype = DType::Decimal(result_decimal_dtype, array.dtype().nullability());
310311
let rhs_const = ConstantArray::new(scalar.clone(), array.len()).into_array();
311312

312313
for lhs_is_array in [true, false] {

vortex-array/src/scalar/typed_view/primitive/numeric_operator.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
66
use std::fmt;
77

8+
use vortex_error::VortexError;
9+
use vortex_error::vortex_err;
10+
811
use crate::scalar_fn::fns::operators::Operator;
912

1013
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -38,3 +41,17 @@ impl From<NumericOperator> for Operator {
3841
}
3942
}
4043
}
44+
45+
impl TryFrom<Operator> for NumericOperator {
46+
type Error = VortexError;
47+
48+
fn try_from(op: Operator) -> Result<Self, Self::Error> {
49+
match op {
50+
Operator::Add => Ok(NumericOperator::Add),
51+
Operator::Sub => Ok(NumericOperator::Sub),
52+
Operator::Mul => Ok(NumericOperator::Mul),
53+
Operator::Div => Ok(NumericOperator::Div),
54+
_ => Err(vortex_err!(InvalidArgument: "{op} is not a numeric operator")),
55+
}
56+
}
57+
}

vortex-array/src/scalar_fn/fns/binary/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ use vortex_session::registry::CachedId;
1717
use crate::ArrayRef;
1818
use crate::ExecutionCtx;
1919
use crate::dtype::DType;
20-
use crate::dtype::DecimalDType;
21-
use crate::dtype::MAX_PRECISION;
2220
use crate::dtype::Nullability;
2321
use crate::expr::and;
2422
use crate::expr::expression::Expression;
@@ -50,14 +48,6 @@ use crate::scalar::Scalar;
5048
#[derive(Clone)]
5149
pub struct Binary;
5250

53-
/// Derive the result type for Add/Sub over operands that already share a decimal dtype.
54-
pub(crate) fn decimal_add_sub_result_dtype(input: DecimalDType) -> DecimalDType {
55-
DecimalDType::new(
56-
input.precision().saturating_add(1).min(MAX_PRECISION),
57-
input.scale(),
58-
)
59-
}
60-
6151
impl ScalarFnVTable for Binary {
6252
type Options = Operator;
6353

@@ -131,12 +121,13 @@ impl ScalarFnVTable for Binary {
131121
if lhs.is_primitive() && lhs.eq_ignore_nullability(rhs) {
132122
return Ok(lhs.with_nullability(lhs.nullability() | rhs.nullability()));
133123
}
124+
134125
if let DType::Decimal(decimal_dtype, _) = lhs
135-
&& matches!(operator, Operator::Add | Operator::Sub)
136126
&& lhs.eq_ignore_nullability(rhs)
137127
{
128+
let numeric_op = NumericOperator::try_from(*operator)?;
138129
return Ok(DType::Decimal(
139-
decimal_add_sub_result_dtype(*decimal_dtype),
130+
numeric_op_result_decimal_dtype(*decimal_dtype, numeric_op)?,
140131
lhs.nullability() | rhs.nullability(),
141132
));
142133
}

0 commit comments

Comments
 (0)