datafusion_functions/regex/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! "regex" DataFusion functions
19
20use std::sync::Arc;
21
22pub mod regexpcount;
23pub mod regexplike;
24pub mod regexpmatch;
25pub mod regexpreplace;
26
27// create UDFs
28make_udf_function!(regexpcount::RegexpCountFunc, regexp_count);
29make_udf_function!(regexpmatch::RegexpMatchFunc, regexp_match);
30make_udf_function!(regexplike::RegexpLikeFunc, regexp_like);
31make_udf_function!(regexpreplace::RegexpReplaceFunc, regexp_replace);
32
33pub mod expr_fn {
34    use datafusion_expr::Expr;
35
36    /// Returns the number of consecutive occurrences of a regular expression in a string.
37    pub fn regexp_count(
38        values: Expr,
39        regex: Expr,
40        start: Option<Expr>,
41        flags: Option<Expr>,
42    ) -> Expr {
43        let mut args = vec![values, regex];
44        if let Some(start) = start {
45            args.push(start);
46        };
47
48        if let Some(flags) = flags {
49            args.push(flags);
50        };
51        super::regexp_count().call(args)
52    }
53
54    /// Returns a list of regular expression matches in a string.
55    pub fn regexp_match(values: Expr, regex: Expr, flags: Option<Expr>) -> Expr {
56        let mut args = vec![values, regex];
57        if let Some(flags) = flags {
58            args.push(flags);
59        };
60        super::regexp_match().call(args)
61    }
62
63    /// Returns true if a has at least one match in a string, false otherwise.
64    pub fn regexp_like(values: Expr, regex: Expr, flags: Option<Expr>) -> Expr {
65        let mut args = vec![values, regex];
66        if let Some(flags) = flags {
67            args.push(flags);
68        };
69        super::regexp_like().call(args)
70    }
71
72    /// Replaces substrings in a string that match.
73    pub fn regexp_replace(
74        string: Expr,
75        pattern: Expr,
76        replacement: Expr,
77        flags: Option<Expr>,
78    ) -> Expr {
79        let mut args = vec![string, pattern, replacement];
80        if let Some(flags) = flags {
81            args.push(flags);
82        };
83        super::regexp_replace().call(args)
84    }
85}
86
87/// Returns all DataFusion functions defined in this package
88pub fn functions() -> Vec<Arc<datafusion_expr::ScalarUDF>> {
89    vec![
90        regexp_count(),
91        regexp_match(),
92        regexp_like(),
93        regexp_replace(),
94    ]
95}