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 5ddf2b4a fix(arrow/compute): honor the input offset in not (#1458)
5ddf2b4a is described below

commit 5ddf2b4a9c17e4c4dbda568012e2e94634e39246
Author: Harrison Crosse <[email protected]>
AuthorDate: Wed Nov 4 12:56:06 2026 -0500

    fix(arrow/compute): honor the input offset in not (#1458)
    
    ### Rationale for this change
    
    `not` on a sliced boolean array can put nulls on the wrong rows. For
    example, `[null, true, null, false]` sliced from offset 1 produces
    `[(null) true (null)]` instead of `[false (null) true]`, with no error.
    
    ### What changes are included in this PR?
    
    `not` now uses `NullIntersection` while keeping `MemPrealloc`, like
    `and`, `or`, `xor` and `and_not`. Arrow C++'s
    
[`invert`](https://github.com/apache/arrow/blob/0ab78e2df489055b71a7d26a666a52823f87f01c/cpp/src/arrow/compute/kernels/scalar_boolean.cc#L488)
    uses the same null handling with preallocation. The executor already
    handles input offsets, chunked input and exec chunks for these kernels,
    so `not` no longer needs its own validity code. The kernel now only
    inverts values.
    
    For unchunked input with nulls, each call now allocates and copies one
    validity bitmap of `ceil(n/8)` bytes, where an offset-0 input could
    previously share its buffer. Unchunked input without nulls gets no
    validity bitmap. Chunked input always gets a preallocated validity
    bitmap, even when no chunk has nulls, and now comes back as one
    contiguous array.
    
    ### Are these changes tested?
    
    I added `TestBooleanNotSliced`, which covers offsets 0, 1 and 9, an
    array without a validity buffer sliced at offset 7, chunked input with
    one chunk lacking a validity buffer, and a small `ChunkSize`. On main,
    offsets 1 and 9, `chunked` and `small exec chunks` fail.
    
    On Linux amd64, `go test -count=1 ./arrow/compute/...`, `go vet`,
    `gofmt` and `golangci-lint` passed, along with the `prek` hooks on the
    changed files, including the Release Audit Tool.
    
    ### Are there any user-facing changes?
    
    Yes. `not` returns the correct nulls on sliced boolean arrays. Chunked
    input now comes back as a single chunk instead of one chunk per input
    chunk, as with `and`, `or` and `xor`, so callers that pair output chunks
    with input chunks will see a change. There is no API change.
    
    Closes https://github.com/apache/arrow-go/issues/1455.
---
 arrow/compute/internal/kernels/scalar_boolean.go |  7 --
 arrow/compute/scalar_bool.go                     |  2 +-
 arrow/compute/scalar_bool_test.go                | 84 ++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 8 deletions(-)

diff --git a/arrow/compute/internal/kernels/scalar_boolean.go 
b/arrow/compute/internal/kernels/scalar_boolean.go
index 36598a09..49a8f4c5 100644
--- a/arrow/compute/internal/kernels/scalar_boolean.go
+++ b/arrow/compute/internal/kernels/scalar_boolean.go
@@ -336,12 +336,5 @@ func (KleeneAndNotOpKernel) CallScalarRight(ctx 
*exec.KernelCtx, left *exec.Arra
 func NotExecKernel(ctx *exec.KernelCtx, batch *exec.ExecSpan, out 
*exec.ExecResult) error {
        bitutil.InvertBitmap(batch.Values[0].Array.Buffers[1].Buf, 
int(batch.Values[0].Array.Offset),
                int(batch.Values[0].Array.Len), out.Buffers[1].Buf, 
int(out.Offset))
-
-       out.Buffers[0] = batch.Values[0].Array.Buffers[0]
-       if out.Buffers[0].SelfAlloc {
-               out.Buffers[0].SelfAlloc = false
-       }
-       out.Nulls = batch.Values[0].Array.Nulls
-
        return nil
 }
diff --git a/arrow/compute/scalar_bool.go b/arrow/compute/scalar_bool.go
index 9a64382a..1f4b6734 100644
--- a/arrow/compute/scalar_bool.go
+++ b/arrow/compute/scalar_bool.go
@@ -136,5 +136,5 @@ func RegisterScalarBoolean(reg FunctionRegistry) {
        makeFunction(reg, "or_kleene", 2, 
kernels.SimpleBinary[kernels.KleeneOrOpKernel],
                orKleeneDoc, exec.NullComputedPrealloc)
        makeFunction(reg, "not", 1, kernels.NotExecKernel, notDoc,
-               exec.NullComputedNoPrealloc)
+               exec.NullIntersection)
 }
diff --git a/arrow/compute/scalar_bool_test.go 
b/arrow/compute/scalar_bool_test.go
index 78db5de6..15ea5996 100644
--- a/arrow/compute/scalar_bool_test.go
+++ b/arrow/compute/scalar_bool_test.go
@@ -20,6 +20,7 @@ package compute_test
 
 import (
        "context"
+       "fmt"
        "strings"
        "testing"
 
@@ -180,3 +181,86 @@ func TestBooleanNot(t *testing.T) {
                        compute.NewDatumWithoutOwning(expected), nil)
        }
 }
+
+func TestBooleanNotSliced(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       defer mem.AssertSize(t, 0)
+       ctx := compute.WithAllocator(context.Background(), mem)
+
+       check := func(t *testing.T, ctx context.Context, input compute.Datum, 
expectedJSON string) {
+               expected, _, err := array.FromJSON(mem, 
arrow.FixedWidthTypes.Boolean, strings.NewReader(expectedJSON))
+               require.NoError(t, err)
+               defer expected.Release()
+
+               result, err := compute.CallFunction(ctx, "not", nil, input)
+               require.NoError(t, err)
+               defer result.Release()
+
+               var actual arrow.Array
+               switch result := result.(type) {
+               case *compute.ArrayDatum:
+                       actual = result.MakeArray()
+               case *compute.ChunkedDatum:
+                       actual, err = array.Concatenate(result.Value.Chunks(), 
mem)
+                       require.NoError(t, err)
+               default:
+                       t.Fatalf("unexpected result kind %s", result.Kind())
+               }
+               defer actual.Release()
+
+               assertArraysEqual(t, expected, actual)
+       }
+
+       arr, _, err := array.FromJSON(mem, arrow.FixedWidthTypes.Boolean, 
strings.NewReader(
+               `[null, true, null, false, true, null, false, true, false, 
false, false, null, null]`))
+       require.NoError(t, err)
+       defer arr.Release()
+
+       for _, tt := range []struct {
+               offset   int64
+               expected string
+       }{
+               {0, `[null, false, null, true]`},
+               {1, `[false, null, true, false]`},
+               {9, `[true, true, null, null]`},
+       } {
+               t.Run(fmt.Sprintf("offset=%d", tt.offset), func(t *testing.T) {
+                       sliced := array.NewSlice(arr, tt.offset, tt.offset+4)
+                       defer sliced.Release()
+                       check(t, ctx, compute.NewDatumWithoutOwning(sliced), 
tt.expected)
+               })
+       }
+
+       noValidity := func(offset, length int) arrow.Array {
+               data := array.NewData(arrow.FixedWidthTypes.Boolean, length,
+                       []*memory.Buffer{nil, arr.Data().Buffers()[1]}, nil, 0, 
offset)
+               defer data.Release()
+               return array.MakeFromData(data)
+       }
+
+       t.Run("no validity buffer/offset=7", func(t *testing.T) {
+               input := noValidity(7, 4)
+               defer input.Release()
+               check(t, ctx, compute.NewDatumWithoutOwning(input), `[false, 
true, true, true]`)
+       })
+
+       t.Run("chunked", func(t *testing.T) {
+               first := array.NewSlice(arr, 1, 4)
+               defer first.Release()
+               second := noValidity(6, 5)
+               defer second.Release()
+               chunked := arrow.NewChunked(arrow.FixedWidthTypes.Boolean, 
[]arrow.Array{first, second})
+               defer chunked.Release()
+               check(t, ctx, compute.NewDatumWithoutOwning(chunked),
+                       `[false, null, true, true, false, true, true, true]`)
+       })
+
+       t.Run("small exec chunks", func(t *testing.T) {
+               ectx := compute.DefaultExecCtx()
+               ectx.ChunkSize = 3
+               sliced := array.NewSlice(arr, 1, 12)
+               defer sliced.Release()
+               check(t, compute.SetExecCtx(ctx, ectx), 
compute.NewDatumWithoutOwning(sliced),
+                       `[false, null, true, false, null, true, false, true, 
true, true, null]`)
+       })
+}

Reply via email to