Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal128 to Decimal256 Overflows #3265

Closed
tustvold opened this issue Dec 3, 2022 · 1 comment · Fixed by #3264
Closed

Decimal128 to Decimal256 Overflows #3265

tustvold opened this issue Dec 3, 2022 · 1 comment · Fixed by #3264
Assignees
Labels
arrow Changes to the arrow crate bug

Comments

@tustvold
Copy link
Contributor

tustvold commented Dec 3, 2022

Describe the bug

#[test]
    fn test_cast_decimal128_to_decimal256_negative() {
        let input_type = DataType::Decimal128(10, 3);
        let output_type = DataType::Decimal256(10, 5);
        assert!(can_cast_types(&input_type, &output_type));
        let array = vec![Some(i128::MAX), Some(i128::MIN)];
        let input_decimal_array = create_decimal_array(array, 10, 3).unwrap();
        let array = Arc::new(input_decimal_array) as ArrayRef;

        let hundred = i256::from_i128(100);
        generate_cast_test_case!(
            &array,
            Decimal256Array,
            &output_type,
            vec![
                Some(i256::from_i128(i128::MAX).mul_wrapping(hundred)),
                Some(i256::from_i128(i128::MIN).mul_wrapping(hundred))
            ]
        );
    }

Fails with

thread 'cast::tests::test_cast_decimal128_to_decimal256_negative' panicked at 'assertion failed: `(left == right)`
  left: `0`,
 right: `17014118346046923173168730371588410572700`', arrow-cast/src/cast.rs:7251:9

The issue is that it is not performing the scaling operation in the destination type

let iter = array.iter().map(|v| {
    v.and_then(|v| v.mul_checked(mul).ok().map(i256::from_i128))
});
let casted_array = unsafe {
    PrimitiveArray::<Decimal256Type>::from_trusted_len_iter(iter)
};
casted_array
    .with_precision_and_scale(*output_precision, *output_scale)
    .map(|a| Arc::new(a) as ArrayRef)

To Reproduce

Expected behavior

Additional context

@tustvold tustvold added the bug label Dec 3, 2022
@tustvold tustvold self-assigned this Dec 3, 2022
@tustvold tustvold changed the title Decimal128 to Decimal256 Silently Overflows Decimal128 to Decimal256 Overflows Dec 3, 2022
@alamb alamb added the arrow Changes to the arrow crate label Dec 9, 2022
@alamb
Copy link
Contributor

alamb commented Dec 9, 2022

label_issue.py automatically added labels {'arrow'} from #3264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants