xinlifoobar commented on code in PR #12080: URL: https://github.com/apache/datafusion/pull/12080#discussion_r1726173183
########## datafusion/functions/src/regex/regexpcount.rs: ########## @@ -0,0 +1,561 @@ +// 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. + +use std::iter::repeat; +use std::sync::Arc; + +use arrow::array::{ + Array, ArrayRef, AsArray, Datum, GenericStringArray, Int64Array, OffsetSizeTrait, + Scalar, +}; +use arrow::datatypes::DataType::{self, Int64, LargeUtf8, Utf8}; +use arrow::datatypes::Int64Type; +use arrow::error::ArrowError; +use datafusion_common::cast::{as_generic_string_array, as_primitive_array}; +use datafusion_common::{ + arrow_err, exec_err, internal_err, DataFusionError, Result, ScalarValue, +}; +use datafusion_expr::TypeSignature::Exact; +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; +use itertools::izip; +use regex::Regex; + +/// regexp_count(string, pattern [, start [, flags ]]) -> int +#[derive(Debug)] +pub struct RegexpCountFunc { + signature: Signature, +} + +impl Default for RegexpCountFunc { + fn default() -> Self { + Self::new() + } +} + +impl RegexpCountFunc { + pub fn new() -> Self { + Self { + signature: Signature::one_of( + vec![ + Exact(vec![Utf8, Utf8]), + Exact(vec![Utf8, Utf8, Int64]), + Exact(vec![Utf8, Utf8, Int64, Utf8]), + Exact(vec![Utf8, Utf8, Int64, LargeUtf8]), + Exact(vec![LargeUtf8, LargeUtf8]), + Exact(vec![LargeUtf8, LargeUtf8, Int64]), + Exact(vec![LargeUtf8, LargeUtf8, Int64, Utf8]), + Exact(vec![LargeUtf8, LargeUtf8, Int64, LargeUtf8]), + ], + Volatility::Immutable, + ), + } + } +} + +impl ScalarUDFImpl for RegexpCountFunc { + fn as_any(&self) -> &dyn std::any::Any { + self + } + + fn name(&self) -> &str { + "regexp_count" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { + use DataType::*; + + Ok(match &arg_types[0] { + Null => Null, + _ => Int64, + }) + } + + fn invoke(&self, args: &[datafusion_expr::ColumnarValue]) -> Result<ColumnarValue> { + let len = args + .iter() + .fold(Option::<usize>::None, |acc, arg| match arg { + ColumnarValue::Scalar(_) => acc, + ColumnarValue::Array(a) => Some(a.len()), + }); + + let is_scalar = len.is_none(); + let inferred_length = len.unwrap_or(1); + let args = args + .iter() + .map(|arg| arg.clone().into_array(inferred_length)) + .collect::<Result<Vec<_>>>()?; + + let result = regexp_count_func(&args); + if is_scalar { + // If all inputs are scalar, keeps output as scalar + let result = result.and_then(|arr| ScalarValue::try_from_array(&arr, 0)); + result.map(ColumnarValue::Scalar) + } else { + result.map(ColumnarValue::Array) + } + } +} + +fn regexp_count_func(args: &[ArrayRef]) -> Result<ArrayRef> { + match args[0].data_type() { + DataType::Utf8 => regexp_count::<i32>(args), + DataType::LargeUtf8 => regexp_count::<i64>(args), + other => { + internal_err!("Unsupported data type {other:?} for function regexp_count") + } + } +} + +pub fn regexp_count<O: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { + let arg_len = args.len(); + + match arg_len { + 2..=4 => { + let values = as_generic_string_array::<O>(&args[0])?; + let regex = as_generic_string_array::<O>(&args[1])?; + let regex_datum: &dyn Datum = if regex.len() != 1 { + regex + } else { + &Scalar::new(regex) Review Comment: I am not Rust expert. Looking into their changelogs, this should named `Propagate temporary lifetime extension into if and match. ` stablized in Rust 1.79. For your interests, https://github.com/rust-lang/rust/pull/121346/ -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org