Skip to content

Commit c13f3bc

Browse files
authored
GH-19667: [C++][Gandiva] Use arrow::Result<std::string> for RegexUtil::SqlLikePatternToPcre (#49879)
### Rationale for this change Replace the `Status` + out-parameter pattern in `RegexUtil::SqlLikePatternToPcre` with `arrow::Result<std::string>`, aligning with modern Arrow API practices. ### What changes are included in this PR? * Updated `RegexUtil::SqlLikePatternToPcre` to return `arrow::Result<std::string>` instead of using an out-parameter * Updated call sites in `regex_functions_holder.cc` to use `ARROW_ASSIGN_OR_RAISE` * Updated tests in `regex_functions_holder_test.cc` to use `ASSERT_OK_AND_ASSIGN` * Removed `GANDIVA_EXPORT` from the inline wrapper to avoid Windows `dllimport` issues * Simplified control flow in `regex_functions_holder.cc` ### Are these changes tested? * Rebuilt with `ninja gandiva` * Ran Gandiva tests successfully ### Are there any user-facing changes? Yes. * GitHub Issue: #19667 Authored-by: Aaditya Srinivasan <aadityasri03@gmail.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 9abd6d3 commit c13f3bc

4 files changed

Lines changed: 19 additions & 21 deletions

File tree

cpp/src/gandiva/regex_functions_holder.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const FunctionNode& node) {
128128
}
129129

130130
Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const std::string& sql_pattern) {
131-
std::string pcre_pattern;
132-
ARROW_RETURN_NOT_OK(RegexUtil::SqlLikePatternToPcre(sql_pattern, pcre_pattern));
131+
ARROW_ASSIGN_OR_RAISE(auto pcre_pattern, RegexUtil::SqlLikePatternToPcre(sql_pattern));
133132

134133
RE2::Options regex_op;
135134
regex_op.set_dot_nl(true); // set dotall mode for the regex.
@@ -148,11 +147,12 @@ Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const std::string& sql_patt
148147
Status::Invalid("The length of escape char ", escape_char,
149148
" in 'like' function is greater than 1"));
150149
std::string pcre_pattern;
150+
151151
if (escape_char.length() == 1) {
152-
ARROW_RETURN_NOT_OK(
153-
RegexUtil::SqlLikePatternToPcre(sql_pattern, escape_char.at(0), pcre_pattern));
152+
ARROW_ASSIGN_OR_RAISE(
153+
pcre_pattern, RegexUtil::SqlLikePatternToPcre(sql_pattern, escape_char.at(0)));
154154
} else {
155-
ARROW_RETURN_NOT_OK(RegexUtil::SqlLikePatternToPcre(sql_pattern, pcre_pattern));
155+
ARROW_ASSIGN_OR_RAISE(pcre_pattern, RegexUtil::SqlLikePatternToPcre(sql_pattern));
156156
}
157157

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

166166
Result<std::shared_ptr<LikeHolder>> LikeHolder::Make(const std::string& sql_pattern,
167167
RE2::Options regex_op) {
168-
std::string pcre_pattern;
169-
ARROW_RETURN_NOT_OK(RegexUtil::SqlLikePatternToPcre(sql_pattern, pcre_pattern));
168+
ARROW_ASSIGN_OR_RAISE(auto pcre_pattern, RegexUtil::SqlLikePatternToPcre(sql_pattern));
170169

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

cpp/src/gandiva/regex_functions_holder_test.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ TEST_F(TestLikeHolder, TestPcreSpecialWithNewLine) {
8888
}
8989

9090
TEST_F(TestLikeHolder, TestRegexEscape) {
91-
std::string res;
92-
ARROW_EXPECT_OK(RegexUtil::SqlLikePatternToPcre("#%hello#_abc_def##", '#', res));
93-
91+
ASSERT_OK_AND_ASSIGN(auto res,
92+
RegexUtil::SqlLikePatternToPcre("#%hello#_abc_def##", '#'));
9493
EXPECT_EQ(res, "%hello_abc.def#");
9594
}
9695

cpp/src/gandiva/regex_util.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ namespace gandiva {
2222
const std::set<char> RegexUtil::pcre_regex_specials_ = {
2323
'[', ']', '(', ')', '|', '^', '-', '+', '*', '?', '{', '}', '$', '\\', '.'};
2424

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

@@ -57,7 +57,7 @@ Status RegexUtil::SqlLikePatternToPcre(const std::string& sql_pattern, char esca
5757
pcre_pattern += cur;
5858
}
5959
}
60-
return Status::OK();
60+
return pcre_pattern;
6161
}
6262

6363
} // namespace gandiva

cpp/src/gandiva/regex_util.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
namespace gandiva {
2828

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

36-
static Status SqlLikePatternToPcre(const std::string& like_pattern,
37-
std::string& pcre_pattern) {
38-
return SqlLikePatternToPcre(like_pattern, 0 /*escape_char*/, pcre_pattern);
36+
static arrow::Result<std::string> SqlLikePatternToPcre(
37+
const std::string& like_pattern) {
38+
return SqlLikePatternToPcre(like_pattern, '\0' /*escape_char*/);
3939
}
4040

4141
private:

0 commit comments

Comments
 (0)