Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2642,7 +2642,10 @@ struct SliceBytesTransform : StringSliceTransformBase {
// on the resulting slice lengths, so return a worst case estimate.
return input_bytes;
}
int64_t max_slice_bytes = (opt.stop - opt.start + opt.step - 1) / opt.step;
// SliceOptions uses INT64_MIN/MAX for open-ended bounds. Clamp stop before
// arithmetic so equivalent out-of-range indices cannot overflow the size estimate.
const int64_t stop = std::clamp(opt.stop, -input_bytes, input_bytes);
Comment thread
Dricmoy marked this conversation as resolved.
int64_t max_slice_bytes = (stop - opt.start + opt.step - 1) / opt.step;
return std::min(input_bytes, ninputs * std::max<int64_t>(0, max_slice_bytes));
}

Expand Down
25 changes: 25 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,19 @@ TYPED_TEST(TestBinaryKernels, SliceBytesBasic) {
}

TYPED_TEST(TestBinaryKernels, SliceBytesPosPos) {
constexpr auto max = std::numeric_limits<int64_t>::max();
SliceOptions options_default_stop{/*start=*/1};
const std::string long_input(300, 'x');
this->CheckUnary(
"binary_slice", this->template MakeArray<std::string>({"abcdef", long_input}),
this->template MakeArray<std::string>({"bcdef", std::string(299, 'x')}),
&options_default_stop);
Comment thread
Dricmoy marked this conversation as resolved.
SliceOptions options_default_stop_step{/*start=*/1, /*stop=*/max, /*step=*/5};
this->CheckUnary("binary_slice",
this->template MakeArray<std::string>({"abcdef", long_input}),
this->template MakeArray<std::string>({"b", std::string(60, 'x')}),
&options_default_stop_step);

SliceOptions options{2, 4};
this->CheckUnary(
"binary_slice",
Expand Down Expand Up @@ -2748,6 +2761,18 @@ TYPED_TEST(TestBinaryKernels, SliceBytesPosNeg) {
}

TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) {
constexpr auto min = std::numeric_limits<int64_t>::min();
const std::string long_input(300, 'x');
SliceOptions options_min{/*start=*/-1, /*stop=*/min};
this->CheckUnary("binary_slice",
this->template MakeArray<std::string>({"abcdef", long_input}),
this->template MakeArray<std::string>({"", ""}), &options_min);
SliceOptions options_min_step_neg{/*start=*/-1, /*stop=*/min, /*step=*/-5};
this->CheckUnary("binary_slice",
this->template MakeArray<std::string>({"abcdef", long_input}),
this->template MakeArray<std::string>({"fa", std::string(60, 'x')}),
&options_min_step_neg);

SliceOptions options{-2, -1};
this->CheckUnary(
"binary_slice",
Expand Down
Loading