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 d9ab2c86 fix(arrow/compute): decode sliced run-end encoded arrays that
end mid-run (#1620)
d9ab2c86 is described below
commit d9ab2c864d922f65ab691603ebc440b05d58fb78
Author: Shivam Sharma <[email protected]>
AuthorDate: Fri Nov 20 22:07:43 2026 +0530
fix(arrow/compute): decode sliced run-end encoded arrays that end mid-run
(#1620)
### Rationale for this change
`run_end_decode` gives wrong results or panics on a run-end encoded
array that is sliced so it ends before its last run does:
```go
ree, _, _ := array.FromJSON(mem,
arrow.RunEndEncodedOf(arrow.PrimitiveTypes.Int32, arrow.BinaryTypes.String),
strings.NewReader(`["a", "a", "b", "b", "c"]`))
slice := array.NewSlice(ree, 0, 3) // ["a", "a", "b"]
compute.RunEndDecodeArray(ctx, slice)
// panic: runtime error: index out of range [4] with length 4
ree, _, _ = array.FromJSON(mem,
arrow.RunEndEncodedOf(arrow.PrimitiveTypes.Int32, arrow.PrimitiveTypes.Int64),
strings.NewReader(`[null, null, 1, 1, 2]`))
out, _ := compute.RunEndDecodeArray(ctx, array.NewSlice(ree, 0, 3))
// out is [(null) (null) 1], but out.NullN() is 0 instead of 2
```
The decoders find the first run in the slice with `sort.Search`, but
then loop over every remaining run end in the run ends child. Nothing
stops the loop at `offset + length`, so the last run in the slice is
decoded at its full length and every run after it is decoded too. For
string and binary values the writes go past the end of the output
offsets and panic. For boolean values the bitmap writes can panic the
same way. For other fixed width values the extra values land in the
buffer padding when there is room, and the extra runs are counted as
valid, so the output null count is wrong (it can even go negative).
The existing `TestDecodeWithOffset` only slices off the front of the
array, so the slice always ended at the last run end.
### What changes are included in this PR?
`newDecoder` now trims the run ends to the run that holds the last
logical value of the slice, and the four loops in the boolean, fixed
width and binary decoders (including the size computation in
`decodeBinary.PreallocOutput`) clamp each run end to `offset + length`.
These are the only loops over run ends in the kernels package.
### Are these changes tested?
Yes. `TestDecodeSliceEndingMidRun` is added to
`RunEndEncodeDecodeSuite`, so it runs for every value type and run end
type in `TestRunEndFunctions`. It decodes the slices `[0, n-1)` and `[1,
n-1)` and checks the values and the null count against the same slice of
the plain array. On `main`, the `uint32 with nulls` cases fail with the
wrong null count (expected 3, got 2; expected 2, got 1), and the boolean
and string cases panic with `index out of range`. All cases pass with
this change.
`go test ./arrow/compute/ ./arrow/compute/internal/kernels/` passes, as
do runs with `-tags assert,test -short` and with `-tags
assert,test,noasm -short`. `go vet`, gofmt and golangci-lint report
nothing for the changed packages.
### Are there any user-facing changes?
Yes, a bug fix. `RunEndDecode` now works on run-end encoded arrays
sliced so that they end in the middle of a run or before the last run.
AI assistance: I used Claude Code (Anthropic) to find this bug and write
the fix and the test. I reviewed the code and ran the tests locally.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../internal/kernels/vector_run_end_encode.go | 13 +++++++++
arrow/compute/vector_run_end_test.go | 34 ++++++++++++++++++++++
2 files changed, 47 insertions(+)
diff --git a/arrow/compute/internal/kernels/vector_run_end_encode.go
b/arrow/compute/internal/kernels/vector_run_end_encode.go
index faedcfba..c6c43f06 100644
--- a/arrow/compute/internal/kernels/vector_run_end_encode.go
+++ b/arrow/compute/internal/kernels/vector_run_end_encode.go
@@ -607,10 +607,12 @@ func (de *decodeBool[R]) ExpandAllRuns(out
*exec.ExecResult) int64 {
runLength, numValid int64
outputValues = out.Buffers[1].Buf
prevRunEnd = R(de.inputOffset)
+ logicalEnd = R(de.inputOffset + de.inputLen)
hasValidity = len(de.inputValidity) != 0 &&
len(out.Buffers[0].Buf) != 0
)
for i, runEnd := range de.inputRunEnds[de.inputPhysicalOffset:] {
+ runEnd = min(runEnd, logicalEnd)
runLength, prevRunEnd = int64(runEnd-prevRunEnd), runEnd
// if this run is a null, clear the bits and update writeOffset
if hasValidity {
@@ -667,10 +669,12 @@ func (de *decodeFixedWidth[R]) ExpandAllRuns(out
*exec.ExecResult) int64 {
width =
de.valueType.(arrow.FixedWidthDataType).Bytes()
inputValues =
de.inputValues[(de.inputValueOffset+de.inputPhysicalOffset)*int64(width):]
prevRunEnd = R(de.inputOffset)
+ logicalEnd = R(de.inputOffset + de.inputLen)
hasValidity = len(de.inputValidity) != 0 &&
len(out.Buffers[0].Buf) != 0
)
for i, runEnd := range de.inputRunEnds[de.inputPhysicalOffset:] {
+ runEnd = min(runEnd, logicalEnd)
runLength, prevRunEnd = int64(runEnd-prevRunEnd), runEnd
// if this run is a null, clear the bits and update writeOffset
if hasValidity {
@@ -722,10 +726,12 @@ func (de *decodeBinary[R, O]) PreallocOutput(ctx
*exec.KernelCtx, out *exec.Exec
var (
runLength int64
prevRunEnd = R(de.inputLogicalOffset)
+ logicalEnd = R(de.inputLogicalOffset + de.inputLen)
totalSize int
)
for i, runEnd := range de.inputRunEnds[de.inputPhysicalOffset:] {
+ runEnd = min(runEnd, logicalEnd)
runLength, prevRunEnd = int64(runEnd-prevRunEnd), runEnd
start := de.inputOffsets[de.inputPhysicalOffset+int64(i)]
@@ -754,10 +760,12 @@ func (de *decodeBinary[R, O]) ExpandAllRuns(out
*exec.ExecResult) int64 {
outputOffsets = exec.GetSpanOffsets[O](out, 1)
outputValues = out.Buffers[2].Buf
prevRunEnd = R(de.inputLogicalOffset)
+ logicalEnd = R(de.inputLogicalOffset +
de.inputLen)
hasValidity = len(de.inputValidity) != 0 &&
len(out.Buffers[0].Buf) != 0
)
for i, runEnd := range de.inputRunEnds[de.inputPhysicalOffset:] {
+ runEnd = min(runEnd, logicalEnd)
runLength, prevRunEnd = int64(runEnd-prevRunEnd), runEnd
// if this run is a null, clear the bits and update writeOffset
@@ -812,6 +820,11 @@ func newDecoder[R RunEndsType](input *exec.ArraySpan)
decoder {
logicalOffset := R(input.Offset)
runEnds := exec.GetSpanValues[R](&input.Children[0], 1)
physicalOffset := sort.Search(len(runEnds), func(i int) bool { return
runEnds[i] > logicalOffset })
+ // a sliced array can end before its last run does, so stop at the run
+ // holding the last logical value. The decoders clamp that run's end.
+ logicalEnd := R(input.Offset + input.Len)
+ physicalEnd := sort.Search(len(runEnds), func(i int) bool { return
runEnds[i] >= logicalEnd }) + 1
+ runEnds = runEnds[:min(physicalEnd, len(runEnds))]
switch dt := input.Children[1].Type.(type) {
case *arrow.BooleanType:
diff --git a/arrow/compute/vector_run_end_test.go
b/arrow/compute/vector_run_end_test.go
index 90d9e279..aeebb947 100644
--- a/arrow/compute/vector_run_end_test.go
+++ b/arrow/compute/vector_run_end_test.go
@@ -174,6 +174,40 @@ func (suite *RunEndEncodeDecodeSuite)
TestDecodeWithOffset() {
assertDatumsEqual(suite.T(), &compute.ArrayDatum{Value: inputOffset},
result, nil, nil)
}
+func (suite *RunEndEncodeDecodeSuite) TestDecodeSliceEndingMidRun() {
+ // skip chunked examples for ease of testing
+ expected, ok := suite.expected.(*compute.ArrayDatum)
+ if !ok {
+ suite.T().SkipNow()
+ }
+
+ input := suite.input.(*compute.ArrayDatum)
+ if input.Len() < 2 {
+ suite.T().SkipNow()
+ }
+
+ // drop the last logical value so that the slice ends before the
+ // last run end, with and without a leading offset
+ for _, bounds := range [][2]int64{{0, input.Len() - 1}, {1, input.Len()
- 1}} {
+ if bounds[0] >= bounds[1] {
+ continue
+ }
+ suite.Run(fmt.Sprintf("slice[%d:%d]", bounds[0], bounds[1]),
func() {
+ expectedSlice := array.NewSliceData(expected.Value,
bounds[0], bounds[1])
+ defer expectedSlice.Release()
+ inputSlice := array.NewSliceData(input.Value,
bounds[0], bounds[1])
+ defer inputSlice.Release()
+
+ result, err := compute.RunEndDecode(suite.ctx,
&compute.ArrayDatum{Value: expectedSlice})
+ suite.Require().NoError(err)
+ defer result.Release()
+
+ assertDatumsEqual(suite.T(), &compute.ArrayDatum{Value:
inputSlice}, result, nil, nil)
+ suite.Equal(int64(inputSlice.NullN()),
result.(*compute.ArrayDatum).NullN())
+ })
+ }
+}
+
func (suite *RunEndEncodeDecodeSuite) TestDecodeWithChildOffset() {
// artificially add a bunch of nulls to the values child of the
// run-end encoded array both before and after the data and then