ump45nose commented on code in PR #1519:
URL: https://github.com/apache/arrow-go/pull/1519#discussion_r4563646047


##########
arrow/decimal128/decimal128.go:
##########
@@ -238,9 +239,41 @@ func FromFloat64(v float64, prec, scale int32) (Num, 
error) {
        return fromPositiveFloat64(v, prec, scale)
 }
 
-var pt5 = big.NewFloat(0.5)
-
 func FromString(v string, prec, scale int32) (n Num, err error) {
+       if lit, ok := decimalstr.Parse(v); ok {
+               return fromDecimalLiteral(lit, prec, scale)
+       }
+       return fromFloatLiteral(v, prec, scale)
+}
+
+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) {
+                       return Num{}, errors.New("bitlen too large for 
decimal128")
+               }
+               if scale < -MaxScale {
+                       return Num{}, fmt.Errorf("scale %d out of range for 
decimal128, must be between %d and %d", scale, -MaxScale, MaxScale)
+               }
+       } else if scale > MaxScale {
+               return Num{}, fmt.Errorf("scale %d out of range for decimal128, 
must be between %d and %d", scale, -MaxScale, MaxScale)
+       } else if lit.AtLeastTwoPower(int64(scale), 128) {
+               return Num{}, errors.New("bitlen too large for decimal128")
+       }
+
+       val, ok := lit.ScaleAndRound(int64(scale))

Review Comment:
   Thanks for catching this. Fixed in b262694 by dropping the `down > 
maxDigits` limit in `ScaleAndRound`. Because ties round away from zero, it now 
cuts the digit string at the rounding place and checks only the first dropped 
digit, with no division. Only the kept digits are still bounded. This one 
change covers `decimal128`, `decimal256` and `Decimal32`/`Decimal64`, since 
they all call the same helper.
   
   `0.` followed by 1030 or 5000 sevens at scale 2 now returns 78 again, and 
the negative case returns -78. I added tests with fractions longer than 1024 
digits to `decimalstr` (including a long run of trailing 9s and `0.125` 
followed by 2000 zeros) and to the `decimal128`, `decimal256` and `decimal` 
`FromString` tests. The new tests fail on 793bc05. I also compared the helper 
with exact `big.Rat` rounding on 200,000 random literals, about 2% of them with 
1000-2500 fraction digits, and found 0 mismatches. `go test` passes for 
`./arrow/internal/decimalstr`, `./arrow/decimal{,128,256}`, `./arrow/array`, 
`./arrow/scalar`, `./parquet/schema` and `./parquet/variant`.
   
   (AI-assisted reply.)
   



-- 
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]

Reply via email to