From 7528d7fe0530aec77d83d54cf15d85b966ccc130 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Thu, 1 Feb 2024 08:52:47 -0500 Subject: [PATCH] Move regexp_match functions into datafusion-functions --- datafusion/functions/Cargo.toml | 5 ++++ datafusion/functions/src/lib.rs | 7 +++++ .../src/regex/inner.rs} | 11 ++----- datafusion/functions/src/regex/mod.rs | 30 +++++++++++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) rename datafusion/{physical-expr/src/regex_expressions.rs => functions/src/regex/inner.rs} (98%) create mode 100644 datafusion/functions/src/regex/mod.rs diff --git a/datafusion/functions/Cargo.toml b/datafusion/functions/Cargo.toml index 6d4a716e2e8e3..149b3a70076d5 100644 --- a/datafusion/functions/Cargo.toml +++ b/datafusion/functions/Cargo.toml @@ -33,6 +33,9 @@ rust-version = { workspace = true } default = ["encoding_expressions"] # enable the encode/decode functions encoding_expressions = ["base64", "hex"] +# enable regexp_match/regexp_replace functions +regex_expressions = ["regex", "hashbrown"] + [lib] @@ -49,3 +52,5 @@ datafusion-execution = { workspace = true } datafusion-expr = { workspace = true } hex = { version = "0.4", optional = true } log = "0.4.20" +regex = { version = "1.8", optional = true } +hashbrown = { version = "0.14", features = ["raw"], optional = true } diff --git a/datafusion/functions/src/lib.rs b/datafusion/functions/src/lib.rs index 91a5c510f0f95..2b90755413b4b 100644 --- a/datafusion/functions/src/lib.rs +++ b/datafusion/functions/src/lib.rs @@ -90,6 +90,13 @@ make_package!( "Hex and binary `encode` and `decode` functions." ); +make_package!( + regex, + "regex_expressions", + "Regular Expressions `regexp_match` and `regexp_replace` functions." +); + + /// Fluent-style API for creating `Expr`s pub mod expr_fn { #[cfg(feature = "encoding_expressions")] diff --git a/datafusion/physical-expr/src/regex_expressions.rs b/datafusion/functions/src/regex/inner.rs similarity index 98% rename from datafusion/physical-expr/src/regex_expressions.rs rename to datafusion/functions/src/regex/inner.rs index bdd272563e752..e73ebb8bbc9ec 100644 --- a/datafusion/physical-expr/src/regex_expressions.rs +++ b/datafusion/functions/src/regex/inner.rs @@ -21,12 +21,7 @@ //! Regex expressions -use arrow::array::{ - new_null_array, Array, ArrayDataBuilder, ArrayRef, BufferBuilder, GenericStringArray, - OffsetSizeTrait, -}; -use arrow_array::builder::{GenericStringBuilder, ListBuilder}; -use arrow_schema::ArrowError; +use arrow::array::{new_null_array, Array, ArrayDataBuilder, ArrayRef, BufferBuilder, GenericStringArray, OffsetSizeTrait, GenericStringBuilder, ListBuilder}; use datafusion_common::{arrow_datafusion_err, plan_err}; use datafusion_common::{ cast::as_generic_string_array, internal_err, DataFusionError, Result, @@ -35,10 +30,8 @@ use datafusion_expr::{ColumnarValue, ScalarFunctionImplementation}; use hashbrown::HashMap; use regex::Regex; use std::sync::{Arc, OnceLock}; +use arrow::error::ArrowError; -use crate::functions::{ - make_scalar_function_inner, make_scalar_function_with_hints, Hint, -}; /// Get the first argument from the given string array. /// diff --git a/datafusion/functions/src/regex/mod.rs b/datafusion/functions/src/regex/mod.rs new file mode 100644 index 0000000000000..61f8588223abf --- /dev/null +++ b/datafusion/functions/src/regex/mod.rs @@ -0,0 +1,30 @@ +// 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. + +mod inner; + + +// create UDFs +make_udf_function!(inner::RegexpMatch, REGEXP_MATCH, regexp_match); +make_udf_function!(inner::RegexpReplace, REGEXP_REPLACE, regexp_replace); + +// Export the functions out of this package, both as expr_fn as well as a list of functions +export_functions!( + (regexp_match, input encoding, "encode the `input`, using the `encoding`. encoding can be base64 or hex"), + (regexp_replace, input encoding, "decode the `input`, using the `encoding`. encoding can be base64 or hex") +); +