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 7eee08ac fix(arrow/compute): treat null dictionary values as null in
is_null and is_not_null (#1461)
7eee08ac is described below
commit 7eee08ac489ec1adc8bdb8f6621ae968ef340e6d
Author: Harrison Crosse <[email protected]>
AuthorDate: Wed Nov 4 14:35:35 2026 -0500
fix(arrow/compute): treat null dictionary values as null in is_null and
is_not_null (#1461)
### Rationale for this change
`is_null` and `is_not_null` read only a dictionary array's index
validity, so a valid index pointing to a null value counts as not null.
With values `["a", null]` and indices `[0, 1, null]`, `is_null` returns
`[false false true]` instead of `[false true true]`.
Arrow C++ treats that second slot as null. pyarrow `26.0.1` returns
`[False, True, True]` for `is_null` and `[True, False, False]` for
`is_valid`, the counterpart of `is_not_null`.
### What changes are included in this PR?
When dictionary values may have nulls, both kernels build a bitmap from
the index validity and the referenced value's validity, checking the
index first, as C++'s
[`dict_util::SetLogicalNullBits`](https://github.com/apache/arrow/blob/0ab78e2df489055b71a7d26a666a52823f87f01c/cpp/src/arrow/util/dict_util.cc#L122)
does. A null slot can hold an index outside the dictionary, so its value
must not be read.
Other inputs keep the existing path. For dictionaries without null
values, `is_not_null` still shares the index validity buffer at offset
0.
Out of scope:
- `nan_is_null`, which the kernel does not read.
- Extension types with dictionary storage.
- Dictionaries whose values are union or run-end encoded, which C++ also
does not handle.
- Dictionaries whose values are a null type, which C++ reports as null
and this change still reports as valid.
- Null, union and run-end encoded arrays themselves, where `is_null`
also disagrees with C++ and each type needs its own handling.
### Are these changes tested?
I added tests for every integer index type, an index outside the
dictionary in a null slot, and indices without a validity buffer. They
also cover index and dictionary offsets crossing a byte boundary, scalar
and chunked inputs, and the unchanged fast path, including buffer
sharing at offset 0. The tests for null dictionary values fail without
the fix.
On macOS arm64, `go test ./arrow/compute/...` passed with and without
`-tags assert,test`, along with `go test -race ./arrow/compute/`, `go
vet`, `gofmt`, golangci-lint and the prek Release Audit Tool.
https://github.com/apache/arrow-go/pull/1457 also changes how these
kernels are registered. This change merges cleanly with it, and the
compute tests pass on the merged tree.
### Are there any user-facing changes?
Yes. `is_null` and `is_not_null` now count a valid index pointing to a
null dictionary value as null. There is no API change.
Closes https://github.com/apache/arrow-go/issues/1452.
---
.../compute/internal/kernels/scalar_comparisons.go | 48 ++++++++
arrow/compute/scalar_compare_test.go | 130 +++++++++++++++++++++
2 files changed, 178 insertions(+)
diff --git a/arrow/compute/internal/kernels/scalar_comparisons.go
b/arrow/compute/internal/kernels/scalar_comparisons.go
index 4c2b4c9f..ead83300 100644
--- a/arrow/compute/internal/kernels/scalar_comparisons.go
+++ b/arrow/compute/internal/kernels/scalar_comparisons.go
@@ -715,10 +715,54 @@ func CompareKernels(op CompareOperator)
[]exec.ScalarKernel {
return kns
}
+func dictHasNullValues(input *exec.ArraySpan) bool {
+ return input.Type.ID() == arrow.DICTIONARY &&
input.Dictionary().MayHaveNulls()
+}
+
+// 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)
+ switch input.Type.(*arrow.DictionaryType).IndexType.ID() {
+ case arrow.INT8, arrow.UINT8:
+ set = setDictBits[uint8]
+ case arrow.INT16, arrow.UINT16:
+ set = setDictBits[uint16]
+ case arrow.INT32, arrow.UINT32:
+ set = setDictBits[uint32]
+ case arrow.INT64, arrow.UINT64:
+ set = setDictBits[uint64]
+ default:
+ return fmt.Errorf("%w: unsupported dictionary index type",
arrow.ErrInvalid)
+ }
+ out.Buffers[1].WrapBuffer(ctx.AllocateBitmap(input.Len))
+ set(input, 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
+ 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.
+ valid := (indexValidity == nil ||
bitutil.BitIsSet(indexValidity, int(input.Offset)+i)) &&
+ bitutil.BitIsSet(valueValidity,
int(dict.Offset)+int(idx))
+ bitutil.SetBitTo(out, i, valid != wantNull)
+ }
+}
+
func isNullExec(ctx *exec.KernelCtx, batch *exec.ExecSpan, out
*exec.ExecResult) error {
out.Release()
input := batch.Values[0].Array
+ if dictHasNullValues(&input) {
+ return dictValidity(ctx, &input, out, true)
+ }
+
validityBuf := input.GetBuffer(0)
out.Buffers[1].WrapBuffer(ctx.AllocateBitmap(input.Len))
if validityBuf != nil {
@@ -733,6 +777,10 @@ func isNotNullExec(ctx *exec.KernelCtx, batch
*exec.ExecSpan, out *exec.ExecResu
out.Release()
input := batch.Values[0].Array
+ if dictHasNullValues(&input) {
+ return dictValidity(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 0e30192f..a6983d03 100644
--- a/arrow/compute/scalar_compare_test.go
+++ b/arrow/compute/scalar_compare_test.go
@@ -19,6 +19,7 @@
package compute_test
import (
+ "bytes"
"context"
"fmt"
"strings"
@@ -1623,6 +1624,135 @@ func (sv *ScalarValiditySuite) TestSlicedInput() {
}
}
+func (sv *ScalarValiditySuite) TestDictionaryNullValues() {
+ values := sv.getArr(arrow.BinaryTypes.String, `["a", null]`)
+ defer values.Release()
+
+ for _, idxType := range []arrow.DataType{
+ arrow.PrimitiveTypes.Int8, arrow.PrimitiveTypes.Uint8,
+ arrow.PrimitiveTypes.Int16, arrow.PrimitiveTypes.Uint16,
+ arrow.PrimitiveTypes.Int32, arrow.PrimitiveTypes.Uint32,
+ arrow.PrimitiveTypes.Int64, arrow.PrimitiveTypes.Uint64,
+ } {
+ sv.Run(idxType.String(), func() {
+ dt := &arrow.DictionaryType{IndexType: idxType,
ValueType: arrow.BinaryTypes.String}
+ indices := sv.getArr(idxType, `[0, 1, null]`)
+ defer indices.Release()
+ check := func(length int, buffers []*memory.Buffer,
nulls int, isNull, isNotNull string) {
+ data := array.NewData(idxType, length, buffers,
nil, nulls, 0)
+ defer data.Release()
+ idx := array.MakeFromData(data)
+ defer idx.Release()
+ dict := array.NewDictionaryArray(dt, idx,
values)
+ defer dict.Release()
+ sv.checkValiditySliced(dict, isNull, isNotNull)
+ }
+
+ check(3, indices.Data().Buffers(), 1, `[false, true,
true]`, `[true, false, false]`)
+ check(2, []*memory.Buffer{nil,
indices.Data().Buffers()[1]}, 0, `[false, true]`, `[true, false]`)
+
+ // the null slot holds -1, or the maximum for unsigned
types
+ width := idxType.(arrow.FixedWidthDataType).BitWidth()
/ 8
+ raw := append([]byte(nil),
indices.Data().Buffers()[1].Bytes()...)
+ copy(raw[2*width:], bytes.Repeat([]byte{0xFF}, width))
+ check(3, []*memory.Buffer{indices.Data().Buffers()[0],
memory.NewBufferBytes(raw)}, 1,
+ `[false, true, true]`, `[true, false, false]`)
+ })
+ }
+}
+
+func (sv *ScalarValiditySuite) TestDictionaryNullValuesSliced() {
+ values := sv.getArr(arrow.BinaryTypes.String,
+ `["x", "x", "x", "x", "x", "x", "x", "x", "a", null, "b",
null]`)
+ defer values.Release()
+ slicedValues := array.NewSlice(values, 8, 12)
+ defer slicedValues.Release()
+ indices := sv.getArr(arrow.PrimitiveTypes.Int32,
+ `[null, 1, null, 3, 0, null, 2, 1, 0, 1, null, 2, 3, null, 0,
0, 3, 1, null, 2]`)
+ defer indices.Release()
+
+ dict := array.NewDictionaryArray(&arrow.DictionaryType{
+ IndexType: arrow.PrimitiveTypes.Int32, ValueType:
arrow.BinaryTypes.String},
+ indices, slicedValues)
+ defer dict.Release()
+ sliced := array.NewSlice(dict, 8, 19)
+ defer sliced.Release()
+
+ sv.checkValiditySliced(sliced,
+ `[false, true, true, false, true, true, false, false, true,
true, true]`,
+ `[true, false, false, true, false, false, true, true, false,
false, false]`)
+}
+
+func (sv *ScalarValiditySuite) TestDictionaryNullValuesScalarAndChunked() {
+ values := sv.getArr(arrow.BinaryTypes.String, `["a", null]`)
+ defer values.Release()
+
+ for _, tt := range []struct {
+ index scalar.Scalar
+ isNull bool
+ }{
+ {scalar.NewInt32Scalar(0), false},
+ {scalar.NewInt32Scalar(1), true},
+ {scalar.MakeNullScalar(arrow.PrimitiveTypes.Int32), true},
+ } {
+ s := scalar.NewDictScalar(tt.index, values)
+ 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(s))
+ sv.Require().NoError(err)
+ got := res.(*compute.ScalarDatum).Value
+
sv.Truef(scalar.Equals(scalar.NewBooleanScalar(expected), got), "%s(%s): got
%s", fn, s, got)
+ res.Release()
+ }
+ s.Release()
+ }
+
+ dt := &arrow.DictionaryType{IndexType: arrow.PrimitiveTypes.Int32,
ValueType: arrow.BinaryTypes.String}
+ indices := sv.getArr(arrow.PrimitiveTypes.Int32, `[0, 1, null]`)
+ defer indices.Release()
+ dict := array.NewDictionaryArray(dt, indices, values)
+ defer dict.Release()
+ tail := array.NewSlice(dict, 1, 3)
+ defer tail.Release()
+ chunked := arrow.NewChunked(dt, []arrow.Array{dict, tail})
+ defer chunked.Release()
+
+ for _, tt := range []struct {
+ fn string
+ expected []string
+ }{
+ {"is_null", []string{`[false, true, true]`, `[true, true]`}},
+ {"is_not_null", []string{`[true, false, false]`, `[false,
false]`}},
+ } {
+ res, err := compute.CallFunction(sv.ctx, tt.fn, nil,
compute.NewDatumWithoutOwning(chunked))
+ sv.Require().NoError(err)
+ got := res.(*compute.ChunkedDatum).Value.Chunks()
+ sv.Require().Len(got, len(tt.expected))
+ for i, expected := range tt.expected {
+ exp := sv.getArr(arrow.FixedWidthTypes.Boolean,
expected)
+ sv.Truef(array.Equal(exp, got[i]), "%s chunk %d:
expected %s, got %s", tt.fn, i, exp, got[i])
+ exp.Release()
+ }
+ res.Release()
+ }
+}
+
+func (sv *ScalarValiditySuite) TestDictionaryNoNullValues() {
+ values := sv.getArr(arrow.BinaryTypes.String, `["a", "b"]`)
+ defer values.Release()
+ indices := sv.getArr(arrow.PrimitiveTypes.Int32, `[0, 1, null]`)
+ defer indices.Release()
+ dict := array.NewDictionaryArray(&arrow.DictionaryType{
+ IndexType: arrow.PrimitiveTypes.Int32, ValueType:
arrow.BinaryTypes.String}, indices, values)
+ defer dict.Release()
+
+ sv.checkValiditySliced(dict, `[false, false, true]`, `[true, true,
false]`)
+
+ res, err := compute.CallFunction(sv.ctx, "is_not_null", nil,
compute.NewDatumWithoutOwning(dict))
+ sv.Require().NoError(err)
+ defer res.Release()
+ sv.Same(dict.Data().Buffers()[0],
res.(*compute.ArrayDatum).Value.Buffers()[1])
+}
+
func (sv *ScalarValiditySuite) TestIsNaN() {
tests := []struct {
in string