datafusion_functions/string/
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//! "string" DataFusion functions
19
20use std::sync::Arc;
21
22use datafusion_expr::ScalarUDF;
23
24pub mod ascii;
25pub mod bit_length;
26pub mod btrim;
27pub mod chr;
28pub mod common;
29pub mod concat;
30pub mod concat_ws;
31pub mod contains;
32pub mod ends_with;
33pub mod levenshtein;
34pub mod lower;
35pub mod ltrim;
36pub mod octet_length;
37pub mod overlay;
38pub mod repeat;
39pub mod replace;
40pub mod rtrim;
41pub mod split_part;
42pub mod starts_with;
43pub mod to_hex;
44pub mod upper;
45pub mod uuid;
46// create UDFs
47make_udf_function!(ascii::AsciiFunc, ascii);
48make_udf_function!(bit_length::BitLengthFunc, bit_length);
49make_udf_function!(btrim::BTrimFunc, btrim);
50make_udf_function!(chr::ChrFunc, chr);
51make_udf_function!(concat::ConcatFunc, concat);
52make_udf_function!(concat_ws::ConcatWsFunc, concat_ws);
53make_udf_function!(ends_with::EndsWithFunc, ends_with);
54make_udf_function!(levenshtein::LevenshteinFunc, levenshtein);
55make_udf_function!(ltrim::LtrimFunc, ltrim);
56make_udf_function!(lower::LowerFunc, lower);
57make_udf_function!(octet_length::OctetLengthFunc, octet_length);
58make_udf_function!(repeat::RepeatFunc, repeat);
59make_udf_function!(replace::ReplaceFunc, replace);
60make_udf_function!(rtrim::RtrimFunc, rtrim);
61make_udf_function!(starts_with::StartsWithFunc, starts_with);
62make_udf_function!(split_part::SplitPartFunc, split_part);
63make_udf_function!(to_hex::ToHexFunc, to_hex);
64make_udf_function!(upper::UpperFunc, upper);
65make_udf_function!(uuid::UuidFunc, uuid);
66make_udf_function!(contains::ContainsFunc, contains);
67pub mod expr_fn {
68    use datafusion_expr::Expr;
69
70    export_functions!((
71        ascii,
72        "Returns the numeric code of the first character of the argument.",
73        arg1
74    ),(
75        bit_length,
76        "Returns the number of bits in the `string`",
77        arg1
78    ),(
79        btrim,
80        "Removes all characters, spaces by default, from both sides of a string",
81        args,
82    ),(
83        chr,
84        "Converts the Unicode code point to a UTF8 character",
85        arg1
86    ),(
87        concat,
88        "Concatenates the text representations of all the arguments. NULL arguments are ignored",
89        args,
90    ),(
91        ends_with,
92        "Returns true if the `string` ends with the `suffix`, false otherwise.",
93        string suffix
94    ),(
95        levenshtein,
96        "Returns the Levenshtein distance between the two given strings",
97        arg1 arg2
98    ),(
99        lower,
100        "Converts a string to lowercase.",
101        arg1
102    ),(
103        ltrim,
104        "Removes all characters, spaces by default, from the beginning of a string",
105        args,
106    ),(
107        octet_length,
108        "returns the number of bytes of a string",
109        args
110    ),(
111        repeat,
112        "Repeats the `string` to `n` times",
113        string n
114    ),(
115        replace,
116        "Replaces all occurrences of `from` with `to` in the `string`",
117        string from to
118    ),(
119        rtrim,
120        "Removes all characters, spaces by default, from the end of a string",
121        args,
122    ),(
123        split_part,
124        "Splits a string based on a delimiter and picks out the desired field based on the index.",
125        string delimiter index
126    ),(
127        starts_with,
128        "Returns true if string starts with prefix.",
129        arg1 arg2
130    ),(
131        to_hex,
132        "Converts an integer to a hexadecimal string.",
133        arg1
134    ),(
135        upper,
136        "Converts a string to uppercase.",
137        arg1
138    ),(
139        uuid,
140        "returns uuid v4 as a string value",
141    ), (
142        contains,
143        "Return true if search_string is found within string.",
144    ));
145
146    #[doc = "Removes all characters, spaces by default, from both sides of a string"]
147    pub fn trim(args: Vec<Expr>) -> Expr {
148        super::btrim().call(args)
149    }
150
151    #[doc = "Concatenates all but the first argument, with separators. The first argument is used as the separator string, and should not be NULL. Other NULL arguments are ignored."]
152    pub fn concat_ws(delimiter: Expr, args: Vec<Expr>) -> Expr {
153        let mut args = args;
154        args.insert(0, delimiter);
155        super::concat_ws().call(args)
156    }
157}
158
159/// Returns all DataFusion functions defined in this package
160pub fn functions() -> Vec<Arc<ScalarUDF>> {
161    vec![
162        ascii(),
163        bit_length(),
164        btrim(),
165        chr(),
166        concat(),
167        concat_ws(),
168        ends_with(),
169        levenshtein(),
170        lower(),
171        ltrim(),
172        octet_length(),
173        repeat(),
174        replace(),
175        rtrim(),
176        split_part(),
177        starts_with(),
178        to_hex(),
179        upper(),
180        uuid(),
181        contains(),
182    ]
183}