Skip to content

Commit

Permalink
fix: cast decimal to decimal should be round the result
Browse files Browse the repository at this point in the history
  • Loading branch information
liukun4515 committed Nov 19, 2022
1 parent 5bce104 commit 949c6fd
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions arrow-cast/src/cast.rs
Expand Up @@ -36,6 +36,7 @@
//! ```

use chrono::{DateTime, NaiveDateTime, NaiveTime, Timelike};
use std::ops::Neg;
use std::sync::Arc;

use crate::display::{array_value_to_string, lexical_to_string};
Expand Down Expand Up @@ -1955,12 +1956,29 @@ fn cast_decimal_to_decimal_safe<const BYTE_WIDTH1: usize, const BYTE_WIDTH2: usi
// For example, input_scale is 4 and output_scale is 3;
// Original value is 11234_i128, and will be cast to 1123_i128.
let div = 10_i128.pow((input_scale - output_scale) as u32);
let half = div / 2;
let neg_half = half.neg();
if BYTE_WIDTH1 == 16 {
let array = array.as_any().downcast_ref::<Decimal128Array>().unwrap();
if BYTE_WIDTH2 == 16 {
let iter = array
.iter()
.map(|v| v.and_then(|v| v.div_checked(div).ok()));
// rounding the result
let iter = array.iter().map(|v| {
v.map(|v| {
// the div must be gt_eq 10, we don't need to check the overflow for the `div`/`mod` operation
let d = v / div;
let r = v % div;
if v >= 0 && r >= half {
d + 1
} else if v < 0 && r <= neg_half {
d - 1
} else {
d
}
})
});
// let iter = array
// .iter()
// .map(|v| v.and_then(|v| v.div_checked(div).ok()));
let casted_array = unsafe {
PrimitiveArray::<Decimal128Type>::from_trusted_len_iter(iter)
};
Expand Down Expand Up @@ -3559,6 +3577,35 @@ mod tests {
.with_precision_and_scale(precision, scale)
}

#[test]
#[cfg(not(feature = "force_validate"))]
fn test_cast_decimal_to_decimal_round() {
let input_type = DataType::Decimal128(20, 4);
let output_type = DataType::Decimal128(20, 3);
assert!(can_cast_types(&input_type, &output_type));
let array = vec![
Some(1123454),
Some(2123456),
Some(-3123453),
Some(-3123456),
None,
];
let input_decimal_array = create_decimal_array(array, 20, 4).unwrap();
let array = Arc::new(input_decimal_array) as ArrayRef;
generate_cast_test_case!(
&array,
Decimal128Array,
&output_type,
vec![
Some(112345_i128),
Some(212346_i128),
Some(-312345_i128),
Some(-312346_i128),
None
]
);
}

#[test]
#[cfg(not(feature = "force_validate"))]
fn test_cast_decimal128_to_decimal128() {
Expand Down

0 comments on commit 949c6fd

Please sign in to comment.