package arrow
import (
"reflect"
"unsafe"
"github.com/apache/arrow-go/v18/arrow/decimal"
"github.com/apache/arrow-go/v18/arrow/float16"
"golang.org/x/exp/constraints"
)
type IntType interface {
~int8 | ~int16 | ~int32 | ~int64
}
type UintType interface {
~uint8 | ~uint16 | ~uint32 | ~uint64
}
type FloatType interface {
float16 .Num | constraints .Float
}
type NumericType interface {
IntType | UintType | constraints .Float
}
type FixedWidthType interface {
IntType | UintType |
FloatType | decimal .DecimalTypes |
DayTimeInterval | MonthDayNanoInterval
}
type TemporalType interface {
Date32 | Date64 | Time32 | Time64 |
Timestamp | Duration | DayTimeInterval |
MonthInterval | MonthDayNanoInterval
}
func reinterpretSlice[Out , T any ](b []T ) []Out {
if cap (b ) == 0 {
return nil
}
out := (*Out )(unsafe .Pointer (&b [:1 ][0 ]))
lenBytes := len (b ) * int (unsafe .Sizeof (b [0 ]))
capBytes := cap (b ) * int (unsafe .Sizeof (b [0 ]))
lenOut := lenBytes / int (unsafe .Sizeof (*out ))
capOut := capBytes / int (unsafe .Sizeof (*out ))
return unsafe .Slice (out , capOut )[:lenOut ]
}
func GetValues [T FixedWidthType ](data ArrayData , i int ) []T {
if data .Buffers ()[i ] == nil || data .Buffers ()[i ].Len () == 0 {
return nil
}
return reinterpretSlice [T ](data .Buffers ()[i ].Bytes ())[data .Offset () : data .Offset ()+data .Len ()]
}
func GetOffsets [T int32 | int64 ](data ArrayData , i int ) []T {
return reinterpretSlice [T ](data .Buffers ()[i ].Bytes ())[data .Offset () : data .Offset ()+data .Len ()+1 ]
}
func GetBytes [T FixedWidthType | ViewHeader ](in []T ) []byte {
return reinterpretSlice [byte ](in )
}
func GetData [T FixedWidthType | ViewHeader ](in []byte ) []T {
return reinterpretSlice [T ](in )
}
var typMap = map [reflect .Type ]DataType {
reflect .TypeOf (false ): FixedWidthTypes .Boolean ,
reflect .TypeOf (int8 (0 )): PrimitiveTypes .Int8 ,
reflect .TypeOf (int16 (0 )): PrimitiveTypes .Int16 ,
reflect .TypeOf (int32 (0 )): PrimitiveTypes .Int32 ,
reflect .TypeOf (int64 (0 )): PrimitiveTypes .Int64 ,
reflect .TypeOf (uint8 (0 )): PrimitiveTypes .Uint8 ,
reflect .TypeOf (uint16 (0 )): PrimitiveTypes .Uint16 ,
reflect .TypeOf (uint32 (0 )): PrimitiveTypes .Uint32 ,
reflect .TypeOf (uint64 (0 )): PrimitiveTypes .Uint64 ,
reflect .TypeOf (float32 (0 )): PrimitiveTypes .Float32 ,
reflect .TypeOf (float64 (0 )): PrimitiveTypes .Float64 ,
reflect .TypeOf (string ("" )): BinaryTypes .String ,
reflect .TypeOf (Date32 (0 )): FixedWidthTypes .Date32 ,
reflect .TypeOf (Date64 (0 )): FixedWidthTypes .Date64 ,
reflect .TypeOf (true ): FixedWidthTypes .Boolean ,
reflect .TypeOf (float16 .Num {}): FixedWidthTypes .Float16 ,
reflect .TypeOf ([]byte {}): BinaryTypes .Binary ,
}
func GetDataType [T NumericType | bool | string | []byte | float16 .Num ]() DataType {
var z T
return typMap [reflect .TypeOf (z )]
}
func GetType [T NumericType | bool | string ]() Type {
var z T
return typMap [reflect .TypeOf (z )].ID ()
}
The pages are generated with Golds v0.8.2 . (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 .