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