datafusion_functions/unicode/
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//! "unicode" DataFusion functions
19
20use std::sync::Arc;
21
22use datafusion_expr::ScalarUDF;
23
24pub mod character_length;
25pub mod find_in_set;
26pub mod initcap;
27pub mod left;
28pub mod lpad;
29pub mod planner;
30pub mod reverse;
31pub mod right;
32pub mod rpad;
33pub mod strpos;
34pub mod substr;
35pub mod substrindex;
36pub mod translate;
37
38// create UDFs
39make_udf_function!(character_length::CharacterLengthFunc, character_length);
40make_udf_function!(find_in_set::FindInSetFunc, find_in_set);
41make_udf_function!(initcap::InitcapFunc, initcap);
42make_udf_function!(left::LeftFunc, left);
43make_udf_function!(lpad::LPadFunc, lpad);
44make_udf_function!(right::RightFunc, right);
45make_udf_function!(reverse::ReverseFunc, reverse);
46make_udf_function!(rpad::RPadFunc, rpad);
47make_udf_function!(strpos::StrposFunc, strpos);
48make_udf_function!(substr::SubstrFunc, substr);
49make_udf_function!(substr::SubstrFunc, substring);
50make_udf_function!(substrindex::SubstrIndexFunc, substr_index);
51make_udf_function!(translate::TranslateFunc, translate);
52
53pub mod expr_fn {
54    use datafusion_expr::Expr;
55
56    export_functions!((
57        character_length,
58        "the number of characters in the `string`",
59        string
60    ),(
61        lpad,
62        "fill up a string to the length by prepending the characters",
63        args,
64    ),(
65        rpad,
66        "fill up a string to the length by appending the characters",
67        args,
68    ),(
69        reverse,
70        "reverses the `string`",
71        string
72    ),(
73        substr,
74        "substring from the `position` to the end",
75        string position
76    ),(
77        substr_index,
78        "Returns the substring from str before count occurrences of the delimiter",
79        string delimiter count
80    ),(
81        strpos,
82        "finds the position from where the `substring` matches the `string`",
83        string substring
84    ),(
85        substring,
86        "substring from the `position` with `length` characters",
87        string position length
88    ),(
89        translate,
90        "replaces the characters in `from` with the counterpart in `to`",
91        string from to
92    ),(
93        right,
94        "returns the last `n` characters in the `string`",
95        string n
96    ),(
97        left,
98        "returns the first `n` characters in the `string`",
99        string n
100    ),(
101        initcap,
102        "converts the first letter of each word in `string` in uppercase and the remaining characters in lowercase",
103        string
104    ),(
105        find_in_set,
106        "Returns a value in the range of 1 to N if the string `str` is in the string list `strlist` consisting of N substrings",
107        string strlist
108    ));
109
110    #[doc = "the number of characters in the `string`"]
111    pub fn char_length(string: Expr) -> Expr {
112        character_length(string)
113    }
114
115    #[doc = "finds the position from where the `substring` matches the `string`"]
116    pub fn instr(string: Expr, substring: Expr) -> Expr {
117        strpos(string, substring)
118    }
119
120    #[doc = "the number of characters in the `string`"]
121    pub fn length(string: Expr) -> Expr {
122        character_length(string)
123    }
124
125    #[doc = "finds the position from where the `substring` matches the `string`"]
126    pub fn position(string: Expr, substring: Expr) -> Expr {
127        strpos(string, substring)
128    }
129}
130
131/// Returns all DataFusion functions defined in this package
132pub fn functions() -> Vec<Arc<ScalarUDF>> {
133    vec![
134        character_length(),
135        find_in_set(),
136        initcap(),
137        left(),
138        lpad(),
139        reverse(),
140        right(),
141        rpad(),
142        strpos(),
143        substr(),
144        substr_index(),
145        translate(),
146    ]
147}