This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new ab18e4ea refactor(arrow/decimal): move `Decimal256` implementation
into `arrow/decimal` (#1646)
ab18e4ea is described below
commit ab18e4ea347d47c4bc257150cb0a1a6a186f2092
Author: Harrison Crosse <[email protected]>
AuthorDate: Fri Nov 20 14:17:05 2026 -0500
refactor(arrow/decimal): move `Decimal256` implementation into
`arrow/decimal` (#1646)
### Rationale for this change
Part of #1516. Moving `Decimal256` into `arrow/decimal` is the first step
toward making `arrow/decimal` the primary decimal package while keeping
`arrow/decimal256` compatible.
### What changes are included in this PR?
I moved `Decimal256` into `arrow/decimal` and turned `arrow/decimal256`
into type aliases and thin wrappers, keeping a dummy call so Go still inlines
the method set (`golang/go#82474`). Internal callers in `arrow/`, `parquet/`
and `internal/` now use `arrow/decimal`, which also adds `MaxScale`.
To avoid extra struct copies through `arrow/decimal256` wrappers on Go 1.26
and 1.27, I rewrote `Decimal256FromI64` and `Decimal256FromDecimal128` as
single composite literals. I also rewrote `Decimal256FromBigInt` to unpack the
`big.Int` words into four local `uint64` variables and apply two's complement
negation with `bits.Add64` before constructing the `Decimal256`, instead of
mutating a local struct and calling `Negate` by value. Over 10 interleaved
`benchstat` rounds on Go 1.26.1 [...]
| Benchmark (PR call) | `main` | PR | Change |
| --- | ---: | ---: | ---: |
| `decimal256.FromI64` | 6.55 ns/elem | 0.76 ns/elem | -88.4% |
| `decimal256.FromDecimal128` | 1.78 ns/elem | 1.06 ns/elem | -40.5% |
| `decimal256.FromBigInt` (1 / multi word) | 17.67 / 17.70 ns/elem | 12.84
/ 12.94 ns/elem | -27.3% / -26.9% |
| `decimal.Decimal256FromBigInt` (1 / multi word) | 17.62 / 17.73 ns/elem |
10.05 / 10.26 ns/elem | -43.0% / -42.1% |
| `Decimal256Builder` append and array construction (`decimal256.FromI64`)
| 16.20 ns/elem | 12.62 ns/elem | -22.1% |
`main` uses `decimal256.FromBigInt` for the direct call row. Unchanged
composite literals `decimal256.FromU64` (0.62 to 0.64 ns/elem, +3.4%) and
`decimal256.New` (0.62 to 0.65 ns/elem, +5.6%) are sensitive to loop alignment.
### Are these changes tested?
Yes. I added layout compatibility checks for `arrow/decimal256` plus unit
tests for `MaxScale`, `Decimal256FromI64`, `Decimal256FromDecimal128` and
`Decimal256FromBigInt` covering boundary and overflow cases.
### Are there any user-facing changes?
Yes. `arrow/decimal` now exports `Decimal256` constructors and `MaxScale`,
while `arrow/decimal256` remains source compatible through aliases and
wrappers. Because `decimal256.Num` is now an alias for `decimal.Decimal256`,
reflection and `%T` formatting report `decimal.Decimal256` instead of
`decimal256.Num`.
---
arrow/array/arreflect/reflect_arrow_to_go.go | 2 +-
arrow/array/arreflect/reflect_go_to_arrow.go | 5 +-
arrow/array/arreflect/reflect_infer.go | 3 +-
arrow/avro/reader_types.go | 4 +-
arrow/compute/arithmetic.go | 8 +-
arrow/compute/internal/kernels/base_arithmetic.go | 26 +-
arrow/compute/internal/kernels/numeric_cast.go | 84 +--
arrow/compute/internal/kernels/rounding.go | 56 +-
.../compute/internal/kernels/scalar_arithmetic.go | 8 +-
.../compute/internal/kernels/scalar_comparisons.go | 20 +-
.../internal/kernels/vector_run_end_encode.go | 4 +-
arrow/csv/reader.go | 4 +-
arrow/decimal/decimal.go | 14 +-
arrow/decimal/decimal256.go | 836 +++++++++++++++++++++
arrow/decimal/decimal_test.go | 139 ++++
arrow/decimal/doc.go | 24 +
arrow/decimal/layout_test.go | 33 +
arrow/decimal256/alias.go | 106 +++
arrow/decimal256/alias_test.go | 100 +++
arrow/decimal256/decimal256.go | 822 --------------------
arrow/internal/arrdata/arrdata.go | 10 +-
arrow/internal/arrjson/arrjson.go | 7 +-
arrow/scalar/append.go | 4 +-
arrow/scalar/decimal.go | 11 +-
arrow/scalar/numeric.gen.go | 22 +-
arrow/scalar/numeric.gen.go.tmpl | 8 +-
arrow/scalar/parse.go | 7 +-
arrow/scalar/scalar.go | 12 +-
internal/utils/decimal_format.go | 4 +-
parquet/pqarrow/column_readers.go | 10 +-
parquet/pqarrow/encode_arrow.go | 12 +-
31 files changed, 1405 insertions(+), 1000 deletions(-)
diff --git a/arrow/array/arreflect/reflect_arrow_to_go.go
b/arrow/array/arreflect/reflect_arrow_to_go.go
index 086dbe6c..53b4d92c 100644
--- a/arrow/array/arreflect/reflect_arrow_to_go.go
+++ b/arrow/array/arreflect/reflect_arrow_to_go.go
@@ -335,7 +335,7 @@ func setDecimalValue(v reflect.Value, arr arrow.Array, i
int) error {
return err
}
if v.Type() != typeOfDec256 {
- return fmt.Errorf("cannot set decimal256.Num into %s:
%w", v.Type(), ErrTypeMismatch)
+ return fmt.Errorf("cannot set decimal.Decimal256 into
%s: %w", v.Type(), ErrTypeMismatch)
}
num := a.Value(i)
v.Set(reflect.ValueOf(num))
diff --git a/arrow/array/arreflect/reflect_go_to_arrow.go
b/arrow/array/arreflect/reflect_go_to_arrow.go
index 424a5ab1..8dd4b016 100644
--- a/arrow/array/arreflect/reflect_go_to_arrow.go
+++ b/arrow/array/arreflect/reflect_go_to_arrow.go
@@ -25,7 +25,6 @@ import (
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/memory"
)
@@ -361,9 +360,9 @@ func appendDecimalValue(b array.Builder, v reflect.Value)
error {
}
tb.Append(n)
case *array.Decimal256Builder:
- n, ok := reflect.TypeAssert[decimal256.Num](v)
+ n, ok := reflect.TypeAssert[decimal.Decimal256](v)
if !ok {
- return fmt.Errorf("expected decimal256.Num, got %s:
%w", v.Type(), ErrTypeMismatch)
+ return fmt.Errorf("expected decimal.Decimal256, got %s:
%w", v.Type(), ErrTypeMismatch)
}
tb.Append(n)
case *array.Decimal32Builder:
diff --git a/arrow/array/arreflect/reflect_infer.go
b/arrow/array/arreflect/reflect_infer.go
index 811edf11..502b9707 100644
--- a/arrow/array/arreflect/reflect_infer.go
+++ b/arrow/array/arreflect/reflect_infer.go
@@ -25,7 +25,6 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
)
var (
@@ -34,7 +33,7 @@ var (
typeOfDec32 = reflect.TypeOf(decimal.Decimal32(0))
typeOfDec64 = reflect.TypeOf(decimal.Decimal64(0))
typeOfDec128 = reflect.TypeOf(decimal128.Num{})
- typeOfDec256 = reflect.TypeOf(decimal256.Num{})
+ typeOfDec256 = reflect.TypeOf(decimal.Decimal256{})
typeOfByteSlice = reflect.TypeOf([]byte{})
typeOfInt = reflect.TypeOf(int(0))
typeOfUint = reflect.TypeOf(uint(0))
diff --git a/arrow/avro/reader_types.go b/arrow/avro/reader_types.go
index c2298e49..6cfd49f6 100644
--- a/arrow/avro/reader_types.go
+++ b/arrow/avro/reader_types.go
@@ -25,8 +25,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/extensions"
"github.com/apache/arrow-go/v18/arrow/memory"
avro "github.com/twmb/avro"
@@ -699,7 +699,7 @@ func appendDecimal256Data(b *array.Decimal256Builder, data
interface{}, typ arro
case nil:
b.AppendNull()
case *big.Rat:
- b.Append(decimal256.FromBigInt(bigRatToBigInt(dt, typ)))
+ b.Append(decimal.Decimal256FromBigInt(bigRatToBigInt(dt, typ)))
default:
return fmt.Errorf("avro: unsupported value of type %T for
Decimal256 column", data)
}
diff --git a/arrow/compute/arithmetic.go b/arrow/compute/arithmetic.go
index 934451fa..fcf3cc26 100644
--- a/arrow/compute/arithmetic.go
+++ b/arrow/compute/arithmetic.go
@@ -25,8 +25,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
"github.com/apache/arrow-go/v18/arrow/compute/internal/kernels"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/scalar"
)
@@ -1004,7 +1004,7 @@ func RegisterScalarArithmetic(reg FunctionRegistry) {
floorFn.AddNewKernel([]exec.InputType{exec.NewIDInput(arrow.DECIMAL128)},
kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal128.Num](kernels.RoundDown), nil)
floorFn.AddNewKernel([]exec.InputType{exec.NewIDInput(arrow.DECIMAL256)},
- kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal256.Num](kernels.RoundDown), nil)
+ kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal.Decimal256](kernels.RoundDown), nil)
reg.AddFunction(floorFn, false)
ceilFn :=
&arithmeticIntegerToFloatingPointFunc{arithmeticFunction{*NewScalarFunction("ceil",
Unary(), ceilDoc), decPromoteNone}}
@@ -1017,7 +1017,7 @@ func RegisterScalarArithmetic(reg FunctionRegistry) {
ceilFn.AddNewKernel([]exec.InputType{exec.NewIDInput(arrow.DECIMAL128)},
kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal128.Num](kernels.RoundUp), nil)
ceilFn.AddNewKernel([]exec.InputType{exec.NewIDInput(arrow.DECIMAL256)},
- kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal256.Num](kernels.RoundUp), nil)
+ kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal.Decimal256](kernels.RoundUp), nil)
reg.AddFunction(ceilFn, false)
truncFn :=
&arithmeticIntegerToFloatingPointFunc{arithmeticFunction{*NewScalarFunction("trunc",
Unary(), truncDoc), decPromoteNone}}
@@ -1030,7 +1030,7 @@ func RegisterScalarArithmetic(reg FunctionRegistry) {
truncFn.AddNewKernel([]exec.InputType{exec.NewIDInput(arrow.DECIMAL128)},
kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal128.Num](kernels.TowardsZero), nil)
truncFn.AddNewKernel([]exec.InputType{exec.NewIDInput(arrow.DECIMAL256)},
- kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal256.Num](kernels.TowardsZero), nil)
+ kernels.OutputFirstType,
kernels.FixedRoundDecimalExec[decimal.Decimal256](kernels.TowardsZero), nil)
reg.AddFunction(truncFn, false)
roundFn :=
&arithmeticIntegerToFloatingPointFunc{arithmeticFunction{*NewScalarFunction("round",
Unary(), roundDoc), decPromoteNone}}
diff --git a/arrow/compute/internal/kernels/base_arithmetic.go
b/arrow/compute/internal/kernels/base_arithmetic.go
index 3e12663b..0a711ec8 100644
--- a/arrow/compute/internal/kernels/base_arithmetic.go
+++ b/arrow/compute/internal/kernels/base_arithmetic.go
@@ -25,8 +25,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
+ dec "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/internal/utils"
"golang.org/x/exp/constraints"
@@ -719,7 +719,7 @@ func SubtractDate32(op ArithmeticOp) exec.ArrayKernelExec {
panic("invalid op for subtractDate32")
}
-type decOps[T decimal128.Num | decimal256.Num] struct {
+type decOps[T decimal128.Num | dec.Decimal256] struct {
Add func(T, T) T
Sub func(T, T) T
Div func(T, T) T
@@ -742,20 +742,20 @@ var dec128Ops = decOps[decimal128.Num]{
Sign: func(a decimal128.Num) int { return a.Sign() },
}
-var dec256Ops = decOps[decimal256.Num]{
- Add: func(a, b decimal256.Num) decimal256.Num { return a.Add(b) },
- Sub: func(a, b decimal256.Num) decimal256.Num { return a.Sub(b) },
- Mul: func(a, b decimal256.Num) decimal256.Num { return a.Mul(b) },
- Div: func(a, b decimal256.Num) decimal256.Num {
+var dec256Ops = decOps[dec.Decimal256]{
+ Add: func(a, b dec.Decimal256) dec.Decimal256 { return a.Add(b) },
+ Sub: func(a, b dec.Decimal256) dec.Decimal256 { return a.Sub(b) },
+ Mul: func(a, b dec.Decimal256) dec.Decimal256 { return a.Mul(b) },
+ Div: func(a, b dec.Decimal256) dec.Decimal256 {
a, _ = a.Div(b)
return a
},
- Abs: func(a decimal256.Num) decimal256.Num { return a.Abs() },
- Neg: func(a decimal256.Num) decimal256.Num { return a.Negate() },
- Sign: func(a decimal256.Num) int { return a.Sign() },
+ Abs: func(a dec.Decimal256) dec.Decimal256 { return a.Abs() },
+ Neg: func(a dec.Decimal256) dec.Decimal256 { return a.Negate() },
+ Sign: func(a dec.Decimal256) int { return a.Sign() },
}
-func getArithmeticOpDecimalImpl[T decimal128.Num | decimal256.Num](op
ArithmeticOp, fns decOps[T]) exec.ArrayKernelExec {
+func getArithmeticOpDecimalImpl[T decimal128.Num | dec.Decimal256](op
ArithmeticOp, fns decOps[T]) exec.ArrayKernelExec {
if op >= OpAddChecked {
op -= OpAddChecked // decimal128/256 checked is the same as
unchecked
}
@@ -799,12 +799,12 @@ func getArithmeticOpDecimalImpl[T decimal128.Num |
decimal256.Num](op Arithmetic
return nil
}
-func getArithmeticDecimal[T decimal128.Num | decimal256.Num](op ArithmeticOp)
exec.ArrayKernelExec {
+func getArithmeticDecimal[T decimal128.Num | dec.Decimal256](op ArithmeticOp)
exec.ArrayKernelExec {
var def T
switch any(def).(type) {
case decimal128.Num:
return getArithmeticOpDecimalImpl(op, dec128Ops)
- case decimal256.Num:
+ case dec.Decimal256:
return getArithmeticOpDecimalImpl(op, dec256Ops)
}
panic("should never get here")
diff --git a/arrow/compute/internal/kernels/numeric_cast.go
b/arrow/compute/internal/kernels/numeric_cast.go
index c8fed2a5..30883bc4 100644
--- a/arrow/compute/internal/kernels/numeric_cast.go
+++ b/arrow/compute/internal/kernels/numeric_cast.go
@@ -26,8 +26,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
+ dec "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/internal/bitutils"
@@ -70,13 +70,13 @@ func CastIntegerToFloating(ctx *exec.KernelCtx, batch
*exec.ExecSpan, out *exec.
return nil
}
-type decimal[T decimal128.Num | decimal256.Num] interface {
+type decimal[T decimal128.Num | dec.Decimal256] interface {
Less(T) bool
Greater(T) bool
LowBits() uint64
}
-func decimalToIntImpl[InT decimal128.Num | decimal256.Num, OutT arrow.IntType
| arrow.UintType](allowOverflow bool, min, max InT, v decimal[InT], err *error)
OutT {
+func decimalToIntImpl[InT decimal128.Num | dec.Decimal256, OutT arrow.IntType
| arrow.UintType](allowOverflow bool, min, max InT, v decimal[InT], err *error)
OutT {
// min and max are the inclusive bounds of the output integer type, so a
// value equal to max (e.g. math.MaxInt64) is in range. Use a strict
// greater-than check rather than >= so the endpoints are not rejected.
@@ -96,33 +96,33 @@ func CastDecimal256ToInteger[T arrow.IntType |
arrow.UintType](ctx *exec.KernelC
ex exec.ArrayKernelExec
minLowBits = uint64(MinOf[T]())
minHiBits int64
- max = decimal256.FromU64(uint64(MaxOf[T]()))
+ max = dec.Decimal256FromU64(uint64(MaxOf[T]()))
)
if MinOf[T]() < 0 {
minHiBits = -1
}
- min := decimal256.New(uint64(minHiBits), uint64(minHiBits),
uint64(minHiBits), minLowBits)
+ min := dec.NewDecimal256(uint64(minHiBits), uint64(minHiBits),
uint64(minHiBits), minLowBits)
if opts.AllowDecimalTruncate {
if inScale < 0 {
- ex = ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal256.Num, err *error) T {
+ ex = ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
dec.Decimal256, err *error) T {
v := val.IncreaseScaleBy(-inScale)
- return decimalToIntImpl[decimal256.Num,
T](opts.AllowIntOverflow, min, max, v, err)
+ return decimalToIntImpl[dec.Decimal256,
T](opts.AllowIntOverflow, min, max, v, err)
})
} else {
- ex = ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal256.Num, err *error) T {
+ ex = ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
dec.Decimal256, err *error) T {
v := val.ReduceScaleBy(inScale, true)
- return decimalToIntImpl[decimal256.Num,
T](opts.AllowIntOverflow, min, max, v, err)
+ return decimalToIntImpl[dec.Decimal256,
T](opts.AllowIntOverflow, min, max, v, err)
})
}
} else {
- ex = ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal256.Num, err *error) T {
+ ex = ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
dec.Decimal256, err *error) T {
v, e := val.Rescale(inScale, 0)
if e != nil {
*err = fmt.Errorf("%w: %s", arrow.ErrInvalid, e)
return T(0)
}
- return decimalToIntImpl[decimal256.Num,
T](opts.AllowIntOverflow, min, max, v, err)
+ return decimalToIntImpl[dec.Decimal256,
T](opts.AllowIntOverflow, min, max, v, err)
})
}
@@ -188,14 +188,14 @@ func integerToDecimal128[T arrow.IntType |
arrow.UintType](inType arrow.Type, ou
}
func integerToDecimal256[T arrow.IntType | arrow.UintType](inType arrow.Type,
outScale int32) exec.ArrayKernelExec {
- var getDecimal func(v T) decimal256.Num
+ var getDecimal func(v T) dec.Decimal256
switch inType {
case arrow.UINT8, arrow.UINT16, arrow.UINT32, arrow.UINT64:
- getDecimal = func(v T) decimal256.Num { return
decimal256.FromU64(uint64(v)) }
+ getDecimal = func(v T) dec.Decimal256 { return
dec.Decimal256FromU64(uint64(v)) }
default:
- getDecimal = func(v T) decimal256.Num { return
decimal256.FromI64(int64(v)) }
+ getDecimal = func(v T) dec.Decimal256 { return
dec.Decimal256FromI64(int64(v)) }
}
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val T, err *error)
decimal256.Num {
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val T, err *error)
dec.Decimal256 {
out, e := getDecimal(val).Rescale(0, outScale)
if e != nil {
*err = e
@@ -204,7 +204,7 @@ func integerToDecimal256[T arrow.IntType |
arrow.UintType](inType arrow.Type, ou
})
}
-func CastIntegerToDecimal[OutT decimal128.Num | decimal256.Num, Arg0
arrow.IntType | arrow.UintType](ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
+func CastIntegerToDecimal[OutT decimal128.Num | dec.Decimal256, Arg0
arrow.IntType | arrow.UintType](ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
var (
precision, scale int32
executor exec.ArrayKernelExec
@@ -238,7 +238,7 @@ func CastIntegerToDecimal[OutT decimal128.Num |
decimal256.Num, Arg0 arrow.IntTy
return executor(ctx, batch, out)
}
-func getCastIntToDecimal[T decimal128.Num | decimal256.Num](inType arrow.Type)
exec.ArrayKernelExec {
+func getCastIntToDecimal[T decimal128.Num | dec.Decimal256](inType arrow.Type)
exec.ArrayKernelExec {
switch inType {
case arrow.UINT8:
return CastIntegerToDecimal[T, uint8]
@@ -263,11 +263,11 @@ func getCastIntToDecimal[T decimal128.Num |
decimal256.Num](inType arrow.Type) e
func unsafeUpscaleDecimal256Out(inputType arrow.Type, by int32)
exec.ArrayKernelExec {
if inputType == arrow.DECIMAL128 {
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal128.Num, err *error) decimal256.Num {
- return
decimal256.FromDecimal128(val).IncreaseScaleBy(by)
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal128.Num, err *error) dec.Decimal256 {
+ return
dec.Decimal256FromDecimal128(val).IncreaseScaleBy(by)
})
}
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val decimal256.Num,
err *error) decimal256.Num {
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val dec.Decimal256,
err *error) dec.Decimal256 {
return val.IncreaseScaleBy(by)
})
}
@@ -278,7 +278,7 @@ func unsafeUpscaleDecimal128Out(inputType arrow.Type, by
int32) exec.ArrayKernel
return val.IncreaseScaleBy(by)
})
}
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val decimal256.Num,
err *error) decimal128.Num {
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val dec.Decimal256,
err *error) decimal128.Num {
vals := val.IncreaseScaleBy(by).Array()
return decimal128.New(int64(vals[1]), vals[0])
})
@@ -286,11 +286,11 @@ func unsafeUpscaleDecimal128Out(inputType arrow.Type, by
int32) exec.ArrayKernel
func unsafeDownscaleDecimal256Out(inputType arrow.Type, by int32)
exec.ArrayKernelExec {
if inputType == arrow.DECIMAL128 {
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal128.Num, err *error) decimal256.Num {
- return decimal256.FromDecimal128(val).ReduceScaleBy(by,
false)
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal128.Num, err *error) dec.Decimal256 {
+ return
dec.Decimal256FromDecimal128(val).ReduceScaleBy(by, false)
})
}
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val decimal256.Num,
err *error) decimal256.Num {
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val dec.Decimal256,
err *error) dec.Decimal256 {
return val.ReduceScaleBy(by, false)
})
}
@@ -301,7 +301,7 @@ func unsafeDownscaleDecimal128Out(inputType arrow.Type, by
int32) exec.ArrayKern
return val.ReduceScaleBy(by, false)
})
}
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val decimal256.Num,
err *error) decimal128.Num {
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val dec.Decimal256,
err *error) decimal128.Num {
vals := val.ReduceScaleBy(by, false).Array()
return decimal128.New(int64(vals[1]), vals[0])
})
@@ -309,11 +309,11 @@ func unsafeDownscaleDecimal128Out(inputType arrow.Type,
by int32) exec.ArrayKern
func safeRescaleDecimal256Out(inputType arrow.Type, outScale, outPrecision,
inScale int32) exec.ArrayKernelExec {
if inputType == arrow.DECIMAL128 {
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal128.Num, err *error) decimal256.Num {
- out, e :=
decimal256.FromDecimal128(val).Rescale(inScale, outScale)
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val
decimal128.Num, err *error) dec.Decimal256 {
+ out, e :=
dec.Decimal256FromDecimal128(val).Rescale(inScale, outScale)
if e != nil {
*err = fmt.Errorf("%w: %s", arrow.ErrInvalid, e)
- return decimal256.Num{}
+ return dec.Decimal256{}
}
if out.FitsInPrecision(outPrecision) {
@@ -321,14 +321,14 @@ func safeRescaleDecimal256Out(inputType arrow.Type,
outScale, outPrecision, inSc
}
*err = fmt.Errorf("%w: decimal value does not fit in
precision", arrow.ErrInvalid)
- return decimal256.Num{}
+ return dec.Decimal256{}
})
}
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val decimal256.Num,
err *error) decimal256.Num {
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val dec.Decimal256,
err *error) dec.Decimal256 {
out, e := val.Rescale(inScale, outScale)
if e != nil {
*err = fmt.Errorf("%w: %s", arrow.ErrInvalid, e)
- return decimal256.Num{}
+ return dec.Decimal256{}
}
if out.FitsInPrecision(outPrecision) {
@@ -336,7 +336,7 @@ func safeRescaleDecimal256Out(inputType arrow.Type,
outScale, outPrecision, inSc
}
*err = fmt.Errorf("%w: decimal value does not fit in
precision", arrow.ErrInvalid)
- return decimal256.Num{}
+ return dec.Decimal256{}
})
}
@@ -357,7 +357,7 @@ func safeRescaleDecimal128Out(inputType arrow.Type,
outScale, outPrecision, inSc
return decimal128.Num{}
})
}
- return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val decimal256.Num,
err *error) decimal128.Num {
+ return ScalarUnaryNotNull(func(_ *exec.KernelCtx, val dec.Decimal256,
err *error) decimal128.Num {
out, e := val.Rescale(inScale, outScale)
if e != nil {
*err = fmt.Errorf("%w: %s", arrow.ErrInvalid, e)
@@ -453,8 +453,8 @@ func CastFloat32ToDecimal(ctx *exec.KernelCtx, batch
*exec.ExecSpan, out *exec.E
})
case *arrow.Decimal256Type:
prec, scale = dt.Precision, dt.Scale
- executor = ScalarUnaryNotNull(func(_ *exec.KernelCtx, v
float32, err *error) decimal256.Num {
- out, e := decimal256.FromFloat32(v, prec, scale)
+ executor = ScalarUnaryNotNull(func(_ *exec.KernelCtx, v
float32, err *error) dec.Decimal256 {
+ out, e := dec.Decimal256FromFloat32(v, prec, scale)
if e == nil {
return out
}
@@ -462,7 +462,7 @@ func CastFloat32ToDecimal(ctx *exec.KernelCtx, batch
*exec.ExecSpan, out *exec.E
if !allowTrunc {
*err = fmt.Errorf("%w: %s", arrow.ErrInvalid, e)
}
- return decimal256.Num{}
+ return dec.Decimal256{}
})
}
@@ -494,8 +494,8 @@ func CastFloat64ToDecimal(ctx *exec.KernelCtx, batch
*exec.ExecSpan, out *exec.E
})
case *arrow.Decimal256Type:
prec, scale = dt.Precision, dt.Scale
- executor = ScalarUnaryNotNull(func(_ *exec.KernelCtx, v
float64, err *error) decimal256.Num {
- out, e := decimal256.FromFloat64(v, prec, scale)
+ executor = ScalarUnaryNotNull(func(_ *exec.KernelCtx, v
float64, err *error) dec.Decimal256 {
+ out, e := dec.Decimal256FromFloat(v, prec, scale)
if e == nil {
return out
}
@@ -503,7 +503,7 @@ func CastFloat64ToDecimal(ctx *exec.KernelCtx, batch
*exec.ExecSpan, out *exec.E
if !allowTrunc {
*err = fmt.Errorf("%w: %s", arrow.ErrInvalid, e)
}
- return decimal256.Num{}
+ return dec.Decimal256{}
})
}
@@ -523,7 +523,7 @@ func CastDecimalToFloat16(ctx *exec.KernelCtx, batch
*exec.ExecSpan, out *exec.E
})
case *arrow.Decimal256Type:
scale := dt.Scale
- executor = ScalarUnaryNotNull(func(_ *exec.KernelCtx, v
decimal256.Num, err *error) float16.Num {
+ executor = ScalarUnaryNotNull(func(_ *exec.KernelCtx, v
dec.Decimal256, err *error) float16.Num {
return float16.New(v.ToFloat32(scale))
})
}
@@ -544,7 +544,7 @@ func CastDecimalToFloating[OutT constraints.Float](ctx
*exec.KernelCtx, batch *e
})
case *arrow.Decimal256Type:
scale := dt.Scale
- executor = ScalarUnaryNotNull(func(_ *exec.KernelCtx, v
decimal256.Num, err *error) OutT {
+ executor = ScalarUnaryNotNull(func(_ *exec.KernelCtx, v
dec.Decimal256, err *error) OutT {
return OutT(v.ToFloat64(scale))
})
}
@@ -957,7 +957,7 @@ func GetCastToDecimal256() []exec.ScalarKernel {
for _, inTy := range intTypes {
kernels = append(kernels, exec.NewScalarKernel(
[]exec.InputType{exec.NewExactInput(inTy)}, outputType,
- getCastIntToDecimal[decimal256.Num](inTy.ID()), nil))
+ getCastIntToDecimal[dec.Decimal256](inTy.ID()), nil))
}
kernels = append(kernels, exec.NewScalarKernel(
diff --git a/arrow/compute/internal/kernels/rounding.go
b/arrow/compute/internal/kernels/rounding.go
index 493dc8e1..faf26613 100644
--- a/arrow/compute/internal/kernels/rounding.go
+++ b/arrow/compute/internal/kernels/rounding.go
@@ -26,8 +26,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
+ dec "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/scalar"
"golang.org/x/exp/constraints"
)
@@ -112,7 +112,7 @@ func isPositive(s scalar.Scalar) bool {
case *scalar.Decimal128:
return s.Value.Greater(decimal128.Num{})
case *scalar.Decimal256:
- return s.Value.Greater(decimal256.Num{})
+ return s.Value.Greater(dec.Decimal256{})
case *scalar.Int8:
return s.Value > 0
case *scalar.Uint8, *scalar.Uint16, *scalar.Uint32, *scalar.Uint64:
@@ -221,7 +221,7 @@ func getFloatRoundImpl[T constraints.Float](mode RoundMode)
func(T) T {
panic("invalid rounding mode")
}
-func getDecRounding[T decimal128.Num | decimal256.Num](mode RoundMode, opsImpl
*roundDecImpl[T]) func(val, remainder T, pow10 T, scale int32) T {
+func getDecRounding[T decimal128.Num | dec.Decimal256](mode RoundMode, opsImpl
*roundDecImpl[T]) func(val, remainder T, pow10 T, scale int32) T {
var (
z T
one = opsImpl.fromI64(1)
@@ -393,7 +393,7 @@ func roundToMultipleFloating[T constraints.Float](ctx
*exec.KernelCtx, batch *ex
return ScalarUnaryNotNull(rnd.call)(ctx, batch, out)
}
-type roundDecImpl[T decimal128.Num | decimal256.Num] struct {
+type roundDecImpl[T decimal128.Num | dec.Decimal256] struct {
*decOps[T]
scaleMultiplier func(int) T
halfScaleMultiplier func(int) T
@@ -421,22 +421,22 @@ var (
fromI64: func(v int64) decimal128.Num { return
decimal128.FromI64(v) },
str: func(a decimal128.Num, scale int32) string
{ return a.ToString(scale) },
}
- roundDec256 = roundDecImpl[decimal256.Num]{
+ roundDec256 = roundDecImpl[dec.Decimal256]{
decOps: &dec256Ops,
- scaleMultiplier: decimal256.GetScaleMultiplier,
- halfScaleMultiplier: decimal256.GetHalfScaleMultiplier,
- divide: func(a, b decimal256.Num) (res, rem
decimal256.Num) { return a.Div(b) },
- fitsInPrec: func(a decimal256.Num, prec int32) bool {
return a.FitsInPrecision(prec) },
- less: func(a, b decimal256.Num) bool { return
a.Less(b) },
- reduceScale: func(a decimal256.Num, scale int32, round
bool) decimal256.Num { return a.ReduceScaleBy(scale, round) },
- increaseScale: func(a decimal256.Num, scale int32)
decimal256.Num { return a.IncreaseScaleBy(scale) },
- lowBits: func(a decimal256.Num) uint64 { return
a.LowBits() },
- fromI64: func(v int64) decimal256.Num { return
decimal256.FromI64(v) },
- str: func(a decimal256.Num, scale int32) string
{ return a.ToString(scale) },
+ scaleMultiplier: dec.Decimal256ScaleMultiplier,
+ halfScaleMultiplier: dec.Decimal256HalfScaleMultiplier,
+ divide: func(a, b dec.Decimal256) (res, rem
dec.Decimal256) { return a.Div(b) },
+ fitsInPrec: func(a dec.Decimal256, prec int32) bool {
return a.FitsInPrecision(prec) },
+ less: func(a, b dec.Decimal256) bool { return
a.Less(b) },
+ reduceScale: func(a dec.Decimal256, scale int32, round
bool) dec.Decimal256 { return a.ReduceScaleBy(scale, round) },
+ increaseScale: func(a dec.Decimal256, scale int32)
dec.Decimal256 { return a.IncreaseScaleBy(scale) },
+ lowBits: func(a dec.Decimal256) uint64 { return
a.LowBits() },
+ fromI64: func(v int64) dec.Decimal256 { return
dec.Decimal256FromI64(v) },
+ str: func(a dec.Decimal256, scale int32) string
{ return a.ToString(scale) },
}
)
-type roundDec[T decimal128.Num | decimal256.Num] struct {
+type roundDec[T decimal128.Num | dec.Decimal256] struct {
ty arrow.DecimalType
mode RoundMode
ndigits int64
@@ -494,14 +494,14 @@ func (rnd *roundDec[T]) call(_ *exec.KernelCtx, arg T, e
*error) T {
return arg
}
-func getRoundKernelDecimal[T decimal128.Num | decimal256.Num]()
exec.ArrayKernelExec {
+func getRoundKernelDecimal[T decimal128.Num | dec.Decimal256]()
exec.ArrayKernelExec {
var def T
switch any(def).(type) {
case decimal128.Num:
return func(ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
return roundKernelDecimal(&roundDec128, ctx, batch, out)
}
- case decimal256.Num:
+ case dec.Decimal256:
return func(ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
return roundKernelDecimal(&roundDec256, ctx, batch, out)
}
@@ -509,7 +509,7 @@ func getRoundKernelDecimal[T decimal128.Num |
decimal256.Num]() exec.ArrayKernel
panic("should never get here")
}
-func roundKernelDecimal[T decimal128.Num | decimal256.Num](opsImpl
*roundDecImpl[T], ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
+func roundKernelDecimal[T decimal128.Num | dec.Decimal256](opsImpl
*roundDecImpl[T], ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
opts := ctx.State.(RoundState)
rnd := roundDec[T]{
ty: out.Type.(arrow.DecimalType),
@@ -529,14 +529,14 @@ func roundKernelDecimal[T decimal128.Num |
decimal256.Num](opsImpl *roundDecImpl
return ScalarUnaryNotNull(rnd.call)(ctx, batch, out)
}
-func getRoundToMultipleKernelDecimal[T decimal128.Num | decimal256.Num]()
exec.ArrayKernelExec {
+func getRoundToMultipleKernelDecimal[T decimal128.Num | dec.Decimal256]()
exec.ArrayKernelExec {
var def T
switch any(def).(type) {
case decimal128.Num:
return func(ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
return roundToMultipleDecimal(&roundDec128, ctx, batch,
out)
}
- case decimal256.Num:
+ case dec.Decimal256:
return func(ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
return roundToMultipleDecimal(&roundDec256, ctx, batch,
out)
}
@@ -544,7 +544,7 @@ func getRoundToMultipleKernelDecimal[T decimal128.Num |
decimal256.Num]() exec.A
panic("should never get here")
}
-func roundToMultipleDecimal[T decimal128.Num | decimal256.Num](opsImpl
*roundDecImpl[T], ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
+func roundToMultipleDecimal[T decimal128.Num | dec.Decimal256](opsImpl
*roundDecImpl[T], ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
opts := ctx.State.(RoundToMultipleState)
rnd := roundToMultipleDec[T]{
ty: out.Type.(arrow.DecimalType),
@@ -598,7 +598,7 @@ func (rnd *roundToMultiple[T]) call(_ *exec.KernelCtx, arg
T, e *error) T {
return roundVal
}
-type roundToMultipleDec[T decimal128.Num | decimal256.Num] struct {
+type roundToMultipleDec[T decimal128.Num | dec.Decimal256] struct {
ty arrow.DecimalType
mode RoundMode
@@ -707,7 +707,7 @@ func UnaryRoundExec(ty arrow.Type) exec.ArrayKernelExec {
case arrow.DECIMAL128:
return getRoundKernelDecimal[decimal128.Num]()
case arrow.DECIMAL256:
- return getRoundKernelDecimal[decimal256.Num]()
+ return getRoundKernelDecimal[dec.Decimal256]()
}
panic("should never get here")
}
@@ -721,7 +721,7 @@ func UnaryRoundToMultipleExec(ty arrow.Type)
exec.ArrayKernelExec {
case arrow.DECIMAL128:
return getRoundToMultipleKernelDecimal[decimal128.Num]()
case arrow.DECIMAL256:
- return getRoundToMultipleKernelDecimal[decimal256.Num]()
+ return getRoundToMultipleKernelDecimal[dec.Decimal256]()
}
panic("should never get here")
}
@@ -775,7 +775,7 @@ func GetSimpleRoundKernels(mode RoundMode)
[]exec.ScalarKernel {
return append(kernels, NullExecKernel(1))
}
-func fixedRoundDecimalExec[T decimal128.Num | decimal256.Num](opsImpl
*roundDecImpl[T], mode RoundMode) exec.ArrayKernelExec {
+func fixedRoundDecimalExec[T decimal128.Num | dec.Decimal256](opsImpl
*roundDecImpl[T], mode RoundMode) exec.ArrayKernelExec {
return func(ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
rnd := roundDec[T]{
ty: out.Type.(arrow.DecimalType),
@@ -795,14 +795,14 @@ func fixedRoundDecimalExec[T decimal128.Num |
decimal256.Num](opsImpl *roundDecI
}
}
-func FixedRoundDecimalExec[T decimal128.Num | decimal256.Num](mode RoundMode)
exec.ArrayKernelExec {
+func FixedRoundDecimalExec[T decimal128.Num | dec.Decimal256](mode RoundMode)
exec.ArrayKernelExec {
var def T
switch any(def).(type) {
case decimal128.Num:
return func() exec.ArrayKernelExec {
return fixedRoundDecimalExec(&roundDec128, mode)
}()
- case decimal256.Num:
+ case dec.Decimal256:
return func() exec.ArrayKernelExec {
return fixedRoundDecimalExec(&roundDec256, mode)
}()
diff --git a/arrow/compute/internal/kernels/scalar_arithmetic.go
b/arrow/compute/internal/kernels/scalar_arithmetic.go
index 0836008e..f0357d3d 100644
--- a/arrow/compute/internal/kernels/scalar_arithmetic.go
+++ b/arrow/compute/internal/kernels/scalar_arithmetic.go
@@ -25,8 +25,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
+ dec "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/scalar"
)
@@ -76,7 +76,7 @@ func GetDecimalBinaryKernels(op ArithmeticOp)
[]exec.ScalarKernel {
}
in128, in256 := exec.NewIDInput(arrow.DECIMAL128),
exec.NewIDInput(arrow.DECIMAL256)
- exec128, exec256 := getArithmeticDecimal[decimal128.Num](op),
getArithmeticDecimal[decimal256.Num](op)
+ exec128, exec256 := getArithmeticDecimal[decimal128.Num](op),
getArithmeticDecimal[dec.Decimal256](op)
return []exec.ScalarKernel{
exec.NewScalarKernel([]exec.InputType{in128, in128}, outType,
exec128, nil),
exec.NewScalarKernel([]exec.InputType{in256, in256}, outType,
exec256, nil),
@@ -99,7 +99,7 @@ func GetDecimalUnaryKernels(op ArithmeticOp)
[]exec.ScalarKernel {
in128 := exec.NewIDInput(arrow.DECIMAL128)
in256 := exec.NewIDInput(arrow.DECIMAL256)
- exec128, exec256 := getArithmeticDecimal[decimal128.Num](op),
getArithmeticDecimal[decimal256.Num](op)
+ exec128, exec256 := getArithmeticDecimal[decimal128.Num](op),
getArithmeticDecimal[dec.Decimal256](op)
return []exec.ScalarKernel{
exec.NewScalarKernel([]exec.InputType{in128}, outType, exec128,
nil),
exec.NewScalarKernel([]exec.InputType{in256}, outType, exec256,
nil),
@@ -175,7 +175,7 @@ func GetArithmeticUnaryFixedIntOutKernels(otype
arrow.DataType, op ArithmeticOp)
kernels = append(kernels, exec.NewScalarKernel(
[]exec.InputType{exec.NewIDInput(arrow.DECIMAL256)},
exec.NewOutputType(arrow.PrimitiveTypes.Int64),
- getArithmeticDecimal[decimal256.Num](op), nil))
+ getArithmeticDecimal[dec.Decimal256](op), nil))
return append(kernels, NullExecKernel(1))
}
diff --git a/arrow/compute/internal/kernels/scalar_comparisons.go
b/arrow/compute/internal/kernels/scalar_comparisons.go
index 3f0eb0c2..3082291b 100644
--- a/arrow/compute/internal/kernels/scalar_comparisons.go
+++ b/arrow/compute/internal/kernels/scalar_comparisons.go
@@ -27,8 +27,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
+ dec "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/memory"
@@ -227,7 +227,7 @@ func genGoCompareKernel[T arrow.FixedWidthType](op
*cmpOp[T]) *CompareData {
}
}
-type decCmp[T decimal128.Num | decimal256.Num] struct {
+type decCmp[T decimal128.Num | dec.Decimal256] struct {
Gt func(T, T) bool
Ge func(T, T) bool
}
@@ -237,12 +237,12 @@ var dec128Cmp = decCmp[decimal128.Num]{
Ge: func(a, b decimal128.Num) bool { return a.GreaterEqual(b) },
}
-var dec256Cmp = decCmp[decimal256.Num]{
- Gt: func(a, b decimal256.Num) bool { return a.Greater(b) },
- Ge: func(a, b decimal256.Num) bool { return a.GreaterEqual(b) },
+var dec256Cmp = decCmp[dec.Decimal256]{
+ Gt: func(a, b dec.Decimal256) bool { return a.Greater(b) },
+ Ge: func(a, b dec.Decimal256) bool { return a.GreaterEqual(b) },
}
-func getCmpDec[T decimal128.Num | decimal256.Num](op CompareOperator, fns
decCmp[T]) *cmpOp[T] {
+func getCmpDec[T decimal128.Num | dec.Decimal256](op CompareOperator, fns
decCmp[T]) *cmpOp[T] {
switch op {
case CmpEQ:
return &cmpOp[T]{
@@ -369,7 +369,7 @@ func getCmpDec[T decimal128.Num | decimal256.Num](op
CompareOperator, fns decCmp
return nil
}
-func genDecimalCompareKernel[T decimal128.Num | decimal256.Num](op
CompareOperator) (ex exec.ArrayKernelExec, data exec.KernelState) {
+func genDecimalCompareKernel[T decimal128.Num | dec.Decimal256](op
CompareOperator) (ex exec.ArrayKernelExec, data exec.KernelState) {
ex = compareKernel[T]
var def T
@@ -381,7 +381,7 @@ func genDecimalCompareKernel[T decimal128.Num |
decimal256.Num](op CompareOperat
funcAS: comparePrimitiveArrayScalar(cmp.arrScalar),
funcSA: comparePrimitiveScalarArray(cmp.scalarArr),
}
- case decimal256.Num:
+ case dec.Decimal256:
cmp := getCmpDec(op, dec256Cmp)
data = &CompareData{
funcAA: comparePrimitiveArrayArray(cmp.arrArr),
@@ -551,7 +551,7 @@ func numericCompareKernel[T arrow.NumericType](ty
exec.InputType, op CompareOper
return
}
-func decimalCompareKernel[T decimal128.Num | decimal256.Num](ty
exec.InputType, op CompareOperator) (kn exec.ScalarKernel) {
+func decimalCompareKernel[T decimal128.Num | dec.Decimal256](ty
exec.InputType, op CompareOperator) (kn exec.ScalarKernel) {
ex, data := genDecimalCompareKernel[T](op)
kn = exec.NewScalarKernelWithSig(&exec.KernelSignature{
InputTypes: []exec.InputType{ty, ty},
@@ -708,7 +708,7 @@ func CompareKernels(op CompareOperator) []exec.ScalarKernel
{
in128, in256 := exec.NewIDInput(arrow.DECIMAL128),
exec.NewIDInput(arrow.DECIMAL256)
kns = append(kns, decimalCompareKernel[decimal128.Num](in128, op),
- decimalCompareKernel[decimal256.Num](in256, op))
+ decimalCompareKernel[dec.Decimal256](in256, op))
inFSB := exec.NewIDInput(arrow.FIXED_SIZE_BINARY)
kns = append(kns, exec.NewScalarKernel([]exec.InputType{inFSB, inFSB},
outType,
diff --git a/arrow/compute/internal/kernels/vector_run_end_encode.go
b/arrow/compute/internal/kernels/vector_run_end_encode.go
index c6c43f06..c35f3a7d 100644
--- a/arrow/compute/internal/kernels/vector_run_end_encode.go
+++ b/arrow/compute/internal/kernels/vector_run_end_encode.go
@@ -27,8 +27,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
+ dec "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/memory"
@@ -500,7 +500,7 @@ func newEncoder[R RunEndsType](input *exec.ArraySpan)
encoder {
case arrow.DECIMAL128:
return createEncoder[R, decimal128.Num](input)
case arrow.DECIMAL256:
- return createEncoder[R, decimal256.Num](input)
+ return createEncoder[R, dec.Decimal256](input)
case arrow.INTERVAL_DAY_TIME:
return createEncoder[R, arrow.DayTimeInterval](input)
case arrow.INTERVAL_MONTH_DAY_NANO:
diff --git a/arrow/csv/reader.go b/arrow/csv/reader.go
index bfba66d2..eab1b293 100644
--- a/arrow/csv/reader.go
+++ b/arrow/csv/reader.go
@@ -31,8 +31,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/memory"
@@ -840,7 +840,7 @@ func (r *Reader) parseDecimal256(field array.Builder, str
string, prec, scale in
return
}
- val, err := decimal256.FromString(str, prec, scale)
+ val, err := decimal.Decimal256FromString(str, prec, scale)
if err != nil {
r.setParseError(err)
field.AppendNull()
diff --git a/arrow/decimal/decimal.go b/arrow/decimal/decimal.go
index 4624b562..64daf73f 100644
--- a/arrow/decimal/decimal.go
+++ b/arrow/decimal/decimal.go
@@ -26,7 +26,6 @@ import (
"unsafe"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/internal/decimalstr"
)
@@ -69,7 +68,6 @@ type (
Decimal32 int32
Decimal64 int64
Decimal128 = decimal128.Num
- Decimal256 = decimal256.Num
)
func MaxPrecision[T DecimalTypes]() int {
@@ -87,6 +85,10 @@ func MaxPrecision[T DecimalTypes]() int {
}
}
+// MaxScale returns the maximum scale for T, which equals its maximum
+// precision, as in Arrow C++.
+func MaxScale[T DecimalTypes]() int { return MaxPrecision[T]() }
+
func (d Decimal32) Negate() Decimal32 { return -d }
func (d Decimal64) Negate() Decimal64 { return -d }
@@ -273,10 +275,6 @@ func Decimal128FromString(v string, prec, scale int32) (n
Decimal128, err error)
return decimal128.FromString(v, prec, scale)
}
-func Decimal256FromString(v string, prec, scale int32) (n Decimal256, err
error) {
- return decimal256.FromString(v, prec, scale)
-}
-
func scalePositiveFloat64(v float64, prec, scale int32) (float64, error) {
v *= math.Pow10(int(scale))
v = math.RoundToEven(v)
@@ -331,10 +329,6 @@ func Decimal128FromFloat(v float64, prec, scale int32)
(Decimal128, error) {
return decimal128.FromFloat64(v, prec, scale)
}
-func Decimal256FromFloat(v float64, prec, scale int32) (Decimal256, error) {
- return decimal256.FromFloat64(v, prec, scale)
-}
-
func (n Decimal32) ToFloat32(scale int32) float32 {
return float32(n.ToFloat64(scale))
}
diff --git a/arrow/decimal/decimal256.go b/arrow/decimal/decimal256.go
new file mode 100644
index 00000000..ed41db82
--- /dev/null
+++ b/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")
+ }
+ // Each 64-bit limb contains two native-sized big.Int words.
+ var arr [4]uint64
+ for i, word := range b {
+ arr[i/2] |= uint64(word) << (32 * (i % 2))
+ }
+ x0, x1, x2, x3 = arr[0], arr[1], arr[2], arr[3]
+ } else {
+ switch len(b) {
+ case 1:
+ x0 = uint64(b[0])
+ case 2:
+ x1, x0 = uint64(b[1]), uint64(b[0])
+ case 3:
+ x2, x1, x0 = uint64(b[2]), uint64(b[1]), uint64(b[0])
+ case 4:
+ x3 = uint64(b[3])
+ if x3>>63 != 0 {
+ panic("arrow/decimal256: cannot represent value
larger than 256bits")
+ }
+ x2, x1, x0 = uint64(b[2]), uint64(b[1]), uint64(b[0])
+ default:
+ panic("arrow/decimal256: cannot represent value larger
than 256bits")
+ }
+ }
+ if v.Sign() < 0 {
+ var carry uint64
+ x0, carry = bits.Add64(^x0, 1, 0)
+ x1, carry = bits.Add64(^x1, 0, carry)
+ x2, carry = bits.Add64(^x2, 0, carry)
+ x3, _ = bits.Add64(^x3, 0, carry)
+ }
+ return Decimal256{[4]uint64{x0, x1, x2, x3}}
+}
+
+func toBigIntPositive256(n Decimal256) *big.Int {
+ if bits.UintSize == 32 {
+ return new(big.Int).SetBits([]big.Word{
+ big.Word(n.arr[0]), big.Word(n.arr[0] >> 32),
+ big.Word(n.arr[1]), big.Word(n.arr[1] >> 32),
+ big.Word(n.arr[2]), big.Word(n.arr[2] >> 32),
+ big.Word(n.arr[3]), big.Word(n.arr[3] >> 32),
+ })
+ }
+ return new(big.Int).SetBits([]big.Word{big.Word(n.arr[0]),
big.Word(n.arr[1]), big.Word(n.arr[2]), big.Word(n.arr[3])})
+}
+
+func (n Decimal256) BigInt() *big.Int {
+ if n.Sign() < 0 {
+ b := toBigIntPositive256(n.Negate())
+ return b.Neg(b)
+ }
+ return toBigIntPositive256(n)
+}
+
+// Greater returns true if the value represented by n is > other
+func (n Decimal256) Greater(other Decimal256) bool {
+ return other.Less(n)
+}
+
+// GreaterEqual returns true if the value represented by n is >= other
+func (n Decimal256) GreaterEqual(other Decimal256) bool {
+ return !n.Less(other)
+}
+
+// Less returns true if the value represented by n is < other
+func (n Decimal256) Less(other Decimal256) bool {
+ switch {
+ case n.arr[3] != other.arr[3]:
+ return int64(n.arr[3]) < int64(other.arr[3])
+ case n.arr[2] != other.arr[2]:
+ return n.arr[2] < other.arr[2]
+ case n.arr[1] != other.arr[1]:
+ return n.arr[1] < other.arr[1]
+ }
+ return n.arr[0] < other.arr[0]
+}
+
+// LessEqual returns true if the value represented by n is <= other
+func (n Decimal256) LessEqual(other Decimal256) bool {
+ return !n.Greater(other)
+}
+
+// Decimal256Max returns the largest Decimal256 that was passed in the
arguments
+func Decimal256Max(first Decimal256, rest ...Decimal256) Decimal256 {
+ answer := first
+ for _, number := range rest {
+ if number.Greater(answer) {
+ answer = number
+ }
+ }
+ return answer
+}
+
+// Decimal256Min returns the smallest Decimal256 that was passed in the
arguments
+func Decimal256Min(first Decimal256, rest ...Decimal256) Decimal256 {
+ answer := first
+ for _, number := range rest {
+ if number.Less(answer) {
+ answer = number
+ }
+ }
+ return answer
+}
+
+// Cmp compares the numbers represented by n and other and returns:
+//
+// +1 if n > other
+// 0 if n == other
+// -1 if n < other
+func (n Decimal256) Cmp(other Decimal256) int {
+ switch {
+ case n.Greater(other):
+ return 1
+ case n.Less(other):
+ return -1
+ }
+ return 0
+}
+
+func (n Decimal256) IncreaseScaleBy(increase int32) Decimal256 {
+ debug.Assert(increase >= 0, "invalid amount to increase scale by")
+ debug.Assert(increase <= 76, "invalid amount to increase scale by")
+
+ v := scaleMultipliers256[increase].BigInt()
+ return Decimal256FromBigInt(v.Mul(n.BigInt(), v))
+}
+
+func (n Decimal256) ReduceScaleBy(reduce int32, round bool) Decimal256 {
+ debug.Assert(reduce >= 0, "invalid amount to reduce scale by")
+ debug.Assert(reduce <= 76, "invalid amount to reduce scale by")
+
+ if reduce == 0 {
+ return n
+ }
+
+ divisor := scaleMultipliers256[reduce].BigInt()
+ result, remainder := divisor.QuoRem(n.BigInt(), divisor, new(big.Int))
+ if round {
+ divisorHalf := scaleMultipliersHalf256[reduce]
+ if remainder.Abs(remainder).Cmp(divisorHalf.BigInt()) != -1 {
+ result.Add(result, big.NewInt(int64(n.Sign())))
+ }
+ }
+ return Decimal256FromBigInt(result)
+}
+
+func (n Decimal256) rescaleWouldCauseDataLoss(deltaScale int64, multiplier
Decimal256) (out Decimal256, loss bool) {
+ if deltaScale < 0 {
+ var remainder Decimal256
+ out, remainder = n.Div(multiplier)
+ return out, remainder != Decimal256{}
+ }
+
+ result := n.BigInt()
+ result.Mul(result, multiplier.BigInt())
+ if result.BitLen() > 255 {
+ return Decimal256{}, true
+ }
+ return Decimal256FromBigInt(result), false
+}
+
+func (n Decimal256) Rescale(original, newscale int32) (out Decimal256, err
error) {
+ if original == newscale || n == (Decimal256{}) {
+ return n, nil
+ }
+
+ deltaScale := int64(newscale) - int64(original)
+ absDeltaScale := deltaScale
+ if absDeltaScale < 0 {
+ absDeltaScale = -absDeltaScale
+ }
+ // Scaling any nonzero value by 10^77 or more overflows or drops every
digit.
+ if absDeltaScale >= int64(len(scaleMultipliers256)) {
+ return Decimal256{}, errors.New("rescale data loss")
+ }
+
+ multiplier := scaleMultipliers256[absDeltaScale]
+ var wouldHaveLoss bool
+ out, wouldHaveLoss = n.rescaleWouldCauseDataLoss(deltaScale, multiplier)
+ if wouldHaveLoss {
+ err = errors.New("rescale data loss")
+ }
+ return
+}
+
+func (n Decimal256) Abs() Decimal256 {
+ switch n.Sign() {
+ case -1:
+ return n.Negate()
+ }
+ return n
+}
+
+func (n Decimal256) FitsInPrecision(prec int32) bool {
+ debug.Assert(prec > 0, "precision must be > 0")
+ debug.Assert(prec <= 76, "precision must be <= 76")
+ return n.Abs().Less(scaleMultipliers256[prec])
+}
+
+func (n Decimal256) ToString(scale int32) string {
+ if scale < -maxScale256 || scale > maxScale256 {
+ panic("arrow/decimal256: scale out of range")
+ }
+
+ value := n.BigInt().String()
+ digits := len(value)
+ if value[0] == '-' {
+ digits--
+ }
+ if digits > maxPrecision256 {
+ return n.ToBigFloat(scale).Text('f', int(scale))
+ }
+ if scale <= 0 {
+ if scale < 0 && value != "0" {
+ value += strings.Repeat("0", int(-scale))
+ }
+ return value
+ }
+
+ negative := value[0] == '-'
+ if negative {
+ value = value[1:]
+ }
+ places := int(scale)
+ var result string
+ if len(value) <= places {
+ result = "0." + strings.Repeat("0", places-len(value)) + value
+ } else {
+ at := len(value) - places
+ result = value[:at] + "." + value[at:]
+ }
+ if negative {
+ return "-" + result
+ }
+ return result
+}
+
+func Decimal256ScaleMultiplier(pow int) Decimal256 { return
scaleMultipliers256[pow] }
+
+func Decimal256HalfScaleMultiplier(pow int) Decimal256 { return
scaleMultipliersHalf256[pow] }
+
+var (
+ scaleMultipliers256 = [...]Decimal256{
+ Decimal256FromU64(1),
+ Decimal256FromU64(10),
+ Decimal256FromU64(100),
+ Decimal256FromU64(1000),
+ Decimal256FromU64(10000),
+ Decimal256FromU64(100000),
+ Decimal256FromU64(1000000),
+ Decimal256FromU64(10000000),
+ Decimal256FromU64(100000000),
+ Decimal256FromU64(1000000000),
+ Decimal256FromU64(10000000000),
+ Decimal256FromU64(100000000000),
+ Decimal256FromU64(1000000000000),
+ Decimal256FromU64(10000000000000),
+ Decimal256FromU64(100000000000000),
+ Decimal256FromU64(1000000000000000),
+ Decimal256FromU64(10000000000000000),
+ Decimal256FromU64(100000000000000000),
+ Decimal256FromU64(1000000000000000000),
+ NewDecimal256(0, 0, 0, 10000000000000000000),
+ NewDecimal256(0, 0, 5, 7766279631452241920),
+ NewDecimal256(0, 0, 54, 3875820019684212736),
+ NewDecimal256(0, 0, 542, 1864712049423024128),
+ NewDecimal256(0, 0, 5421, 200376420520689664),
+ NewDecimal256(0, 0, 54210, 2003764205206896640),
+ NewDecimal256(0, 0, 542101, 1590897978359414784),
+ NewDecimal256(0, 0, 5421010, 15908979783594147840),
+ NewDecimal256(0, 0, 54210108, 11515845246265065472),
+ NewDecimal256(0, 0, 542101086, 4477988020393345024),
+ NewDecimal256(0, 0, 5421010862, 7886392056514347008),
+ NewDecimal256(0, 0, 54210108624, 5076944270305263616),
+ NewDecimal256(0, 0, 542101086242, 13875954555633532928),
+ NewDecimal256(0, 0, 5421010862427, 9632337040368467968),
+ NewDecimal256(0, 0, 54210108624275, 4089650035136921600),
+ NewDecimal256(0, 0, 542101086242752, 4003012203950112768),
+ NewDecimal256(0, 0, 5421010862427522, 3136633892082024448),
+ NewDecimal256(0, 0, 54210108624275221, 12919594847110692864),
+ NewDecimal256(0, 0, 542101086242752217, 68739955140067328),
+ NewDecimal256(0, 0, 5421010862427522170, 687399551400673280),
+ NewDecimal256(0, 2, 17316620476856118468, 6873995514006732800),
+ NewDecimal256(0, 29, 7145508105175220139, 13399722918938673152),
+ NewDecimal256(0, 293, 16114848830623546549,
4870020673419870208),
+ NewDecimal256(0, 2938, 13574535716559052564,
11806718586779598848),
+ NewDecimal256(0, 29387, 6618148649623664334,
7386721425538678784),
+ NewDecimal256(0, 293873, 10841254275107988496,
80237960548581376),
+ NewDecimal256(0, 2938735, 16178822382532126880,
802379605485813760),
+ NewDecimal256(0, 29387358, 14214271235644855872,
8023796054858137600),
+ NewDecimal256(0, 293873587, 13015503840481697412,
6450984253743169536),
+ NewDecimal256(0, 2938735877, 1027829888850112811,
9169610316303040512),
+ NewDecimal256(0, 29387358770, 10278298888501128114,
17909126868192198656),
+ NewDecimal256(0, 293873587705, 10549268516463523069,
13070572018536022016),
+ NewDecimal256(0, 2938735877055, 13258964796087472617,
1578511669393358848),
+ NewDecimal256(0, 29387358770557, 3462439444907864858,
15785116693933588480),
+ NewDecimal256(0, 293873587705571, 16177650375369096972,
10277214349659471872),
+ NewDecimal256(0, 2938735877055718, 14202551164014556797,
10538423128046960640),
+ NewDecimal256(0, 29387358770557187, 12898303124178706663,
13150510911921848320),
+ NewDecimal256(0, 293873587705571876, 18302566799529756941,
2377900603251621888),
+ NewDecimal256(0, 2938735877055718769, 17004971331911604867,
5332261958806667264),
+ NewDecimal256(1, 10940614696847636083, 4029016655730084128,
16429131440647569408),
+ NewDecimal256(15, 17172426599928602752, 3396678409881738056,
16717361816799281152),
+ NewDecimal256(159, 5703569335900062977, 15520040025107828953,
1152921504606846976),
+ NewDecimal256(1593, 1695461137871974930, 7626447661401876602,
11529215046068469760),
+ NewDecimal256(15930, 16954611378719749304, 2477500319180559562,
4611686018427387904),
+ NewDecimal256(159309, 3525417123811528497, 6328259118096044006,
9223372036854775808),
+ NewDecimal256(1593091, 16807427164405733357,
7942358959831785217, 0),
+ NewDecimal256(15930919, 2053574980671369030,
5636613303479645706, 0),
+ NewDecimal256(159309191, 2089005733004138687,
1025900813667802212, 0),
+ NewDecimal256(1593091911, 2443313256331835254,
10259008136678022120, 0),
+ NewDecimal256(15930919111, 5986388489608800929,
10356360998232463120, 0),
+ NewDecimal256(159309191113, 4523652674959354447,
11329889613776873120, 0),
+ NewDecimal256(1593091911132, 8343038602174441244,
2618431695511421504, 0),
+ NewDecimal256(15930919111324, 9643409726906205977,
7737572881404663424, 0),
+ NewDecimal256(159309191113245, 4200376900514301694,
3588752519208427776, 0),
+ NewDecimal256(1593091911132452, 5110280857723913709,
17440781118374726144, 0),
+ NewDecimal256(15930919111324522, 14209320429820033867,
8387114520361296896, 0),
+ NewDecimal256(159309191113245227, 12965995782233477362,
10084168908774762496, 0),
+ NewDecimal256(1593091911132452277, 532749306367912313,
8607968719199866880, 0),
+ }
+
+ scaleMultipliersHalf256 = [...]Decimal256{
+ Decimal256FromU64(0),
+ Decimal256FromU64(5),
+ Decimal256FromU64(50),
+ Decimal256FromU64(500),
+ Decimal256FromU64(5000),
+ Decimal256FromU64(50000),
+ Decimal256FromU64(500000),
+ Decimal256FromU64(5000000),
+ Decimal256FromU64(50000000),
+ Decimal256FromU64(500000000),
+ Decimal256FromU64(5000000000),
+ Decimal256FromU64(50000000000),
+ Decimal256FromU64(500000000000),
+ Decimal256FromU64(5000000000000),
+ Decimal256FromU64(50000000000000),
+ Decimal256FromU64(500000000000000),
+ Decimal256FromU64(5000000000000000),
+ Decimal256FromU64(50000000000000000),
+ Decimal256FromU64(500000000000000000),
+ Decimal256FromU64(5000000000000000000),
+ NewDecimal256(0, 0, 2, 13106511852580896768),
+ NewDecimal256(0, 0, 27, 1937910009842106368),
+ NewDecimal256(0, 0, 271, 932356024711512064),
+ NewDecimal256(0, 0, 2710, 9323560247115120640),
+ NewDecimal256(0, 0, 27105, 1001882102603448320),
+ NewDecimal256(0, 0, 271050, 10018821026034483200),
+ NewDecimal256(0, 0, 2710505, 7954489891797073920),
+ NewDecimal256(0, 0, 27105054, 5757922623132532736),
+ NewDecimal256(0, 0, 271050543, 2238994010196672512),
+ NewDecimal256(0, 0, 2710505431, 3943196028257173504),
+ NewDecimal256(0, 0, 27105054312, 2538472135152631808),
+ NewDecimal256(0, 0, 271050543121, 6937977277816766464),
+ NewDecimal256(0, 0, 2710505431213, 14039540557039009792),
+ NewDecimal256(0, 0, 27105054312137, 11268197054423236608),
+ NewDecimal256(0, 0, 271050543121376, 2001506101975056384),
+ NewDecimal256(0, 0, 2710505431213761, 1568316946041012224),
+ NewDecimal256(0, 0, 27105054312137610, 15683169460410122240),
+ NewDecimal256(0, 0, 271050543121376108, 9257742014424809472),
+ NewDecimal256(0, 0, 2710505431213761085, 343699775700336640),
+ NewDecimal256(0, 1, 8658310238428059234, 3436997757003366400),
+ NewDecimal256(0, 14, 12796126089442385877,
15923233496324112384),
+ NewDecimal256(0, 146, 17280796452166549082,
11658382373564710912),
+ NewDecimal256(0, 1469, 6787267858279526282,
5903359293389799424),
+ NewDecimal256(0, 14693, 12532446361666607975,
3693360712769339392),
+ NewDecimal256(0, 146936, 14643999174408770056,
40118980274290688),
+ NewDecimal256(0, 1469367, 17312783228120839248,
401189802742906880),
+ NewDecimal256(0, 14693679, 7107135617822427936,
4011898027429068800),
+ NewDecimal256(0, 146936793, 15731123957095624514,
3225492126871584768),
+ NewDecimal256(0, 1469367938, 9737286981279832213,
13808177195006296064),
+ NewDecimal256(0, 14693679385, 5139149444250564057,
8954563434096099328),
+ NewDecimal256(0, 146936793852, 14498006295086537342,
15758658046122786816),
+ NewDecimal256(0, 1469367938527, 15852854434898512116,
10012627871551455232),
+ NewDecimal256(0, 14693679385278, 10954591759308708237,
7892558346966794240),
+ NewDecimal256(0, 146936793852785, 17312197224539324294,
5138607174829735936),
+ NewDecimal256(0, 1469367938527859, 7101275582007278398,
14492583600878256128),
+ NewDecimal256(0, 14693679385278593, 15672523598944129139,
15798627492815699968),
+ NewDecimal256(0, 146936793852785938, 9151283399764878470,
10412322338480586752),
+ NewDecimal256(0, 1469367938527859384, 17725857702810578241,
11889503016258109440),
+ NewDecimal256(0, 14693679385278593849, 11237880364719817872,
8214565720323784704),
+ NewDecimal256(7, 17809585336819077184, 1698339204940869028,
8358680908399640576),
+ NewDecimal256(79, 12075156704804807296, 16983392049408690284,
9799832789158199296),
+ NewDecimal256(796, 10071102605790763273, 3813223830700938301,
5764607523034234880),
+ NewDecimal256(7965, 8477305689359874652, 1238750159590279781,
2305843009213693952),
+ NewDecimal256(79654, 10986080598760540056,
12387501595902797811, 4611686018427387904),
+ NewDecimal256(796545, 17627085619057642486,
13194551516770668416, 9223372036854775808),
+ NewDecimal256(7965459, 10250159527190460323,
2818306651739822853, 0),
+ NewDecimal256(79654595, 10267874903356845151,
9736322443688676914, 0),
+ NewDecimal256(796545955, 10445028665020693435,
5129504068339011060, 0),
+ NewDecimal256(7965459555, 12216566281659176272,
14401552535971007368, 0),
+ NewDecimal256(79654595556, 11485198374334453031,
14888316843743212368, 0),
+ NewDecimal256(796545955566, 4171519301087220622,
1309215847755710752, 0),
+ NewDecimal256(7965459555662, 4821704863453102988,
13092158477557107520, 0),
+ NewDecimal256(79654595556622, 11323560487111926655,
1794376259604213888, 0),
+ NewDecimal256(796545955566226, 2555140428861956854,
17943762596042138880, 0),
+ NewDecimal256(7965459555662261, 7104660214910016933,
13416929297035424256, 0),
+ NewDecimal256(79654595556622613, 15706369927971514489,
5042084454387381248, 0),
+ NewDecimal256(796545955566226138, 9489746690038731964,
13527356396454709248, 0),
+ }
+
+ float64PowersOfTen256 = [...]float64{
+ 1e-76, 1e-75, 1e-74, 1e-73, 1e-72, 1e-71, 1e-70, 1e-69, 1e-68,
1e-67, 1e-66, 1e-65,
+ 1e-64, 1e-63, 1e-62, 1e-61, 1e-60, 1e-59, 1e-58, 1e-57, 1e-56,
1e-55, 1e-54, 1e-53,
+ 1e-52, 1e-51, 1e-50, 1e-49, 1e-48, 1e-47, 1e-46, 1e-45, 1e-44,
1e-43, 1e-42, 1e-41,
+ 1e-40, 1e-39, 1e-38, 1e-37, 1e-36, 1e-35, 1e-34, 1e-33, 1e-32,
1e-31, 1e-30, 1e-29,
+ 1e-28, 1e-27, 1e-26, 1e-25, 1e-24, 1e-23, 1e-22, 1e-21, 1e-20,
1e-19, 1e-18, 1e-17,
+ 1e-16, 1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-9, 1e-8,
1e-7, 1e-6, 1e-5,
+ 1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
+ 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18,
1e19,
+ 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
1e30, 1e31,
+ 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39, 1e40, 1e41,
1e42, 1e43,
+ 1e44, 1e45, 1e46, 1e47, 1e48, 1e49, 1e50, 1e51, 1e52, 1e53,
1e54, 1e55,
+ 1e56, 1e57, 1e58, 1e59, 1e60, 1e61, 1e62, 1e63, 1e64, 1e65,
1e66, 1e67,
+ 1e68, 1e69, 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76,
+ }
+)
diff --git a/arrow/decimal/decimal_test.go b/arrow/decimal/decimal_test.go
index 49cff5f7..da67db2d 100644
--- a/arrow/decimal/decimal_test.go
+++ b/arrow/decimal/decimal_test.go
@@ -26,6 +26,7 @@ import (
"testing"
"github.com/apache/arrow-go/v18/arrow/decimal"
+ "github.com/apache/arrow-go/v18/arrow/decimal128"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -688,3 +689,141 @@ func TestFromStringLongFraction(t *testing.T) {
})
}
}
+
+func TestMaxScale(t *testing.T) {
+ assert.Equal(t, 9, decimal.MaxScale[decimal.Decimal32]())
+ assert.Equal(t, 18, decimal.MaxScale[decimal.Decimal64]())
+ assert.Equal(t, 38, decimal.MaxScale[decimal.Decimal128]())
+ assert.Equal(t, 76, decimal.MaxScale[decimal.Decimal256]())
+}
+
+func TestDecimal256FromI64AndDecimal128(t *testing.T) {
+ ints := []int64{
+ 0, 1, -1, 2, -2, math.MaxInt32, math.MinInt32, 1 << 32, -(1 <<
32),
+ math.MaxInt64, math.MinInt64, math.MinInt64 + 1,
+ }
+ x := uint64(0x9e3779b97f4a7c15)
+ next := func() uint64 {
+ x ^= x << 13
+ x ^= x >> 7
+ x ^= x << 17
+ return x
+ }
+ for range 1000 {
+ ints = append(ints, int64(next())>>(next()%63))
+ }
+
+ for _, v := range ints {
+ ext := uint64(0)
+ if v < 0 {
+ ext = math.MaxUint64
+ }
+ n := decimal.Decimal256FromI64(v)
+ if want := [4]uint64{uint64(v), ext, ext, ext}; n.Array() !=
want {
+ t.Fatalf("Decimal256FromI64(%d).Array() = %x, want %x",
v, n.Array(), want)
+ }
+ if n.BigInt().Cmp(big.NewInt(v)) != 0 {
+ t.Fatalf("Decimal256FromI64(%d) = %v", v, n.BigInt())
+ }
+
+ lo := next()
+ for _, d := range []decimal128.Num{decimal128.New(v, lo),
decimal128.New(v, 0), decimal128.New(v, math.MaxUint64), decimal128.FromI64(v)}
{
+ n := decimal.Decimal256FromDecimal128(d)
+ if want := [4]uint64{d.LowBits(), uint64(d.HighBits()),
ext, ext}; n.Array() != want {
+ t.Fatalf("Decimal256FromDecimal128(%v).Array()
= %x, want %x", d, n.Array(), want)
+ }
+ if n.BigInt().Cmp(d.BigInt()) != 0 {
+ t.Fatalf("Decimal256FromDecimal128(%v) = %v",
d, n.BigInt())
+ }
+ }
+ }
+}
+
+func TestDecimal256FromBigInt(t *testing.T) {
+ one := big.NewInt(1)
+ pow2 := func(shift uint) *big.Int {
+ return new(big.Int).Lsh(one, shift)
+ }
+ maxPos := new(big.Int).Sub(pow2(255), one)
+ minNeg := new(big.Int).Neg(maxPos)
+
+ distinct := new(big.Int).SetBits([]big.Word{0x1111})
+ distinct.Lsh(distinct, 64).Or(distinct,
new(big.Int).SetUint64(0x2222333344445555))
+ distinct.Lsh(distinct, 64).Or(distinct,
new(big.Int).SetUint64(0x6666777788889999))
+ distinct.Lsh(distinct, 64).Or(distinct,
new(big.Int).SetUint64(0xaaaabbbbccccdddd))
+
+ exactCases := []struct {
+ name string
+ val *big.Int
+ want [4]uint64
+ }{
+ {"zero", big.NewInt(0), [4]uint64{0, 0, 0, 0}},
+ {"pos_1", big.NewInt(1), [4]uint64{1, 0, 0, 0}},
+ {"neg_1", big.NewInt(-1), [4]uint64{math.MaxUint64,
math.MaxUint64, math.MaxUint64, math.MaxUint64}},
+ {"pos_2word_pow64", pow2(64), [4]uint64{0, 1, 0, 0}},
+ {"neg_2word_pow64", new(big.Int).Neg(pow2(64)), [4]uint64{0,
math.MaxUint64, math.MaxUint64, math.MaxUint64}},
+ {"pos_3word_pow128", pow2(128), [4]uint64{0, 0, 1, 0}},
+ {"neg_3word_pow128", new(big.Int).Neg(pow2(128)), [4]uint64{0,
0, math.MaxUint64, math.MaxUint64}},
+ {"pos_4word_pow192", pow2(192), [4]uint64{0, 0, 0, 1}},
+ {"neg_4word_pow192", new(big.Int).Neg(pow2(192)), [4]uint64{0,
0, 0, math.MaxUint64}},
+ {"pos_distinct", distinct, [4]uint64{0xaaaabbbbccccdddd,
0x6666777788889999, 0x2222333344445555, 0x1111}},
+ {"neg_distinct", new(big.Int).Neg(distinct),
[4]uint64{^uint64(0xaaaabbbbccccdddd) + 1, ^uint64(0x6666777788889999),
^uint64(0x2222333344445555), ^uint64(0x1111)}},
+ {"max_pos_2_255_minus_1", maxPos, [4]uint64{math.MaxUint64,
math.MaxUint64, math.MaxUint64, math.MaxInt64}},
+ {"min_neg_minus_2_255_plus_1", minNeg, [4]uint64{1, 0, 0, 1 <<
63}},
+ }
+ for _, tc := range exactCases {
+ t.Run(tc.name, func(t *testing.T) {
+ got := decimal.Decimal256FromBigInt(tc.val)
+ assert.Equal(t, tc.want, got.Array())
+ assert.Equal(t, 0, got.BigInt().Cmp(tc.val),
"round-trip %s: got %v, want %v", tc.name, got.BigInt(), tc.val)
+ })
+ }
+
+ // Carry-propagation and multi-word round-trip coverage across 1..255
bits.
+ x := uint64(0x9e3779b97f4a7c15)
+ next := func() uint64 {
+ x ^= x << 13
+ x ^= x >> 7
+ x ^= x << 17
+ return x
+ }
+ var buf [32]byte
+ for bitlen := uint(1); bitlen <= 255; bitlen++ {
+ for _, sign := range []int{1, -1} {
+ for i := 0; i < 4; i++ {
+ w := next()
+ for j := 0; j < 8; j++ {
+ buf[i*8+j] = byte(w >> (8 * j))
+ }
+ }
+ v := new(big.Int).SetBytes(buf[:])
+ v.Rsh(v, 256-bitlen)
+ if sign < 0 {
+ v.Neg(v)
+ }
+ got := decimal.Decimal256FromBigInt(v)
+ if got.BigInt().Cmp(v) != 0 {
+ t.Fatalf("Decimal256FromBigInt(bitlen=%d,
sign=%d): got %v, want %v", bitlen, sign, got.BigInt(), v)
+ }
+ }
+ }
+
+ const overflowMsg = "arrow/decimal256: cannot represent value larger
than 256bits"
+ for _, tc := range []struct {
+ name string
+ val *big.Int
+ }{
+ {"pos_2_255", pow2(255)},
+ {"neg_2_255", new(big.Int).Neg(pow2(255))},
+ {"pos_2_256", pow2(256)},
+ {"neg_2_256", new(big.Int).Neg(pow2(256))},
+ {"pos_2_300", pow2(300)},
+ {"neg_2_300", new(big.Int).Neg(pow2(300))},
+ } {
+ t.Run("overflow_"+tc.name, func(t *testing.T) {
+ assert.PanicsWithValue(t, overflowMsg, func() {
+ _ = decimal.Decimal256FromBigInt(tc.val)
+ })
+ })
+ }
+}
diff --git a/arrow/decimal/doc.go b/arrow/decimal/doc.go
new file mode 100644
index 00000000..c99760cd
--- /dev/null
+++ b/arrow/decimal/doc.go
@@ -0,0 +1,24 @@
+// 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 implements the Arrow decimal types [Decimal32], [Decimal64],
+// [Decimal128] and [Decimal256]. A value holds only the integer coefficient:
+// functions and methods take the precision and scale as arguments.
+//
+// The decimal256 package provides the Decimal256 API under its original
+// names, such as decimal256.Num and decimal256.FromString, as aliases of and
+// forwarders to this package.
+package decimal
diff --git a/arrow/decimal/layout_test.go b/arrow/decimal/layout_test.go
new file mode 100644
index 00000000..efa37650
--- /dev/null
+++ b/arrow/decimal/layout_test.go
@@ -0,0 +1,33 @@
+// 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 (
+ "testing"
+ "unsafe"
+
+ "github.com/stretchr/testify/assert"
+)
+
+// Array buffers, IPC endian swapping and scalars reinterpret Decimal256 values
+// as four little endian uint64 limbs.
+func TestDecimal256Layout(t *testing.T) {
+ var n Decimal256
+ assert.EqualValues(t, 32, unsafe.Sizeof(n))
+ assert.EqualValues(t, 0, unsafe.Offsetof(n.arr))
+ assert.Equal(t, [4]uint64{4, 3, 2, 1}, NewDecimal256(1, 2, 3, 4).arr)
+}
diff --git a/arrow/decimal256/alias.go b/arrow/decimal256/alias.go
new file mode 100644
index 00000000..f7cdf1ab
--- /dev/null
+++ b/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)
+ _ = n.Greater(*new(Num))
+ _ = n.GreaterEqual(*new(Num))
+ _ = n.IncreaseScaleBy(0)
+ _ = n.Less(*new(Num))
+ _ = n.LessEqual(*new(Num))
+ _ = n.LowBits()
+ _ = n.Mul(*new(Num))
+ _ = n.Negate()
+ _ = n.Pow(*new(Num))
+ _ = n.ReduceScaleBy(0, false)
+ _, _ = n.Rescale(0, 0)
+ _ = n.Sign()
+ _ = n.Sub(*new(Num))
+ _ = n.ToBigFloat(0)
+ _ = n.ToFloat32(0)
+ _ = n.ToFloat64(0)
+ _ = n.ToString(0)
+}
diff --git a/arrow/decimal256/alias_test.go b/arrow/decimal256/alias_test.go
new file mode 100644
index 00000000..63fed574
--- /dev/null
+++ b/arrow/decimal256/alias_test.go
@@ -0,0 +1,100 @@
+// 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
+
+import (
+ _ "embed"
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "reflect"
+ "sort"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+//go:embed alias.go
+var aliasSource []byte
+
+// TestInlineStubCoversAllNumMethods fails when the inlining stub at the
bottom of
+// alias.go stops matching the method set of Num, which silently rebreaks
+// https://github.com/golang/go/issues/82474 for importers of this package.
+func TestInlineStubCoversAllNumMethods(t *testing.T) {
+ typ := reflect.TypeOf(Num{})
+ want := make([]string, 0, typ.NumMethod())
+ for i := range typ.NumMethod() {
+ want = append(want, typ.Method(i).Name)
+ }
+ sort.Strings(want)
+
+ got := inlinedMethods(t)
+ sort.Strings(got)
+
+ assert.Equal(t, want, got, "alias.go inlining stub must call every
method of Num")
+}
+
+func inlinedMethods(t *testing.T) []string {
+ t.Helper()
+
+ f, err := parser.ParseFile(token.NewFileSet(), "alias.go", aliasSource,
0)
+ require.NoError(t, err)
+
+ var stub *ast.FuncLit
+ for _, decl := range f.Decls {
+ gd, ok := decl.(*ast.GenDecl)
+ if !ok || gd.Tok != token.VAR {
+ continue
+ }
+ for _, spec := range gd.Specs {
+ vs, ok := spec.(*ast.ValueSpec)
+ if !ok {
+ continue
+ }
+ for _, v := range vs.Values {
+ lit, ok := v.(*ast.FuncLit)
+ if !ok {
+ continue
+ }
+ require.Nil(t, stub, "alias.go has more than
one inlining stub")
+ stub = lit
+ }
+ }
+ }
+ require.NotNil(t, stub, "alias.go inlining stub not found")
+ require.Len(t, stub.Type.Params.List, 1)
+ require.Len(t, stub.Type.Params.List[0].Names, 1)
+ param := stub.Type.Params.List[0].Names[0].Name
+
+ var methods []string
+ ast.Inspect(stub.Body, func(node ast.Node) bool {
+ call, ok := node.(*ast.CallExpr)
+ if !ok {
+ return true
+ }
+ sel, ok := call.Fun.(*ast.SelectorExpr)
+ if !ok {
+ return true
+ }
+ if id, ok := sel.X.(*ast.Ident); ok && id.Name == param {
+ methods = append(methods, sel.Sel.Name)
+ }
+ return true
+ })
+ return methods
+}
diff --git a/arrow/decimal256/decimal256.go b/arrow/decimal256/decimal256.go
deleted file mode 100644
index 5ffc5617..00000000
--- a/arrow/decimal256/decimal256.go
+++ /dev/null
@@ -1,822 +0,0 @@
-// 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
-
-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 (
- MaxPrecision = 76
- MaxScale = 76
-)
-
-func GetMaxValue(prec int32) Num {
- return scaleMultipliers[prec].Sub(FromU64(1))
-}
-
-type Num struct {
- // arr[0] is the lowest bits, arr[3] is the highest bits
- arr [4]uint64
-}
-
-// 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 Num{[4]uint64{x4, x3, x2, x1}}
-}
-
-func (n Num) Array() [4]uint64 { return n.arr }
-
-func (n Num) LowBits() uint64 { return n.arr[0] }
-
-func FromDecimal128(n decimal128.Num) Num {
- var topBits uint64
- if n.Sign() < 0 {
- topBits = math.MaxUint64
- }
- return New(topBits, topBits, uint64(n.HighBits()), n.LowBits())
-}
-
-func FromU64(v uint64) Num {
- return Num{[4]uint64{v, 0, 0, 0}}
-}
-
-func FromI64(v int64) Num {
- switch {
- case v > 0:
- return New(0, 0, 0, uint64(v))
- case v < 0:
- return New(math.MaxUint64, math.MaxUint64, math.MaxUint64,
uint64(v))
- default:
- return Num{}
- }
-}
-
-func (n Num) Negate() Num {
- 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 Num) Add(rhs Num) Num {
- var carry uint64
- for i, v := range n.arr {
- n.arr[i], carry = bits.Add64(v, rhs.arr[i], carry)
- }
- return n
-}
-
-func (n Num) Sub(rhs Num) Num {
- return n.Add(rhs.Negate())
-}
-
-func (n Num) Mul(rhs Num) Num {
- b := n.BigInt()
- return FromBigInt(b.Mul(b, rhs.BigInt()))
-}
-
-func (n Num) Div(rhs Num) (res, rem Num) {
- b := n.BigInt()
- out, remainder := b.QuoRem(b, rhs.BigInt(), &big.Int{})
- return FromBigInt(out), FromBigInt(remainder)
-}
-
-func (n Num) Pow(rhs Num) Num {
- b := n.BigInt()
- return FromBigInt(b.Exp(b, rhs.BigInt(), nil))
-}
-
-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
256-bit
- // storage, before using the scale.
- if lit.AtLeastTwoPower(0, 255) {
- return Num{}, errors.New("bitlen too large for
decimal256")
- }
- if scale < -MaxScale {
- return Num{}, fmt.Errorf("scale %d out of range for
decimal256, must be between %d and %d", scale, -MaxScale, MaxScale)
- }
- } else if scale > MaxScale {
- return Num{}, fmt.Errorf("scale %d out of range for decimal256,
must be between %d and %d", scale, -MaxScale, MaxScale)
- } else if lit.AtLeastTwoPower(int64(scale), 256) {
- return Num{}, errors.New("bitlen too large for decimal256")
- }
-
- val, ok := lit.ScaleAndRound(int64(scale))
- if !ok || val.BitLen() > 255 {
- return Num{}, errors.New("bitlen too large for decimal256")
- }
- n = FromBigInt(val)
- if !n.FitsInPrecision(prec) {
- err = fmt.Errorf("value %v doesn't fit in precision %d", n,
prec)
- }
- return
-}
-
-func fromFloatLiteral(v string, prec, scale int32) (n Num, 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 Num{}, 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 Num{}, errors.New("bitlen too large for
decimal256")
- }
- if scale < -MaxScale {
- return Num{}, fmt.Errorf("scale %d out of range for
decimal256, must be between %d and %d", scale, -MaxScale, MaxScale)
- }
- } else if scale > MaxScale {
- return Num{}, fmt.Errorf("scale %d out of range for decimal256,
must be between %d and %d", scale, -MaxScale, MaxScale)
- }
-
- if scale != 0 {
- if scale > 0 {
- out.Mul(out,
(&big.Float{}).SetInt(scaleMultipliers[scale].BigInt())).SetPrec(precInBits)
- } else {
- out.Quo(out,
(&big.Float{}).SetInt(scaleMultipliers[-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 Num{}, errors.New("bitlen too large for
decimal256")
- }
- }
- val := decimalstr.RoundScaled(out, v, int64(scale))
- if val.BitLen() > 255 {
- return Num{}, errors.New("bitlen too large for decimal256")
- }
- n = FromBigInt(val)
- if !n.FitsInPrecision(prec) {
- err = fmt.Errorf("value %v doesn't fit in precision %d", n,
prec)
- }
- return
-}
-
-func FromFloat32(v float32, prec, scale int32) (Num, error) {
- debug.Assert(prec > 0 && prec <= 76, "invalid precision for converting
to decimal256")
-
- if math.IsInf(float64(v), 0) {
- return Num{}, fmt.Errorf("cannot convert %f to decimal256", v)
- }
-
- if v < 0 {
- dec, err := fromPositiveFloat32(-v, prec, scale)
- if err != nil {
- return dec, err
- }
- return dec.Negate(), nil
- }
- return fromPositiveFloat32(v, prec, scale)
-}
-
-func FromFloat64(v float64, prec, scale int32) (Num, error) {
- debug.Assert(prec > 0 && prec <= 76, "invalid precision for converting
to decimal256")
-
- if math.IsInf(v, 0) {
- return Num{}, fmt.Errorf("cannot convert %f to decimal256", v)
- }
-
- if v < 0 {
- dec, err := fromPositiveFloat64(-v, prec, scale)
- if err != nil {
- return dec, err
- }
- return dec.Negate(), nil
- }
- return fromPositiveFloat64(v, prec, scale)
-}
-
-// this has to exist despite sharing some code with fromPositiveFloat64
-// 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:
-//
-// FromFloat64(float64(v), 20, 4)
-//
-// vs
-//
-// FromFloat32(v, 20, 4)
-//
-// because float64(v) == 1844674629206016 rather than 1844674600000000
-func fromPositiveFloat32(v float32, prec, scale int32) (Num, error) {
- val, err := scalePositiveFloat64(float64(v), prec, scale)
- if err != nil {
- return Num{}, 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(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 Num{[4]uint64{uint64(arr[0]), uint64(arr[1]), uint64(arr[2]),
uint64(arr[3])}}, nil
-}
-
-func scalePositiveFloat64(v float64, prec, scale int32) (float64, error) {
- var pscale float64
- if scale >= -76 && scale <= 76 {
- pscale = float64PowersOfTen[scale+76]
- } else {
- pscale = math.Pow10(int(scale))
- }
-
- v *= pscale
- v = math.RoundToEven(v)
- maxabs := float64PowersOfTen[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(v float64, prec, scale int32) (Num, error) {
- val, err := scalePositiveFloat64(v, prec, scale)
- if err != nil {
- return Num{}, err
- }
-
- return splitPositiveFloat64(val), nil
-}
-
-// splitPositiveFloat64 converts an already scaled and rounded non-negative
-// float64 into the 64-bit words of a Num.
-func splitPositiveFloat64(val float64) Num {
- 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 Num{[4]uint64{uint64(arr[0]), uint64(arr[1]), uint64(arr[2]),
uint64(arr[3])}}
-}
-
-func (n Num) 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 * float64PowersOfTen[-scale+76]
- }
-
- return x * math.Pow10(-int(scale))
-}
-
-func (n Num) ToFloat32(scale int32) float32 { return
float32(n.ToFloat64(scale)) }
-
-func (n Num) ToFloat64(scale int32) float64 {
- if n.Sign() < 0 {
- return -n.Negate().tofloat64Positive(scale)
- }
- return n.tofloat64Positive(scale)
-}
-
-func (n Num) ToBigFloat(scale int32) *big.Float {
- if scale < -MaxScale || scale > MaxScale {
- panic("arrow/decimal256: scale out of range")
- }
- f := (&big.Float{}).SetInt(n.BigInt())
- if scale < 0 {
- f.SetPrec(256).Mul(f,
(&big.Float{}).SetInt(scaleMultipliers[-scale].BigInt()))
- } else {
- f.SetPrec(256).Quo(f,
(&big.Float{}).SetInt(scaleMultipliers[scale].BigInt()))
- }
- return f
-}
-
-func (n Num) Sign() int {
- if n == (Num{}) {
- return 0
- }
- return int(1 | (int64(n.arr[3]) >> 63))
-}
-
-// FromBigInt converts a big.Int to a Num. It panics if v.BitLen() > 255.
-func FromBigInt(v *big.Int) (n Num) {
- bitlen := v.BitLen()
- if bitlen > 255 {
- panic("arrow/decimal256: cannot represent value larger than
256bits")
- } else if bitlen == 0 {
- return
- }
-
- b := v.Bits()
- if bits.UintSize == 32 {
- // Each 64-bit limb contains two native-sized big.Int words.
- for i, word := range b {
- n.arr[i/2] |= uint64(word) << (32 * (i % 2))
- }
- } else {
- for i, word := range b {
- n.arr[i] = uint64(word)
- }
- }
- if v.Sign() < 0 {
- return n.Negate()
- }
- return
-}
-
-func toBigIntPositive(n Num) *big.Int {
- if bits.UintSize == 32 {
- return new(big.Int).SetBits([]big.Word{
- big.Word(n.arr[0]), big.Word(n.arr[0] >> 32),
- big.Word(n.arr[1]), big.Word(n.arr[1] >> 32),
- big.Word(n.arr[2]), big.Word(n.arr[2] >> 32),
- big.Word(n.arr[3]), big.Word(n.arr[3] >> 32),
- })
- }
- return new(big.Int).SetBits([]big.Word{big.Word(n.arr[0]),
big.Word(n.arr[1]), big.Word(n.arr[2]), big.Word(n.arr[3])})
-}
-
-func (n Num) BigInt() *big.Int {
- if n.Sign() < 0 {
- b := toBigIntPositive(n.Negate())
- return b.Neg(b)
- }
- return toBigIntPositive(n)
-}
-
-// Greater returns true if the value represented by n is > other
-func (n Num) Greater(other Num) bool {
- return other.Less(n)
-}
-
-// GreaterEqual returns true if the value represented by n is >= other
-func (n Num) GreaterEqual(other Num) bool {
- return !n.Less(other)
-}
-
-// Less returns true if the value represented by n is < other
-func (n Num) Less(other Num) bool {
- switch {
- case n.arr[3] != other.arr[3]:
- return int64(n.arr[3]) < int64(other.arr[3])
- case n.arr[2] != other.arr[2]:
- return n.arr[2] < other.arr[2]
- case n.arr[1] != other.arr[1]:
- return n.arr[1] < other.arr[1]
- }
- return n.arr[0] < other.arr[0]
-}
-
-// LessEqual returns true if the value represented by n is <= other
-func (n Num) LessEqual(other Num) bool {
- return !n.Greater(other)
-}
-
-// Max returns the largest Decimal256 that was passed in the arguments
-func Max(first Num, rest ...Num) Num {
- answer := first
- for _, number := range rest {
- if number.Greater(answer) {
- answer = number
- }
- }
- return answer
-}
-
-// Min returns the smallest Decimal256 that was passed in the arguments
-func Min(first Num, rest ...Num) Num {
- answer := first
- for _, number := range rest {
- if number.Less(answer) {
- answer = number
- }
- }
- return answer
-}
-
-// Cmp compares the numbers represented by n and other and returns:
-//
-// +1 if n > other
-// 0 if n == other
-// -1 if n < other
-func (n Num) Cmp(other Num) int {
- switch {
- case n.Greater(other):
- return 1
- case n.Less(other):
- return -1
- }
- return 0
-}
-
-func (n Num) IncreaseScaleBy(increase int32) Num {
- debug.Assert(increase >= 0, "invalid amount to increase scale by")
- debug.Assert(increase <= 76, "invalid amount to increase scale by")
-
- v := scaleMultipliers[increase].BigInt()
- return FromBigInt(v.Mul(n.BigInt(), v))
-}
-
-func (n Num) ReduceScaleBy(reduce int32, round bool) Num {
- debug.Assert(reduce >= 0, "invalid amount to reduce scale by")
- debug.Assert(reduce <= 76, "invalid amount to reduce scale by")
-
- if reduce == 0 {
- return n
- }
-
- divisor := scaleMultipliers[reduce].BigInt()
- result, remainder := divisor.QuoRem(n.BigInt(), divisor, new(big.Int))
- if round {
- divisorHalf := scaleMultipliersHalf[reduce]
- if remainder.Abs(remainder).Cmp(divisorHalf.BigInt()) != -1 {
- result.Add(result, big.NewInt(int64(n.Sign())))
- }
- }
- return FromBigInt(result)
-}
-
-func (n Num) rescaleWouldCauseDataLoss(deltaScale int64, multiplier Num) (out
Num, loss bool) {
- if deltaScale < 0 {
- var remainder Num
- out, remainder = n.Div(multiplier)
- return out, remainder != Num{}
- }
-
- result := n.BigInt()
- result.Mul(result, multiplier.BigInt())
- if result.BitLen() > 255 {
- return Num{}, true
- }
- return FromBigInt(result), false
-}
-
-func (n Num) Rescale(original, newscale int32) (out Num, err error) {
- if original == newscale || n == (Num{}) {
- return n, nil
- }
-
- deltaScale := int64(newscale) - int64(original)
- absDeltaScale := deltaScale
- if absDeltaScale < 0 {
- absDeltaScale = -absDeltaScale
- }
- // Scaling any nonzero value by 10^77 or more overflows or drops every
digit.
- if absDeltaScale >= int64(len(scaleMultipliers)) {
- return Num{}, errors.New("rescale data loss")
- }
-
- multiplier := scaleMultipliers[absDeltaScale]
- var wouldHaveLoss bool
- out, wouldHaveLoss = n.rescaleWouldCauseDataLoss(deltaScale, multiplier)
- if wouldHaveLoss {
- err = errors.New("rescale data loss")
- }
- return
-}
-
-func (n Num) Abs() Num {
- switch n.Sign() {
- case -1:
- return n.Negate()
- }
- return n
-}
-
-func (n Num) FitsInPrecision(prec int32) bool {
- debug.Assert(prec > 0, "precision must be > 0")
- debug.Assert(prec <= 76, "precision must be <= 76")
- return n.Abs().Less(scaleMultipliers[prec])
-}
-
-func (n Num) ToString(scale int32) string {
- if scale < -MaxScale || scale > MaxScale {
- panic("arrow/decimal256: scale out of range")
- }
-
- value := n.BigInt().String()
- digits := len(value)
- if value[0] == '-' {
- digits--
- }
- if digits > MaxPrecision {
- return n.ToBigFloat(scale).Text('f', int(scale))
- }
- if scale <= 0 {
- if scale < 0 && value != "0" {
- value += strings.Repeat("0", int(-scale))
- }
- return value
- }
-
- negative := value[0] == '-'
- if negative {
- value = value[1:]
- }
- places := int(scale)
- var result string
- if len(value) <= places {
- result = "0." + strings.Repeat("0", places-len(value)) + value
- } else {
- at := len(value) - places
- result = value[:at] + "." + value[at:]
- }
- if negative {
- return "-" + result
- }
- return result
-}
-
-func GetScaleMultiplier(pow int) Num { return scaleMultipliers[pow] }
-
-func GetHalfScaleMultiplier(pow int) Num { return scaleMultipliersHalf[pow] }
-
-var (
- scaleMultipliers = [...]Num{
- FromU64(1),
- FromU64(10),
- FromU64(100),
- FromU64(1000),
- FromU64(10000),
- FromU64(100000),
- FromU64(1000000),
- FromU64(10000000),
- FromU64(100000000),
- FromU64(1000000000),
- FromU64(10000000000),
- FromU64(100000000000),
- FromU64(1000000000000),
- FromU64(10000000000000),
- FromU64(100000000000000),
- FromU64(1000000000000000),
- FromU64(10000000000000000),
- FromU64(100000000000000000),
- FromU64(1000000000000000000),
- New(0, 0, 0, 10000000000000000000),
- New(0, 0, 5, 7766279631452241920),
- New(0, 0, 54, 3875820019684212736),
- New(0, 0, 542, 1864712049423024128),
- New(0, 0, 5421, 200376420520689664),
- New(0, 0, 54210, 2003764205206896640),
- New(0, 0, 542101, 1590897978359414784),
- New(0, 0, 5421010, 15908979783594147840),
- New(0, 0, 54210108, 11515845246265065472),
- New(0, 0, 542101086, 4477988020393345024),
- New(0, 0, 5421010862, 7886392056514347008),
- New(0, 0, 54210108624, 5076944270305263616),
- New(0, 0, 542101086242, 13875954555633532928),
- New(0, 0, 5421010862427, 9632337040368467968),
- New(0, 0, 54210108624275, 4089650035136921600),
- New(0, 0, 542101086242752, 4003012203950112768),
- New(0, 0, 5421010862427522, 3136633892082024448),
- New(0, 0, 54210108624275221, 12919594847110692864),
- New(0, 0, 542101086242752217, 68739955140067328),
- New(0, 0, 5421010862427522170, 687399551400673280),
- New(0, 2, 17316620476856118468, 6873995514006732800),
- New(0, 29, 7145508105175220139, 13399722918938673152),
- New(0, 293, 16114848830623546549, 4870020673419870208),
- New(0, 2938, 13574535716559052564, 11806718586779598848),
- New(0, 29387, 6618148649623664334, 7386721425538678784),
- New(0, 293873, 10841254275107988496, 80237960548581376),
- New(0, 2938735, 16178822382532126880, 802379605485813760),
- New(0, 29387358, 14214271235644855872, 8023796054858137600),
- New(0, 293873587, 13015503840481697412, 6450984253743169536),
- New(0, 2938735877, 1027829888850112811, 9169610316303040512),
- New(0, 29387358770, 10278298888501128114, 17909126868192198656),
- New(0, 293873587705, 10549268516463523069,
13070572018536022016),
- New(0, 2938735877055, 13258964796087472617,
1578511669393358848),
- New(0, 29387358770557, 3462439444907864858,
15785116693933588480),
- New(0, 293873587705571, 16177650375369096972,
10277214349659471872),
- New(0, 2938735877055718, 14202551164014556797,
10538423128046960640),
- New(0, 29387358770557187, 12898303124178706663,
13150510911921848320),
- New(0, 293873587705571876, 18302566799529756941,
2377900603251621888),
- New(0, 2938735877055718769, 17004971331911604867,
5332261958806667264),
- New(1, 10940614696847636083, 4029016655730084128,
16429131440647569408),
- New(15, 17172426599928602752, 3396678409881738056,
16717361816799281152),
- New(159, 5703569335900062977, 15520040025107828953,
1152921504606846976),
- New(1593, 1695461137871974930, 7626447661401876602,
11529215046068469760),
- New(15930, 16954611378719749304, 2477500319180559562,
4611686018427387904),
- New(159309, 3525417123811528497, 6328259118096044006,
9223372036854775808),
- New(1593091, 16807427164405733357, 7942358959831785217, 0),
- New(15930919, 2053574980671369030, 5636613303479645706, 0),
- New(159309191, 2089005733004138687, 1025900813667802212, 0),
- New(1593091911, 2443313256331835254, 10259008136678022120, 0),
- New(15930919111, 5986388489608800929, 10356360998232463120, 0),
- New(159309191113, 4523652674959354447, 11329889613776873120, 0),
- New(1593091911132, 8343038602174441244, 2618431695511421504, 0),
- New(15930919111324, 9643409726906205977, 7737572881404663424,
0),
- New(159309191113245, 4200376900514301694, 3588752519208427776,
0),
- New(1593091911132452, 5110280857723913709,
17440781118374726144, 0),
- New(15930919111324522, 14209320429820033867,
8387114520361296896, 0),
- New(159309191113245227, 12965995782233477362,
10084168908774762496, 0),
- New(1593091911132452277, 532749306367912313,
8607968719199866880, 0),
- }
-
- scaleMultipliersHalf = [...]Num{
- FromU64(0),
- FromU64(5),
- FromU64(50),
- FromU64(500),
- FromU64(5000),
- FromU64(50000),
- FromU64(500000),
- FromU64(5000000),
- FromU64(50000000),
- FromU64(500000000),
- FromU64(5000000000),
- FromU64(50000000000),
- FromU64(500000000000),
- FromU64(5000000000000),
- FromU64(50000000000000),
- FromU64(500000000000000),
- FromU64(5000000000000000),
- FromU64(50000000000000000),
- FromU64(500000000000000000),
- FromU64(5000000000000000000),
- New(0, 0, 2, 13106511852580896768),
- New(0, 0, 27, 1937910009842106368),
- New(0, 0, 271, 932356024711512064),
- New(0, 0, 2710, 9323560247115120640),
- New(0, 0, 27105, 1001882102603448320),
- New(0, 0, 271050, 10018821026034483200),
- New(0, 0, 2710505, 7954489891797073920),
- New(0, 0, 27105054, 5757922623132532736),
- New(0, 0, 271050543, 2238994010196672512),
- New(0, 0, 2710505431, 3943196028257173504),
- New(0, 0, 27105054312, 2538472135152631808),
- New(0, 0, 271050543121, 6937977277816766464),
- New(0, 0, 2710505431213, 14039540557039009792),
- New(0, 0, 27105054312137, 11268197054423236608),
- New(0, 0, 271050543121376, 2001506101975056384),
- New(0, 0, 2710505431213761, 1568316946041012224),
- New(0, 0, 27105054312137610, 15683169460410122240),
- New(0, 0, 271050543121376108, 9257742014424809472),
- New(0, 0, 2710505431213761085, 343699775700336640),
- New(0, 1, 8658310238428059234, 3436997757003366400),
- New(0, 14, 12796126089442385877, 15923233496324112384),
- New(0, 146, 17280796452166549082, 11658382373564710912),
- New(0, 1469, 6787267858279526282, 5903359293389799424),
- New(0, 14693, 12532446361666607975, 3693360712769339392),
- New(0, 146936, 14643999174408770056, 40118980274290688),
- New(0, 1469367, 17312783228120839248, 401189802742906880),
- New(0, 14693679, 7107135617822427936, 4011898027429068800),
- New(0, 146936793, 15731123957095624514, 3225492126871584768),
- New(0, 1469367938, 9737286981279832213, 13808177195006296064),
- New(0, 14693679385, 5139149444250564057, 8954563434096099328),
- New(0, 146936793852, 14498006295086537342,
15758658046122786816),
- New(0, 1469367938527, 15852854434898512116,
10012627871551455232),
- New(0, 14693679385278, 10954591759308708237,
7892558346966794240),
- New(0, 146936793852785, 17312197224539324294,
5138607174829735936),
- New(0, 1469367938527859, 7101275582007278398,
14492583600878256128),
- New(0, 14693679385278593, 15672523598944129139,
15798627492815699968),
- New(0, 146936793852785938, 9151283399764878470,
10412322338480586752),
- New(0, 1469367938527859384, 17725857702810578241,
11889503016258109440),
- New(0, 14693679385278593849, 11237880364719817872,
8214565720323784704),
- New(7, 17809585336819077184, 1698339204940869028,
8358680908399640576),
- New(79, 12075156704804807296, 16983392049408690284,
9799832789158199296),
- New(796, 10071102605790763273, 3813223830700938301,
5764607523034234880),
- New(7965, 8477305689359874652, 1238750159590279781,
2305843009213693952),
- New(79654, 10986080598760540056, 12387501595902797811,
4611686018427387904),
- New(796545, 17627085619057642486, 13194551516770668416,
9223372036854775808),
- New(7965459, 10250159527190460323, 2818306651739822853, 0),
- New(79654595, 10267874903356845151, 9736322443688676914, 0),
- New(796545955, 10445028665020693435, 5129504068339011060, 0),
- New(7965459555, 12216566281659176272, 14401552535971007368, 0),
- New(79654595556, 11485198374334453031, 14888316843743212368, 0),
- New(796545955566, 4171519301087220622, 1309215847755710752, 0),
- New(7965459555662, 4821704863453102988, 13092158477557107520,
0),
- New(79654595556622, 11323560487111926655, 1794376259604213888,
0),
- New(796545955566226, 2555140428861956854, 17943762596042138880,
0),
- New(7965459555662261, 7104660214910016933,
13416929297035424256, 0),
- New(79654595556622613, 15706369927971514489,
5042084454387381248, 0),
- New(796545955566226138, 9489746690038731964,
13527356396454709248, 0),
- }
-
- float64PowersOfTen = [...]float64{
- 1e-76, 1e-75, 1e-74, 1e-73, 1e-72, 1e-71, 1e-70, 1e-69, 1e-68,
1e-67, 1e-66, 1e-65,
- 1e-64, 1e-63, 1e-62, 1e-61, 1e-60, 1e-59, 1e-58, 1e-57, 1e-56,
1e-55, 1e-54, 1e-53,
- 1e-52, 1e-51, 1e-50, 1e-49, 1e-48, 1e-47, 1e-46, 1e-45, 1e-44,
1e-43, 1e-42, 1e-41,
- 1e-40, 1e-39, 1e-38, 1e-37, 1e-36, 1e-35, 1e-34, 1e-33, 1e-32,
1e-31, 1e-30, 1e-29,
- 1e-28, 1e-27, 1e-26, 1e-25, 1e-24, 1e-23, 1e-22, 1e-21, 1e-20,
1e-19, 1e-18, 1e-17,
- 1e-16, 1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-9, 1e-8,
1e-7, 1e-6, 1e-5,
- 1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
- 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18,
1e19,
- 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
1e30, 1e31,
- 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39, 1e40, 1e41,
1e42, 1e43,
- 1e44, 1e45, 1e46, 1e47, 1e48, 1e49, 1e50, 1e51, 1e52, 1e53,
1e54, 1e55,
- 1e56, 1e57, 1e58, 1e59, 1e60, 1e61, 1e62, 1e63, 1e64, 1e65,
1e66, 1e67,
- 1e68, 1e69, 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76,
- }
-)
diff --git a/arrow/internal/arrdata/arrdata.go
b/arrow/internal/arrdata/arrdata.go
index b4f0e626..642ffc63 100644
--- a/arrow/internal/arrdata/arrdata.go
+++ b/arrow/internal/arrdata/arrdata.go
@@ -23,8 +23,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"github.com/apache/arrow-go/v18/arrow/ipc"
"github.com/apache/arrow-go/v18/arrow/memory"
@@ -805,10 +805,10 @@ func makeDecimal256sRecords() []arrow.RecordBatch {
}, nil,
)
- dec256s := func(vs []uint64) []decimal256.Num {
- o := make([]decimal256.Num, len(vs))
+ dec256s := func(vs []uint64) []decimal.Decimal256 {
+ o := make([]decimal.Decimal256, len(vs))
for i, v := range vs {
- o[i] = decimal256.New(v, v, v, v)
+ o[i] = decimal.NewDecimal256(v, v, v, v)
}
return o
}
@@ -1333,7 +1333,7 @@ func arrayOf(mem memory.Allocator, a interface{}, valids
[]bool) arrow.Array {
aa := bldr.NewDecimal128Array()
return aa
- case []decimal256.Num:
+ case []decimal.Decimal256:
bldr := array.NewDecimal256Builder(mem, decimal256Type)
defer bldr.Release()
diff --git a/arrow/internal/arrjson/arrjson.go
b/arrow/internal/arrjson/arrjson.go
index afd34f50..ca2882c5 100644
--- a/arrow/internal/arrjson/arrjson.go
+++ b/arrow/internal/arrjson/arrjson.go
@@ -31,7 +31,6 @@ import (
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"github.com/apache/arrow-go/v18/arrow/internal/dictutils"
"github.com/apache/arrow-go/v18/arrow/ipc"
@@ -2150,15 +2149,15 @@ func decimal256ToJSON(arr *array.Decimal256)
[]interface{} {
return o
}
-func decimal256FromJSON(vs []interface{}) []decimal256.Num {
+func decimal256FromJSON(vs []interface{}) []decimal.Decimal256 {
var tmp big.Int
- o := make([]decimal256.Num, len(vs))
+ o := make([]decimal.Decimal256, len(vs))
for i, v := range vs {
if err := tmp.UnmarshalJSON([]byte(v.(string))); err != nil {
panic(fmt.Errorf("could not convert %v (%T) to
decimal256: %w", v, v, err))
}
- o[i] = decimal256.FromBigInt(&tmp)
+ o[i] = decimal.Decimal256FromBigInt(&tmp)
}
return o
}
diff --git a/arrow/scalar/append.go b/arrow/scalar/append.go
index 61025dc4..88e1d989 100644
--- a/arrow/scalar/append.go
+++ b/arrow/scalar/append.go
@@ -23,15 +23,15 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"golang.org/x/exp/constraints"
)
type primitives interface {
bool | float16.Num | decimal128.Num |
- decimal256.Num | constraints.Integer | constraints.Float |
+ decimal.Decimal256 | constraints.Integer | constraints.Float |
arrow.DayTimeInterval | arrow.MonthInterval |
arrow.MonthDayNanoInterval
}
diff --git a/arrow/scalar/decimal.go b/arrow/scalar/decimal.go
index 7d478871..876d8a07 100644
--- a/arrow/scalar/decimal.go
+++ b/arrow/scalar/decimal.go
@@ -23,7 +23,6 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/internal/utils"
)
@@ -109,15 +108,15 @@ func (s *decimalScalar[T]) CastTo(to arrow.DataType)
(Scalar, error) {
}
}
-func toDecimal256[T decimal.DecimalTypes](v T) decimal256.Num {
+func toDecimal256[T decimal.DecimalTypes](v T) decimal.Decimal256 {
switch v := any(v).(type) {
case decimal.Decimal32:
- return decimal256.FromI64(int64(v))
+ return decimal.Decimal256FromI64(int64(v))
case decimal.Decimal64:
- return decimal256.FromI64(int64(v))
+ return decimal.Decimal256FromI64(int64(v))
case decimal128.Num:
- return decimal256.FromDecimal128(v)
+ return decimal.Decimal256FromDecimal128(v)
default:
- return v.(decimal256.Num)
+ return v.(decimal.Decimal256)
}
}
diff --git a/arrow/scalar/numeric.gen.go b/arrow/scalar/numeric.gen.go
index cb8513c5..bd33c9f8 100644
--- a/arrow/scalar/numeric.gen.go
+++ b/arrow/scalar/numeric.gen.go
@@ -25,8 +25,8 @@ import (
"unsafe"
"github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
)
type Int8 struct {
@@ -89,7 +89,7 @@ func (s *Int8) CastTo(dt arrow.DataType) (Scalar, error) {
case *arrow.Decimal128Type:
return NewDecimal128Scalar(decimal128.FromI64(int64(s.Value)),
dt), nil
case *arrow.Decimal256Type:
- return NewDecimal256Scalar(decimal256.FromI64(int64(s.Value)),
dt), nil
+ return
NewDecimal256Scalar(decimal.Decimal256FromI64(int64(s.Value)), dt), nil
}
return nil, fmt.Errorf("invalid scalar cast from type int8 to type %s",
dt)
@@ -159,7 +159,7 @@ func (s *Int16) CastTo(dt arrow.DataType) (Scalar, error) {
case *arrow.Decimal128Type:
return NewDecimal128Scalar(decimal128.FromI64(int64(s.Value)),
dt), nil
case *arrow.Decimal256Type:
- return NewDecimal256Scalar(decimal256.FromI64(int64(s.Value)),
dt), nil
+ return
NewDecimal256Scalar(decimal.Decimal256FromI64(int64(s.Value)), dt), nil
}
return nil, fmt.Errorf("invalid scalar cast from type int16 to type
%s", dt)
@@ -229,7 +229,7 @@ func (s *Int32) CastTo(dt arrow.DataType) (Scalar, error) {
case *arrow.Decimal128Type:
return NewDecimal128Scalar(decimal128.FromI64(int64(s.Value)),
dt), nil
case *arrow.Decimal256Type:
- return NewDecimal256Scalar(decimal256.FromI64(int64(s.Value)),
dt), nil
+ return
NewDecimal256Scalar(decimal.Decimal256FromI64(int64(s.Value)), dt), nil
}
return nil, fmt.Errorf("invalid scalar cast from type int32 to type
%s", dt)
@@ -299,7 +299,7 @@ func (s *Int64) CastTo(dt arrow.DataType) (Scalar, error) {
case *arrow.Decimal128Type:
return NewDecimal128Scalar(decimal128.FromI64(int64(s.Value)),
dt), nil
case *arrow.Decimal256Type:
- return NewDecimal256Scalar(decimal256.FromI64(int64(s.Value)),
dt), nil
+ return
NewDecimal256Scalar(decimal.Decimal256FromI64(int64(s.Value)), dt), nil
}
return nil, fmt.Errorf("invalid scalar cast from type int64 to type
%s", dt)
@@ -369,7 +369,7 @@ func (s *Uint8) CastTo(dt arrow.DataType) (Scalar, error) {
case *arrow.Decimal128Type:
return NewDecimal128Scalar(decimal128.FromI64(int64(s.Value)),
dt), nil
case *arrow.Decimal256Type:
- return NewDecimal256Scalar(decimal256.FromI64(int64(s.Value)),
dt), nil
+ return
NewDecimal256Scalar(decimal.Decimal256FromI64(int64(s.Value)), dt), nil
}
return nil, fmt.Errorf("invalid scalar cast from type uint8 to type
%s", dt)
@@ -439,7 +439,7 @@ func (s *Uint16) CastTo(dt arrow.DataType) (Scalar, error) {
case *arrow.Decimal128Type:
return NewDecimal128Scalar(decimal128.FromI64(int64(s.Value)),
dt), nil
case *arrow.Decimal256Type:
- return NewDecimal256Scalar(decimal256.FromI64(int64(s.Value)),
dt), nil
+ return
NewDecimal256Scalar(decimal.Decimal256FromI64(int64(s.Value)), dt), nil
}
return nil, fmt.Errorf("invalid scalar cast from type uint16 to type
%s", dt)
@@ -509,7 +509,7 @@ func (s *Uint32) CastTo(dt arrow.DataType) (Scalar, error) {
case *arrow.Decimal128Type:
return NewDecimal128Scalar(decimal128.FromI64(int64(s.Value)),
dt), nil
case *arrow.Decimal256Type:
- return NewDecimal256Scalar(decimal256.FromI64(int64(s.Value)),
dt), nil
+ return
NewDecimal256Scalar(decimal.Decimal256FromI64(int64(s.Value)), dt), nil
}
return nil, fmt.Errorf("invalid scalar cast from type uint32 to type
%s", dt)
@@ -579,7 +579,7 @@ func (s *Uint64) CastTo(dt arrow.DataType) (Scalar, error) {
case *arrow.Decimal128Type:
return NewDecimal128Scalar(decimal128.FromU64(s.Value), dt), nil
case *arrow.Decimal256Type:
- return NewDecimal256Scalar(decimal256.FromU64(s.Value), dt), nil
+ return NewDecimal256Scalar(decimal.Decimal256FromU64(s.Value),
dt), nil
}
return nil, fmt.Errorf("invalid scalar cast from type uint64 to type
%s", dt)
@@ -664,7 +664,7 @@ func (s *Float32) CastTo(dt arrow.DataType) (Scalar, error)
{
}
return NewDecimal128Scalar(v, dt), nil
case *arrow.Decimal256Type:
- v, err := decimal256.FromFloat32(s.Value, dt.Precision,
dt.Scale)
+ v, err := decimal.Decimal256FromFloat32(s.Value, dt.Precision,
dt.Scale)
if err != nil {
return nil, err
}
@@ -753,7 +753,7 @@ func (s *Float64) CastTo(dt arrow.DataType) (Scalar, error)
{
}
return NewDecimal128Scalar(v, dt), nil
case *arrow.Decimal256Type:
- v, err := decimal256.FromFloat64(s.Value, dt.Precision,
dt.Scale)
+ v, err := decimal.Decimal256FromFloat(s.Value, dt.Precision,
dt.Scale)
if err != nil {
return nil, err
}
diff --git a/arrow/scalar/numeric.gen.go.tmpl b/arrow/scalar/numeric.gen.go.tmpl
index c0733d6e..abc92feb 100644
--- a/arrow/scalar/numeric.gen.go.tmpl
+++ b/arrow/scalar/numeric.gen.go.tmpl
@@ -107,21 +107,21 @@ func (s *{{.Name}}) CastTo(dt arrow.DataType) (Scalar,
error) {
{{end -}}
case *arrow.Decimal256Type:
{{if eq .Name "Float32" -}}
- v, err := decimal256.FromFloat32(s.Value, dt.Precision, dt.Scale)
+ v, err := decimal.Decimal256FromFloat32(s.Value, dt.Precision,
dt.Scale)
if err != nil {
return nil, err
}
return NewDecimal256Scalar(v, dt), nil
{{else if eq .Name "Float64" -}}
- v, err := decimal256.FromFloat64(s.Value, dt.Precision, dt.Scale)
+ v, err := decimal.Decimal256FromFloat(s.Value, dt.Precision, dt.Scale)
if err != nil {
return nil, err
}
return NewDecimal256Scalar(v, dt), nil
{{else if eq .Name "Uint64" -}}
- return NewDecimal256Scalar(decimal256.FromU64(s.Value), dt), nil
+ return NewDecimal256Scalar(decimal.Decimal256FromU64(s.Value), dt), nil
{{else -}}
- return NewDecimal256Scalar(decimal256.FromI64(int64(s.Value)), dt), nil
+ return NewDecimal256Scalar(decimal.Decimal256FromI64(int64(s.Value)),
dt), nil
{{end -}}
}
diff --git a/arrow/scalar/parse.go b/arrow/scalar/parse.go
index cbcb3702..d05a8774 100644
--- a/arrow/scalar/parse.go
+++ b/arrow/scalar/parse.go
@@ -28,7 +28,6 @@ import (
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"github.com/apache/arrow-go/v18/arrow/memory"
)
@@ -594,7 +593,7 @@ func MakeScalarParam(val interface{}, dt arrow.DataType)
(Scalar, error) {
return NewDecimal128Scalar(n, ty), nil
case arrow.DECIMAL256:
ty := dt.(*arrow.Decimal256Type)
- n, err := decimal256.FromString(v,
ty.Precision, ty.Scale)
+ n, err := decimal.Decimal256FromString(v,
ty.Precision, ty.Scale)
if err != nil {
return nil, err
}
@@ -677,7 +676,7 @@ func MakeScalarParam(val interface{}, dt arrow.DataType)
(Scalar, error) {
}
return NewDecimal128Scalar(v, dt), nil
- case decimal256.Num:
+ case decimal.Decimal256:
if _, ok := dt.(*arrow.Decimal256Type); !ok {
return nil, fmt.Errorf("mismatch cannot create
decimal256 scalar with incorrect data type")
}
@@ -940,7 +939,7 @@ func ParseScalar(dt arrow.DataType, val string) (Scalar,
error) {
return NewDecimal128Scalar(n, typ), nil
case arrow.DECIMAL256:
typ := dt.(*arrow.Decimal256Type)
- n, err := decimal256.FromString(val, typ.Precision, typ.Scale)
+ n, err := decimal.Decimal256FromString(val, typ.Precision,
typ.Scale)
if err != nil {
return nil, err
}
diff --git a/arrow/scalar/scalar.go b/arrow/scalar/scalar.go
index 7798d8a1..782cfbf1 100644
--- a/arrow/scalar/scalar.go
+++ b/arrow/scalar/scalar.go
@@ -31,8 +31,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/bitutil"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/encoded"
"github.com/apache/arrow-go/v18/arrow/endian"
"github.com/apache/arrow-go/v18/arrow/float16"
@@ -322,7 +322,7 @@ func (s *Decimal128) CastTo(to arrow.DataType) (Scalar,
error) {
return NewDecimal128Scalar(newVal, to), nil
case arrow.DECIMAL256:
to := to.(*arrow.Decimal256Type)
- newVal, err :=
decimal256.FromDecimal128(s.Value).Rescale(dt.Scale, to.Scale)
+ newVal, err :=
decimal.Decimal256FromDecimal128(s.Value).Rescale(dt.Scale, to.Scale)
if err != nil {
return nil, err
}
@@ -347,7 +347,7 @@ func NewDecimal128Scalar(val decimal128.Num, typ
arrow.DataType) *Decimal128 {
type Decimal256 struct {
scalar
- Value decimal256.Num
+ Value decimal.Decimal256
}
func (s *Decimal256) Data() []byte {
@@ -400,7 +400,7 @@ func (s *Decimal256) CastTo(to arrow.DataType) (Scalar,
error) {
return nil, fmt.Errorf("cannot cast non-nil decimal256 scalar to type
%s", to)
}
-func NewDecimal256Scalar(val decimal256.Num, typ arrow.DataType) *Decimal256 {
+func NewDecimal256Scalar(val decimal.Decimal256, typ arrow.DataType)
*Decimal256 {
return &Decimal256{scalar{typ, true}, val}
}
@@ -916,7 +916,7 @@ func MakeArrayFromScalar(sc Scalar, length int, mem
memory.Allocator) (arrow.Arr
defer data.Release()
return array.MakeFromData(data), nil
case *Decimal256:
- data :=
finishFixedWidth(arrow.Decimal256Traits.CastToBytes([]decimal256.Num{s.Value}))
+ data :=
finishFixedWidth(arrow.Decimal256Traits.CastToBytes([]decimal.Decimal256{s.Value}))
defer data.Release()
return array.MakeFromData(data), nil
case *Dictionary:
@@ -1218,7 +1218,7 @@ func Hash(seed maphash.Seed, s Scalar) uint64 {
binary.Write(&h, endian.Native, v.LowBits())
hash()
binary.Write(&h, endian.Native, uint64(v.HighBits()))
- case decimal256.Num:
+ case decimal.Decimal256:
arr := v.Array()
binary.Write(&h, endian.Native, arr[3])
hash()
diff --git a/internal/utils/decimal_format.go b/internal/utils/decimal_format.go
index b020b536..5ce8e7ff 100644
--- a/internal/utils/decimal_format.go
+++ b/internal/utils/decimal_format.go
@@ -23,8 +23,8 @@ import (
"strings"
"github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
)
// FormatDecimalG formats n, a value of type typ, in 'g' style. It returns an
@@ -39,7 +39,7 @@ func FormatDecimalG[T interface{ ToBigFloat(int32) *big.Float
}](n T, typ arrow.
case arrow.DECIMAL128:
maxScale = decimal128.MaxScale
case arrow.DECIMAL256:
- maxScale = decimal256.MaxScale
+ maxScale = int32(decimal.MaxScale[decimal.Decimal256]())
}
if maxScale > 0 && (scale < -maxScale || scale > maxScale) {
return "", fmt.Errorf("scale %d out of range for decimal%d,
must be between %d and %d", scale, typ.BitWidth(), -maxScale, maxScale)
diff --git a/parquet/pqarrow/column_readers.go
b/parquet/pqarrow/column_readers.go
index c1705b9c..5a9be4ba 100644
--- a/parquet/pqarrow/column_readers.go
+++ b/parquet/pqarrow/column_readers.go
@@ -29,8 +29,8 @@ import (
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/arrow/compute"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/apache/arrow-go/v18/internal/utils"
"github.com/apache/arrow-go/v18/parquet"
@@ -1005,7 +1005,7 @@ func transferDecimalIntegerValues[In
parquetInteger](values []In, dt arrow.Type)
data := make([]byte,
arrow.Decimal256Traits.BytesRequired(len(values)))
out := arrow.Decimal256Traits.CastFromBytes(data)
for i, value := range values {
- out[i] = decimal256.FromI64(int64(value))
+ out[i] = decimal.Decimal256FromI64(int64(value))
}
return data
}
@@ -1096,14 +1096,14 @@ func bigEndianToDecimal128(buf []byte) (decimal128.Num,
error) {
return decimal128.New(hi, uint64(lo)), nil
}
-func bigEndianToDecimal256(buf []byte) (decimal256.Num, error) {
+func bigEndianToDecimal256(buf []byte) (decimal.Decimal256, error) {
const (
minDecimalBytes = 1
maxDecimalBytes = 32
)
if len(buf) < minDecimalBytes || len(buf) > maxDecimalBytes {
- return decimal256.Num{},
+ return decimal.Decimal256{},
fmt.Errorf("%w: length of byte array for
bigEndianToDecimal256 was %d but must be between %d and %d",
arrow.ErrInvalid, len(buf), minDecimalBytes,
maxDecimalBytes)
}
@@ -1139,7 +1139,7 @@ func bigEndianToDecimal256(buf []byte) (decimal256.Num,
error) {
buf = buf[:len(buf)-wordLen]
}
- return decimal256.New(littleEndian[3], littleEndian[2],
littleEndian[1], littleEndian[0]), nil
+ return decimal.NewDecimal256(littleEndian[3], littleEndian[2],
littleEndian[1], littleEndian[0]), nil
}
type varOrFixedBin interface {
diff --git a/parquet/pqarrow/encode_arrow.go b/parquet/pqarrow/encode_arrow.go
index cfa7b145..4d90a1c0 100644
--- a/parquet/pqarrow/encode_arrow.go
+++ b/parquet/pqarrow/encode_arrow.go
@@ -29,8 +29,8 @@ import (
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/bitutil"
+ "github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/decimal128"
- "github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/apache/arrow-go/v18/internal/utils"
"github.com/apache/arrow-go/v18/parquet"
@@ -88,7 +88,7 @@ func decimal128FitsInt64(val decimal128.Num) bool {
}
}
-func decimal256FitsInt32(val decimal256.Num) bool {
+func decimal256FitsInt32(val decimal.Decimal256) bool {
words := val.Array()
switch {
case words[1] == 0 && words[2] == 0 && words[3] == 0:
@@ -100,7 +100,7 @@ func decimal256FitsInt32(val decimal256.Num) bool {
}
}
-func decimal256FitsInt64(val decimal256.Num) bool {
+func decimal256FitsInt64(val decimal.Decimal256) bool {
words := val.Array()
switch {
case words[1] == 0 && words[2] == 0 && words[3] == 0:
@@ -131,8 +131,8 @@ func decimal128FitsTargetPrecision(val decimal128.Num,
precision int32) bool {
return val.FitsInPrecision(precision)
}
-func decimal256FitsTargetPrecision(val decimal256.Num, precision int32) bool {
- if precision <= 0 || precision > decimal256.MaxPrecision {
+func decimal256FitsTargetPrecision(val decimal.Decimal256, precision int32)
bool {
+ if precision <= 0 || precision >
int32(decimal.MaxPrecision[decimal.Decimal256]()) {
return false
}
// Abs overflows for the minimum two's-complement value.
@@ -796,7 +796,7 @@ func writeDenseArrow(ctx *arrowWriteContext, cw
file.ColumnChunkWriter, leafArr
ctx.dataBuffer.ResizeNoShrink((leafArr.Len() -
leafArr.NullN()) * dt.BitWidth())
scratch := ctx.dataBuffer.Bytes()
typeLen := wr.Descr().TypeLength()
- fixDecimalEndianness := func(in decimal256.Num)
parquet.FixedLenByteArray {
+ fixDecimalEndianness := func(in decimal.Decimal256)
parquet.FixedLenByteArray {
out := scratch[offset : offset+typeLen]
vals := in.Array()
binary.BigEndian.PutUint64(scratch, vals[3])