Replaces substring(s) matching a PCRE-like regular expression. The full list of supported features and syntax can be found at Supported flags with the addition of 'g' can be found at # Examples ```ignore # use datafusion::prelude::*; # use datafusion::error::Result; # #[tokio::main] # async fn
(
string_array: U,
pattern_array: U,
replacement_array: U,
flags_array: Option<U>,
)
| 335 | /// # } |
| 336 | /// ``` |
| 337 | pub fn regexp_replace<'a, T: OffsetSizeTrait, U>( |
| 338 | string_array: U, |
| 339 | pattern_array: U, |
| 340 | replacement_array: U, |
| 341 | flags_array: Option<U>, |
| 342 | ) -> Result<ArrayRef> |
| 343 | where |
| 344 | U: ArrayAccessor<Item = &'a str>, |
| 345 | { |
| 346 | // Default implementation for regexp_replace, assumes all args are arrays |
| 347 | // and args is a sequence of 3 or 4 elements. |
| 348 | |
| 349 | // creating Regex is expensive so create hashmap for memoization |
| 350 | let mut patterns: HashMap<String, Regex> = HashMap::new(); |
| 351 | |
| 352 | let datatype = string_array.data_type().to_owned(); |
| 353 | |
| 354 | let string_array_iter = ArrayIter::new(string_array); |
| 355 | let pattern_array_iter = ArrayIter::new(pattern_array); |
| 356 | let replacement_array_iter = ArrayIter::new(replacement_array); |
| 357 | |
| 358 | match flags_array { |
| 359 | None => { |
| 360 | let result_iter = string_array_iter |
| 361 | .zip(pattern_array_iter) |
| 362 | .zip(replacement_array_iter) |
| 363 | .map(|((string, pattern), replacement)| { |
| 364 | match (string, pattern, replacement) { |
| 365 | (Some(string), Some(pattern), Some(replacement)) => { |
| 366 | let replacement = regex_replace_posix_groups(replacement); |
| 367 | // if patterns hashmap already has regexp then use else create and return |
| 368 | let re = match patterns.get(pattern) { |
| 369 | Some(re) => Ok(re), |
| 370 | None => match Regex::new(pattern) { |
| 371 | Ok(re) => { |
| 372 | patterns.insert(pattern.to_string(), re); |
| 373 | Ok(patterns.get(pattern).unwrap()) |
| 374 | } |
| 375 | Err(err) => { |
| 376 | Err(DataFusionError::External(Box::new(err))) |
| 377 | } |
| 378 | }, |
| 379 | }; |
| 380 | |
| 381 | Some(re.map(|re| re.replace(string, replacement.as_str()))) |
| 382 | .transpose() |
| 383 | } |
| 384 | _ => Ok(None), |
| 385 | } |
| 386 | }); |
| 387 | |
| 388 | match datatype { |
| 389 | DataType::Utf8 | DataType::LargeUtf8 => { |
| 390 | let result = |
| 391 | result_iter.collect::<Result<GenericStringArray<T>>>()?; |
| 392 | Ok(Arc::new(result) as ArrayRef) |
| 393 | } |
| 394 | DataType::Utf8View => { |
nothing calls this directly
no test coverage detected
searching dependent graphs…