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
32 changes: 30 additions & 2 deletions cpp/src/arrow/compute/kernels/scalar_string_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,27 @@ struct StringSplitExec {
// Keep the temporary storage across individual values, to minimize reallocations
std::vector<std::string_view> parts;
Options options;
bool is_utf8 = false;

explicit StringSplitExec(const Options& options) : options(options) {}

// Advance by one codepoint for UTF-8 input, by one byte otherwise.
const uint8_t* NextCharacter(const uint8_t* p, const uint8_t* end) const {
++p;
if (is_utf8) {
while (p < end && (*p & 0xC0) == 0x80) {
++p;
}
}
return p;
}

static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
return StringSplitExec{State::Get(ctx)}.Execute(ctx, batch, out);
}

Status Execute(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
const bool is_utf8 = is_string_or_string_view(batch[0].type()->id());
is_utf8 = is_string_or_string_view(batch[0].type()->id());
SplitFinder finder;
RETURN_NOT_OK(finder.PreExec(options, is_utf8));
// TODO(wesm): refactor to not require creating ArrayData
Expand Down Expand Up @@ -401,14 +413,30 @@ struct StringSplitExec {
}
} else {
const uint8_t* i = begin;
// Where to search for the next separator. This is ahead of `i` only after
// an empty separator was skipped (only possible with regex patterns).
const uint8_t* search_from = begin;
bool last_separator_empty = false;
while (max_splits != 0) {
const uint8_t *separator_begin, *separator_end;
// find with whatever algo the part we will 'cut out'
if (finder->Find(i, end, &separator_begin, &separator_end, options)) {
if (finder->Find(search_from, end, &separator_begin, &separator_end, options)) {
if (separator_begin == separator_end && separator_begin == i &&
last_separator_empty) {
// Like Python's re.split, an empty separator is not allowed right
// after another empty separator: search again from the next character.
if (search_from == end) {
break;
}
search_from = NextCharacter(search_from, end);
continue;
}
// the part till the beginning of the 'cut'
RETURN_NOT_OK(
builder->Append(i, static_cast<string_offset_type>(separator_begin - i)));
i = separator_end;
search_from = separator_end;
last_separator_empty = separator_begin == separator_end;
max_splits--;
} else {
// if we cannot find a separator, we're done
Expand Down
30 changes: 30 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,36 @@ TYPED_TEST(TestBaseBinaryKernels, SplitRegex) {
&options);
}

TYPED_TEST(TestBaseBinaryKernels, SplitRegexEmptyMatch) {
// GH-55842: patterns that can match the empty string used to loop forever
SplitPatternOptions options{""};
this->CheckUnary("split_pattern_regex", R"(["abc", "", null])", list(this->type()),
R"([["", "a", "b", "c", ""], ["", ""], null])", &options);

options.pattern = "a*";
this->CheckUnary("split_pattern_regex", R"(["bbb", "axbc", "baab"])",
list(this->type()),
R"([["", "b", "b", "b", ""], ["", "", "x", "b", "c", ""],
["", "b", "", "b", ""]])",
&options);

options.pattern = "x*";
this->CheckUnary("split_pattern_regex", R"(["axbc"])", list(this->type()),
R"([["", "a", "", "b", "c", ""]])", &options);

options.pattern = "";
options.max_splits = 2;
this->CheckUnary("split_pattern_regex", R"(["abc"])", list(this->type()),
R"([["", "a", "bc"]])", &options);
}

TYPED_TEST(TestStringKernels, SplitRegexEmptyMatchUtf8) {
// An empty match must advance by a whole codepoint, not a single byte
SplitPatternOptions options{""};
this->CheckUnary("split_pattern_regex", R"(["πaθ"])", list(this->type()),
R"([["", "π", "a", "θ", ""]])", &options);
}

TYPED_TEST(TestBaseBinaryKernels, SplitRegexReverse) {
SplitPatternOptions options{"a+|b", /*max_splits=*/1, /*reverse=*/true};
Datum input = ArrayFromJSON(this->type(), R"(["a"])");
Expand Down
12 changes: 12 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,18 @@ def test_split_pattern_regex():
result = pc.split_pattern_regex(
arr, pattern="---", max_splits=1, reverse=True)

# GH-55842: patterns matching the empty string used to hang
arr = pa.array(["abc", "axbc", ""])
result = pc.split_pattern_regex(arr, pattern="")
expected = pa.array([["", "a", "b", "c", ""],
["", "a", "x", "b", "c", ""], ["", ""]])
assert expected.equals(result)

result = pc.split_pattern_regex(arr, pattern="x*")
expected = pa.array([["", "a", "b", "c", ""],
["", "a", "", "b", "c", ""], ["", ""]])
assert expected.equals(result)


def test_min_max():
# An example generated function wrapper with possible options
Expand Down