Skip to content

Commit a2a9dce

Browse files
GH-50524: [C++] Honor array offset in pairwise_diff (#50858)
### Rationale for this change `ArraySpan::SetSlice` replaces the offset. `pairwise_diff` copied the input span and then sliced from `left_start`/`right_start`, so a sliced array was read from the parent buffer. With `[99, 1, 4, 9, 16]` sliced to `[1, 4, 9, 16]`, period=1 produced `[-98, 3, 5, 7]` instead of `[null, 3, 5, 7]`. ### What changes are included in this PR? The kernel passes `input.offset + left_start` (and the same for the right side). The regression test uses a sliced int64 array for both period signs, on `pairwise_diff` and `pairwise_diff_checked`. ### Are these changes tested? Yes, `TestPairwiseDiff.SlicedInput` in `vector_pairwise_test.cc`. ### Are there any user-facing changes? `pairwise_diff` on a sliced array now diffs the sliced values. Callers who passed a slice and got parent-buffer values will see different (correct) output. * GitHub Issue: #50524 Authored-by: Krishnanand G <118352827+Krishnanand-G@users.noreply.github.com> Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent 9b96d70 commit a2a9dce

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

cpp/src/arrow/compute/kernels/vector_pairwise.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ Status PairwiseExecImpl(KernelContext* ctx, const ArraySpan& input,
7373
}
7474
result->null_count = null_count;
7575
// prepare input span
76+
// SetSlice overwrites offset. Keep the input's offset so a sliced
77+
// array is not read from the start of the parent buffer.
7678
ArraySpan left(input);
77-
left.SetSlice(left_start, computed_length);
79+
left.SetSlice(input.offset + left_start, computed_length);
7880
ArraySpan right(input);
79-
right.SetSlice(right_start, computed_length);
81+
right.SetSlice(input.offset + right_start, computed_length);
8082
// prepare output span
8183
ArraySpan output_span;
8284
output_span.SetMembers(*result);

cpp/src/arrow/compute/kernels/vector_pairwise_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ TEST_F(TestPairwiseDiff, Numeric) {
151151
}
152152
}
153153

154+
TEST_F(TestPairwiseDiff, SlicedInput) {
155+
// Slice() keeps a nonzero offset into the parent buffer. The kernel
156+
// used to treat that offset as zero and read values before the slice.
157+
auto base = ArrayFromJSON(int64(), "[99, 1, 4, 9, 16, 88]");
158+
auto sliced = base->Slice(1, 4);
159+
160+
{
161+
PairwiseOptions options(1);
162+
auto expected = ArrayFromJSON(int64(), "[null, 3, 5, 7]");
163+
CheckVectorUnary("pairwise_diff", sliced, expected, &options);
164+
CheckVectorUnary("pairwise_diff_checked", sliced, expected, &options);
165+
}
166+
{
167+
PairwiseOptions options(-1);
168+
auto expected = ArrayFromJSON(int64(), "[-3, -5, -7, null]");
169+
CheckVectorUnary("pairwise_diff", sliced, expected, &options);
170+
CheckVectorUnary("pairwise_diff_checked", sliced, expected, &options);
171+
}
172+
}
173+
154174
TEST_F(TestPairwiseDiff, Overflow) {
155175
{
156176
PairwiseOptions options(1);

0 commit comments

Comments
 (0)