This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 95464ee8 fix(arrow/compute): honor NullOptions.NanIsNull in is_null 
(#1488)
95464ee8 is described below

commit 95464ee8591611cf21ec8d1b2e15c4c420cc639b
Author: Harrison Crosse <[email protected]>
AuthorDate: Mon Nov 9 11:49:31 2026 -0500

    fix(arrow/compute): honor NullOptions.NanIsNull in is_null (#1488)
    
    ### Rationale for this change
    
    `is_null` ignores `NullOptions.NanIsNull`, reporting NaN as not null.
    
    ### What changes are included in this PR?
    
    With the option set, `float16`, `float32` and `float64` arrays, scalars
    and dictionary values report NaN as null. `is_not_null`, unions, run-end
    encoded and extension inputs are unchanged.
    
    `is_null` defaults to `NullOptions{}` and rejects other option types
    with `arrow.ErrInvalid`.
    
    ### Are these changes tested?
    
    The NaN cases in `TestIsNullNanIsNull` and
    `TestIsNullNanIsNullDictionary`, plus `TestIsNullInvalidOptions`, fail
    on main and pass with this change.
    
    On macOS arm64 with Go 1.26.10, `go test ./arrow/...` with arrow-testing
    and parquet-testing checked out, `-race -tags assert,test` tests, vet,
    `gofmt`, lint and the Release Audit Tool passed.
    
    ### Are there any user-facing changes?
    
    Yes. `compute.NullOptions` becomes an alias of `kernels.NullOptions` so
    kernels can read it. Composite literals and serialization still work,
    but the reflected type name and package path change. `apidiff` reports:
    
    ```text
    Incompatible changes:
    - NullOptions: changed from NullOptions to 
github.com/apache/arrow-go/v18/arrow/compute/internal/kernels.NullOptions
    ```
    
    Closes https://github.com/apache/arrow-go/issues/1484.
---
 arrow/compute/expression.go                        |  10 +-
 .../compute/internal/kernels/scalar_comparisons.go |  91 ++++++++++++++++---
 arrow/compute/scalar_compare.go                    |   1 +
 arrow/compute/scalar_compare_test.go               | 101 +++++++++++++++++++++
 4 files changed, 185 insertions(+), 18 deletions(-)

diff --git a/arrow/compute/expression.go b/arrow/compute/expression.go
index 14715a38..307946d1 100644
--- a/arrow/compute/expression.go
+++ b/arrow/compute/expression.go
@@ -457,11 +457,9 @@ type MakeStructOptions struct {
 
 func (MakeStructOptions) TypeName() string { return "MakeStructOptions" }
 
-type NullOptions struct {
-       NanIsNull bool `compute:"nan_is_null"`
-}
-
-func (NullOptions) TypeName() string { return "NullOptions" }
+// NullOptions configures is_null. With NanIsNull, NaN values of float16,
+// float32 and float64 arrays and dictionaries are reported as null.
+type NullOptions = kernels.NullOptions
 
 type StrptimeOptions struct {
        Format string         `compute:"format"`
@@ -693,7 +691,7 @@ func GreaterEqual(lhs, rhs Expression) Expression {
 // IsNull creates an expression that returns true if the passed in expression 
is
 // null. Optionally treating NaN as null if desired.
 func IsNull(lhs Expression, nanIsNull bool) Expression {
-       return NewCall("is_null", []Expression{lhs}, &NullOptions{nanIsNull})
+       return NewCall("is_null", []Expression{lhs}, &NullOptions{NanIsNull: 
nanIsNull})
 }
 
 // IsValid is the inverse of IsNull
diff --git a/arrow/compute/internal/kernels/scalar_comparisons.go 
b/arrow/compute/internal/kernels/scalar_comparisons.go
index c211b8d8..3f0eb0c2 100644
--- a/arrow/compute/internal/kernels/scalar_comparisons.go
+++ b/arrow/compute/internal/kernels/scalar_comparisons.go
@@ -29,6 +29,7 @@ import (
        "github.com/apache/arrow-go/v18/arrow/compute/exec"
        "github.com/apache/arrow-go/v18/arrow/decimal128"
        "github.com/apache/arrow-go/v18/arrow/decimal256"
+       "github.com/apache/arrow-go/v18/arrow/float16"
        "github.com/apache/arrow-go/v18/arrow/internal/debug"
        "github.com/apache/arrow-go/v18/arrow/memory"
        "github.com/apache/arrow-go/v18/arrow/scalar"
@@ -727,9 +728,9 @@ func dictHasNullValues(input *exec.ArraySpan) bool {
 // dictValidity writes a bitmap to out, starting at bit 0, with a bit set for
 // each slot that is logically null when wantNull is true, or logically valid
 // otherwise. A slot is null if its index is null or refers to a null
-// dictionary value.
-func dictValidity(ctx *exec.KernelCtx, input *exec.ArraySpan, out 
*exec.ExecResult, wantNull bool) error {
-       var set func(*exec.ArraySpan, []byte, bool)
+// dictionary value, or to a NaN floating-point value when nanIsNull is true.
+func dictValidity(ctx *exec.KernelCtx, input *exec.ArraySpan, out 
*exec.ExecResult, wantNull, nanIsNull bool) error {
+       var set func(*exec.ArraySpan, []byte, int64, []byte, bool)
        switch input.Type.(*arrow.DictionaryType).IndexType.ID() {
        case arrow.INT8, arrow.UINT8:
                set = setDictBits[uint8]
@@ -742,21 +743,34 @@ func dictValidity(ctx *exec.KernelCtx, input 
*exec.ArraySpan, out *exec.ExecResu
        default:
                return fmt.Errorf("%w: unsupported dictionary index type", 
arrow.ErrInvalid)
        }
+
+       dict := input.Dictionary()
+       valueValidity, valueOffset := dict.Buffers[0].Buf, dict.Offset
+       if nanIsNull && arrow.IsFloating(dict.Type.ID()) {
+               buf := ctx.AllocateBitmap(dict.Len)
+               defer buf.Release()
+               valueValidity, valueOffset = buf.Bytes(), 0
+               if dict.Buffers[0].Buf != nil {
+                       bitutil.CopyBitmap(dict.Buffers[0].Buf, 
int(dict.Offset), int(dict.Len), valueValidity, 0)
+               } else {
+                       bitutil.SetBitsTo(valueValidity, 0, dict.Len, true)
+               }
+               setNaNBits(dict, valueValidity, false)
+       }
+
        out.Buffers[1].WrapBuffer(ctx.AllocateBitmap(input.Len))
-       set(input, out.Buffers[1].Buf, wantNull)
+       set(input, valueValidity, valueOffset, out.Buffers[1].Buf, wantNull)
        return nil
 }
 
-func setDictBits[T arrow.UintType](input *exec.ArraySpan, out []byte, wantNull 
bool) {
-       dict := input.Dictionary()
-       valueValidity := dict.Buffers[0].Buf
+func setDictBits[T arrow.UintType](input *exec.ArraySpan, valueValidity 
[]byte, valueOffset int64, out []byte, wantNull bool) {
        indexValidity := input.Buffers[0].Buf
        for i, idx := range exec.GetSpanValues[T](input, 1) {
                // Signed indices are read as unsigned. Only a valid slot's 
index must
                // be in range, so check index validity first. Null-type values 
have no
                // validity bitmap and are all null.
                valid := (indexValidity == nil || 
bitutil.BitIsSet(indexValidity, int(input.Offset)+i)) &&
-                       valueValidity != nil && bitutil.BitIsSet(valueValidity, 
int(dict.Offset)+int(idx))
+                       valueValidity != nil && bitutil.BitIsSet(valueValidity, 
int(valueOffset)+int(idx))
                bitutil.SetBitTo(out, i, valid != wantNull)
        }
 }
@@ -848,12 +862,62 @@ func isValidAt(span *exec.ArraySpan, i int64) bool {
        return true
 }
 
+// setNaNBits sets bit i of out to v for each NaN slot i of a float16, float32
+// or float64 span, and does nothing for other types.
+func setNaNBits(input *exec.ArraySpan, out []byte, v bool) {
+       switch input.Type.ID() {
+       case arrow.FLOAT16:
+               for i, x := range exec.GetSpanValues[float16.Num](input, 1) {
+                       if x.IsNaN() {
+                               bitutil.SetBitTo(out, i, v)
+                       }
+               }
+       case arrow.FLOAT32:
+               for i, x := range exec.GetSpanValues[float32](input, 1) {
+                       if x != x {
+                               bitutil.SetBitTo(out, i, v)
+                       }
+               }
+       case arrow.FLOAT64:
+               for i, x := range exec.GetSpanValues[float64](input, 1) {
+                       if x != x {
+                               bitutil.SetBitTo(out, i, v)
+                       }
+               }
+       }
+}
+
+type NullOptions struct {
+       NanIsNull bool `compute:"nan_is_null"`
+}
+
+func (NullOptions) TypeName() string { return "NullOptions" }
+
+func initIsNull(_ *exec.KernelCtx, args exec.KernelInitArgs) 
(exec.KernelState, error) {
+       switch opts := args.Options.(type) {
+       // exprs initializes kernels without applying the default options
+       case nil:
+               return NullOptions{}, nil
+       case NullOptions:
+               return opts, nil
+       case *NullOptions:
+               if opts == nil {
+                       return NullOptions{}, nil
+               }
+               return *opts, nil
+       }
+       return nil, fmt.Errorf("%w: attempted to initialize kernel state from 
invalid function options",
+               arrow.ErrInvalid)
+}
+
 func isNullExec(ctx *exec.KernelCtx, batch *exec.ExecSpan, out 
*exec.ExecResult) error {
        out.Release()
        input := batch.Values[0].Array
+       opts, _ := ctx.State.(NullOptions)
 
-       if dictHasNullValues(&input) {
-               return dictValidity(ctx, &input, out, true)
+       floatDict := input.Type.ID() == arrow.DICTIONARY && 
arrow.IsFloating(input.Dictionary().Type.ID())
+       if dictHasNullValues(&input) || (opts.NanIsNull && floatDict) {
+               return dictValidity(ctx, &input, out, true, opts.NanIsNull)
        }
 
        if hasLogicalNulls(&input) {
@@ -866,6 +930,9 @@ func isNullExec(ctx *exec.KernelCtx, batch *exec.ExecSpan, 
out *exec.ExecResult)
                bitutil.InvertBitmap(validityBuf.Bytes(), int(input.Offset), 
int(input.Len),
                        out.Buffers[1].Buf, 0)
        }
+       if opts.NanIsNull {
+               setNaNBits(&input, out.Buffers[1].Buf, true)
+       }
 
        return nil
 }
@@ -875,7 +942,7 @@ func isNotNullExec(ctx *exec.KernelCtx, batch 
*exec.ExecSpan, out *exec.ExecResu
        input := batch.Values[0].Array
 
        if dictHasNullValues(&input) {
-               return dictValidity(ctx, &input, out, false)
+               return dictValidity(ctx, &input, out, false, false)
        }
 
        if hasLogicalNulls(&input) {
@@ -903,7 +970,7 @@ func IsNullNotNullKernels() []exec.ScalarKernel {
        out := exec.NewOutputType(arrow.FixedWidthTypes.Boolean)
 
        results := make([]exec.ScalarKernel, 2)
-       results[0] = exec.NewScalarKernel([]exec.InputType{in}, out, 
isNullExec, nil)
+       results[0] = exec.NewScalarKernel([]exec.InputType{in}, out, 
isNullExec, initIsNull)
        results[0].NullHandling = exec.NullComputedNoPrealloc
        results[0].MemAlloc = exec.MemNoPrealloc
 
diff --git a/arrow/compute/scalar_compare.go b/arrow/compute/scalar_compare.go
index 2636868f..21c0b414 100644
--- a/arrow/compute/scalar_compare.go
+++ b/arrow/compute/scalar_compare.go
@@ -166,6 +166,7 @@ func RegisterScalarComparisons(reg FunctionRegistry) {
 
        isOrNotNullKns := kernels.IsNullNotNullKernels()
        isNullFn := NewScalarFunction("is_null", Unary(), EmptyFuncDoc)
+       isNullFn.SetDefaultOptions(&NullOptions{})
        if err := isNullFn.AddKernel(isOrNotNullKns[0]); err != nil {
                panic(err)
        }
diff --git a/arrow/compute/scalar_compare_test.go 
b/arrow/compute/scalar_compare_test.go
index f2abcaa0..e8117562 100644
--- a/arrow/compute/scalar_compare_test.go
+++ b/arrow/compute/scalar_compare_test.go
@@ -1969,6 +1969,107 @@ func (sv *ScalarValiditySuite) TestLogicalNullScalars() 
{
        }
 }
 
+func (sv *ScalarValiditySuite) checkIsNull(input arrow.Array, opts 
compute.FunctionOptions, expected string) {
+       exp := sv.getArr(arrow.FixedWidthTypes.Boolean, expected)
+       defer exp.Release()
+
+       res, err := compute.CallFunction(sv.ctx, "is_null", opts, 
compute.NewDatumWithoutOwning(input))
+       sv.Require().NoError(err)
+       result := res.(*compute.ArrayDatum).MakeArray()
+       res.Release()
+       defer result.Release()
+       sv.Truef(array.Equal(exp, result), "is_null(%s, %v): expected %s, got 
%s", input, opts, exp, result)
+
+       for i := 0; i < input.Len(); i++ {
+               s, err := scalar.GetScalar(input, i)
+               sv.Require().NoError(err)
+               res, err := compute.CallFunction(sv.ctx, "is_null", opts, 
compute.NewDatumWithoutOwning(s))
+               sv.Require().NoError(err)
+               got := res.(*compute.ScalarDatum).Value
+               
sv.Truef(scalar.Equals(scalar.NewBooleanScalar(exp.(*array.Boolean).Value(i)), 
got),
+                       "is_null(%s, %v): got %s", s, opts, got)
+               res.Release()
+               if r, ok := s.(scalar.Releasable); ok {
+                       r.Release()
+               }
+       }
+}
+
+func (sv *ScalarValiditySuite) TestIsNullNanIsNull() {
+       for _, typ := range []arrow.DataType{
+               arrow.FixedWidthTypes.Float16, arrow.PrimitiveTypes.Float32, 
arrow.PrimitiveTypes.Float64,
+       } {
+               sv.Run(typ.String(), func() {
+                       arr := sv.getArr(typ, `[1.5, 0.0, -0.0, null, "Inf", 
"-Inf", "NaN"]`)
+                       defer arr.Release()
+                       sv.checkIsNull(arr, nil, `[false, false, false, true, 
false, false, false]`)
+                       sv.checkIsNull(arr, (*compute.NullOptions)(nil), 
`[false, false, false, true, false, false, false]`)
+                       sv.checkIsNull(arr, &compute.NullOptions{NanIsNull: 
true}, `[false, false, false, true, false, false, true]`)
+                       sv.checkIsNull(arr, compute.NullOptions{NanIsNull: 
true}, `[false, false, false, true, false, false, true]`)
+
+                       long := sv.getArr(typ, `[1, "NaN", null, 2, "NaN", 3, 
4, 5, 6, "NaN", null, 7, "NaN"]`)
+                       defer long.Release()
+                       sliced := array.NewSlice(long, 9, 13)
+                       defer sliced.Release()
+                       sv.checkIsNull(sliced, &compute.NullOptions{NanIsNull: 
true}, `[true, true, false, true]`)
+               })
+       }
+}
+
+func (sv *ScalarValiditySuite) TestIsNullNanIsNullDictionary() {
+       nanIsNull := &compute.NullOptions{NanIsNull: true}
+       for _, valueType := range []arrow.DataType{
+               arrow.FixedWidthTypes.Float16, arrow.PrimitiveTypes.Float32, 
arrow.PrimitiveTypes.Float64,
+       } {
+               sv.Run(valueType.String(), func() {
+                       dictType := &arrow.DictionaryType{IndexType: 
arrow.PrimitiveTypes.Int32, ValueType: valueType}
+                       makeDict := func(indices, values arrow.Array) 
arrow.Array {
+                               defer indices.Release()
+                               defer values.Release()
+                               return array.NewDictionaryArray(dictType, 
indices, values)
+                       }
+
+                       dict := makeDict(sv.getArr(arrow.PrimitiveTypes.Int32, 
`[0, 1, 2, 3, null, 1]`),
+                               sv.getArr(valueType, `[1.5, "NaN", -0.0, 
null]`))
+                       defer dict.Release()
+                       sv.checkIsNull(dict, nil, `[false, false, false, true, 
true, false]`)
+                       sv.checkIsNull(dict, nanIsNull, `[false, true, false, 
true, true, true]`)
+
+                       withValidity := sv.getArr(valueType, `[1.5, "NaN"]`)
+                       data := array.NewData(valueType, 2, 
[]*memory.Buffer{nil, withValidity.Data().Buffers()[1]}, nil, 0, 0)
+                       withValidity.Release()
+                       noValidity := array.MakeFromData(data)
+                       data.Release()
+                       noNullValues := 
makeDict(sv.getArr(arrow.PrimitiveTypes.Int32, `[0, 1, null]`), noValidity)
+                       defer noNullValues.Release()
+                       sv.checkIsNull(noNullValues, nanIsNull, `[false, true, 
true]`)
+
+                       allValues := sv.getArr(valueType, `[1, 2, 3, 4, 5, 6, 
7, 8, 9, "NaN", null, 1.5]`)
+                       defer allValues.Release()
+                       indices := sv.getArr(arrow.PrimitiveTypes.Int32,
+                               `[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, null, 0, 1]`)
+                       defer indices.Release()
+                       slicedValues := array.NewSlice(allValues, 9, 12)
+                       defer slicedValues.Release()
+                       slicedIndices := array.NewSlice(indices, 9, 14)
+                       defer slicedIndices.Release()
+                       sliced := array.NewDictionaryArray(dictType, 
slicedIndices, slicedValues)
+                       defer sliced.Release()
+                       sv.checkIsNull(sliced, nanIsNull, `[true, false, true, 
true, true]`)
+               })
+       }
+}
+
+func (sv *ScalarValiditySuite) TestIsNullInvalidOptions() {
+       arr := sv.getArr(arrow.PrimitiveTypes.Float64, `[1, null]`)
+       defer arr.Release()
+       res, err := compute.CallFunction(sv.ctx, "is_null", 
&compute.RoundOptions{}, compute.NewDatumWithoutOwning(arr))
+       if res != nil {
+               res.Release()
+       }
+       sv.ErrorIs(err, arrow.ErrInvalid)
+}
+
 func (sv *ScalarValiditySuite) TestIsNaN() {
        tests := []struct {
                in       string

Reply via email to