fix: Improve ergonomics for the Decimal type#341
fix: Improve ergonomics for the Decimal type#341Divyanshu-s13 wants to merge 4 commits intoapache:mainfrom
Conversation
- Comprehensive tests for isNegativeDecimal, negateDecimal - Tests for toDecimalString and toDecimalNumber conversions - Tests for fromDecimalString parsing with roundtrips - Integration tests for negation and edge cases
|
Thanks for these utilities. I think we should try to use them in the default converters so that #77 gets fixed. Either you could do this here or we can do it in a follow up when you fixed the CI errors and we merged. |
I have fixed CI error can you merge it. |
|
Could you check lint failures? https://github.com/apache/arrow-js/actions/runs/19644239168/job/56300291328?pr=341#step:5:8 FYI: You can run CI on your fork by enabling GitHub Actions on your fork. |
- Change 'let n' to 'const n' (prefer-const) - Use ternary for magnitude assignment (unicorn/prefer-ternary) - Use ternary for result composition (unicorn/prefer-ternary)
I have fix this can you merge it. |
| } | ||
|
|
||
| // Calculate divisor as 10^scale | ||
| // Using a loop instead of BigInt exponentiation (**) for ES2015 compatibility |
There was a problem hiding this comment.
Should we bump to something beyond es2015 for better code?
|
It might be worth it to include Decimal256 support (as suggested by Coderabbit) |
|
I started looking at this in November. Have you seen my question in #341 (comment)? |
domoritz
left a comment
There was a problem hiding this comment.
Thanks for making these utilities. I wonder, can we also use some of these utilities in other tests that already exist?
| * ``` | ||
| * @ignore | ||
| */ | ||
| export function toDecimalString(value: Uint32Array, scale: number): string { |
There was a problem hiding this comment.
Can you test this with Decimal256? I'm not 100% sure this method support it.
| } | ||
|
|
||
| // Trim trailing zeros in fractional part | ||
| fracPart = fracPart.replace(/0+$/, ''); |
There was a problem hiding this comment.
Wouldn't this mean that a scale of 5 would still return "0.0" rather than "0.00000"?
| * ``` | ||
| * @ignore | ||
| */ | ||
| export function fromDecimalString(str: string, scale: number): Uint32Array { |
There was a problem hiding this comment.
If toDecimalString supports Decimal256, shouldn't this support it as well?
| const [wholePart = '0', fracPart = ''] = str.split('.'); | ||
|
|
||
| // Pad or truncate fractional part to match scale | ||
| const adjustedFrac = (fracPart + '0'.repeat(scale)).slice(0, scale); |
There was a problem hiding this comment.
If the input string has more fractional digits than scale, are they silently truncated without rounding?
| }; | ||
|
|
||
| // Detect sign via MSB of most-significant word | ||
| const isNegative = (value[3] & 0x80000000) !== 0; |
There was a problem hiding this comment.
Why is this not the same as isNegativeDecimal?
What's Changed
Added a new decimal.ts module that provides high-level utilities for working with Decimal128 and Decimal256 values in JavaScript. These utilities make decimal handling more ergonomic by removing the need for low-level bit manipulation and aligning behavior with Arrow C++ semantics. The module is exported through the util namespace and integrates cleanly without introducing breaking changes.
Closes #81.