Skip to main content

datafusion_spark/function/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
18pub mod ascii;
19pub mod base64;
20pub mod char;
21pub mod concat;
22pub mod elt;
23pub mod format_string;
24pub mod ilike;
25pub mod length;
26pub mod like;
27pub mod luhn_check;
28pub mod space;
29pub mod substring;
30
31use datafusion_expr::ScalarUDF;
32use datafusion_functions::make_udf_function;
33use std::sync::Arc;
34
35make_udf_function!(ascii::SparkAscii, ascii);
36make_udf_function!(base64::SparkBase64, base64);
37make_udf_function!(char::CharFunc, char);
38make_udf_function!(concat::SparkConcat, concat);
39make_udf_function!(ilike::SparkILike, ilike);
40make_udf_function!(length::SparkLengthFunc, length);
41make_udf_function!(elt::SparkElt, elt);
42make_udf_function!(like::SparkLike, like);
43make_udf_function!(luhn_check::SparkLuhnCheck, luhn_check);
44make_udf_function!(format_string::FormatStringFunc, format_string);
45make_udf_function!(space::SparkSpace, space);
46make_udf_function!(substring::SparkSubstring, substring);
47make_udf_function!(base64::SparkUnBase64, unbase64);
48
49pub mod expr_fn {
50    use datafusion_functions::export_functions;
51
52    export_functions!((
53        ascii,
54        "Returns the ASCII code point of the first character of string.",
55        arg1
56    ));
57    export_functions!((
58        base64,
59        "Encodes the input binary `bin` into a base64 string.",
60        bin
61    ));
62    export_functions!((
63        char,
64        "Returns the ASCII character having the binary equivalent to col. If col is larger than 256 the result is equivalent to char(col % 256).",
65        arg1
66    ));
67    export_functions!((
68        concat,
69        "Concatenates multiple input strings into a single string. Returns NULL if any input is NULL.",
70        args
71    ));
72    export_functions!((
73        elt,
74        "Returns the n-th input (1-indexed), e.g. returns 2nd input when n is 2. The function returns NULL if the index is 0 or exceeds the length of the array.",
75        select_col arg1 arg2 argn
76    ));
77    export_functions!((
78        ilike,
79        "Returns true if str matches pattern (case insensitive).",
80        str pattern
81    ));
82    export_functions!((
83        length,
84        "Returns the character length of string data or number of bytes of binary data. The length of string data includes the trailing spaces. The length of binary data includes binary zeros.",
85        arg1
86    ));
87    export_functions!((
88        like,
89        "Returns true if str matches pattern (case sensitive).",
90        str pattern
91    ));
92    export_functions!((
93        luhn_check,
94        "Returns whether the input string of digits is valid according to the Luhn algorithm.",
95        arg1
96    ));
97    export_functions!((
98        format_string,
99        "Returns a formatted string from printf-style format strings.",
100        strfmt args
101    ));
102    export_functions!((space, "Returns a string consisting of n spaces.", arg1));
103    export_functions!((
104        substring,
105        "Returns the substring from string `str` starting at position `pos` with length `length.",
106        str pos length
107    ));
108    export_functions!((
109        unbase64,
110        "Decodes the input string `str` from a base64 string into binary data.",
111        str
112    ));
113}
114
115pub fn functions() -> Vec<Arc<ScalarUDF>> {
116    vec![
117        ascii(),
118        base64(),
119        char(),
120        concat(),
121        elt(),
122        ilike(),
123        length(),
124        like(),
125        luhn_check(),
126        format_string(),
127        space(),
128        substring(),
129        unbase64(),
130    ]
131}