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

Check overflow while casting floating point value to decimal128 #3021

Merged
merged 7 commits into from Nov 6, 2022

Conversation

viirya
Copy link
Member

@viirya viirya commented Nov 5, 2022

Which issue does this PR close?

Closes #3020.

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Nov 5, 2022
Copy link
Member Author

@viirya viirya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For floating point to decimal256 case, currently we convert f64 to i128 and read it as i256. We don't get if the f64 is out of range of decimal256 actually. This seems to be wrong.

I will deal with it in another PR.

if cast_options.safe {
let iter = array.iter().map(|v| {
v.and_then(|v| {
let mul_v = (mul * v.as_()).round() as i128;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this overflow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't need to validate against precision, the multiplication of f64 itself won't overflow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only case that might be "overflow" here is an infinity value. After converting to i128, it is i128::MAX that is over the range of decimal 128.

Comment on lines 362 to 364
if precision <= DECIMAL128_MAX_PRECISION
&& (mul_v > MAX_DECIMAL_FOR_EACH_PRECISION[precision as usize - 1]
|| mul_v < MIN_DECIMAL_FOR_EACH_PRECISION[precision as usize - 1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to validate precision, as we don't in other places. This is explicitly an opt-in, we only need to error if the underlying value is truncated/overflows - i.e. data loss has occurred.

&DataType::Decimal128(38, 30),
&CastOptions { safe: false },
);
assert!(casted_array.is_err());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically we do something like

let err = operation.unwrap_err().to_string();
assert!(err.contains("EXPECTED), "{}", err)

To avoid false positives

v
)))
} else {
Ok(mul_v as i128)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still confused as to how this can't overflow?

If the float value was i128::MAX any scale greater than 1 would result in this conversion overflowing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting a floating value larger than i128::MAX to i128 will get i128::MAX.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't overflow and won't wrapping back to smaller value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we also return errors for truncation when casting from say u64 to u32? I think it would a bit surprising to return an error for wrapping overflow but not saturating?

)))
} else {
let integer = mul_v as i128;
if integer == i128::MAX || integer == i128::MIN {
Copy link
Contributor

@tustvold tustvold Nov 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use TryInto here, or some other fallible conversion instead of as, I think this will be faster and also avoid a false positive on i128::MAX?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we need to implement From<f64> for i128?

error[E0277]: the trait bound `i128: From<f64>` is not satisfied

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... It seems support for this is unstable rust-lang/rust#67057

Perhaps we could use https://docs.rs/num/latest/num/trait.ToPrimitive.html ?

This appears to be correctly checked https://docs.rs/num-traits/0.2.14/src/num_traits/cast.rs.html#310. We could also copy this logic

.try_unary::<_, Decimal128Type, _>(|v| {
mul.mul_checked(v.as_()).and_then(|value| {
let mul_v = value.round();
let integer: i128 = mul_v.to_i128().ok_or_else(|| {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It covers f64::INFINITY, f64::MAX, etc. cases.

arrow-cast/src/cast.rs Outdated Show resolved Hide resolved
viirya and others added 2 commits November 5, 2022 21:55
Co-authored-by: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com>
@tustvold tustvold merged commit 108e7d2 into apache:master Nov 6, 2022
@ursabot
Copy link

ursabot commented Nov 6, 2022

Benchmark runs are scheduled for baseline = 4f525fe and contender = 108e7d2. 108e7d2 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@viirya
Copy link
Member Author

viirya commented Nov 6, 2022

Thanks @tustvold

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check overflow when casting floating point value to decimal128
3 participants