Skip to content

fix: Handle Decimal16 SqlDecimal values when shredding - #515

Merged
CurtHagenlocher merged 1 commit into
apache:mainfrom
dragonknightk:fix/498
Nov 30, 2026
Merged

CurtHagenlocher merged 1 commit into
apache:mainfrom
dragonknightk:fix/498

Conversation

@dragonknightk

Copy link
Copy Markdown
Contributor

What's Changed

Shredding a variant into a Decimal16 column threw InvalidCastException. VariantShredder extracts those values as SqlDecimal, but ShreddedVariantArrayBuilder.Build always cast them to System.Decimal. It also read the column scale only from System.Decimal values, so a Decimal16 column would have got scale 0. VariantUnshredder.Reconstruct had the same cast and threw for every Decimal16 typed value from the shredder.

BuildDecimalArray now takes the scale from SqlDecimal values too, and appends them with Decimal128Array.Builder.Append(SqlDecimal) so values outside the System.Decimal range stay exact. CreateVariantFromTyped now passes a SqlDecimal to FromDecimal16, and keeps a System.Decimal fallback for callers that still pass that type.

The new test Primitive_Decimal16 builds Decimal16 rows with different scales, including one value outside the System.Decimal range. It checks the column is Decimal128(38, 3) and every value keeps its exact digits, then round-trips through the reader. Reconstruct_Decimal16 covers the unshred path with a value outside the System.Decimal range, and a System.Decimal input that takes the compatibility fallback.

ShredSchema.FromArrowType dropping the precision and scale of Decimal128Type is a separate problem and is not changed here.

Closes #498.

AI Disclosure

Human-led contribution. Grok Bot, working for dragonknightk, assisted with this change, including the code, tests, and description. All submitted content was reviewed by a human before submission.

## What's Changed

Shredding a variant into a `Decimal16` column threw `InvalidCastException`. `VariantShredder` extracts those values as `SqlDecimal`, but `ShreddedVariantArrayBuilder.Build` always cast them to `System.Decimal`. It also read the column scale only from `System.Decimal` values, so a `Decimal16` column would have got scale 0. `VariantUnshredder.Reconstruct` had the same cast and threw for every `Decimal16` typed value from the shredder.

`BuildDecimalArray` now takes the scale from `SqlDecimal` values too, and appends them with `Decimal128Array.Builder.Append(SqlDecimal)` so values outside the `System.Decimal` range stay exact. `CreateVariantFromTyped` now passes a `SqlDecimal` to `FromDecimal16`, and keeps a `System.Decimal` fallback for callers that still pass that type.

The new test `Primitive_Decimal16` builds `Decimal16` rows with different scales, including one value outside the `System.Decimal` range. It checks the column is `Decimal128(38, 3)` and every value keeps its exact digits, then round-trips through the reader. `Reconstruct_Decimal16` covers the unshred path with a value outside the `System.Decimal` range, and a `System.Decimal` input that takes the compatibility fallback.

`ShredSchema.FromArrowType` dropping the precision and scale of `Decimal128Type` is a separate problem and is not changed here.

Closes issue 498

Signed-off-by: dragonknightk <37928252+dragonknightk@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the Decimal16 shredding and reconstruction changes.

No open issues 🎉

What changed in this PR

Fixes Decimal16 shredding and reconstruction failures described in #498 by handling SqlDecimal directly, preserving values beyond the System.Decimal range.

Changes:

  • Derives column scale from SqlDecimal values and appends them without narrowing conversions.
  • Reconstructs Decimal16 values from SqlDecimal while retaining compatibility with System.Decimal inputs.
  • Adds regression coverage for mixed scales, exact large values, and both reconstruction paths.
File Description
test/​Apache.Arrow.Operations.Tests/​Shredding/​VariantUnshredderTests.cs Tests large Decimal16 reconstruction and the compatibility fallback.
test/​Apache.Arrow.Operations.Tests/​Shredding/​ShreddedVariantArrayBuilderTests.cs Tests column precision, mixed scales, exact values, and reader round-trips.
src/​Apache.Arrow.Operations/​Shredding/​VariantUnshredder.cs Handles SqlDecimal during Decimal16 reconstruction.
src/​Apache.Arrow.Operations/​Shredding/​ShreddedVariantArrayBuilder.cs Reads SqlDecimal scales and uses the matching append overload.

Review effort: Balanced

@CurtHagenlocher CurtHagenlocher 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.

Thanks!

@CurtHagenlocher
CurtHagenlocher merged commit 045e4a1 into apache:main Nov 30, 2026
14 checks passed
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.

ShreddedVariantArrayBuilder throws InvalidCastException for Decimal16 typed values

3 participants