Skip to content

Commit

Permalink
fix: issue introduced in #6833 - less than equal check for scale in d…
Browse files Browse the repository at this point in the history
…ecimal conversion (#7070)

* fix <= check for scale in decimal conversion

* Update arrow-cast/src/cast/mod.rs

name change

Co-authored-by: Arttu <Blizzara@users.noreply.github.com>

* remove incorrect comment

---------

Co-authored-by: Arttu <Blizzara@users.noreply.github.com>
  • Loading branch information
himadripal and Blizzara authored Feb 5, 2025
1 parent 9327f47 commit 1019f5b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 1 addition & 2 deletions arrow-cast/src/cast/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ where
let array: PrimitiveArray<T> =
if input_scale == output_scale && input_precision <= output_precision {
array.clone()
} else if input_scale < output_scale {
// the scale doesn't change, but precision may change and cause overflow
} else if input_scale <= output_scale {
convert_to_bigger_or_equal_scale_decimal::<T, T>(
array,
input_scale,
Expand Down
21 changes: 20 additions & 1 deletion arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9980,7 +9980,26 @@ mod tests {
};
let result = cast_with_options(&array, &output_type, &options);
assert_eq!(result.unwrap_err().to_string(),
"Invalid argument error: 123456790 is too large to store in a Decimal128 of precision 6. Max is 999999");
"Invalid argument error: 123456789 is too large to store in a Decimal128 of precision 6. Max is 999999");
}

#[test]
fn test_decimal_to_decimal_same_scale() {
let array = vec![Some(520)];
let array = create_decimal_array(array, 4, 2).unwrap();
let input_type = DataType::Decimal128(4, 2);
let output_type = DataType::Decimal128(3, 2);
assert!(can_cast_types(&input_type, &output_type));

let options = CastOptions {
safe: false,
..Default::default()
};
let result = cast_with_options(&array, &output_type, &options);
assert_eq!(
result.unwrap().as_primitive::<Decimal128Type>().value(0),
520
);
}

#[test]
Expand Down

0 comments on commit 1019f5b

Please sign in to comment.