package array
import (
"bytes"
"encoding/base64"
"fmt"
"strings"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/internal/bitutils"
"github.com/apache/arrow-go/v18/internal/json"
)
type FixedSizeBinary struct {
array
valueBytes []byte
bytewidth int32
}
func NewFixedSizeBinaryData (data arrow .ArrayData ) *FixedSizeBinary {
a := &FixedSizeBinary {bytewidth : int32 (data .DataType ().(arrow .FixedWidthDataType ).BitWidth () / 8 )}
a .refCount .Add (1 )
a .setData (data .(*Data ))
return a
}
func (a *FixedSizeBinary ) Value (i int ) []byte {
if i < 0 || i >= a .Len () {
panic ("arrow/array: index out of range" )
}
i += a .data .offset
var (
bw = int (a .bytewidth )
beg = i * bw
end = (i + 1 ) * bw
)
return a .valueBytes [beg :end ]
}
func (a *FixedSizeBinary ) ValueStr (i int ) string {
if a .IsNull (i ) {
return NullValueStr
}
return base64 .StdEncoding .EncodeToString (a .Value (i ))
}
func (a *FixedSizeBinary ) String () string {
o := new (strings .Builder )
o .WriteString ("[" )
for i := 0 ; i < a .Len (); i ++ {
if i > 0 {
o .WriteString (" " )
}
switch {
case a .IsNull (i ):
o .WriteString (NullValueStr )
default :
fmt .Fprintf (o , "%q" , a .Value (i ))
}
}
o .WriteString ("]" )
return o .String ()
}
func (a *FixedSizeBinary ) setData (data *Data ) {
a .array .setData (data )
vals := data .buffers [1 ]
if vals != nil {
a .valueBytes = vals .Bytes ()
}
}
func (a *FixedSizeBinary ) GetOneForMarshal (i int ) interface {} {
if a .IsNull (i ) {
return nil
}
return a .Value (i )
}
func (a *FixedSizeBinary ) ValueAsAny (i int ) any {
if a .IsNull (i ) {
return nil
}
return a .Value (i )
}
func (a *FixedSizeBinary ) MarshalJSON () ([]byte , error ) {
vals := make ([]interface {}, a .Len ())
for i := 0 ; i < a .Len (); i ++ {
if a .IsValid (i ) {
vals [i ] = a .Value (i )
} else {
vals [i ] = nil
}
}
return json .Marshal (vals )
}
func arrayEqualFixedSizeBinary(left , right *FixedSizeBinary ) bool {
if left .Len () < 8 {
return arrayEqualFixedSizeBinaryScalar (left , right )
}
width := int (left .bytewidth )
leftStart := left .Offset () * width
leftEnd := leftStart + left .Len ()*width
rightStart := right .Offset () * width
rightEnd := rightStart + right .Len ()*width
leftBitmap := left .NullBitmapBytes ()
if len (leftBitmap ) == 0 ||
(left .NullN () == 0 && bitutil .CountSetBits (leftBitmap , left .Offset (), left .Len ()) == left .Len ()) {
return bytes .Equal (left .valueBytes [leftStart :leftEnd ], right .valueBytes [rightStart :rightEnd ])
}
if useScalarEqualityForValidityRuns (left ) {
return arrayEqualFixedSizeBinaryScalar (left , right )
}
runs := bitutils .NewSetBitRunReader (
leftBitmap , int64 (left .Offset ()), int64 (left .Len ()),
)
for {
run := runs .NextRun ()
if run .Length == 0 {
return true
}
start := int (run .Pos )
end := start + int (run .Length )
leftRunStart := (left .Offset () + start ) * width
leftRunEnd := (left .Offset () + end ) * width
rightRunStart := (right .Offset () + start ) * width
rightRunEnd := (right .Offset () + end ) * width
if !bytes .Equal (
left .valueBytes [leftRunStart :leftRunEnd ],
right .valueBytes [rightRunStart :rightRunEnd ],
) {
return false
}
}
}
func arrayEqualFixedSizeBinaryScalar(left , right *FixedSizeBinary ) bool {
for i := 0 ; i < left .Len (); i ++ {
if left .IsNull (i ) {
continue
}
if !bytes .Equal (left .Value (i ), right .Value (i )) {
return false
}
}
return true
}
var _ arrow .Array = (*FixedSizeBinary )(nil )
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 .