| 23 | '[', ']', '(', ')', '|', '^', '-', '+', '*', '?', '{', '}', '$', '\\', '.'}; |
| 24 | |
| 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 |
| 28 | /// escaped with '\\'. |
| 29 | std::string pcre_pattern; |
| 30 | for (size_t idx = 0; idx < sql_pattern.size(); ++idx) { |
| 31 | auto cur = sql_pattern.at(idx); |
| 32 | |
| 33 | // Escape any char that is special for pcre regex |
| 34 | if (pcre_regex_specials_.find(cur) != pcre_regex_specials_.end()) { |
| 35 | pcre_pattern += "\\"; |
| 36 | } |
| 37 | |
| 38 | if (cur == escape_char) { |
| 39 | // escape char must be followed by '_', '%' or the escape char itself. |
| 40 | ++idx; |
| 41 | ARROW_RETURN_IF( |
| 42 | idx == sql_pattern.size(), |
| 43 | Status::Invalid("Unexpected escape char at the end of pattern ", sql_pattern)); |
| 44 | |
| 45 | cur = sql_pattern.at(idx); |
| 46 | if (cur == '_' || cur == '%' || cur == escape_char) { |
| 47 | pcre_pattern += cur; |
| 48 | } else { |
| 49 | return Status::Invalid("Invalid escape sequence in pattern ", sql_pattern, |
| 50 | " at offset ", idx); |
| 51 | } |
| 52 | } else if (cur == '_') { |
| 53 | pcre_pattern += '.'; |
| 54 | } else if (cur == '%') { |
| 55 | pcre_pattern += ".*"; |
| 56 | } else { |
| 57 | pcre_pattern += cur; |
| 58 | } |
| 59 | } |
| 60 | return pcre_pattern; |
| 61 | } |
| 62 | |
| 63 | } // namespace gandiva |