MCPcopy Create free account
hub / github.com/apache/arrow-rs / regexp_match

Function regexp_match

arrow-string/src/regexp.rs:414–518  ·  view source on GitHub ↗

Extract all groups matched by a regular expression for a given String array. Modelled after the Postgres [regexp_match]. Returns a ListArray of [`GenericStringArray`] with each element containing the leftmost-first match of the corresponding index in `regex_array` to string in `array` If there is no match, the list element is NULL. If a match is found, and the pattern contains no capturing par

(
    array: &dyn Array,
    regex_array: &dyn Datum,
    flags_array: Option<&dyn Datum>,
)

Source from the content-addressed store, hash-verified

412///
413/// [regexp_match]: https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP
414pub fn regexp_match(
415 array: &dyn Array,
416 regex_array: &dyn Datum,
417 flags_array: Option<&dyn Datum>,
418) -> Result<ArrayRef, ArrowError> {
419 let (rhs, is_rhs_scalar) = regex_array.get();
420
421 if array.data_type() != rhs.data_type() {
422 return Err(ArrowError::ComputeError(
423 "regexp_match() requires both array and pattern to be either Utf8, Utf8View or LargeUtf8"
424 .to_string(),
425 ));
426 }
427
428 let (flags, is_flags_scalar) = match flags_array {
429 Some(flags) => {
430 let (flags, is_flags_scalar) = flags.get();
431 (Some(flags), Some(is_flags_scalar))
432 }
433 None => (None, None),
434 };
435
436 if is_flags_scalar.is_some() && is_rhs_scalar != is_flags_scalar.unwrap() {
437 return Err(ArrowError::ComputeError(
438 "regexp_match() requires both pattern and flags to be either scalar or array"
439 .to_string(),
440 ));
441 }
442
443 if flags_array.is_some() && rhs.data_type() != flags.unwrap().data_type() {
444 return Err(ArrowError::ComputeError(
445 "regexp_match() requires both pattern and flags to be either Utf8, Utf8View or LargeUtf8"
446 .to_string(),
447 ));
448 }
449
450 if is_rhs_scalar {
451 // Regex and flag is scalars
452 let (regex, flag) = match rhs.data_type() {
453 DataType::Utf8View => get_scalar_pattern_flag_utf8view(rhs, flags),
454 DataType::Utf8 => get_scalar_pattern_flag::<i32>(rhs, flags),
455 DataType::LargeUtf8 => get_scalar_pattern_flag::<i64>(rhs, flags),
456 _ => {
457 return Err(ArrowError::ComputeError(
458 "regexp_match() requires pattern to be either Utf8, Utf8View or LargeUtf8"
459 .to_string(),
460 ));
461 }
462 };
463
464 if regex.is_none() {
465 return Ok(new_null_array(
466 &DataType::List(Arc::new(Field::new_list_field(
467 array.data_type().clone(),
468 true,
469 ))),
470 array.len(),
471 ));

Callers 1

bench_regexpFunction · 0.85

Calls 14

new_null_arrayFunction · 0.85
regexp_scalar_matchFunction · 0.85
regexp_array_matchFunction · 0.85
as_string_viewMethod · 0.80
ListClass · 0.50
getMethod · 0.45
data_typeMethod · 0.45
cloneMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected