|
| 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 | +use std::any::Any; |
| 19 | +use std::cmp::Ordering; |
| 20 | +use std::sync::Arc; |
| 21 | + |
| 22 | +use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait}; |
| 23 | +use arrow::datatypes::DataType; |
| 24 | + |
| 25 | +use datafusion_common::cast::{as_generic_string_array, as_int64_array}; |
| 26 | +use datafusion_common::exec_err; |
| 27 | +use datafusion_common::Result; |
| 28 | +use datafusion_expr::TypeSignature::Exact; |
| 29 | +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; |
| 30 | + |
| 31 | +use crate::utils::{make_scalar_function, utf8_to_str_type}; |
| 32 | + |
| 33 | +#[derive(Debug)] |
| 34 | +pub(super) struct LeftFunc { |
| 35 | + signature: Signature, |
| 36 | +} |
| 37 | + |
| 38 | +impl LeftFunc { |
| 39 | + pub fn new() -> Self { |
| 40 | + use DataType::*; |
| 41 | + Self { |
| 42 | + signature: Signature::one_of( |
| 43 | + vec![Exact(vec![Utf8, Int64]), Exact(vec![LargeUtf8, Int64])], |
| 44 | + Volatility::Immutable, |
| 45 | + ), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl ScalarUDFImpl for LeftFunc { |
| 51 | + fn as_any(&self) -> &dyn Any { |
| 52 | + self |
| 53 | + } |
| 54 | + |
| 55 | + fn name(&self) -> &str { |
| 56 | + "left" |
| 57 | + } |
| 58 | + |
| 59 | + fn signature(&self) -> &Signature { |
| 60 | + &self.signature |
| 61 | + } |
| 62 | + |
| 63 | + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { |
| 64 | + utf8_to_str_type(&arg_types[0], "left") |
| 65 | + } |
| 66 | + |
| 67 | + fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { |
| 68 | + match args[0].data_type() { |
| 69 | + DataType::Utf8 => make_scalar_function(left::<i32>, vec![])(args), |
| 70 | + DataType::LargeUtf8 => make_scalar_function(left::<i64>, vec![])(args), |
| 71 | + other => exec_err!("Unsupported data type {other:?} for function left"), |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Returns first n characters in the string, or when n is negative, returns all but last |n| characters. |
| 77 | +/// left('abcde', 2) = 'ab' |
| 78 | +/// The implementation uses UTF-8 code points as characters |
| 79 | +pub fn left<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { |
| 80 | + let string_array = as_generic_string_array::<T>(&args[0])?; |
| 81 | + let n_array = as_int64_array(&args[1])?; |
| 82 | + let result = string_array |
| 83 | + .iter() |
| 84 | + .zip(n_array.iter()) |
| 85 | + .map(|(string, n)| match (string, n) { |
| 86 | + (Some(string), Some(n)) => match n.cmp(&0) { |
| 87 | + Ordering::Less => { |
| 88 | + let len = string.chars().count() as i64; |
| 89 | + Some(if n.abs() < len { |
| 90 | + string.chars().take((len + n) as usize).collect::<String>() |
| 91 | + } else { |
| 92 | + "".to_string() |
| 93 | + }) |
| 94 | + } |
| 95 | + Ordering::Equal => Some("".to_string()), |
| 96 | + Ordering::Greater => { |
| 97 | + Some(string.chars().take(n as usize).collect::<String>()) |
| 98 | + } |
| 99 | + }, |
| 100 | + _ => None, |
| 101 | + }) |
| 102 | + .collect::<GenericStringArray<T>>(); |
| 103 | + |
| 104 | + Ok(Arc::new(result) as ArrayRef) |
| 105 | +} |
| 106 | + |
| 107 | +#[cfg(test)] |
| 108 | +mod tests { |
| 109 | + use arrow::array::{Array, StringArray}; |
| 110 | + use arrow::datatypes::DataType::Utf8; |
| 111 | + |
| 112 | + use datafusion_common::{Result, ScalarValue}; |
| 113 | + use datafusion_expr::{ColumnarValue, ScalarUDFImpl}; |
| 114 | + |
| 115 | + use crate::unicode::left::LeftFunc; |
| 116 | + use crate::utils::test::test_function; |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_functions() -> Result<()> { |
| 120 | + test_function!( |
| 121 | + LeftFunc::new(), |
| 122 | + &[ |
| 123 | + ColumnarValue::Scalar(ScalarValue::from("abcde")), |
| 124 | + ColumnarValue::Scalar(ScalarValue::from(2i64)), |
| 125 | + ], |
| 126 | + Ok(Some("ab")), |
| 127 | + &str, |
| 128 | + Utf8, |
| 129 | + StringArray |
| 130 | + ); |
| 131 | + test_function!( |
| 132 | + LeftFunc::new(), |
| 133 | + &[ |
| 134 | + ColumnarValue::Scalar(ScalarValue::from("abcde")), |
| 135 | + ColumnarValue::Scalar(ScalarValue::from(200i64)), |
| 136 | + ], |
| 137 | + Ok(Some("abcde")), |
| 138 | + &str, |
| 139 | + Utf8, |
| 140 | + StringArray |
| 141 | + ); |
| 142 | + test_function!( |
| 143 | + LeftFunc::new(), |
| 144 | + &[ |
| 145 | + ColumnarValue::Scalar(ScalarValue::from("abcde")), |
| 146 | + ColumnarValue::Scalar(ScalarValue::from(-2i64)), |
| 147 | + ], |
| 148 | + Ok(Some("abc")), |
| 149 | + &str, |
| 150 | + Utf8, |
| 151 | + StringArray |
| 152 | + ); |
| 153 | + test_function!( |
| 154 | + LeftFunc::new(), |
| 155 | + &[ |
| 156 | + ColumnarValue::Scalar(ScalarValue::from("abcde")), |
| 157 | + ColumnarValue::Scalar(ScalarValue::from(-200i64)), |
| 158 | + ], |
| 159 | + Ok(Some("")), |
| 160 | + &str, |
| 161 | + Utf8, |
| 162 | + StringArray |
| 163 | + ); |
| 164 | + test_function!( |
| 165 | + LeftFunc::new(), |
| 166 | + &[ |
| 167 | + ColumnarValue::Scalar(ScalarValue::from("abcde")), |
| 168 | + ColumnarValue::Scalar(ScalarValue::from(0i64)), |
| 169 | + ], |
| 170 | + Ok(Some("")), |
| 171 | + &str, |
| 172 | + Utf8, |
| 173 | + StringArray |
| 174 | + ); |
| 175 | + test_function!( |
| 176 | + LeftFunc::new(), |
| 177 | + &[ |
| 178 | + ColumnarValue::Scalar(ScalarValue::Utf8(None)), |
| 179 | + ColumnarValue::Scalar(ScalarValue::from(2i64)), |
| 180 | + ], |
| 181 | + Ok(None), |
| 182 | + &str, |
| 183 | + Utf8, |
| 184 | + StringArray |
| 185 | + ); |
| 186 | + test_function!( |
| 187 | + LeftFunc::new(), |
| 188 | + &[ |
| 189 | + ColumnarValue::Scalar(ScalarValue::from("abcde")), |
| 190 | + ColumnarValue::Scalar(ScalarValue::Int64(None)), |
| 191 | + ], |
| 192 | + Ok(None), |
| 193 | + &str, |
| 194 | + Utf8, |
| 195 | + StringArray |
| 196 | + ); |
| 197 | + test_function!( |
| 198 | + LeftFunc::new(), |
| 199 | + &[ |
| 200 | + ColumnarValue::Scalar(ScalarValue::from("joséésoj")), |
| 201 | + ColumnarValue::Scalar(ScalarValue::from(5i64)), |
| 202 | + ], |
| 203 | + Ok(Some("joséé")), |
| 204 | + &str, |
| 205 | + Utf8, |
| 206 | + StringArray |
| 207 | + ); |
| 208 | + test_function!( |
| 209 | + LeftFunc::new(), |
| 210 | + &[ |
| 211 | + ColumnarValue::Scalar(ScalarValue::from("joséésoj")), |
| 212 | + ColumnarValue::Scalar(ScalarValue::from(-3i64)), |
| 213 | + ], |
| 214 | + Ok(Some("joséé")), |
| 215 | + &str, |
| 216 | + Utf8, |
| 217 | + StringArray |
| 218 | + ); |
| 219 | + #[cfg(not(feature = "unicode_expressions"))] |
| 220 | + test_function!( |
| 221 | + LeftFunc::new(), |
| 222 | + &[ |
| 223 | + ColumnarValue::Scalar(ScalarValue::from("abcde")), |
| 224 | + ColumnarValue::Scalar(ScalarValue::from(2i64)), |
| 225 | + ], |
| 226 | + internal_err!( |
| 227 | + "function left requires compilation with feature flag: unicode_expressions." |
| 228 | + ), |
| 229 | + &str, |
| 230 | + Utf8, |
| 231 | + StringArray |
| 232 | + ); |
| 233 | + |
| 234 | + Ok(()) |
| 235 | + } |
| 236 | +} |
0 commit comments