Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: regexp_split fails in empty match pattern #12305

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
18 changes: 12 additions & 6 deletions velox/functions/lib/Re2Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,14 @@ struct Re2RegexpSplit {

const auto re2String = re2::StringPiece(string.data(), string.size());

size_t pos = 0;
size_t currPos = 0;
size_t lastPos = 0;
const char* start = string.data();

re2::StringPiece subMatches[1];
while (re->Match(
re2String,
pos,
currPos,
string.size(),
RE2::Anchor::UNANCHORED,
subMatches,
Expand All @@ -423,16 +424,21 @@ struct Re2RegexpSplit {
const auto offset = fullMatch.data() - start;
const auto size = fullMatch.size();

out.add_item().setNoCopy(StringView(string.data() + pos, offset - pos));
out.add_item().setNoCopy(
StringView(string.data() + lastPos, offset - lastPos));

pos = offset + size;
currPos = offset + size;
lastPos = currPos;
// Only change currPos if encounters empty string match. The lastPos is
// not changed to make sure the produced string has correct position
// when encountering empty string matches in next match.
if (UNLIKELY(size == 0)) {
++pos;
++currPos;
}
}

out.add_item().setNoCopy(
StringView(string.data() + pos, string.size() - pos));
StringView(string.data() + lastPos, string.size() - lastPos));
}

private:
Expand Down
11 changes: 11 additions & 0 deletions velox/functions/lib/tests/Re2FunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,17 @@ TEST_F(Re2FunctionsTest, split) {
{"a", "b"},
});
assertEqualVectors(expected, result);

input = makeRowVector({
makeFlatVector<std::string>({
"abcd",
}),
});
result = evaluate("regexp_split(c0, '')", input);
expected = makeArrayVector<std::string>({
{"", "a", "b", "c", "d", ""},
});
assertEqualVectors(expected, result);
}

TEST_F(Re2FunctionsTest, parseSubstrings) {
Expand Down
Loading