diff --git a/cpp/src/arrow/compute/kernels/scalar_string.cc b/cpp/src/arrow/compute/kernels/scalar_string.cc index be1cc83d158..65196b2a491 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string.cc @@ -1332,7 +1332,7 @@ struct ReplaceSubString { if (batch[0].kind() == Datum::ARRAY) { // We already know how many strings we have, so we can use Reserve/UnsafeAppend - RETURN_NOT_OK(offset_builder.Reserve(batch[0].array()->length)); + RETURN_NOT_OK(offset_builder.Reserve(batch[0].array()->length + 1)); offset_builder.UnsafeAppend(0); // offsets start at 0 const ArrayData& input = *batch[0].array(); diff --git a/cpp/src/arrow/compute/kernels/scalar_string_test.cc b/cpp/src/arrow/compute/kernels/scalar_string_test.cc index cb74b1449b5..a59634b7be8 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_test.cc @@ -499,6 +499,14 @@ TYPED_TEST(TestStringKernels, ReplaceSubstringRegex) { ReplaceSubstringOptions options_regex2{"(a.a)", "aba\\1"}; this->CheckUnary("replace_substring_regex", R"(["aaaaaa"])", this->type(), R"(["abaaaaabaaaa"])", &options_regex2); + + // ARROW-12774 + ReplaceSubstringOptions options_regex3{"X", "Y"}; + this->CheckUnary("replace_substring_regex", + R"(["A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A"])", + this->type(), + R"(["A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A"])", + &options_regex3); } TYPED_TEST(TestStringKernels, ReplaceSubstringRegexLimited) {