pitrou commented on a change in pull request #9838:
URL: https://github.com/apache/arrow/pull/9838#discussion_r604870513



##########
File path: cpp/src/arrow/compute/kernels/scalar_string.cc
##########
@@ -411,40 +411,106 @@ void TransformMatchSubstring(const uint8_t* pattern, 
int64_t pattern_length,
 
 using MatchSubstringState = OptionsWrapper<MatchSubstringOptions>;
 
-template <typename Type>
+template <typename Type, template <typename offset_type> class Matcher>
 struct MatchSubstring {
   using offset_type = typename Type::offset_type;
   static void Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
-    MatchSubstringOptions arg = MatchSubstringState::Get(ctx);
-    const uint8_t* pat = reinterpret_cast<const uint8_t*>(arg.pattern.c_str());
-    const int64_t pat_size = arg.pattern.length();
+    // TODO Cache matcher across invocations (for regex compilation)
+    Matcher<offset_type> matcher(ctx, MatchSubstringState::Get(ctx));
+    if (ctx->HasError()) return;
     StringBoolTransform<Type>(
         ctx, batch,
-        [pat, pat_size](const void* offsets, const uint8_t* data, int64_t 
length,
-                        int64_t output_offset, uint8_t* output) {
-          TransformMatchSubstring<offset_type>(
-              pat, pat_size, reinterpret_cast<const offset_type*>(offsets), 
data, length,
-              output_offset, output);
+        [&matcher](const void* offsets, const uint8_t* data, int64_t length,
+                   int64_t output_offset, uint8_t* output) {
+          matcher.Match(reinterpret_cast<const offset_type*>(offsets), data, 
length,
+                        output_offset, output);
         },
         out);
   }
 };
 
+template <typename offset_type>
+struct PlainSubstringMatcher {
+  const MatchSubstringOptions& options_;
+
+  PlainSubstringMatcher(KernelContext* ctx, const MatchSubstringOptions& 
options)
+      : options_(options) {}
+
+  void Match(const offset_type* offsets, const uint8_t* data, int64_t length,
+             int64_t output_offset, uint8_t* output) {
+    const uint8_t* pat = reinterpret_cast<const 
uint8_t*>(options_.pattern.c_str());
+    const int64_t pat_size = options_.pattern.length();
+    TransformMatchSubstring<offset_type>(pat, pat_size, offsets, data, length,
+                                         output_offset, output);
+  }
+};
+
 const FunctionDoc match_substring_doc(
     "Match strings against literal pattern",
     ("For each string in `strings`, emit true iff it contains a given 
pattern.\n"
      "Null inputs emit null.  The pattern must be given in 
MatchSubstringOptions."),
     {"strings"}, "MatchSubstringOptions");
 
+#ifdef ARROW_WITH_RE2
+template <typename offset_type>

Review comment:
       There's no conceptual reason for matchers to be templated on offset 
type. The iteration loop on string values could be moved out of the matcher and 
into the calling `MatchSubstring` class. Then all a matcher does is take a 
`string_view` or similar and return the index where the pattern was found (or 
presumably -1 if not found).




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to