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 eac6d152 fix(arrow/compute): take and filter month_day_nano intervals 
(#1669)
eac6d152 is described below

commit eac6d152b438b60f850e41ff37fc27b8af52a3f1
Author: Shivam Sharma <[email protected]>
AuthorDate: Tue Nov 24 06:09:54 2026 +0530

    fix(arrow/compute): take and filter month_day_nano intervals (#1669)
    
    ### Rationale for this change
    
    `compute.TakeArray` and `compute.FilterArray` fail for
    `month_day_nano_interval` arrays:
    
    ```
    invalid: invalid values byte width for take
    type error: invalid values bit width
    ```
    
    The same happens for structs, lists and record batches with such a
    column, and for chunked arrays.
    
    The primitive take and filter kernels are registered with
    `exec.Primitive()`, which also matches the 128-bit
    `INTERVAL_MONTH_DAY_NANO` type, but `PrimitiveTake`,
    `ChunkedPrimitiveTake` and `PrimitiveFilter` only handle bit widths up
    to 64. The output buffers they allocate before returning the error are
    also leaked.
    
    ### What changes are included in this PR?
    
    - `month_day_nano_interval` gets the take and filter kernels built on
    `FSBImpl`, the ones `decimal128` already uses for its 16-byte values.
    They are registered ahead of the primitive kernels, since dispatch takes
    the first kernel that matches.
    - `ChunkedTakeSupported` returns false for this type, so chunked take
    concatenates the values like it does for decimals instead of asking for
    a chunked kernel that the `FSBImpl` kernel doesn't have.
    
    ### Are these changes tested?
    
    Yes, `TestSelectionMonthDayNanoInterval` covers take with null and out
    of bounds indices and sliced values, filter with both null selection
    behaviors, take with chunked values and indices, and take on a struct
    with a `month_day_nano_interval` field, all with a checked allocator.
    Without the fix all four subtests fail with the errors above.
    
    On Windows amd64 with Go 1.26.9, `go test -tags assert,test
    ./arrow/compute/...` and `go test -short -tags assert,test,noasm
    ./arrow/compute/...` pass, and `go vet` is clean for the changed
    packages.
    
    ### Are there any user-facing changes?
    
    Take and filter now work for `month_day_nano_interval` arrays, and for
    the nested types and tables that contain them. No API changes.
    
    This PR was prepared with AI assistance (Claude Code).
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 arrow/compute/internal/kernels/vector_selection.go |   9 ++
 arrow/compute/vector_selection_test.go             | 122 +++++++++++++++++++++
 2 files changed, 131 insertions(+)

diff --git a/arrow/compute/internal/kernels/vector_selection.go 
b/arrow/compute/internal/kernels/vector_selection.go
index 599f3e11..95f7fc07 100644
--- a/arrow/compute/internal/kernels/vector_selection.go
+++ b/arrow/compute/internal/kernels/vector_selection.go
@@ -2389,11 +2389,19 @@ type SelectionKernelData struct {
 }
 
 func ChunkedTakeSupported(dt arrow.DataType) bool {
+       // month_day_nano uses the fixed size kernel, which has no chunked 
variant
+       if dt.ID() == arrow.INTERVAL_MONTH_DAY_NANO {
+               return false
+       }
        return arrow.IsPrimitive(dt.ID()) || arrow.IsBaseBinary(dt.ID())
 }
 
 func GetVectorSelectionKernels() (filterkernels, takeKernels 
[]SelectionKernelData) {
+       // the primitive kernels only handle bit widths up to 64, so the 128-bit
+       // month_day_nano interval is copied as fixed size values instead. These
+       // must come before the primitive kernels, which also match it.
        filterkernels = []SelectionKernelData{
+               {In: exec.NewIDInput(arrow.INTERVAL_MONTH_DAY_NANO), Exec: 
FilterExec(FSBImpl)},
                {In: exec.NewMatchedInput(exec.Primitive()), Exec: 
PrimitiveFilter},
                {In: exec.NewExactInput(arrow.Null), Exec: NullFilter},
                {In: exec.NewIDInput(arrow.DECIMAL128), Exec: 
FilterExec(FSBImpl)},
@@ -2405,6 +2413,7 @@ func GetVectorSelectionKernels() (filterkernels, 
takeKernels []SelectionKernelDa
 
        takeKernels = []SelectionKernelData{
                {In: exec.NewExactInput(arrow.Null), Exec: NullTake},
+               {In: exec.NewIDInput(arrow.INTERVAL_MONTH_DAY_NANO), Exec: 
TakeExec(FSBImpl)},
                {In: exec.NewMatchedInput(exec.Primitive()), Exec: 
PrimitiveTake, Chunked: ChunkedPrimitiveTake},
                {In: exec.NewIDInput(arrow.DECIMAL128), Exec: 
TakeExec(FSBImpl)},
                {In: exec.NewIDInput(arrow.DECIMAL256), Exec: 
TakeExec(FSBImpl)},
diff --git a/arrow/compute/vector_selection_test.go 
b/arrow/compute/vector_selection_test.go
index 717cd633..dd26ee0a 100644
--- a/arrow/compute/vector_selection_test.go
+++ b/arrow/compute/vector_selection_test.go
@@ -2031,6 +2031,128 @@ func TestTakeChecksDenseSlicedIndices(t *testing.T) {
        }
 }
 
+func TestSelectionMonthDayNanoInterval(t *testing.T) {
+       dt := arrow.FixedWidthTypes.MonthDayNanoInterval
+       values := `[{"months": 1, "days": 2, "nanoseconds": 3}, null,
+               {"months": -4, "days": 5, "nanoseconds": -6}, {"months": 7, 
"days": -8, "nanoseconds": 9}]`
+
+       fromJSON := func(t *testing.T, mem memory.Allocator, dt arrow.DataType, 
str string) arrow.Array {
+               arr, _, err := array.FromJSON(mem, dt, strings.NewReader(str))
+               require.NoError(t, err)
+               return arr
+       }
+
+       t.Run("take", func(t *testing.T) {
+               mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+               defer mem.AssertSize(t, 0)
+               ctx := compute.WithAllocator(context.Background(), mem)
+
+               vals := fromJSON(t, mem, dt, values)
+               defer vals.Release()
+               indices := fromJSON(t, mem, arrow.PrimitiveTypes.Int32, `[3, 
null, 1, 0, 0]`)
+               defer indices.Release()
+               expected := fromJSON(t, mem, dt, `[{"months": 7, "days": -8, 
"nanoseconds": 9}, null, null,
+                       {"months": 1, "days": 2, "nanoseconds": 3}, {"months": 
1, "days": 2, "nanoseconds": 3}]`)
+               defer expected.Release()
+
+               out, err := compute.TakeArray(ctx, vals, indices)
+               require.NoError(t, err)
+               defer out.Release()
+               assertArraysEqual(t, expected, out)
+
+               // sliced values
+               sliced := array.NewSlice(vals, 2, 4)
+               defer sliced.Release()
+               indices2 := fromJSON(t, mem, arrow.PrimitiveTypes.Int8, `[1, 
0]`)
+               defer indices2.Release()
+               expected2 := fromJSON(t, mem, dt, `[{"months": 7, "days": -8, 
"nanoseconds": 9},
+                       {"months": -4, "days": 5, "nanoseconds": -6}]`)
+               defer expected2.Release()
+               out2, err := compute.TakeArray(ctx, sliced, indices2)
+               require.NoError(t, err)
+               defer out2.Release()
+               assertArraysEqual(t, expected2, out2)
+
+               badIndices := fromJSON(t, mem, arrow.PrimitiveTypes.Int32, `[0, 
4]`)
+               defer badIndices.Release()
+               _, err = compute.TakeArray(ctx, vals, badIndices)
+               assert.ErrorIs(t, err, arrow.ErrIndex)
+       })
+
+       t.Run("filter", func(t *testing.T) {
+               mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+               defer mem.AssertSize(t, 0)
+               ctx := compute.WithAllocator(context.Background(), mem)
+
+               vals := fromJSON(t, mem, dt, values)
+               defer vals.Release()
+               filter := fromJSON(t, mem, arrow.FixedWidthTypes.Boolean, 
`[true, true, null, false]`)
+               defer filter.Release()
+
+               out, err := compute.FilterArray(ctx, vals, filter, 
*compute.DefaultFilterOptions())
+               require.NoError(t, err)
+               defer out.Release()
+               expected := fromJSON(t, mem, dt, `[{"months": 1, "days": 2, 
"nanoseconds": 3}, null]`)
+               defer expected.Release()
+               assertArraysEqual(t, expected, out)
+
+               out2, err := compute.FilterArray(ctx, vals, filter, 
compute.FilterOptions{NullSelection: compute.SelectionEmitNulls})
+               require.NoError(t, err)
+               defer out2.Release()
+               expected2 := fromJSON(t, mem, dt, `[{"months": 1, "days": 2, 
"nanoseconds": 3}, null, null]`)
+               defer expected2.Release()
+               assertArraysEqual(t, expected2, out2)
+       })
+
+       t.Run("chunked", func(t *testing.T) {
+               mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+               defer mem.AssertSize(t, 0)
+               ctx := compute.WithAllocator(context.Background(), mem)
+
+               chunked, err := array.ChunkedFromJSON(mem, dt, []string{
+                       `[{"months": 1, "days": 2, "nanoseconds": 3}, null]`,
+                       `[{"months": 7, "days": -8, "nanoseconds": 9}]`})
+               require.NoError(t, err)
+               defer chunked.Release()
+               indices, err := array.ChunkedFromJSON(mem, 
arrow.PrimitiveTypes.Int8, []string{`[2, 0]`, `[1]`})
+               require.NoError(t, err)
+               defer indices.Release()
+
+               result, err := compute.Take(ctx, *compute.DefaultTakeOptions(),
+                       &compute.ChunkedDatum{Value: chunked}, 
&compute.ChunkedDatum{Value: indices})
+               require.NoError(t, err)
+               defer result.Release()
+
+               out := result.(*compute.ChunkedDatum).Value
+               expected, err := array.ChunkedFromJSON(mem, dt, []string{
+                       `[{"months": 7, "days": -8, "nanoseconds": 9}, 
{"months": 1, "days": 2, "nanoseconds": 3}]`, `[null]`})
+               require.NoError(t, err)
+               defer expected.Release()
+               assert.Truef(t, array.ChunkedEqual(expected, out), "expected 
%s, got %s", expected, out)
+       })
+
+       t.Run("struct", func(t *testing.T) {
+               mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+               defer mem.AssertSize(t, 0)
+               ctx := compute.WithAllocator(context.Background(), mem)
+
+               st := arrow.StructOf(arrow.Field{Name: "a", Type: dt, Nullable: 
true})
+               vals := fromJSON(t, mem, st, `[{"a": {"months": 1, "days": 2, 
"nanoseconds": 3}}, null,
+                       {"a": null}, {"a": {"months": 7, "days": -8, 
"nanoseconds": 9}}]`)
+               defer vals.Release()
+               indices := fromJSON(t, mem, arrow.PrimitiveTypes.Int32, `[3, 2, 
1, 0]`)
+               defer indices.Release()
+               expected := fromJSON(t, mem, st, `[{"a": {"months": 7, "days": 
-8, "nanoseconds": 9}}, {"a": null},
+                       null, {"a": {"months": 1, "days": 2, "nanoseconds": 
3}}]`)
+               defer expected.Release()
+
+               out, err := compute.TakeArray(ctx, vals, indices)
+               require.NoError(t, err)
+               defer out.Release()
+               assertArraysEqual(t, expected, out)
+       })
+}
+
 func TestTakeKernels(t *testing.T) {
        suite.Run(t, new(TakeKernelTest))
        for _, dt := range numericTypes {

Reply via email to