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

Cast numeric to decimal256 #2923

Merged
merged 5 commits into from Oct 27, 2022
Merged

Conversation

viirya
Copy link
Member

@viirya viirya commented Oct 25, 2022

Which issue does this PR close?

Closes #2922.

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 Oct 25, 2022
@alamb
Copy link
Contributor

alamb commented Oct 27, 2022

@tustvold would it be possible to review this PR (so we can get it into the release candidate I plan to make tomorrow)?

Copy link
Contributor

@tustvold tustvold left a comment

Choose a reason for hiding this comment

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

I have a couple of reservations about merging this as is:

  • The decimal256 conversion will silently wrap at the boundary of an i128
  • The conversion in general is unchecked, which is inconsistent with the other numeric conversions

The former I think should be fixed before merge, the latter was already inconsistent for decimal128 so can probably slide

Comment on lines 372 to 373
// with_precision_and_scale validates the
// value is within range for the output precision
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment isn't correct anymore

Comment on lines 412 to 413
// with_precision_and_scale validates the
// value is within range for the output precision
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

where
<T as ArrowPrimitiveType>::Native: AsPrimitive<i256>,
{
let mul: i256 = i256::from_i128(10_i128.pow(scale as u32));
Copy link
Contributor

Choose a reason for hiding this comment

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

This can overflow

(i64::MAX as i128).checked_mul(10_i128.checked_pow(DECIMAL256_MAX_SCALE as _).unwrap()).unwrap()

I think this needs a pow function on i256 and to then compute based on this

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I missed this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Add pow to i256: #2955


// with_precision_and_scale validates the
// value is within range for the output precision
cast_primitive_to_decimal256(array, |v| v.as_().wrapping_mul(mul), precision, scale)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am aware the decimal128 conversions did this before, but this can silently overflow. This is inconsistent with how we handle other integer conversions in cast_numeric_arrays which uses https://docs.rs/num-traits/latest/num_traits/cast/trait.NumCast.html

Copy link
Member Author

Choose a reason for hiding this comment

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

We can fix decimal128 together later.

@viirya viirya mentioned this pull request Oct 27, 2022
@viirya viirya merged commit 73416f8 into apache:master Oct 27, 2022
@viirya
Copy link
Member Author

viirya commented Oct 27, 2022

Merged. Thanks.

@ursabot
Copy link

ursabot commented Oct 28, 2022

Benchmark runs are scheduled for baseline = b4872b7 and contender = 73416f8. 73416f8 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

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.

Cast numeric to decimal256
4 participants