Skip to content

Commit c4a0850

Browse files
author
water
authored
GH-51018: [C++] Fix indices_nonzero segfault on chunkless ChunkedArray (#51021)
### Rationale for this change `pyarrow.compute.indices_nonzero()` segfaults (SIGSEGV) on a `ChunkedArray` with zero chunks. A zero-length `Array` and a `ChunkedArray` holding one empty chunk both work; only the chunkless case crashes. ### Are these changes tested? Yes, by new test case. ### Are there any user-facing changes? Only a bugfix. * GitHub Issue: #51018 Authored-by: waterWang <[email protected]> Signed-off-by: Rok Mihevc <[email protected]>
1 parent 875cd2e commit c4a0850

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎cpp/src/arrow/compute/kernels/vector_selection.cc‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ Status DoNonZero(const std::vector<ArraySpan>& arrays, int64_t total_length,
264264
UInt64Builder builder;
265265
RETURN_NOT_OK(builder.Reserve(total_length));
266266

267-
NonZeroVisitor visitor(&builder, arrays);
268-
RETURN_NOT_OK(VisitTypeInline(*arrays[0].type, &visitor));
267+
if (!arrays.empty()) {
268+
NonZeroVisitor visitor(&builder, arrays);
269+
RETURN_NOT_OK(VisitTypeInline(*arrays[0].type, &visitor));
270+
}
269271
return builder.FinishInternal(out);
270272
}
271273

‎cpp/src/arrow/compute/kernels/vector_selection_test.cc‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,6 +2920,14 @@ TEST(TestIndicesNonZero, IndicesNonZero) {
29202920
actual, CallFunction("indices_nonzero", {static_cast<Datum>(chunked_arr_empty)}));
29212921
AssertArraysEqual(*ArrayFromJSON(uint64(), "[0, 2, 3, 5]"), *actual.make_array(),
29222922
/*verbose*/ true);
2923+
2924+
// chunked with zero chunks (GH-51018)
2925+
ChunkedArray chunked_arr_no_chunks({}, type);
2926+
ASSERT_OK_AND_ASSIGN(
2927+
actual,
2928+
CallFunction("indices_nonzero", {static_cast<Datum>(chunked_arr_no_chunks)}));
2929+
AssertArraysEqual(*ArrayFromJSON(uint64(), "[]"), *actual.make_array(),
2930+
/*verbose*/ true);
29232931
}
29242932
}
29252933

0 commit comments

Comments
 (0)