-
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
Support unicode character for initcap
function
#13752
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb24d4a
Support unicode character for 'initcap' function
tlm365 7ef87e9
Update unit tests
tlm365 8323c48
Fix clippy warning
tlm365 23089aa
Update sqllogictests - initcap
tlm365 810977c
Update scalar_functions.md docs
tlm365 1064aa0
Add suggestions change
tlm365 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,9 @@ | |
use std::any::Any; | ||
use std::sync::{Arc, OnceLock}; | ||
|
||
use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait, StringArray}; | ||
use arrow::array::{ | ||
Array, ArrayRef, GenericStringBuilder, OffsetSizeTrait, StringViewBuilder, | ||
}; | ||
use arrow::datatypes::DataType; | ||
|
||
use crate::utils::{make_scalar_function, utf8_to_str_type}; | ||
|
@@ -74,7 +76,7 @@ impl ScalarUDFImpl for InitcapFunc { | |
DataType::LargeUtf8 => make_scalar_function(initcap::<i64>, vec![])(args), | ||
DataType::Utf8View => make_scalar_function(initcap_utf8view, vec![])(args), | ||
other => { | ||
exec_err!("Unsupported data type {other:?} for function initcap") | ||
exec_err!("Unsupported data type {other:?} for function `initcap`") | ||
} | ||
} | ||
} | ||
|
@@ -90,9 +92,8 @@ fn get_initcap_doc() -> &'static Documentation { | |
DOCUMENTATION.get_or_init(|| { | ||
Documentation::builder( | ||
DOC_SECTION_STRING, | ||
"Capitalizes the first character in each word in the ASCII input string. \ | ||
Words are delimited by non-alphanumeric characters.\n\n\ | ||
Note this function does not support UTF-8 characters.", | ||
"Capitalizes the first character in each word in the input string. \ | ||
Words are delimited by non-alphanumeric characters.", | ||
"initcap(str)", | ||
) | ||
.with_sql_example( | ||
|
@@ -123,50 +124,70 @@ fn get_initcap_doc() -> &'static Documentation { | |
fn initcap<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
let string_array = as_generic_string_array::<T>(&args[0])?; | ||
|
||
// first map is the iterator, second is for the `Option<_>` | ||
let result = string_array | ||
.iter() | ||
.map(initcap_string) | ||
.collect::<GenericStringArray<T>>(); | ||
let mut builder = GenericStringBuilder::<T>::with_capacity( | ||
string_array.len(), | ||
string_array.value_data().len(), | ||
); | ||
|
||
Ok(Arc::new(result) as ArrayRef) | ||
string_array.iter().for_each(|str| match str { | ||
Some(s) => { | ||
let initcap_str = initcap_string(s); | ||
builder.append_value(initcap_str); | ||
} | ||
None => builder.append_null(), | ||
}); | ||
|
||
Ok(Arc::new(builder.finish()) as ArrayRef) | ||
} | ||
|
||
fn initcap_utf8view(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
let string_view_array = as_string_view_array(&args[0])?; | ||
|
||
let result = string_view_array | ||
.iter() | ||
.map(initcap_string) | ||
.collect::<StringArray>(); | ||
let mut builder = StringViewBuilder::with_capacity(string_view_array.len()); | ||
|
||
string_view_array.iter().for_each(|str| match str { | ||
Some(s) => { | ||
let initcap_str = initcap_string(s); | ||
builder.append_value(initcap_str); | ||
} | ||
None => builder.append_null(), | ||
}); | ||
|
||
Ok(Arc::new(result) as ArrayRef) | ||
Ok(Arc::new(builder.finish()) as ArrayRef) | ||
} | ||
|
||
fn initcap_string(input: Option<&str>) -> Option<String> { | ||
input.map(|s| { | ||
let mut result = String::with_capacity(s.len()); | ||
let mut prev_is_alphanumeric = false; | ||
fn initcap_string(input: &str) -> String { | ||
let mut result = String::with_capacity(input.len()); | ||
let mut prev_is_alphanumeric = false; | ||
|
||
for c in s.chars() { | ||
let transformed = if prev_is_alphanumeric { | ||
c.to_ascii_lowercase() | ||
if input.is_ascii() { | ||
for c in input.chars() { | ||
if prev_is_alphanumeric { | ||
result.push(c.to_ascii_lowercase()); | ||
} else { | ||
c.to_ascii_uppercase() | ||
result.push(c.to_ascii_uppercase()); | ||
}; | ||
result.push(transformed); | ||
prev_is_alphanumeric = c.is_ascii_alphanumeric(); | ||
} | ||
} else { | ||
for c in input.chars() { | ||
if prev_is_alphanumeric { | ||
result.extend(c.to_lowercase()); | ||
} else { | ||
result.extend(c.to_uppercase()); | ||
} | ||
prev_is_alphanumeric = c.is_alphanumeric(); | ||
} | ||
} | ||
|
||
result | ||
}) | ||
result | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::string::initcap::InitcapFunc; | ||
use crate::unicode::initcap::InitcapFunc; | ||
use crate::utils::test::test_function; | ||
use arrow::array::{Array, StringArray}; | ||
use arrow::array::{Array, StringArray, StringViewArray}; | ||
use arrow::datatypes::DataType::Utf8; | ||
use datafusion_common::{Result, ScalarValue}; | ||
use datafusion_expr::{ColumnarValue, ScalarUDFImpl}; | ||
|
@@ -181,6 +202,19 @@ mod tests { | |
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
vec![ColumnarValue::Scalar(ScalarValue::Utf8(Some( | ||
"êM ả ñAnDÚ ÁrBOL ОлЕГ ИвАНОВИч ÍslENsku ÞjóðaRiNNaR εΛλΗΝΙκΉ" | ||
.to_string() | ||
)))], | ||
Ok(Some( | ||
"Êm Ả Ñandú Árbol Олег Иванович Íslensku Þjóðarinnar Ελληνική" | ||
)), | ||
&str, | ||
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
vec![ColumnarValue::Scalar(ScalarValue::from(""))], | ||
|
@@ -205,6 +239,7 @@ mod tests { | |
Utf8, | ||
StringArray | ||
); | ||
|
||
test_function!( | ||
InitcapFunc::new(), | ||
vec![ColumnarValue::Scalar(ScalarValue::Utf8View(Some( | ||
|
@@ -213,7 +248,7 @@ mod tests { | |
Ok(Some("Hi Thomas")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
StringViewArray | ||
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. 👍 |
||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
|
@@ -223,7 +258,20 @@ mod tests { | |
Ok(Some("Hi Thomas With M0re Than 12 Chars")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
StringViewArray | ||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
vec![ColumnarValue::Scalar(ScalarValue::Utf8View(Some( | ||
"đẸp đẼ êM ả ñAnDÚ ÁrBOL ОлЕГ ИвАНОВИч ÍslENsku ÞjóðaRiNNaR εΛλΗΝΙκΉ" | ||
.to_string() | ||
)))], | ||
Ok(Some( | ||
"Đẹp Đẽ Êm Ả Ñandú Árbol Олег Иванович Íslensku Þjóðarinnar Ελληνική" | ||
)), | ||
&str, | ||
Utf8, | ||
StringViewArray | ||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
|
@@ -233,15 +281,15 @@ mod tests { | |
Ok(Some("")), | ||
&str, | ||
Utf8, | ||
StringArray | ||
StringViewArray | ||
); | ||
test_function!( | ||
InitcapFunc::new(), | ||
vec![ColumnarValue::Scalar(ScalarValue::Utf8View(None))], | ||
Ok(None), | ||
&str, | ||
Utf8, | ||
StringArray | ||
StringViewArray | ||
); | ||
|
||
Ok(()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -460,7 +460,7 @@ Andrew Datafusion📊🔥 | |
Xiangpeng Datafusion数据融合 | ||
Raphael Datafusionдатафусион | ||
Under_Score Un Iść Core | ||
Percent Pan Tadeusz Ma Iść W KąT | ||
Percent Pan Tadeusz Ma Iść W Ką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. I've checked and this result is compatible with PostgreSQL |
||
(empty) (empty) | ||
(empty) (empty) | ||
% (empty) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Incompatible arg should be checked during planning before, thus here is unreachable, and we can use
internal_err
to indicate a potential bug if it's executedThere 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.
@2010YOUY01 Thanks for reviewing,
Make sense to me👍