Skip to content

Improvements to decimal conversion#292

Merged
CurtHagenlocher merged 3 commits into
apache:mainfrom
CurtHagenlocher:GH-247
Mar 25, 2026
Merged

Improvements to decimal conversion#292
CurtHagenlocher merged 3 commits into
apache:mainfrom
CurtHagenlocher:GH-247

Conversation

@CurtHagenlocher
Copy link
Copy Markdown
Contributor

What's Changed

  • Fixes DecimalUtility.GetDecimal to correctly handle high-scale values (e.g., Decimal256 with precision 76, scale 38) that previously threw OverflowException — addresses issue Decimal256Array could be a little more forgiving #247
  • Adds a fast path using Int128 on .NET 7+ that avoids BigInteger allocation for values that fit in 128 bits
  • Replaces the old fractional-part overflow check (which threw) with FractionToDecimal, which gracefully reduces precision to fit within decimal's ~28-digit mantissa

Closes #247.
Partially addresses #96.

…ues (e.g., Decimal256 with precision 76, scale 38) that previously threw OverflowException — addresses issue apache#247

  - Adds a fast path using Int128 on .NET 7+ that avoids BigInteger allocation for values that fit in 128 bits
  - Replaces the old fractional-part overflow check (which threw) with FractionToDecimal, which gracefully reduces precision to fit within decimal's ~28-digit mantissa
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves decimal conversion for Arrow decimal arrays, fixing high-scale conversion overflows (notably Decimal256 precision 76 / scale 38) and adding a .NET 7+ fast path to reduce BigInteger allocations.

Changes:

  • Fixes DecimalUtility.GetDecimal to handle high-scale values more gracefully by reducing fractional precision instead of throwing.
  • Adds a .NET 7+ (net8.0 in this repo) Int128-based conversion fast path for 128-bit values (and 256-bit values that sign-extend into 128 bits).
  • Expands test coverage for high-precision/scale GetValue and introduces a new benchmark for decimal access patterns.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Apache.Arrow/DecimalUtility.cs Adds Int128 fast path and replaces fractional overflow throwing with truncation-based conversion.
src/Apache.Arrow/Arrays/Decimal128Array.cs Adds TryGetValue convenience API for decimal retrieval.
src/Apache.Arrow/Arrays/Decimal256Array.cs Adds TryGetValue convenience API for decimal retrieval.
test/Apache.Arrow.Tests/DecimalUtilityTests.cs Updates expectations so high-scale Decimal256 conversion no longer throws.
test/Apache.Arrow.Tests/Decimal128ArrayTests.cs Adds high-scale GetValue/TryGetValue tests for Decimal128.
test/Apache.Arrow.Tests/Decimal256ArrayTests.cs Adds high-scale GetValue/TryGetValue tests for Decimal256 (issue #247 scenario).
test/Apache.Arrow.Benchmarks/DecimalArrayBenchmark.cs Adds new BenchmarkDotNet benchmarks for decimal conversions/accessors.
.gitignore Ignores BenchmarkDotNet output artifacts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Apache.Arrow/Arrays/Decimal128Array.cs Outdated
Comment thread src/Apache.Arrow/Arrays/Decimal256Array.cs Outdated
Comment thread src/Apache.Arrow/DecimalUtility.cs Outdated
Comment thread test/Apache.Arrow.Benchmarks/DecimalArrayBenchmark.cs
Comment thread test/Apache.Arrow.Benchmarks/DecimalArrayBenchmark.cs
Copy link
Copy Markdown
Contributor

@adamreeve adamreeve left a comment

Choose a reason for hiding this comment

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

Looks good thanks Curt, I just have some minor suggestions

Comment thread src/Apache.Arrow/Arrays/Decimal128Array.cs
Comment thread test/Apache.Arrow.Tests/Decimal128ArrayTests.cs
@CurtHagenlocher CurtHagenlocher merged commit 009762e into apache:main Mar 25, 2026
14 checks passed
@CurtHagenlocher CurtHagenlocher deleted the GH-247 branch April 23, 2026 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decimal256Array could be a little more forgiving

3 participants