hcrosse commented on code in PR #1646:
URL: https://github.com/apache/arrow-go/pull/1646#discussion_r4645826763


##########
arrow/decimal/decimal256.go:
##########
@@ -0,0 +1,836 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package decimal
+
+import (
+       "errors"
+       "fmt"
+       "math"
+       "math/big"
+       "math/bits"
+       "strings"
+
+       "github.com/apache/arrow-go/v18/arrow/decimal128"
+       "github.com/apache/arrow-go/v18/arrow/internal/debug"
+       "github.com/apache/arrow-go/v18/arrow/internal/decimalstr"
+)
+
+const (
+       maxPrecision256 = 76
+       maxScale256     = 76
+)
+
+func Decimal256MaxValue(prec int32) Decimal256 {
+       return scaleMultipliers256[prec].Sub(Decimal256FromU64(1))
+}
+
+type Decimal256 struct {
+       // arr[0] is the lowest bits, arr[3] is the highest bits
+       arr [4]uint64
+}
+
+// NewDecimal256 returns a new signed 256-bit integer value where x1 contains
+// the highest bits with the rest of the values in order down to the
+// lowest bits
+//
+//     ie: NewDecimal256(1, 2, 3, 4) returns with the elements in 
little-endian order
+//         {4, 3, 2, 1} but each value is still represented as the native 
endianness
+func NewDecimal256(x1, x2, x3, x4 uint64) Decimal256 {
+       return Decimal256{[4]uint64{x4, x3, x2, x1}}
+}
+
+func (n Decimal256) Array() [4]uint64 { return n.arr }
+
+func (n Decimal256) LowBits() uint64 { return n.arr[0] }
+
+// Like Decimal256FromI64, this stays a single composite literal.
+func Decimal256FromDecimal128(n decimal128.Num) Decimal256 {
+       top := uint64(n.HighBits() >> 63)
+       return Decimal256{[4]uint64{n.LowBits(), uint64(n.HighBits()), top, 
top}}
+}
+
+func Decimal256FromU64(v uint64) Decimal256 {
+       return Decimal256{[4]uint64{v, 0, 0, 0}}
+}
+
+// Keep this a single composite literal. Callers that inline it then build the
+// value in place instead of copying it through stack temporaries.
+func Decimal256FromI64(v int64) Decimal256 {
+       s := uint64(v >> 63)
+       return Decimal256{[4]uint64{uint64(v), s, s, s}}
+}
+
+func (n Decimal256) Negate() Decimal256 {
+       var carry uint64 = 1
+       for i := range n.arr {
+               n.arr[i] = ^n.arr[i] + carry
+               if n.arr[i] != 0 {
+                       carry = 0
+               }
+       }
+       return n
+}
+
+func (n Decimal256) Add(rhs Decimal256) Decimal256 {
+       var carry uint64
+       for i, v := range n.arr {
+               n.arr[i], carry = bits.Add64(v, rhs.arr[i], carry)
+       }
+       return n
+}
+
+func (n Decimal256) Sub(rhs Decimal256) Decimal256 {
+       return n.Add(rhs.Negate())
+}
+
+func (n Decimal256) Mul(rhs Decimal256) Decimal256 {
+       b := n.BigInt()
+       return Decimal256FromBigInt(b.Mul(b, rhs.BigInt()))
+}
+
+func (n Decimal256) Div(rhs Decimal256) (res, rem Decimal256) {
+       b := n.BigInt()
+       out, remainder := b.QuoRem(b, rhs.BigInt(), &big.Int{})
+       return Decimal256FromBigInt(out), Decimal256FromBigInt(remainder)
+}
+
+func (n Decimal256) Pow(rhs Decimal256) Decimal256 {
+       b := n.BigInt()
+       return Decimal256FromBigInt(b.Exp(b, rhs.BigInt(), nil))
+}
+
+func Decimal256FromString(v string, prec, scale int32) (n Decimal256, err 
error) {
+       if lit, ok := decimalstr.Parse(v); ok {
+               return fromDecimalLiteral256(lit, prec, scale)
+       }
+       return fromFloatLiteral256(v, prec, scale)
+}
+
+func fromDecimalLiteral256(lit decimalstr.Parsed, prec, scale int32) (n 
Decimal256, err error) {
+       if scale < 0 {
+               // Reject an unscaled magnitude that would not fit in signed 
256-bit
+               // storage, before using the scale.
+               if lit.AtLeastTwoPower(0, 255) {
+                       return Decimal256{}, errors.New("bitlen too large for 
decimal256")
+               }
+               if scale < -maxScale256 {
+                       return Decimal256{}, fmt.Errorf("scale %d out of range 
for decimal256, must be between %d and %d", scale, -maxScale256, maxScale256)
+               }
+       } else if scale > maxScale256 {
+               return Decimal256{}, fmt.Errorf("scale %d out of range for 
decimal256, must be between %d and %d", scale, -maxScale256, maxScale256)
+       } else if lit.AtLeastTwoPower(int64(scale), 256) {
+               return Decimal256{}, errors.New("bitlen too large for 
decimal256")
+       }
+
+       val, ok := lit.ScaleAndRound(int64(scale))
+       if !ok || val.BitLen() > 255 {
+               return Decimal256{}, errors.New("bitlen too large for 
decimal256")
+       }
+       n = Decimal256FromBigInt(val)
+       if !n.FitsInPrecision(prec) {
+               err = fmt.Errorf("value %v doesn't fit in precision %d", n, 
prec)
+       }
+       return
+}
+
+func fromFloatLiteral256(v string, prec, scale int32) (n Decimal256, err 
error) {
+       // time for some math!
+       // Our input precision means "number of digits of precision" but the
+       // math/big library refers to precision in floating point terms
+       // where it refers to the "number of bits of precision in the mantissa".
+       // So we need to figure out how many bits we should use for precision,
+       // based on the input precision. Too much precision and we aren't 
rounding
+       // when we should. Too little precision and we round when we shouldn't.
+       //
+       // In general, the number of decimal digits you get from a given number
+       // of bits will be:
+       //
+       //      digits = log[base 10](2^nbits)
+       //
+       // it thus follows that:
+       //
+       //      digits = nbits * log[base 10](2)
+       //  nbits = digits / log[base 10](2)
+       //
+       // So we need to account for our scale since we're going to be 
multiplying
+       // by 10^scale in order to get the integral value we're actually going 
to use
+       // So to get our number of bits we do:
+       //
+       //      (prec + scale + 1) / log[base10](2)
+       //
+       // Finally, we still have a sign bit, so we -1 to account for the sign 
bit.
+       // Aren't floating point numbers fun?
+       var precInBits = uint(math.Round(float64(prec+scale+1)/math.Log10(2))) 
+ 1
+
+       var out *big.Float
+       out, _, err = big.ParseFloat(v, 10, 255, big.ToNearestEven)
+       if err != nil {
+               return
+       }
+       if out.IsInf() {
+               return Decimal256{}, fmt.Errorf("cannot convert %s to 
decimal256", v)
+       }
+
+       if scale < 0 {
+               // Check the magnitude before converting, which would allocate 
as many
+               // bits as the value has.
+               if out.MantExp(nil) > 255 {
+                       return Decimal256{}, errors.New("bitlen too large for 
decimal256")
+               }
+               if scale < -maxScale256 {
+                       return Decimal256{}, fmt.Errorf("scale %d out of range 
for decimal256, must be between %d and %d", scale, -maxScale256, maxScale256)
+               }
+       } else if scale > maxScale256 {
+               return Decimal256{}, fmt.Errorf("scale %d out of range for 
decimal256, must be between %d and %d", scale, -maxScale256, maxScale256)
+       }
+
+       if scale != 0 {
+               if scale > 0 {
+                       out.Mul(out, 
(&big.Float{}).SetInt(scaleMultipliers256[scale].BigInt())).SetPrec(precInBits)
+               } else {
+                       out.Quo(out, 
(&big.Float{}).SetInt(scaleMultipliers256[-scale].BigInt())).SetPrec(precInBits)
+               }
+               // Reject before rounding and converting, which would allocate 
as many
+               // bits as the value has.
+               if scale > 0 && (out.IsInf() || out.MantExp(nil) > 256) {
+                       return Decimal256{}, errors.New("bitlen too large for 
decimal256")
+               }
+       }
+       val := decimalstr.RoundScaled(out, v, int64(scale))
+       if val.BitLen() > 255 {
+               return Decimal256{}, errors.New("bitlen too large for 
decimal256")
+       }
+       n = Decimal256FromBigInt(val)
+       if !n.FitsInPrecision(prec) {
+               err = fmt.Errorf("value %v doesn't fit in precision %d", n, 
prec)
+       }
+       return
+}
+
+func Decimal256FromFloat32(v float32, prec, scale int32) (Decimal256, error) {
+       debug.Assert(prec > 0 && prec <= 76, "invalid precision for converting 
to decimal256")
+
+       if math.IsInf(float64(v), 0) {
+               return Decimal256{}, fmt.Errorf("cannot convert %f to 
decimal256", v)
+       }
+
+       if v < 0 {
+               dec, err := fromPositiveFloat32_256(-v, prec, scale)
+               if err != nil {
+                       return dec, err
+               }
+               return dec.Negate(), nil
+       }
+       return fromPositiveFloat32_256(v, prec, scale)
+}
+
+func Decimal256FromFloat(v float64, prec, scale int32) (Decimal256, error) {
+       debug.Assert(prec > 0 && prec <= 76, "invalid precision for converting 
to decimal256")
+
+       if math.IsInf(v, 0) {
+               return Decimal256{}, fmt.Errorf("cannot convert %f to 
decimal256", v)
+       }
+
+       if v < 0 {
+               dec, err := fromPositiveFloat64_256(-v, prec, scale)
+               if err != nil {
+                       return dec, err
+               }
+               return dec.Negate(), nil
+       }
+       return fromPositiveFloat64_256(v, prec, scale)
+}
+
+// this has to exist despite sharing some code with fromPositiveFloat64_256
+// because if we don't do the casts back to float32 in between each
+// step, we end up with a significantly different answer!
+// Aren't floating point values so much fun?
+//
+// example value to use:
+//
+//     v := float32(1.8446746e+15)
+//
+// You'll end up with a different values if you do:
+//
+//     Decimal256FromFloat(float64(v), 20, 4)
+//
+// vs
+//
+//     Decimal256FromFloat32(v, 20, 4)
+//
+// because float64(v) == 1844674629206016 rather than 1844674600000000
+func fromPositiveFloat32_256(v float32, prec, scale int32) (Decimal256, error) 
{
+       val, err := scalePositiveFloat64_256(float64(v), prec, scale)
+       if err != nil {
+               return Decimal256{}, err
+       }
+
+       v = float32(val)
+       if math.IsInf(float64(v), 1) {
+               // the scaled value is past the float32 range but can still fit
+               // in a decimal256, so split it as a float64 instead
+               return splitPositiveFloat64_256(val), nil
+       }
+
+       var arr [4]float32
+       arr[3] = float32(math.Floor(math.Ldexp(float64(v), -192)))
+       v -= float32(math.Ldexp(float64(arr[3]), 192))
+       arr[2] = float32(math.Floor(math.Ldexp(float64(v), -128)))
+       v -= float32(math.Ldexp(float64(arr[2]), 128))
+       arr[1] = float32(math.Floor(math.Ldexp(float64(v), -64)))
+       v -= float32(math.Ldexp(float64(arr[1]), 64))
+       arr[0] = v
+
+       debug.Assert(arr[3] >= 0, "bad conversion float64 to decimal256")
+       debug.Assert(arr[3] < 1.8446744073709552e+19, "bad conversion float64 
to decimal256") // 2**64
+       debug.Assert(arr[2] >= 0, "bad conversion float64 to decimal256")
+       debug.Assert(arr[2] < 1.8446744073709552e+19, "bad conversion float64 
to decimal256") // 2**64
+       debug.Assert(arr[1] >= 0, "bad conversion float64 to decimal256")
+       debug.Assert(arr[1] < 1.8446744073709552e+19, "bad conversion float64 
to decimal256") // 2**64
+       debug.Assert(arr[0] >= 0, "bad conversion float64 to decimal256")
+       debug.Assert(arr[0] < 1.8446744073709552e+19, "bad conversion float64 
to decimal256") // 2**64
+       return Decimal256{[4]uint64{uint64(arr[0]), uint64(arr[1]), 
uint64(arr[2]), uint64(arr[3])}}, nil
+}
+
+func scalePositiveFloat64_256(v float64, prec, scale int32) (float64, error) {
+       var pscale float64
+       if scale >= -76 && scale <= 76 {
+               pscale = float64PowersOfTen256[scale+76]
+       } else {
+               pscale = math.Pow10(int(scale))
+       }
+
+       v *= pscale
+       v = math.RoundToEven(v)
+       maxabs := float64PowersOfTen256[prec+76]
+       if v <= -maxabs || v >= maxabs {
+               return 0, fmt.Errorf("cannot convert %f to 
decimal256(precision=%d, scale=%d): overflow",
+                       v, prec, scale)
+       }
+       return v, nil
+}
+
+func fromPositiveFloat64_256(v float64, prec, scale int32) (Decimal256, error) 
{
+       val, err := scalePositiveFloat64_256(v, prec, scale)
+       if err != nil {
+               return Decimal256{}, err
+       }
+
+       return splitPositiveFloat64_256(val), nil
+}
+
+// splitPositiveFloat64_256 converts an already scaled and rounded non-negative
+// float64 into the 64-bit words of a Decimal256.
+func splitPositiveFloat64_256(val float64) Decimal256 {
+       var arr [4]float64
+       arr[3] = math.Floor(math.Ldexp(val, -192))
+       val -= math.Ldexp(arr[3], 192)
+       arr[2] = math.Floor(math.Ldexp(val, -128))
+       val -= math.Ldexp(arr[2], 128)
+       arr[1] = math.Floor(math.Ldexp(val, -64))
+       val -= math.Ldexp(arr[1], 64)
+       arr[0] = val
+
+       debug.Assert(arr[3] >= 0, "bad conversion float64 to decimal256")
+       debug.Assert(arr[3] < 1.8446744073709552e+19, "bad conversion float64 
to decimal256") // 2**64
+       debug.Assert(arr[2] >= 0, "bad conversion float64 to decimal256")
+       debug.Assert(arr[2] < 1.8446744073709552e+19, "bad conversion float64 
to decimal256") // 2**64
+       debug.Assert(arr[1] >= 0, "bad conversion float64 to decimal256")
+       debug.Assert(arr[1] < 1.8446744073709552e+19, "bad conversion float64 
to decimal256") // 2**64
+       debug.Assert(arr[0] >= 0, "bad conversion float64 to decimal256")
+       debug.Assert(arr[0] < 1.8446744073709552e+19, "bad conversion float64 
to decimal256") // 2**64
+       return Decimal256{[4]uint64{uint64(arr[0]), uint64(arr[1]), 
uint64(arr[2]), uint64(arr[3])}}
+}
+
+func (n Decimal256) tofloat64Positive(scale int32) float64 {
+       const (
+               twoTo64  float64 = 1.8446744073709552e+19
+               twoTo128 float64 = 3.402823669209385e+38
+               twoTo192 float64 = 6.277101735386681e+57
+       )
+
+       x := float64(n.arr[3]) * twoTo192
+       x += float64(n.arr[2]) * twoTo128
+       x += float64(n.arr[1]) * twoTo64
+       x += float64(n.arr[0])
+
+       if scale >= -76 && scale <= 76 {
+               return x * float64PowersOfTen256[-scale+76]
+       }
+
+       return x * math.Pow10(-int(scale))
+}
+
+func (n Decimal256) ToFloat32(scale int32) float32 { return 
float32(n.ToFloat64(scale)) }
+
+func (n Decimal256) ToFloat64(scale int32) float64 {
+       if n.Sign() < 0 {
+               return -n.Negate().tofloat64Positive(scale)
+       }
+       return n.tofloat64Positive(scale)
+}
+
+func (n Decimal256) ToBigFloat(scale int32) *big.Float {
+       if scale < -maxScale256 || scale > maxScale256 {
+               panic("arrow/decimal256: scale out of range")
+       }
+       f := (&big.Float{}).SetInt(n.BigInt())
+       if scale < 0 {
+               f.SetPrec(256).Mul(f, 
(&big.Float{}).SetInt(scaleMultipliers256[-scale].BigInt()))
+       } else {
+               f.SetPrec(256).Quo(f, 
(&big.Float{}).SetInt(scaleMultipliers256[scale].BigInt()))
+       }
+       return f
+}
+
+func (n Decimal256) Sign() int {
+       if n == (Decimal256{}) {
+               return 0
+       }
+       return int(1 | (int64(n.arr[3]) >> 63))
+}
+
+// Decimal256FromBigInt converts a big.Int to a Decimal256. It panics if 
v.BitLen() > 255.
+func Decimal256FromBigInt(v *big.Int) Decimal256 {
+       b := v.Bits()
+       if len(b) == 0 {
+               return Decimal256{}
+       }
+
+       var x0, x1, x2, x3 uint64
+       if bits.UintSize == 32 {
+               if v.BitLen() > 255 {
+                       panic("arrow/decimal256: cannot represent value larger 
than 256bits")
+               }

Review Comment:
   The panic message is preserved verbatim from `arrow/decimal256` (matching 
the existing scale panic above) so callers or tests matching the existing 
string do not break during the move.



##########
arrow/decimal256/alias.go:
##########
@@ -0,0 +1,106 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package decimal256 provides the Decimal256 API of [decimal] under its 
original
+// names. Its types and functions are aliases of and forwarders to [decimal].
+package decimal256
+
+import (
+       "math/big"
+
+       "github.com/apache/arrow-go/v18/arrow/decimal"
+       "github.com/apache/arrow-go/v18/arrow/decimal128"
+)
+
+const (
+       MaxPrecision = 76
+       MaxScale     = 76
+)
+
+// Num is an alias of [decimal.Decimal256].
+type Num = decimal.Decimal256
+
+func GetMaxValue(prec int32) Num { return decimal.Decimal256MaxValue(prec) }
+
+// New returns a new signed 256-bit integer value where x1 contains
+// the highest bits with the rest of the values in order down to the
+// lowest bits
+//
+//     ie: New(1, 2, 3, 4) returns with the elements in little-endian order
+//         {4, 3, 2, 1} but each value is still represented as the native 
endianness
+func New(x1, x2, x3, x4 uint64) Num { return decimal.NewDecimal256(x1, x2, x3, 
x4) }
+
+func FromDecimal128(n decimal128.Num) Num { return 
decimal.Decimal256FromDecimal128(n) }
+
+func FromU64(v uint64) Num { return decimal.Decimal256FromU64(v) }
+
+func FromI64(v int64) Num { return decimal.Decimal256FromI64(v) }
+
+func FromString(v string, prec, scale int32) (n Num, err error) {
+       return decimal.Decimal256FromString(v, prec, scale)
+}
+
+func FromFloat32(v float32, prec, scale int32) (Num, error) {
+       return decimal.Decimal256FromFloat32(v, prec, scale)
+}
+
+func FromFloat64(v float64, prec, scale int32) (Num, error) {
+       return decimal.Decimal256FromFloat(v, prec, scale)
+}
+
+// FromBigInt converts a big.Int to a Num. It panics if v.BitLen() > 255.
+func FromBigInt(v *big.Int) Num { return decimal.Decimal256FromBigInt(v) }
+
+// Max returns the largest Decimal256 that was passed in the arguments
+func Max(first Num, rest ...Num) Num { return decimal.Decimal256Max(first, 
rest...) }
+
+// Min returns the smallest Decimal256 that was passed in the arguments
+func Min(first Num, rest ...Num) Num { return decimal.Decimal256Min(first, 
rest...) }
+
+func GetScaleMultiplier(pow int) Num { return 
decimal.Decimal256ScaleMultiplier(pow) }
+
+func GetHalfScaleMultiplier(pow int) Num { return 
decimal.Decimal256HalfScaleMultiplier(pow) }
+
+// Calling every method of Num here keeps their inline bodies in the export 
data of
+// this package. Without it, packages that import decimal256 but not decimal 
stop
+// inlining them, see https://github.com/golang/go/issues/82474. Remove it 
when the
+// minimum supported Go version includes the fix.
+var _ = func(n Num) {
+       _ = n.Abs()
+       _ = n.Add(*new(Num))
+       _ = n.Array()
+       _ = n.BigInt()
+       _ = n.Cmp(*new(Num))
+       _, _ = n.Div(*new(Num))
+       _ = n.FitsInPrecision(0)

Review Comment:
   This function value is never called and exists only to keep method bodies in 
export data (the line above also passes zero to `Div`), so zero arguments are 
intentional.



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