package compute
import (
"context"
"fmt"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/compute/internal/kernels"
)
var (
uniqueDoc = FunctionDoc {
Summary : "Compute unique elements" ,
Description : "Return an array with distinct values.\n" +
"Nulls in the input are considered a distinct value" ,
ArgNames : []string {"array" },
}
dictionaryEncodeDoc = FunctionDoc {
Summary : "Dictionary encode an array" ,
Description : "Return a dictionary-encoded array with the distinct values in the dictionary.\n" +
"If the input is already dictionary encoded, it is returned unchanged,\n" +
"including its index type and null representation.\n" +
"Newly encoded arrays use int32 dictionary indices." ,
ArgNames : []string {"array" },
OptionsType : "DictionaryEncodeOptions" ,
}
)
type NullEncodingBehavior = kernels .NullEncodingBehavior
const (
NullEncodingMask = kernels .NullEncodingMask
NullEncodingEncode = kernels .NullEncodingEncode
)
type DictionaryEncodeOptions = kernels .DictionaryEncodeOptions
func Unique (ctx context .Context , values Datum ) (Datum , error ) {
return CallFunction (ctx , "unique" , nil , values )
}
func UniqueArray (ctx context .Context , values arrow .Array ) (arrow .Array , error ) {
out , err := Unique (ctx , &ArrayDatum {Value : values .Data ()})
if err != nil {
return nil , err
}
defer out .Release ()
return out .(*ArrayDatum ).MakeArray (), nil
}
func DictionaryEncode (ctx context .Context , opts DictionaryEncodeOptions , values Datum ) (Datum , error ) {
return CallFunction (ctx , "dictionary_encode" , &opts , values )
}
func DictionaryEncodeArray (ctx context .Context , opts DictionaryEncodeOptions , values arrow .Array ) (arrow .Array , error ) {
datum , err := DictionaryEncode (ctx , opts , &ArrayDatum {Value : values .Data ()})
if err != nil {
return nil , err
}
defer datum .Release ()
switch out := datum .(type ) {
case *ArrayDatum :
return out .MakeArray (), nil
case *ChunkedDatum :
return array .Concatenate (out .Chunks (), GetAllocator (ctx ))
default :
return nil , fmt .Errorf (
"%w: dictionary_encode returned unexpected datum kind %s" ,
arrow .ErrInvalid ,
datum .Kind (),
)
}
}
func RegisterVectorHash (reg FunctionRegistry ) {
unique , _ , dictEncode := kernels .GetVectorHashKernels ()
uniqFn := NewVectorFunction ("unique" , Unary (), uniqueDoc )
for _ , vd := range unique {
if err := uniqFn .AddKernel (vd ); err != nil {
panic (err )
}
}
reg .AddFunction (uniqFn , false )
dictFn := NewVectorFunction ("dictionary_encode" , Unary (), dictionaryEncodeDoc )
dictFn .SetDefaultOptions (&DictionaryEncodeOptions {})
for _ , vd := range dictEncode {
if err := dictFn .AddKernel (vd ); err != nil {
panic (err )
}
}
reg .AddFunction (dictFn , false )
}
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 .