Skip to content
Merged
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
13 changes: 6 additions & 7 deletions cpp/src/gandiva/regex_functions_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const FunctionNode& node) {
}

Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const std::string& sql_pattern) {
std::string pcre_pattern;
ARROW_RETURN_NOT_OK(RegexUtil::SqlLikePatternToPcre(sql_pattern, pcre_pattern));
ARROW_ASSIGN_OR_RAISE(auto pcre_pattern, RegexUtil::SqlLikePatternToPcre(sql_pattern));

RE2::Options regex_op;
regex_op.set_dot_nl(true); // set dotall mode for the regex.
Expand All @@ -148,11 +147,12 @@ Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const std::string& sql_patt
Status::Invalid("The length of escape char ", escape_char,
" in 'like' function is greater than 1"));
std::string pcre_pattern;

if (escape_char.length() == 1) {
ARROW_RETURN_NOT_OK(
RegexUtil::SqlLikePatternToPcre(sql_pattern, escape_char.at(0), pcre_pattern));
ARROW_ASSIGN_OR_RAISE(
pcre_pattern, RegexUtil::SqlLikePatternToPcre(sql_pattern, escape_char.at(0)));
} else {
ARROW_RETURN_NOT_OK(RegexUtil::SqlLikePatternToPcre(sql_pattern, pcre_pattern));
ARROW_ASSIGN_OR_RAISE(pcre_pattern, RegexUtil::SqlLikePatternToPcre(sql_pattern));
}

auto lholder = std::shared_ptr<LikeHolder>(new LikeHolder(pcre_pattern, regex_op));
Expand All @@ -165,8 +165,7 @@ Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const std::string& sql_patt

Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const std::string& sql_pattern,
RE2::Options regex_op) {
std::string pcre_pattern;
ARROW_RETURN_NOT_OK(RegexUtil::SqlLikePatternToPcre(sql_pattern, pcre_pattern));
ARROW_ASSIGN_OR_RAISE(auto pcre_pattern, RegexUtil::SqlLikePatternToPcre(sql_pattern));

auto lholder = std::shared_ptr<LikeHolder>(new LikeHolder(pcre_pattern, regex_op));

Expand Down
5 changes: 2 additions & 3 deletions cpp/src/gandiva/regex_functions_holder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ TEST_F(TestLikeHolder, TestPcreSpecialWithNewLine) {
}

TEST_F(TestLikeHolder, TestRegexEscape) {
std::string res;
ARROW_EXPECT_OK(RegexUtil::SqlLikePatternToPcre("#%hello#_abc_def##", '#', res));

ASSERT_OK_AND_ASSIGN(auto res,
RegexUtil::SqlLikePatternToPcre("#%hello#_abc_def##", '#'));
EXPECT_EQ(res, "%hello_abc.def#");
}

Expand Down
10 changes: 5 additions & 5 deletions cpp/src/gandiva/regex_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ namespace gandiva {
const std::set<char> RegexUtil::pcre_regex_specials_ = {
'[', ']', '(', ')', '|', '^', '-', '+', '*', '?', '{', '}', '$', '\\', '.'};

Status RegexUtil::SqlLikePatternToPcre(const std::string& sql_pattern, char escape_char,
std::string& pcre_pattern) {
/// Characters that are considered special by pcre regex. These needs to be
arrow::Result<std::string> RegexUtil::SqlLikePatternToPcre(const std::string& sql_pattern,
char escape_char) {
/// Characters that are considered special by pcre regex. These need to be
/// escaped with '\\'.
pcre_pattern.clear();
std::string pcre_pattern;
for (size_t idx = 0; idx < sql_pattern.size(); ++idx) {
auto cur = sql_pattern.at(idx);

Expand Down Expand Up @@ -57,7 +57,7 @@ Status RegexUtil::SqlLikePatternToPcre(const std::string& sql_pattern, char esca
pcre_pattern += cur;
}
}
return Status::OK();
return pcre_pattern;
}

} // namespace gandiva
12 changes: 6 additions & 6 deletions cpp/src/gandiva/regex_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
namespace gandiva {

/// \brief Utility class for converting sql patterns to pcre patterns.
class GANDIVA_EXPORT RegexUtil {
class RegexUtil {
public:
// Convert an sql pattern to a pcre pattern
static Status SqlLikePatternToPcre(const std::string& like_pattern, char escape_char,
std::string& pcre_pattern);
static GANDIVA_EXPORT arrow::Result<std::string> SqlLikePatternToPcre(
const std::string& like_pattern, char escape_char);

Comment thread
Reranko05 marked this conversation as resolved.
static Status SqlLikePatternToPcre(const std::string& like_pattern,
std::string& pcre_pattern) {
return SqlLikePatternToPcre(like_pattern, 0 /*escape_char*/, pcre_pattern);
static arrow::Result<std::string> SqlLikePatternToPcre(
const std::string& like_pattern) {
return SqlLikePatternToPcre(like_pattern, '\0' /*escape_char*/);
}

private:
Expand Down
Loading