package compute
import (
"context"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
"github.com/apache/arrow-go/v18/arrow/compute/internal/kernels"
)
type (
ScalarAggregateOptions = kernels .ScalarAggregateOptions
CountOptions = kernels .CountOptions
CountMode = kernels .CountMode
)
const (
CountOnlyValid = kernels .CountOnlyValid
CountOnlyNull = kernels .CountOnlyNull
CountAllRows = kernels .CountAllRows
)
func DefaultScalarAggregateOptions () *ScalarAggregateOptions {
return kernels .DefaultScalarAggregateOptions ()
}
func DefaultCountOptions () *CountOptions { return kernels .DefaultCountOptions () }
var (
countDoc = FunctionDoc {
Summary : "Count the number of values" ,
Description : "By default, only non-null values are counted.\n" +
"This can be changed through the mode option." ,
ArgNames : []string {"array" },
OptionsType : "CountOptions" ,
}
sumDoc = FunctionDoc {
Summary : "Compute the sum of a numeric array" ,
Description : "Null values are ignored by default. If the skip_nulls\n" +
"option is set to false, then a null is emitted as soon as one of\n" +
"the values is null. A null is also emitted when fewer than\n" +
"min_count non-null values are seen, which by default means that\n" +
"an empty or an all-null input produces a null." ,
ArgNames : []string {"array" },
OptionsType : "ScalarAggregateOptions" ,
}
)
var sumKernelTypes = []struct {
in arrow .DataType
out arrow .DataType
}{
{arrow .PrimitiveTypes .Int8 , arrow .PrimitiveTypes .Int64 },
{arrow .PrimitiveTypes .Int16 , arrow .PrimitiveTypes .Int64 },
{arrow .PrimitiveTypes .Int32 , arrow .PrimitiveTypes .Int64 },
{arrow .PrimitiveTypes .Int64 , arrow .PrimitiveTypes .Int64 },
{arrow .PrimitiveTypes .Uint8 , arrow .PrimitiveTypes .Uint64 },
{arrow .PrimitiveTypes .Uint16 , arrow .PrimitiveTypes .Uint64 },
{arrow .PrimitiveTypes .Uint32 , arrow .PrimitiveTypes .Uint64 },
{arrow .PrimitiveTypes .Uint64 , arrow .PrimitiveTypes .Uint64 },
{arrow .PrimitiveTypes .Float32 , arrow .PrimitiveTypes .Float64 },
{arrow .PrimitiveTypes .Float64 , arrow .PrimitiveTypes .Float64 },
{arrow .FixedWidthTypes .Boolean , arrow .PrimitiveTypes .Uint64 },
{arrow .Null , arrow .PrimitiveTypes .Int64 },
}
func RegisterScalarAggregates (reg FunctionRegistry ) {
countFn := NewScalarAggregateFunction ("count" , Unary (), countDoc )
countFn .SetDefaultOptions (DefaultCountOptions ())
if err := countFn .AddNewKernel ([]exec .InputType {{}}, exec .NewOutputType (arrow .PrimitiveTypes .Int64 ),
kernels .CountInit , kernels .AggregateConsume , kernels .AggregateMerge , kernels .AggregateFinalize ); err != nil {
panic (err )
}
reg .AddFunction (countFn , false )
sumFn := NewScalarAggregateFunction ("sum" , Unary (), sumDoc )
sumFn .SetDefaultOptions (DefaultScalarAggregateOptions ())
for _ , kt := range sumKernelTypes {
if err := sumFn .AddNewKernel ([]exec .InputType {exec .NewExactInput (kt .in )}, exec .NewOutputType (kt .out ),
kernels .SumInit , kernels .AggregateConsume , kernels .AggregateMerge , kernels .AggregateFinalize ); err != nil {
panic (err )
}
}
reg .AddFunction (sumFn , false )
}
func Count (ctx context .Context , opts CountOptions , value Datum ) (Datum , error ) {
return CallFunction (ctx , "count" , &opts , value )
}
func Sum (ctx context .Context , opts ScalarAggregateOptions , value Datum ) (Datum , error ) {
return CallFunction (ctx , "sum" , &opts , value )
}
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 .