Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Jan 24, 2025
1 parent edd7e8f commit 94d3442
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
9 changes: 7 additions & 2 deletions velox/functions/lib/Re2Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,13 @@ bool re2Extract(
result.setNoCopy(row, StringView(extracted.data(), extracted.size()));
return !StringView::isInline(extracted.size());
} else {
result.setNull(row, true);
return false;
if (emptyNoMatch) {
result.setNoCopy(row, StringView(nullptr, 0));
return true;
} else {
result.setNull(row, true);
return false;
}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions velox/functions/lib/tests/Re2FunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ std::shared_ptr<exec::VectorFunction> makeRegexExtract(
return makeRe2Extract(name, inputArgs, config, /*emptyNoMatch=*/false);
}

std::shared_ptr<exec::VectorFunction> makeRegexExtractEmptyNoMatch(
const std::string& name,
const std::vector<exec::VectorFunctionArg>& inputArgs,
const core::QueryConfig& config) {
return makeRe2Extract(name, inputArgs, config, /*emptyNoMatch=*/true);
}

class Re2FunctionsTest : public test::FunctionBaseTest {
public:
static void SetUpTestCase() {
Expand All @@ -54,6 +61,8 @@ class Re2FunctionsTest : public test::FunctionBaseTest {
"re2_search", re2SearchSignatures(), makeRe2Search);
exec::registerStatefulVectorFunction(
"re2_extract", re2ExtractSignatures(), makeRegexExtract);
exec::registerStatefulVectorFunction(
"re2_extract_empty_no_match", re2ExtractSignatures(), makeRegexExtractEmptyNoMatch);
exec::registerStatefulVectorFunction(
"re2_extract_all", re2ExtractAllSignatures(), makeRe2ExtractAll);
exec::registerStatefulVectorFunction("like", likeSignatures(), makeLike);
Expand Down Expand Up @@ -383,6 +392,22 @@ TEST_F(Re2FunctionsTest, regexExtract) {
});
}

template <typename F>
void testRe2ExtractEmptyNoMatch(F&& regexExtract) {
// Group case that mismatch.
EXPECT_EQ(
regexExtract("rat cat\nbat dog", "ra(.)|blah(.)(.)", 2), "");
}

TEST_F(Re2FunctionsTest, regexExtract) {
testRe2ExtractEmptyNoMatch([&](std::optional<std::string> str,
std::optional<std::string> pattern,
std::optional<int> group) {
return evaluateOnce<std::string>(
"re2_extract_empty_no_match(c0, c1, c2)", str, pattern, group);
});
}

TEST_F(Re2FunctionsTest, regexExtractBigintGroupId) {
testRe2Extract([&](std::optional<std::string> str,
std::optional<std::string> pattern,
Expand Down

0 comments on commit 94d3442

Please sign in to comment.