comphead commented on code in PR #23547:
URL: https://github.com/apache/datafusion/pull/23547#discussion_r3580694089


##########
datafusion/functions/src/regex/regexpmatch.rs:
##########
@@ -145,6 +153,63 @@ impl ScalarUDFImpl for RegexpMatchFunc {
     }
 }
 
+/// Runs `regexp_match` with the pattern (and flags, if given) passed to the
+/// kernel as scalar [`Datum`]s, so the regex is compiled once for the whole
+/// array.
+///
+/// Applies when the values are an array, the pattern is a non-null scalar of
+/// the same string type as the values, and the flags, if given, are a scalar 
of
+/// that same type and are not the unsupported "global" flag.
+///
+/// Returns `Ok(None)` for every other argument shape, leaving the caller's
+/// general path to materialize each argument as an array, zip the rows, and
+/// raise whatever error the shape warrants.
+fn regexp_match_scalar_pattern(args: &[ColumnarValue]) -> 
Result<Option<ArrayRef>> {
+    let (values, pattern, flags) = match args {
+        [values, pattern] => (values, pattern, None),
+        [values, pattern, flags] => (values, pattern, Some(flags)),
+        _ => return Ok(None),
+    };
+
+    let (ColumnarValue::Array(values), ColumnarValue::Scalar(pattern)) =
+        (values, pattern)
+    else {
+        return Ok(None);
+    };
+    let flags = match flags {
+        None => None,
+        Some(ColumnarValue::Scalar(flags)) => Some(flags),
+        // An array of flags has to be zipped with the values row by row.
+        Some(ColumnarValue::Array(_)) => return Ok(None),
+    };
+
+    if !matches!(pattern.try_as_str(), Some(Some(_)))
+        || flags.is_some_and(|flags| flags.try_as_str() == Some(Some("g")))
+    {
+        return Ok(None);
+    }
+
+    // The kernel requires the values, the pattern and the flags to share one
+    // string type.
+    let value_type = values.data_type();
+    if &pattern.data_type() != value_type
+        || flags.is_some_and(|flags| &flags.data_type() != value_type)
+    {
+        return Ok(None);
+    }

Review Comment:
   ```suggestion
   let flags = match flags {
       Some(ColumnarValue::Array(_)) => return Ok(None),
       Some(ColumnarValue::Scalar(flags)) => Some(flags),
       None => None,
   };
   
   let value_type = values.data_type();
   
   if !matches!(pattern.try_as_str(), Some(Some(_)))
       || &pattern.data_type() != value_type
       || flags.is_some_and(|flags| {
           flags.try_as_str() == Some(Some("g")) || &flags.data_type() != 
value_type
       })
   {
       return Ok(None);
   }
   ```
   
   more concise IMO



-- 
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.

To unsubscribe, e-mail: [email protected]

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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to