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 5a8b2f8b fix(arrow/compute): handle null, union and run-end encoded
arrays in is_null and is_not_null (#1465)
5a8b2f8b is described below
commit 5a8b2f8bdc382c2c59a6d7b356c9d619abfacb2a
Author: Harrison Crosse <[email protected]>
AuthorDate: Thu Nov 5 12:09:13 2026 -0500
fix(arrow/compute): handle null, union and run-end encoded arrays in
is_null and is_not_null (#1465)
### Rationale for this change
Filters on `is_null` or `is_not_null` keep or drop the wrong rows for
null, union and run-end encoded arrays. These types have no validity
bitmap, so the kernels treat every slot as valid, as reported in
https://github.com/apache/arrow-go/issues/1462.
### What changes are included in this PR?
These inputs now use their logical validity, matching Arrow C++'s
[`SetLogicalNullBits`](https://github.com/apache/arrow/blob/0ab78e2df489055b71a7d26a666a52823f87f01c/cpp/src/arrow/compute/kernels/scalar_validity.cc#L120-L178).
A null array is all null. A union slot takes the validity of the child
slot it selects, with nested unions and run-end encoded children
resolving their own logical nulls. A run-end encoded slot takes the
validity of its run's value.
Two C++ quirks are kept. A dictionary child of a union is checked only
by its index validity, so a null dictionary value there counts as valid.
The values of a top-level run-end encoded array are checked only by
their validity bitmap, so `null` or union values there count as valid.
Inputs that have a validity bitmap keep the existing path.
A dictionary whose values are the `null` type now reports every slot as
null. Those values have no validity bitmap, so the dictionary path from
https://github.com/apache/arrow-go/pull/1461 treated them as valid. This
follows C++'s `IsValidIsNullDictionaryOfNullType` test, added in
[`scalar_validity_test.cc`](https://github.com/apache/arrow/blob/00c32a69cb0dbe7a45a8c764638611b7bb021410/cpp/src/arrow/compute/kernels/scalar_validity_test.cc#L205-L210)
with the `list_contains` change. It is not in a release yet. pyarrow 21,
23 and 25 report only index nulls here, while 26.0.0 and 26.0.1 segfault
on this input.
### Are these changes tested?
I added:
- `TestNullType` for whole and sliced null arrays.
- `TestSparseUnion` for non-identity type codes and sliced children, and
`TestDenseUnion` for selected child offsets and slices.
- `TestRunEndEncoded` for `int16`, `int32` and `int64` run ends, sliced
values and run ends, a boundary slice, and the C++ validity-bitmap
quirk.
- `TestNestedUnion` for nested unions, including run-end encoded and
dictionary children.
- `TestLogicalNullScalars` for null, union and run-end encoded scalars.
- `TestDictionaryNullTypeValues` for whole and sliced dictionaries with
`null` values.
Expected values come from pyarrow `26.0.0`, except the dictionary with
`null` values, which follows the C++ test. All new cases fail on main
except those pinning the deliberate quirks and the run-end encoded
scalar cases. Those pass on main and fail when their behavior is
deliberately changed.
On macOS arm64 with Go 1.26.10, `gofmt`, `go vet`, `go test
./arrow/compute/...` with and without `-tags assert,test`, `go test
-race ./arrow/compute/`, `golangci-lint` and the Release Audit Tool
passed.
### Are there any user-facing changes?
Yes. `is_null` and `is_not_null` results change for null, union and
run-end encoded inputs, and dictionaries whose values are the `null`
type. For the `null` type, `is_null` now returns all true instead of all
false, and `is_not_null` returns all false instead of all true. There is
no API change.
Closes https://github.com/apache/arrow-go/issues/1462.
---
.../compute/internal/kernels/scalar_comparisons.go | 107 ++++++++++-
arrow/compute/scalar_compare_test.go | 205 +++++++++++++++++++++
2 files changed, 309 insertions(+), 3 deletions(-)
diff --git a/arrow/compute/internal/kernels/scalar_comparisons.go
b/arrow/compute/internal/kernels/scalar_comparisons.go
index ead83300..c211b8d8 100644
--- a/arrow/compute/internal/kernels/scalar_comparisons.go
+++ b/arrow/compute/internal/kernels/scalar_comparisons.go
@@ -21,6 +21,7 @@ package kernels
import (
"bytes"
"fmt"
+ "sort"
"unsafe"
"github.com/apache/arrow-go/v18/arrow"
@@ -716,7 +717,11 @@ func CompareKernels(op CompareOperator)
[]exec.ScalarKernel {
}
func dictHasNullValues(input *exec.ArraySpan) bool {
- return input.Type.ID() == arrow.DICTIONARY &&
input.Dictionary().MayHaveNulls()
+ if input.Type.ID() != arrow.DICTIONARY {
+ return false
+ }
+ dict := input.Dictionary()
+ return dict.MayHaveNulls() || dict.Type.ID() == arrow.NULL
}
// dictValidity writes a bitmap to out, starting at bit 0, with a bit set for
@@ -748,13 +753,101 @@ func setDictBits[T arrow.UintType](input
*exec.ArraySpan, out []byte, wantNull b
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.
+ // 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)) &&
- bitutil.BitIsSet(valueValidity,
int(dict.Offset)+int(idx))
+ valueValidity != nil && bitutil.BitIsSet(valueValidity,
int(dict.Offset)+int(idx))
bitutil.SetBitTo(out, i, valid != wantNull)
}
}
+// hasLogicalNulls reports whether input's nulls are not described by a
+// validity bitmap, as for null, union and run-end encoded types.
+func hasLogicalNulls(input *exec.ArraySpan) bool {
+ // a run-end encoded scalar promoted to an array has a validity bitmap
and
+ // no children
+ if input.Buffers[0].Buf != nil {
+ return false
+ }
+ switch input.Type.ID() {
+ case arrow.NULL, arrow.SPARSE_UNION, arrow.DENSE_UNION,
arrow.RUN_END_ENCODED:
+ return true
+ }
+ return false
+}
+
+// logicalValidity writes a bitmap to out like dictValidity, for the types
+// accepted by hasLogicalNulls. It follows Arrow C++'s SetLogicalNullBits: a
+// union slot takes the validity of its child slot, and a run-end encoded slot
+// takes the validity bitmap of its value.
+func logicalValidity(ctx *exec.KernelCtx, input *exec.ArraySpan, out
*exec.ExecResult, wantNull bool) error {
+ out.Buffers[1].WrapBuffer(ctx.AllocateBitmap(input.Len))
+ bits := out.Buffers[1].Buf
+ switch input.Type.ID() {
+ case arrow.NULL:
+ bitutil.SetBitsTo(bits, 0, input.Len, wantNull)
+ case arrow.RUN_END_ENCODED:
+ values := &input.Children[1]
+ run := findRun(input, 0)
+ for pos := int64(0); pos < input.Len; run++ {
+ end := min(runEnd(input, run)-input.Offset, input.Len)
+ valid := values.Buffers[0].Buf == nil ||
+ bitutil.BitIsSet(values.Buffers[0].Buf,
int(values.Offset)+run)
+ bitutil.SetBitsTo(bits, pos, end-pos, valid != wantNull)
+ pos = end
+ }
+ default:
+ for i := int64(0); i < input.Len; i++ {
+ bitutil.SetBitTo(bits, int(i), isValidAt(input, i) !=
wantNull)
+ }
+ }
+ return nil
+}
+
+// findRun returns the index of the run that holds slot i of a run-end encoded
+// span.
+func findRun(span *exec.ArraySpan, i int64) int {
+ return sort.Search(int(span.Children[0].Len), func(j int) bool { return
runEnd(span, j) > span.Offset+i })
+}
+
+// runEnd returns the logical end of run j of a run-end encoded span.
+func runEnd(span *exec.ArraySpan, j int) int64 {
+ runEnds := &span.Children[0]
+ switch runEnds.Type.ID() {
+ case arrow.INT16:
+ return int64(exec.GetSpanValues[int16](runEnds, 1)[j])
+ case arrow.INT32:
+ return int64(exec.GetSpanValues[int32](runEnds, 1)[j])
+ default:
+ return int64(exec.GetSpanValues[int64](runEnds, 1)[j])
+ }
+}
+
+// isValidAt reports whether slot i of span is valid, following Arrow C++'s
+// ArraySpan::IsValid. Unlike the top-level kernels, it ignores null dictionary
+// values, and it checks the value of a run-end encoded array with isValidAt
+// rather than the validity bitmap of the values.
+func isValidAt(span *exec.ArraySpan, i int64) bool {
+ if span.Buffers[0].Buf != nil {
+ return bitutil.BitIsSet(span.Buffers[0].Buf, int(span.Offset+i))
+ }
+ switch span.Type.ID() {
+ case arrow.NULL:
+ return false
+ case arrow.SPARSE_UNION, arrow.DENSE_UNION:
+ code := exec.GetSpanValues[arrow.UnionTypeCode](span, 1)[i]
+ child :=
&span.Children[span.Type.(arrow.UnionType).ChildIDs()[code]]
+ j := span.Offset + i
+ if span.Type.ID() == arrow.DENSE_UNION {
+ j = int64(exec.GetSpanValues[int32](span, 2)[i])
+ }
+ return isValidAt(child, j)
+ case arrow.RUN_END_ENCODED:
+ return isValidAt(&span.Children[1], int64(findRun(span, i)))
+ }
+ return true
+}
+
func isNullExec(ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
out.Release()
input := batch.Values[0].Array
@@ -763,6 +856,10 @@ func isNullExec(ctx *exec.KernelCtx, batch *exec.ExecSpan,
out *exec.ExecResult)
return dictValidity(ctx, &input, out, true)
}
+ if hasLogicalNulls(&input) {
+ return logicalValidity(ctx, &input, out, true)
+ }
+
validityBuf := input.GetBuffer(0)
out.Buffers[1].WrapBuffer(ctx.AllocateBitmap(input.Len))
if validityBuf != nil {
@@ -781,6 +878,10 @@ func isNotNullExec(ctx *exec.KernelCtx, batch
*exec.ExecSpan, out *exec.ExecResu
return dictValidity(ctx, &input, out, false)
}
+ if hasLogicalNulls(&input) {
+ return logicalValidity(ctx, &input, out, false)
+ }
+
validityBuf := input.GetBuffer(0)
switch {
case validityBuf == nil:
diff --git a/arrow/compute/scalar_compare_test.go
b/arrow/compute/scalar_compare_test.go
index a6983d03..ad93630a 100644
--- a/arrow/compute/scalar_compare_test.go
+++ b/arrow/compute/scalar_compare_test.go
@@ -1683,6 +1683,19 @@ func (sv *ScalarValiditySuite)
TestDictionaryNullValuesSliced() {
`[true, false, false, true, false, false, true, true, false,
false, false]`)
}
+func (sv *ScalarValiditySuite) TestDictionaryNullTypeValues() {
+ values := array.NewNull(1)
+ defer values.Release()
+ indices := sv.getArr(arrow.PrimitiveTypes.Int8, `[0, null, 0]`)
+ defer indices.Release()
+ dict := array.NewDictionaryArray(&arrow.DictionaryType{
+ IndexType: arrow.PrimitiveTypes.Int8, ValueType: arrow.Null},
indices, values)
+ defer dict.Release()
+
+ sv.checkValiditySliced(dict, `[true, true, true]`, `[false, false,
false]`)
+ sv.checkValiditySlice(dict, 1, 3, `[true, true]`, `[false, false]`)
+}
+
func (sv *ScalarValiditySuite) TestDictionaryNullValuesScalarAndChunked() {
values := sv.getArr(arrow.BinaryTypes.String, `["a", null]`)
defer values.Release()
@@ -1753,6 +1766,198 @@ func (sv *ScalarValiditySuite)
TestDictionaryNoNullValues() {
sv.Same(dict.Data().Buffers()[0],
res.(*compute.ArrayDatum).Value.Buffers()[1])
}
+func (sv *ScalarValiditySuite) checkValiditySlice(input arrow.Array, lo, hi
int64, isNull, isNotNull string) {
+ sliced := array.NewSlice(input, lo, hi)
+ defer sliced.Release()
+ sv.checkValiditySliced(sliced, isNull, isNotNull)
+}
+
+func (sv *ScalarValiditySuite) TestNullType() {
+ arr := array.NewNull(3)
+ defer arr.Release()
+ sv.checkValiditySliced(arr, `[true, true, true]`, `[false, false,
false]`)
+ sv.checkValiditySlice(arr, 1, 3, `[true, true]`, `[false, false]`)
+}
+
+func (sv *ScalarValiditySuite) TestSparseUnion() {
+ typeIDs := sv.getArr(arrow.PrimitiveTypes.Int8, `[5, 2, 5, 2]`)
+ defer typeIDs.Release()
+ ints := sv.getArr(arrow.PrimitiveTypes.Int32, `[9, 1, null, 3, 4]`)
+ defer ints.Release()
+ strs := sv.getArr(arrow.BinaryTypes.String, `["z", "a", "b", null,
null]`)
+ defer strs.Release()
+ slicedInts := array.NewSlice(ints, 1, 5)
+ defer slicedInts.Release()
+ slicedStrs := array.NewSlice(strs, 1, 5)
+ defer slicedStrs.Release()
+ arr, err := array.NewSparseUnionFromArraysWithFieldCodes(typeIDs,
+ []arrow.Array{slicedInts, slicedStrs}, []string{"i", "s"},
[]arrow.UnionTypeCode{2, 5})
+ sv.Require().NoError(err)
+ defer arr.Release()
+
+ // arr is ["a", null, null, 4]
+ sv.checkValiditySliced(arr, `[false, true, true, false]`, `[true,
false, false, true]`)
+ sv.checkValiditySlice(arr, 2, 4, `[true, false]`, `[false, true]`)
+}
+
+func (sv *ScalarValiditySuite) TestDenseUnion() {
+ typeIDs := sv.getArr(arrow.PrimitiveTypes.Int8, `[5, 2, 2, 5]`)
+ defer typeIDs.Release()
+ offsets := sv.getArr(arrow.PrimitiveTypes.Int32, `[1, 1, 0, 0]`)
+ defer offsets.Release()
+ ints := sv.getArr(arrow.PrimitiveTypes.Int32, `[null, 1]`)
+ defer ints.Release()
+ strs := sv.getArr(arrow.BinaryTypes.String, `["a", null]`)
+ defer strs.Release()
+ arr, err := array.NewDenseUnionFromArraysWithFieldCodes(typeIDs,
offsets,
+ []arrow.Array{ints, strs}, []string{"i", "s"},
[]arrow.UnionTypeCode{2, 5})
+ sv.Require().NoError(err)
+ defer arr.Release()
+
+ sv.checkValiditySliced(arr, `[true, false, true, false]`, `[false,
true, false, true]`)
+ sv.checkValiditySlice(arr, 1, 3, `[false, true]`, `[true, false]`)
+}
+
+func (sv *ScalarValiditySuite) TestRunEndEncoded() {
+ allValues := sv.getArr(arrow.PrimitiveTypes.Int32, `[null, 1, null, 2]`)
+ defer allValues.Release()
+ values := array.NewSlice(allValues, 1, 4)
+ defer values.Release()
+ nulls := array.NewNull(2)
+ defer nulls.Release()
+ unionIDs := sv.getArr(arrow.PrimitiveTypes.Int8, `[0, 0]`)
+ defer unionIDs.Release()
+ unionInts := sv.getArr(arrow.PrimitiveTypes.Int32, `[1, null]`)
+ defer unionInts.Release()
+ union, err := array.NewSparseUnionFromArrays(unionIDs,
[]arrow.Array{unionInts})
+ sv.Require().NoError(err)
+ defer union.Release()
+
+ for _, runEndType := range []arrow.DataType{
+ arrow.PrimitiveTypes.Int16, arrow.PrimitiveTypes.Int32,
arrow.PrimitiveTypes.Int64,
+ } {
+ sv.Run(runEndType.String(), func() {
+ allRunEnds := sv.getArr(runEndType, `[1, 2, 3, 5]`)
+ defer allRunEnds.Release()
+ runEnds := array.NewSlice(allRunEnds, 1, 4)
+ defer runEnds.Release()
+ arr := array.NewRunEndEncodedArray(runEnds, values, 5,
0)
+ defer arr.Release()
+
+ // arr is [1, 1, null, 2, 2]
+ sv.checkValiditySliced(arr, `[false, false, true,
false, false]`, `[true, true, false, true, true]`)
+ sv.checkValiditySlice(arr, 1, 4, `[false, true,
false]`, `[true, false, true]`)
+ sv.checkValiditySlice(arr, 2, 5, `[true, false,
false]`, `[false, true, true]`)
+
+ // As in Arrow C++, only the validity bitmap of the
values is checked.
+ shortRunEnds := sv.getArr(runEndType, `[2, 3]`)
+ defer shortRunEnds.Release()
+ for _, v := range []arrow.Array{nulls, union} {
+ arr :=
array.NewRunEndEncodedArray(shortRunEnds, v, 3, 0)
+ sv.checkValiditySliced(arr, `[false, false,
false]`, `[true, true, true]`)
+ arr.Release()
+ }
+ })
+ }
+}
+
+func (sv *ScalarValiditySuite) TestNestedUnion() {
+ innerIDs := sv.getArr(arrow.PrimitiveTypes.Int8, `[0, 0]`)
+ defer innerIDs.Release()
+ ints := sv.getArr(arrow.PrimitiveTypes.Int32, `[1, null]`)
+ defer ints.Release()
+ inner, err := array.NewSparseUnionFromArrays(innerIDs,
[]arrow.Array{ints})
+ sv.Require().NoError(err)
+ defer inner.Release()
+ nulls := array.NewNull(1)
+ defer nulls.Release()
+ typeIDs := sv.getArr(arrow.PrimitiveTypes.Int8, `[1, 0, 1]`)
+ defer typeIDs.Release()
+ offsets := sv.getArr(arrow.PrimitiveTypes.Int32, `[0, 0, 1]`)
+ defer offsets.Release()
+ arr, err := array.NewDenseUnionFromArrays(typeIDs, offsets,
[]arrow.Array{nulls, inner})
+ sv.Require().NoError(err)
+ defer arr.Release()
+
+ sv.checkValiditySliced(arr, `[false, true, true]`, `[true, false,
false]`)
+
+ runEnds := sv.getArr(arrow.PrimitiveTypes.Int32, `[1, 2]`)
+ defer runEnds.Release()
+ ree := array.NewRunEndEncodedArray(runEnds, ints, 2, 0)
+ defer ree.Release()
+ indices := sv.getArr(arrow.PrimitiveTypes.Int32, `[0, 1]`)
+ defer indices.Release()
+ strs := sv.getArr(arrow.BinaryTypes.String, `["a", null]`)
+ defer strs.Release()
+ dict := array.NewDictionaryArray(&arrow.DictionaryType{
+ IndexType: arrow.PrimitiveTypes.Int32, ValueType:
arrow.BinaryTypes.String}, indices, strs)
+ defer dict.Release()
+
+ // As in Arrow C++, a run-end encoded child is checked logically and a
+ // dictionary child only by its index validity.
+ for _, tt := range []struct {
+ child arrow.Array
+ isNull, isNotNull string
+ }{
+ {ree, `[false, true]`, `[true, false]`},
+ {dict, `[false, false]`, `[true, true]`},
+ } {
+ arr, err := array.NewSparseUnionFromArrays(innerIDs,
[]arrow.Array{tt.child})
+ sv.Require().NoError(err)
+ sv.checkValiditySliced(arr, tt.isNull, tt.isNotNull)
+ arr.Release()
+ }
+}
+
+func (sv *ScalarValiditySuite) TestLogicalNullScalars() {
+ typeIDs := sv.getArr(arrow.PrimitiveTypes.Int8, `[0, 0]`)
+ defer typeIDs.Release()
+ ints := sv.getArr(arrow.PrimitiveTypes.Int32, `[1, null]`)
+ defer ints.Release()
+ offsets := sv.getArr(arrow.PrimitiveTypes.Int32, `[0, 1]`)
+ defer offsets.Release()
+ sparse, err := array.NewSparseUnionFromArrays(typeIDs,
[]arrow.Array{ints})
+ sv.Require().NoError(err)
+ defer sparse.Release()
+ dense, err := array.NewDenseUnionFromArrays(typeIDs, offsets,
[]arrow.Array{ints})
+ sv.Require().NoError(err)
+ defer dense.Release()
+ runEnds := sv.getArr(arrow.PrimitiveTypes.Int32, `[1, 2]`)
+ defer runEnds.Release()
+ ree := array.NewRunEndEncodedArray(runEnds, ints, 2, 0)
+ defer ree.Release()
+
+ type testCase struct {
+ s scalar.Scalar
+ isNull bool
+ }
+ tests := []testCase{{scalar.ScalarNull, true}}
+ for _, tt := range []struct {
+ arr arrow.Array
+ idx int
+ isNull bool
+ }{
+ {sparse, 1, true}, {dense, 1, true}, {ree, 0, false}, {ree, 1,
true},
+ } {
+ s, err := scalar.GetScalar(tt.arr, tt.idx)
+ sv.Require().NoError(err)
+ if r, ok := s.(scalar.Releasable); ok {
+ defer r.Release()
+ }
+ tests = append(tests, testCase{s, tt.isNull})
+ }
+
+ for _, tt := range tests {
+ for fn, expected := range map[string]bool{"is_null": tt.isNull,
"is_not_null": !tt.isNull} {
+ res, err := compute.CallFunction(sv.ctx, fn, nil,
compute.NewDatumWithoutOwning(tt.s))
+ sv.Require().NoError(err)
+
sv.Truef(scalar.Equals(scalar.NewBooleanScalar(expected),
res.(*compute.ScalarDatum).Value),
+ "%s(%s): expected %t, got %s", fn, tt.s,
expected, res)
+ res.Release()
+ }
+ }
+}
+
func (sv *ScalarValiditySuite) TestIsNaN() {
tests := []struct {
in string