package compute
import (
"context"
"fmt"
"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"
"github.com/apache/arrow-go/v18/arrow/scalar"
)
var (
cumulativeSumDoc = FunctionDoc {
Summary : "Compute the cumulative sum of numeric input" ,
Description : `Return the cumulative sum of the input array. Integer values
wrap on overflow; nulls stop the remaining output unless SkipNulls is enabled.
A nil Start uses zero. For chunked input, accumulation continues across all
chunks and the result is returned as a chunked array.` ,
ArgNames : []string {"values" },
OptionsType : "CumulativeOptions" ,
}
cumulativeSumCheckedDoc = FunctionDoc {
Summary : "Compute cumulative sum of numeric input with overflow checking" ,
Description : `Return the cumulative sum of the input array and report
integer overflow. Null handling and Start follow CumulativeOptions. For
chunked input, accumulation continues across all chunks and the result is
returned as a chunked array.` ,
ArgNames : []string {"values" },
OptionsType : "CumulativeOptions" ,
}
)
type CumulativeOptions = kernels .CumulativeOptions
func safeCastScalar(ctx *exec .KernelCtx , start scalar .Scalar , typ arrow .DataType ) (scalar .Scalar , error ) {
input := NewDatumWithoutOwning (start )
result , err := CastDatum (ctx .Ctx , input , SafeCastOptions (typ ))
if err != nil {
return nil , err
}
casted , ok := result .(*ScalarDatum )
if !ok {
result .Release ()
return nil , fmt .Errorf ("%w: safe cast of cumulative sum start value returned %T" , arrow .ErrInvalid , result )
}
value := casted .Value
casted .Value = nil
result .Release ()
return value , nil
}
func RegisterVectorCumulative (reg FunctionRegistry ) {
sum , checked := kernels .GetVectorCumulativeKernels (safeCastScalar )
sumFn := NewVectorFunction ("cumulative_sum" , Unary (), cumulativeSumDoc )
sumFn .SetDefaultOptions (&CumulativeOptions {})
for _ , k := range sum {
if err := sumFn .AddKernel (k ); err != nil {
panic (err )
}
}
reg .AddFunction (sumFn , false )
checkedFn := NewVectorFunction ("cumulative_sum_checked" , Unary (), cumulativeSumCheckedDoc )
checkedFn .SetDefaultOptions (&CumulativeOptions {})
for _ , k := range checked {
if err := checkedFn .AddKernel (k ); err != nil {
panic (err )
}
}
reg .AddFunction (checkedFn , false )
}
func CumulativeSum (ctx context .Context , opts CumulativeOptions , values Datum ) (Datum , error ) {
return CallFunction (ctx , "cumulative_sum" , &opts , values )
}
func CumulativeSumChecked (ctx context .Context , opts CumulativeOptions , values Datum ) (Datum , error ) {
return CallFunction (ctx , "cumulative_sum_checked" , &opts , values )
}
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 .