zeroshade commented on code in PR #1707:
URL: https://github.com/apache/arrow-go/pull/1707#discussion_r4743190694
##########
arrow/decimal128/decimal128.go:
##########
@@ -248,9 +248,9 @@ func FromString(v string, prec, scale int32) (n Num, err
error) {
func fromDecimalLiteral(lit decimalstr.Parsed, prec, scale int32) (n Num, err
error) {
if scale < 0 {
- // Reject an unscaled magnitude that would not fit in signed
128-bit
- // storage, before using the scale.
- if lit.AtLeastTwoPower(0, 127) {
+ // Check the magnitude after applying the negative scale: a
large
+ // literal can scale down to a value that fits.
+ if lit.AtLeastTwoPower(int64(scale), 127) {
Review Comment:
With a negative scale, the bit-length check runs before the scale-range
check; with a positive scale it runs after. So `"1e40"` at scale -39 now
reports the scale error, but `"1e100"` at scale -39 still reports "bitlen too
large".
Could both signs check the scale range first and then call
`AtLeastTwoPower(int64(scale), ...)` once? That would also merge the 127 and
128 bounds used by the two branches, since the `BitLen()` check after
`ScaleAndRound` is what enforces the storage limit. The same applies to
`fromDecimalLiteral256` in `arrow/decimal/decimal256.go`.
##########
arrow/decimal/decimal.go:
##########
@@ -199,9 +199,9 @@ func fromDecimalLiteral[T interface {
FitsInPrecision(int32) bool
}](lit decimalstr.Parsed, prec, scale int32, nbits uint) (n T, err error) {
if scale < 0 {
- // Check the unscaled magnitude before using the scale,
matching the
- // previous MantExp > nbits bound (|x| >= 2^nbits).
- if lit.AtLeastTwoPower(0, int(nbits)) {
+ // Check the magnitude after applying the negative scale: a
large
+ // literal can scale down to a value that fits.
+ if lit.AtLeastTwoPower(int64(scale), int(nbits)) {
return n, fmt.Errorf("bitlen too large for decimal%d",
nbits)
}
} else if lit.AtLeastTwoPower(int64(scale), int(nbits)) {
Review Comment:
Both branches now make the same call, so this can collapse to a single `if
lit.AtLeastTwoPower(int64(scale), int(nbits)) { ... }`.
##########
arrow/decimal/decimal.go:
##########
@@ -199,9 +199,9 @@ func fromDecimalLiteral[T interface {
FitsInPrecision(int32) bool
}](lit decimalstr.Parsed, prec, scale int32, nbits uint) (n T, err error) {
if scale < 0 {
Review Comment:
Unlike decimal128/decimal256, the generic path never checks the scale range.
Before this change, the check on the unscaled literal at least rejected large
literals. Now `Decimal32FromString("1e1000000000", 9, -1000000000)` returns `1`
with no error. Small literals were already accepted at any scale on main (`"1"`
at scale -1e9 gives 0).
Could you add the same range check the wider types have, using
`MaxScale[T]()`, and a test for it?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]