Repository navigation
fix: Handle Decimal16 SqlDecimal values when shredding - #515
Merged
Merged
Conversation
## 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>
There was a problem hiding this comment.
🟢 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
SqlDecimalvalues and appends them without narrowing conversions. - Reconstructs Decimal16 values from
SqlDecimalwhile retaining compatibility withSystem.Decimalinputs. - 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's Changed
Shredding a variant into a
Decimal16column threwInvalidCastException.VariantShredderextracts those values asSqlDecimal, butShreddedVariantArrayBuilder.Buildalways cast them toSystem.Decimal. It also read the column scale only fromSystem.Decimalvalues, so aDecimal16column would have got scale 0.VariantUnshredder.Reconstructhad the same cast and threw for everyDecimal16typed value from the shredder.BuildDecimalArraynow takes the scale fromSqlDecimalvalues too, and appends them withDecimal128Array.Builder.Append(SqlDecimal)so values outside theSystem.Decimalrange stay exact.CreateVariantFromTypednow passes aSqlDecimaltoFromDecimal16, and keeps aSystem.Decimalfallback for callers that still pass that type.The new test
Primitive_Decimal16buildsDecimal16rows with different scales, including one value outside theSystem.Decimalrange. It checks the column isDecimal128(38, 3)and every value keeps its exact digits, then round-trips through the reader.Reconstruct_Decimal16covers the unshred path with a value outside theSystem.Decimalrange, and aSystem.Decimalinput that takes the compatibility fallback.ShredSchema.FromArrowTypedropping the precision and scale ofDecimal128Typeis 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.