seddonm1 commented on a change in pull request #9654: URL: https://github.com/apache/arrow/pull/9654#discussion_r589813852
########## File path: rust/datafusion/src/physical_plan/regex_expressions.rs ########## @@ -0,0 +1,161 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Some of these functions reference the Postgres documentation +// or implementation to ensure compatibility and are subject to +// the Postgres license. + +//! Regex expressions + +use std::any::type_name; +use std::sync::Arc; + +use crate::error::{DataFusionError, Result}; +use arrow::array::{ArrayRef, GenericStringArray, StringOffsetSizeTrait}; +use hashbrown::HashMap; +use regex::Regex; + +macro_rules! downcast_string_arg { + ($ARG:expr, $NAME:expr, $T:ident) => {{ + $ARG.as_any() + .downcast_ref::<GenericStringArray<T>>() + .ok_or_else(|| { + DataFusionError::Internal(format!( + "could not cast {} to {}", + $NAME, + type_name::<GenericStringArray<T>>() + )) + })? + }}; +} + +/// replace POSIX capture groups (like \1) with Rust Regex group (like ${1}) +/// used by regexp_replace +fn regex_replace_posix_groups(replacement: &str) -> String { + lazy_static! { + static ref CAPTURE_GROUPS_RE: Regex = Regex::new("(\\\\)(\\d*)").unwrap(); + } + CAPTURE_GROUPS_RE + .replace_all(replacement, "$${$2}") + .into_owned() +} + +/// Replaces substring(s) matching a POSIX regular expression +/// regexp_replace('Thomas', '.[mN]a.', 'M') = 'ThM' +pub fn regexp_replace<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { + // creating Regex is expensive so create hashmap for memoization + let mut patterns: HashMap<String, Regex> = HashMap::new(); Review comment: There is definitely more work to be done with regex but we have feature flagged it so we should easily be able to add more here then promote them up. ---------------------------------------------------------------- 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: us...@infra.apache.org