package scalar
import (
"fmt"
"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"
"github.com/apache/arrow-go/v18/internal/utils"
)
type decimalScalar[T interface {
decimal .DecimalTypes
decimal .Num [T ]
}] struct {
scalar
Value T
}
type (
Decimal32 = decimalScalar [decimal .Decimal32 ]
Decimal64 = decimalScalar [decimal .Decimal64 ]
)
func NewDecimal32Scalar (val decimal .Decimal32 , typ arrow .DataType ) *Decimal32 {
return &Decimal32 {scalar {typ , true }, val }
}
func NewDecimal64Scalar (val decimal .Decimal64 , typ arrow .DataType ) *Decimal64 {
return &Decimal64 {scalar {typ , true }, val }
}
func (s *decimalScalar [T ]) Data () []byte {
return unsafe .Slice ((*byte )(unsafe .Pointer (&s .Value )), unsafe .Sizeof (s .Value ))
}
func (s *decimalScalar [T ]) value () interface {} { return s .Value }
func (s *decimalScalar [T ]) equals (rhs Scalar ) bool {
return s .Value == rhs .(*decimalScalar [T ]).Value
}
func (s *decimalScalar [T ]) String () string {
if !s .Valid {
return "null"
}
val , err := s .CastTo (arrow .BinaryTypes .String )
if err != nil {
return "..."
}
return string (val .(*String ).Value .Bytes ())
}
func (s *decimalScalar [T ]) CastTo (to arrow .DataType ) (Scalar , error ) {
if !s .Valid {
return MakeNullScalar (to ), nil
}
dt := s .Type .(arrow .DecimalType )
if to .ID () == arrow .STRING {
str , err := utils .FormatDecimalG (s .Value , dt )
if err != nil {
return nil , err
}
return NewStringScalar (str ), nil
}
toDec , ok := to .(arrow .DecimalType )
if !ok || toDec .BitWidth () < dt .BitWidth () {
return nil , fmt .Errorf ("cannot cast non-nil %s scalar to type %s" , s .Type , to )
}
newVal , err := toDecimal256 (s .Value ).Rescale (dt .GetScale (), toDec .GetScale ())
if err != nil {
return nil , err
}
if !newVal .FitsInPrecision (toDec .GetPrecision ()) {
return nil , fmt .Errorf ("%s value %v will not fit in new precision %d" , to , newVal , toDec .GetPrecision ())
}
switch to .ID () {
case arrow .DECIMAL32 :
return NewDecimal32Scalar (decimal .Decimal32 (newVal .LowBits ()), to ), nil
case arrow .DECIMAL64 :
return NewDecimal64Scalar (decimal .Decimal64 (newVal .LowBits ()), to ), nil
case arrow .DECIMAL128 :
return NewDecimal128Scalar (decimal128 .New (int64 (newVal .Array ()[1 ]), newVal .LowBits ()), to ), nil
default :
return NewDecimal256Scalar (newVal , to ), nil
}
}
func toDecimal256[T decimal .DecimalTypes ](v T ) decimal256 .Num {
switch v := any (v ).(type ) {
case decimal .Decimal32 :
return decimal256 .FromI64 (int64 (v ))
case decimal .Decimal64 :
return decimal256 .FromI64 (int64 (v ))
case decimal128 .Num :
return decimal256 .FromDecimal128 (v )
default :
return v .(decimal256 .Num )
}
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .