diff --git a/cpp/src/gandiva/regex_functions_holder.cc b/cpp/src/gandiva/regex_functions_holder.cc index 1c9e44d61b23..6c0c3d40f127 100644 --- a/cpp/src/gandiva/regex_functions_holder.cc +++ b/cpp/src/gandiva/regex_functions_holder.cc @@ -128,8 +128,7 @@ Result> LikeHolder::Make(const FunctionNode& node) { } Result> 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. @@ -148,11 +147,12 @@ Result> 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(new LikeHolder(pcre_pattern, regex_op)); @@ -165,8 +165,7 @@ Result> LikeHolder::Make(const std::string& sql_patt Result> 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(new LikeHolder(pcre_pattern, regex_op)); diff --git a/cpp/src/gandiva/regex_functions_holder_test.cc b/cpp/src/gandiva/regex_functions_holder_test.cc index d0cef5068749..4d7b0fd3192d 100644 --- a/cpp/src/gandiva/regex_functions_holder_test.cc +++ b/cpp/src/gandiva/regex_functions_holder_test.cc @@ -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#"); } diff --git a/cpp/src/gandiva/regex_util.cc b/cpp/src/gandiva/regex_util.cc index abdd579d1f5e..5d68cefef859 100644 --- a/cpp/src/gandiva/regex_util.cc +++ b/cpp/src/gandiva/regex_util.cc @@ -22,11 +22,11 @@ namespace gandiva { const std::set 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 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); @@ -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 diff --git a/cpp/src/gandiva/regex_util.h b/cpp/src/gandiva/regex_util.h index cf0002b8cdf2..baaf8212bd7d 100644 --- a/cpp/src/gandiva/regex_util.h +++ b/cpp/src/gandiva/regex_util.h @@ -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 SqlLikePatternToPcre( + const std::string& like_pattern, char escape_char); - static Status SqlLikePatternToPcre(const std::string& like_pattern, - std::string& pcre_pattern) { - return SqlLikePatternToPcre(like_pattern, 0 /*escape_char*/, pcre_pattern); + static arrow::Result SqlLikePatternToPcre( + const std::string& like_pattern) { + return SqlLikePatternToPcre(like_pattern, '\0' /*escape_char*/); } private: