fix(arrow/decimal): widen Decimal64 rescale to 64 bits - #1307
Open
winklemad wants to merge 1 commit into
Open
Conversation
Decimal64.rescaleWouldCauseDataLoss was a copy of the Decimal32 version and still used bits.Div32/bits.Mul32 with uint32(n)/uint32(multiplier), truncating any Decimal64 value above uint32 max (~4.29e9) to 32 bits. Rescale then returned a corrupt value and/or a spurious "rescale data loss" error for values that are well within Decimal64's range, e.g. Decimal64(1e10).Rescale(2, 1) returned 141006540 with an error instead of 1000000000. Use bits.Div64/bits.Mul64 over uint64. The existing test asserted the truncated behavior for Decimal64; corrected it and added a genuine overflow case. Signed-off-by: Madan Kumar <winklemad@outlook.com>
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.
Rationale for this change
Decimal64.rescaleWouldCauseDataLoss(arrow/decimal/decimal.go) is a copy of theDecimal32version that was never widened to 64 bits — it still usesbits.Div32/bits.Mul32withuint32(n)/uint32(multiplier). AnyDecimal64whose magnitude exceedsuint32max (~4.29e9, well inside Decimal64's 18-digit range) is truncated to 32 bits before the multiply/divide, soRescalereturns a corrupt value and/or a spuriousrescale data losserror.Concretely, on
main:What changes are included in this PR?
bits.Div64/bits.Mul64overuint64inDecimal64.rescaleWouldCauseDataLoss(theDecimal32version is already correct for 32 bits).TestDecimalRescalehad baked in the truncated behavior: it asserted thatDecimal64(555555).Rescale(0, 5)returnsrescale data loss, but555555at scale 5 is55,555,500,000, which fits inDecimal64and is lossless. Corrected that assertion, checked the exact result, and added a value that genuinely overflowsDecimal64(5e18rescaled up) so the real data-loss path stays covered.RED→GREEN verified: the updated
TestDecimalRescalefails on the unpatched code and passes with the fix; the fullarrow/decimalpackage suite is green;gofmt/go vet/golangci-lintclean.Are these changes tested?
Yes —
arrow/decimal/decimal_test.go.Are there any user-facing changes?
Decimal64.Rescalenow returns correct results (and no spurious error) for values above ~4.29e9. No API change.