-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move Left, Lpad, Reverse, Right, Rpad functions to datafusion_functions #9841
Changes from 16 commits
2026df4
b6d2172
f6e481e
6a450b4
71d47a3
879cabe
f42b9bc
fc8ff76
a94a4f6
ee3ff9f
43c6e66
2bdb2d5
b07b7b6
e3860fa
47eac75
ca3c92f
684591c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,245 @@ | ||||||||||
// Licensed to the Apache Software Foundation (ASF) under one | ||||||||||
// or more contributor license agreements. See the NOTICE file | ||||||||||
// distributed with this work for additional information | ||||||||||
// regarding copyright ownership. The ASF licenses this file | ||||||||||
// to you under the Apache License, Version 2.0 (the | ||||||||||
// "License"); you may not use this file except in compliance | ||||||||||
// with the License. You may obtain a copy of the License at | ||||||||||
// | ||||||||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
// | ||||||||||
// Unless required by applicable law or agreed to in writing, | ||||||||||
// software distributed under the License is distributed on an | ||||||||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||||
// KIND, either express or implied. See the License for the | ||||||||||
// specific language governing permissions and limitations | ||||||||||
// under the License. | ||||||||||
|
||||||||||
use std::any::Any; | ||||||||||
use std::cmp::Ordering; | ||||||||||
use std::sync::Arc; | ||||||||||
|
||||||||||
use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait}; | ||||||||||
use arrow::datatypes::DataType; | ||||||||||
|
||||||||||
use datafusion_common::cast::{as_generic_string_array, as_int64_array}; | ||||||||||
use datafusion_common::exec_err; | ||||||||||
use datafusion_common::Result; | ||||||||||
use datafusion_expr::TypeSignature::Exact; | ||||||||||
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; | ||||||||||
|
||||||||||
use crate::utils::{make_scalar_function, utf8_to_str_type}; | ||||||||||
|
||||||||||
#[derive(Debug)] | ||||||||||
pub(super) struct LeftFunc { | ||||||||||
signature: Signature, | ||||||||||
} | ||||||||||
|
||||||||||
impl LeftFunc { | ||||||||||
pub fn new() -> Self { | ||||||||||
use DataType::*; | ||||||||||
Self { | ||||||||||
signature: Signature::one_of( | ||||||||||
vec![Exact(vec![Utf8, Int64]), Exact(vec![LargeUtf8, Int64])], | ||||||||||
Volatility::Immutable, | ||||||||||
), | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
impl ScalarUDFImpl for LeftFunc { | ||||||||||
fn as_any(&self) -> &dyn Any { | ||||||||||
self | ||||||||||
} | ||||||||||
|
||||||||||
fn name(&self) -> &str { | ||||||||||
"left" | ||||||||||
} | ||||||||||
|
||||||||||
fn signature(&self) -> &Signature { | ||||||||||
&self.signature | ||||||||||
} | ||||||||||
|
||||||||||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||||||||||
utf8_to_str_type(&arg_types[0], "left") | ||||||||||
} | ||||||||||
|
||||||||||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||||||||||
match args[0].data_type() { | ||||||||||
DataType::Utf8 => make_scalar_function(left::<i32>, vec![])(args), | ||||||||||
DataType::LargeUtf8 => make_scalar_function(left::<i64>, vec![])(args), | ||||||||||
other => exec_err!("Unsupported data type {other:?} for function left"), | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/// Returns first n characters in the string, or when n is negative, returns all but last |n| characters. | ||||||||||
/// left('abcde', 2) = 'ab' | ||||||||||
/// The implementation uses UTF-8 code points as characters | ||||||||||
pub fn left<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | ||||||||||
let string_array = as_generic_string_array::<T>(&args[0])?; | ||||||||||
let n_array = as_int64_array(&args[1])?; | ||||||||||
let result = string_array | ||||||||||
.iter() | ||||||||||
.zip(n_array.iter()) | ||||||||||
.map(|(string, n)| match (string, n) { | ||||||||||
(Some(string), Some(n)) => match n.cmp(&0) { | ||||||||||
Ordering::Less => { | ||||||||||
let len = string.chars().count() as i64; | ||||||||||
Some(if n.abs() < len { | ||||||||||
string.chars().take((len + n) as usize).collect::<String>() | ||||||||||
} else { | ||||||||||
"".to_string() | ||||||||||
}) | ||||||||||
} | ||||||||||
Ordering::Equal => Some("".to_string()), | ||||||||||
Ordering::Greater => { | ||||||||||
Some(string.chars().take(n as usize).collect::<String>()) | ||||||||||
} | ||||||||||
}, | ||||||||||
_ => None, | ||||||||||
}) | ||||||||||
.collect::<GenericStringArray<T>>(); | ||||||||||
|
||||||||||
Ok(Arc::new(result) as ArrayRef) | ||||||||||
} | ||||||||||
|
||||||||||
#[cfg(test)] | ||||||||||
mod tests { | ||||||||||
use arrow::array::{Array, StringArray}; | ||||||||||
use arrow::datatypes::DataType::Utf8; | ||||||||||
|
||||||||||
use datafusion_common::{Result, ScalarValue}; | ||||||||||
use datafusion_expr::{ColumnarValue, ScalarUDFImpl}; | ||||||||||
|
||||||||||
use crate::unicode::left::LeftFunc; | ||||||||||
use crate::utils::test::test_function; | ||||||||||
|
||||||||||
#[test] | ||||||||||
fn test_functions() -> Result<()> { | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("abcde")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(2))), | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally not needed as this code is just moved, but I think you can write this more concisely with
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (same thing applies to the rest of the tests in this file and and the others) |
||||||||||
], | ||||||||||
Ok(Some("ab")), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("abcde")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(200))), | ||||||||||
], | ||||||||||
Ok(Some("abcde")), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("abcde")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(-2))), | ||||||||||
], | ||||||||||
Ok(Some("abc")), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("abcde")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(-200))), | ||||||||||
], | ||||||||||
Ok(Some("")), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("abcde")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(0))), | ||||||||||
], | ||||||||||
Ok(Some("")), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(None)), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(2))), | ||||||||||
], | ||||||||||
Ok(None), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("abcde")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(None)), | ||||||||||
], | ||||||||||
Ok(None), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("joséésoj")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(5))), | ||||||||||
], | ||||||||||
Ok(Some("joséé")), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(feature = "unicode_expressions")] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new(), | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("joséésoj")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(-3))), | ||||||||||
], | ||||||||||
Ok(Some("joséé")), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
#[cfg(not(feature = "unicode_expressions"))] | ||||||||||
test_function!( | ||||||||||
LeftFunc::new90, | ||||||||||
&[ | ||||||||||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("abcde")))), | ||||||||||
ColumnarValue::Scalar(ScalarValue::Int64(Some(2))), | ||||||||||
], | ||||||||||
internal_err!( | ||||||||||
"function left requires compilation with feature flag: unicode_expressions." | ||||||||||
), | ||||||||||
&str, | ||||||||||
Utf8, | ||||||||||
StringArray | ||||||||||
); | ||||||||||
|
||||||||||
Ok(()) | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the whole module is cfg'd we can probably remove these guards on individual tests