-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Update INITCAP scalar function to support Utf8View #11888
Changes from 1 commit
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 |
---|---|---|
|
@@ -21,7 +21,7 @@ use std::sync::Arc; | |
use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait}; | ||
use arrow::datatypes::DataType; | ||
|
||
use datafusion_common::cast::as_generic_string_array; | ||
use datafusion_common::cast::{as_generic_string_array, as_string_view_array}; | ||
use datafusion_common::{exec_err, Result}; | ||
use datafusion_expr::{ColumnarValue, Volatility}; | ||
use datafusion_expr::{ScalarUDFImpl, Signature}; | ||
|
@@ -45,7 +45,7 @@ impl InitcapFunc { | |
Self { | ||
signature: Signature::uniform( | ||
1, | ||
vec![Utf8, LargeUtf8], | ||
vec![Utf8, LargeUtf8, Utf8View], | ||
Volatility::Immutable, | ||
), | ||
} | ||
|
@@ -73,6 +73,9 @@ impl ScalarUDFImpl for InitcapFunc { | |
match args[0].data_type() { | ||
DataType::Utf8 => make_scalar_function(initcap::<i32>, vec![])(args), | ||
DataType::LargeUtf8 => make_scalar_function(initcap::<i64>, vec![])(args), | ||
DataType::Utf8View => { | ||
make_scalar_function(initcap_utf8view::<i32>, vec![])(args) | ||
} | ||
other => { | ||
exec_err!("Unsupported data type {other:?} for function initcap") | ||
} | ||
|
@@ -88,28 +91,40 @@ fn initcap<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | |
// first map is the iterator, second is for the `Option<_>` | ||
let result = string_array | ||
.iter() | ||
.map(|string| { | ||
string.map(|string: &str| { | ||
let mut char_vector = Vec::<char>::new(); | ||
let mut previous_character_letter_or_number = false; | ||
for c in string.chars() { | ||
if previous_character_letter_or_number { | ||
char_vector.push(c.to_ascii_lowercase()); | ||
} else { | ||
char_vector.push(c.to_ascii_uppercase()); | ||
} | ||
previous_character_letter_or_number = c.is_ascii_uppercase() | ||
|| c.is_ascii_lowercase() | ||
|| c.is_ascii_digit(); | ||
} | ||
char_vector.iter().collect::<String>() | ||
}) | ||
}) | ||
.map(initcap_string) | ||
.collect::<GenericStringArray<T>>(); | ||
|
||
Ok(Arc::new(result) as ArrayRef) | ||
} | ||
|
||
fn initcap_utf8view<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
xinlifoobar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let string_view_array = as_string_view_array(&args[0])?; | ||
|
||
let result = string_view_array | ||
.iter() | ||
.map(initcap_string) | ||
.collect::<GenericStringArray<T>>(); | ||
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. The return type is a 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. The current |
||
|
||
Ok(Arc::new(result) as ArrayRef) | ||
} | ||
|
||
fn initcap_string(string: Option<&str>) -> Option<String> { | ||
string.map(|string: &str| { | ||
let mut char_vector = Vec::<char>::new(); | ||
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. I suspect you could make this faster by creating the vector once and then resetting on each loop -- like let mut char_vector = Vec::<char>::new();
string.map(|string: &str| {
char_vector.clear();
...
} |
||
let mut previous_character_letter_or_number = false; | ||
for c in string.chars() { | ||
if previous_character_letter_or_number { | ||
char_vector.push(c.to_ascii_lowercase()); | ||
} else { | ||
char_vector.push(c.to_ascii_uppercase()); | ||
} | ||
previous_character_letter_or_number = | ||
c.is_ascii_uppercase() || c.is_ascii_lowercase() || c.is_ascii_digit(); | ||
} | ||
char_vector.iter().collect::<String>() | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::string::initcap::InitcapFunc; | ||
|
@@ -153,6 +168,34 @@ mod tests { | |
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::Utf8View(Some( | ||
"hi THOMAS".to_string() | ||
)))], | ||
Ok(Some("Hi Thomas")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::Utf8View(Some( | ||
"".to_string() | ||
)))], | ||
Ok(Some("")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
&[ColumnarValue::Scalar(ScalarValue::Utf8View(None))], | ||
Ok(None), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
xinlifoobar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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.
I am considering it might be a bit heavy to make this a
macro
, we wouldn't have a lot of initcap_* like this.