Skip to content
Closed
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
18 changes: 13 additions & 5 deletions arrow-arith/src/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ fn decimal_op<T: DecimalType>(

// Follow the Hive decimal arithmetic rules
// https://cwiki.apache.org/confluence/download/attachments/27362075/Hive_Decimal_Precision_Scale_Support.pdf
// And the Calcite rules
// https://github.com/apache/arrow/blob/36ddbb531cac9b9e512dfa3776d1d64db588209f/java/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/DecimalTypeUtil.java#L46
let array: PrimitiveArray<T> = match op {
Op::Add | Op::AddWrapping | Op::Sub | Op::SubWrapping => {
// max(s1, s2)
Expand Down Expand Up @@ -794,9 +796,8 @@ fn decimal_op<T: DecimalType>(
}

Op::Div => {
// Follow postgres and MySQL adding a fixed scale increment of 4
// s1 + 4
let result_scale = s1.saturating_add(4).min(T::MAX_SCALE);
// max(6, s1 + p2 + 1)
let result_scale = 6.max(s1 + *p2 as i8 + 1).min(T::MAX_SCALE);
let mul_pow = result_scale - s1 + s2;

// p1 - s1 + s2 + result_scale
Expand Down Expand Up @@ -1126,10 +1127,17 @@ mod tests {
);

let result = div(&a, &b).unwrap();
assert_eq!(result.data_type(), &DataType::Decimal128(17, 7));
assert_eq!(result.data_type(), &DataType::Decimal128(26, 16));
assert_eq!(
result.as_primitive::<Decimal128Type>().values(),
&[27777, 0, 162078, 11133333, -1300000, 402]
&[
27777777777777,
0,
162078651685393,
11133333333333333,
-1300000000000000,
402684563758
]
);

let result = rem(&a, &b).unwrap();
Expand Down