package kernels
import (
"fmt"
"math"
"unsafe"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
)
func validateUtf8View(input *exec .ArraySpan ) error {
views := arrow .ViewHeaderTraits .CastFromBytes (input .Buffers [1 ].Buf )
dataBuffers := make ([][]byte , 0 , len (input .Buffers )-2 )
for i := 2 ; i < len (input .Buffers ); i ++ {
dataBuffers = append (dataBuffers , input .Buffers [i ].Buf )
}
return validateUTF8Sequence (input .Buffers [0 ].Buf , input .Offset , input .Len ,
func (pos int64 ) []byte {
h := &views [input .Offset +pos ]
if h .IsInline () {
return h .InlineBytes ()
}
off := h .BufferOffset ()
return dataBuffers [h .BufferIndex ()][off : off +int32 (h .Len ())]
})
}
func unsafeStringBytes(s string ) []byte {
if len (s ) == 0 {
return nil
}
return unsafe .Slice (unsafe .StringData (s ), len (s ))
}
type binaryAppender struct {
bldr array .Builder
appendBytes func ([]byte )
reserveData func (int )
}
func newBinaryAppender(bldr array .Builder ) (binaryAppender , error ) {
switch b := bldr .(type ) {
case *array .BinaryBuilder :
return binaryAppender {bldr : b , appendBytes : b .Append , reserveData : b .ReserveData }, nil
case *array .StringBuilder :
return binaryAppender {bldr : b , appendBytes : b .BinaryBuilder .Append , reserveData : b .ReserveData }, nil
case *array .LargeStringBuilder :
return binaryAppender {bldr : b , appendBytes : b .BinaryBuilder .Append , reserveData : b .ReserveData }, nil
case *array .BinaryViewBuilder :
return binaryAppender {bldr : b , appendBytes : b .Append , reserveData : b .ReserveData }, nil
case *array .StringViewBuilder :
return binaryAppender {bldr : b , appendBytes : b .BinaryViewBuilder .Append , reserveData : b .ReserveData }, nil
default :
return binaryAppender {}, fmt .Errorf ("%w: unsupported builder type %T for binary-like output" ,
arrow .ErrNotImplemented , bldr )
}
}
func binaryLikeValueAccessor(arr arrow .Array ) (func (int ) []byte , error ) {
switch a := arr .(type ) {
case *array .Binary :
return a .Value , nil
case *array .LargeBinary :
return a .Value , nil
case *array .String :
return func (i int ) []byte { return unsafeStringBytes (a .Value (i )) }, nil
case *array .LargeString :
return func (i int ) []byte { return unsafeStringBytes (a .Value (i )) }, nil
case *array .FixedSizeBinary :
return a .Value , nil
case *array .BinaryView :
return a .Value , nil
case *array .StringView :
return func (i int ) []byte { return unsafeStringBytes (a .Value (i )) }, nil
default :
return nil , fmt .Errorf ("%w: unsupported binary-like type: %s" ,
arrow .ErrNotImplemented , arr .DataType ())
}
}
func appendBinaryValues(arr arrow .Array , getVal func (int ) []byte , ba binaryAppender ) {
for i := 0 ; i < arr .Len (); i ++ {
if arr .IsNull (i ) {
ba .bldr .AppendNull ()
continue
}
ba .appendBytes (getVal (i ))
}
}
func SumOutOfLineBytes (n int , isNull func (int ) bool , valueLen func (int ) int ) (int64 , error ) {
var total int64
for i := 0 ; i < n ; i ++ {
if isNull (i ) {
continue
}
vlen := valueLen (i )
if !arrow .IsViewInline (vlen ) {
total += int64 (vlen )
if total > math .MaxInt32 {
return 0 , fmt .Errorf ("%w: view out-of-line payload (%d bytes) exceeds single-buffer limit (%d bytes)" ,
arrow .ErrInvalid , total , math .MaxInt32 )
}
}
}
return total , nil
}
func CastBinaryToBinaryView (ctx *exec .KernelCtx , batch *exec .ExecSpan , out *exec .ExecResult ) error {
opts := ctx .State .(CastState )
input := &batch .Values [0 ].Array
outputType := out .Type .(arrow .BinaryDataType )
if shouldValidateUTF8 (input .Type , outputType , opts .AllowInvalidUtf8 ) {
switch input .Type .ID () {
case arrow .BINARY :
if err := validateUtf8 [int32 ](input ); err != nil {
return err
}
case arrow .LARGE_BINARY :
if err := validateUtf8 [int64 ](input ); err != nil {
return err
}
case arrow .FIXED_SIZE_BINARY :
if err := validateUtf8Fsb (input ); err != nil {
return err
}
}
}
rawBldr := array .NewBuilder (exec .GetAllocator (ctx .Ctx ), out .Type )
defer rawBldr .Release ()
rawBldr .Reserve (int (input .Len ))
ba , err := newBinaryAppender (rawBldr )
if err != nil {
return err
}
arr := input .MakeArray ()
defer arr .Release ()
getVal , err := binaryLikeValueAccessor (arr )
if err != nil {
return fmt .Errorf ("%w: unsupported input type for cast to %s: %s" ,
arrow .ErrNotImplemented , out .Type , input .Type )
}
outOfLineTotal , err := SumOutOfLineBytes (arr .Len (), arr .IsNull ,
func (i int ) int { return len (getVal (i )) })
if err != nil {
return fmt .Errorf ("cast from %s to %s: %w" , input .Type , out .Type , err )
}
if outOfLineTotal > 0 {
ba .reserveData (int (outOfLineTotal ))
}
appendBinaryValues (arr , getVal , ba )
result := ba .bldr .NewArray ()
defer result .Release ()
out .TakeOwnership (result .Data ())
return nil
}
func CastBinaryViewToBinary [OutOffsetT int32 | int64 ](ctx *exec .KernelCtx , batch *exec .ExecSpan , out *exec .ExecResult ) error {
opts := ctx .State .(CastState )
input := &batch .Values [0 ].Array
inputType := input .Type .(arrow .BinaryDataType )
outputType := out .Type .(arrow .BinaryDataType )
if shouldValidateUTF8 (inputType , outputType , opts .AllowInvalidUtf8 ) {
if err := validateUtf8View (input ); err != nil {
return err
}
}
rawBldr := array .NewBuilder (exec .GetAllocator (ctx .Ctx ), out .Type )
defer rawBldr .Release ()
rawBldr .Reserve (int (input .Len ))
ba , err := newBinaryAppender (rawBldr )
if err != nil {
return err
}
arr := input .MakeArray ()
defer arr .Release ()
getVal , err := binaryLikeValueAccessor (arr )
if err != nil {
return fmt .Errorf ("%w: unsupported input type for view-to-binary cast: %s" ,
arrow .ErrNotImplemented , input .Type )
}
var totalBytes int64
for i := 0 ; i < arr .Len (); i ++ {
if !arr .IsNull (i ) {
totalBytes += int64 (len (getVal (i )))
}
}
if totalBytes > int64 (MaxOf [OutOffsetT ]()) {
return fmt .Errorf ("%w: failed casting from %s to %s: input array too large" ,
arrow .ErrInvalid , input .Type , out .Type )
}
maxInt := int64 (^uint (0 ) >> 1 )
if totalBytes > maxInt {
return fmt .Errorf ("%w: failed casting from %s to %s: output data buffer too large for this platform" ,
arrow .ErrInvalid , input .Type , out .Type )
}
if totalBytes > 0 {
ba .reserveData (int (totalBytes ))
}
appendBinaryValues (arr , getVal , ba )
result := ba .bldr .NewArray ()
defer result .Release ()
out .TakeOwnership (result .Data ())
return nil
}
func CastBinaryViewToBinaryView (ctx *exec .KernelCtx , batch *exec .ExecSpan , out *exec .ExecResult ) error {
opts := ctx .State .(CastState )
input := &batch .Values [0 ].Array
inputType := input .Type .(arrow .BinaryDataType )
outputType := out .Type .(arrow .BinaryDataType )
if shouldValidateUTF8 (inputType , outputType , opts .AllowInvalidUtf8 ) {
if err := validateUtf8View (input ); err != nil {
return err
}
}
return ZeroCopyCastExec (ctx , batch , out )
}
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 .