Skip to content

Cast numeric to decimal256#2923

Merged
viirya merged 5 commits into
apache:masterfrom
viirya:cast_numeric_to_decimal256
Oct 27, 2022
Merged

Cast numeric to decimal256#2923
viirya merged 5 commits into
apache:masterfrom
viirya:cast_numeric_to_decimal256

Conversation

@viirya

@viirya viirya commented Oct 25, 2022

Copy link
Copy Markdown
Member

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
@viirya viirya force-pushed the cast_numeric_to_decimal256 branch from e3aa805 to bf7f475 Compare October 25, 2022 07:13
@alamb alamb requested a review from liukun4515 October 26, 2022 21:36
@alamb

alamb commented Oct 27, 2022

Copy link
Copy Markdown
Contributor

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

@tustvold tustvold left a comment

Copy link
Copy Markdown
Contributor

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 thread arrow/src/compute/kernels/cast.rs Outdated
Comment on lines +372 to +373
// with_precision_and_scale validates the
// value is within range for the output precision

Copy link
Copy Markdown
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 thread arrow/src/compute/kernels/cast.rs Outdated
Comment on lines +412 to +413
// with_precision_and_scale validates the
// value is within range for the output precision

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here

Comment thread arrow/src/compute/kernels/cast.rs Outdated
where
<T as ArrowPrimitiveType>::Native: AsPrimitive<i256>,
{
let mul: i256 = i256::from_i128(10_i128.pow(scale as u32));

Copy link
Copy Markdown
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

viirya commented Oct 27, 2022

Copy link
Copy Markdown
Member Author

Merged. Thanks.

@ursabot

ursabot commented Oct 28, 2022

Copy link
Copy Markdown

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