datafusion_functions/string/
lower.rs1use arrow::datatypes::DataType;
19use std::any::Any;
20
21use crate::string::common::to_lower;
22use crate::utils::utf8_to_str_type;
23use datafusion_common::Result;
24use datafusion_expr::{ColumnarValue, Documentation};
25use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
26use datafusion_macros::user_doc;
27
28#[user_doc(
29 doc_section(label = "String Functions"),
30 description = "Converts a string to lower-case.",
31 syntax_example = "lower(str)",
32 sql_example = r#"```sql
33> select lower('Ångström');
34+-------------------------+
35| lower(Utf8("Ångström")) |
36+-------------------------+
37| ångström |
38+-------------------------+
39```"#,
40 standard_argument(name = "str", prefix = "String"),
41 related_udf(name = "initcap"),
42 related_udf(name = "upper")
43)]
44#[derive(Debug)]
45pub struct LowerFunc {
46 signature: Signature,
47}
48
49impl Default for LowerFunc {
50 fn default() -> Self {
51 Self::new()
52 }
53}
54
55impl LowerFunc {
56 pub fn new() -> Self {
57 Self {
58 signature: Signature::string(1, Volatility::Immutable),
59 }
60 }
61}
62
63impl ScalarUDFImpl for LowerFunc {
64 fn as_any(&self) -> &dyn Any {
65 self
66 }
67
68 fn name(&self) -> &str {
69 "lower"
70 }
71
72 fn signature(&self) -> &Signature {
73 &self.signature
74 }
75
76 fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
77 utf8_to_str_type(&arg_types[0], "lower")
78 }
79
80 fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
81 to_lower(&args.args, "lower")
82 }
83
84 fn documentation(&self) -> Option<&Documentation> {
85 self.doc()
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 use arrow::array::{Array, ArrayRef, StringArray};
93 use std::sync::Arc;
94
95 fn to_lower(input: ArrayRef, expected: ArrayRef) -> Result<()> {
96 let func = LowerFunc::new();
97
98 let args = ScalarFunctionArgs {
99 number_rows: input.len(),
100 args: vec![ColumnarValue::Array(input)],
101 return_type: &DataType::Utf8,
102 };
103
104 let result = match func.invoke_with_args(args)? {
105 ColumnarValue::Array(result) => result,
106 _ => unreachable!("lower"),
107 };
108 assert_eq!(&expected, &result);
109 Ok(())
110 }
111
112 #[test]
113 fn lower_maybe_optimization() -> Result<()> {
114 let input = Arc::new(StringArray::from(vec![
115 Some("农历新年"),
116 None,
117 Some("DATAFUSION"),
118 Some("0123456789"),
119 Some(""),
120 ])) as ArrayRef;
121
122 let expected = Arc::new(StringArray::from(vec![
123 Some("农历新年"),
124 None,
125 Some("datafusion"),
126 Some("0123456789"),
127 Some(""),
128 ])) as ArrayRef;
129
130 to_lower(input, expected)
131 }
132
133 #[test]
134 fn lower_full_optimization() -> Result<()> {
135 let input = Arc::new(StringArray::from(vec![
136 Some("ARROW"),
137 None,
138 Some("DATAFUSION"),
139 Some("0123456789"),
140 Some(""),
141 ])) as ArrayRef;
142
143 let expected = Arc::new(StringArray::from(vec![
144 Some("arrow"),
145 None,
146 Some("datafusion"),
147 Some("0123456789"),
148 Some(""),
149 ])) as ArrayRef;
150
151 to_lower(input, expected)
152 }
153
154 #[test]
155 fn lower_partial_optimization() -> Result<()> {
156 let input = Arc::new(StringArray::from(vec![
157 Some("ARROW"),
158 None,
159 Some("DATAFUSION"),
160 Some("@_"),
161 Some("0123456789"),
162 Some(""),
163 Some("\t\n"),
164 Some("ὈΔΥΣΣΕΎΣ"),
165 Some("TSCHÜSS"),
166 Some("Ⱦ"), Some("农历新年"),
168 ])) as ArrayRef;
169
170 let expected = Arc::new(StringArray::from(vec![
171 Some("arrow"),
172 None,
173 Some("datafusion"),
174 Some("@_"),
175 Some("0123456789"),
176 Some(""),
177 Some("\t\n"),
178 Some("ὀδυσσεύς"),
179 Some("tschüss"),
180 Some("ⱦ"),
181 Some("农历新年"),
182 ])) as ArrayRef;
183
184 to_lower(input, expected)
185 }
186}