-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Make lower and upper emit Utf8View for Utf8View input #20616
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
Changes from 3 commits
837f896
d92a711
d553ac1
825b423
5adb958
4ddfc61
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 |
|---|---|---|
|
|
@@ -20,9 +20,10 @@ | |
| use std::sync::Arc; | ||
|
|
||
| use crate::strings::make_and_append_view; | ||
| use crate::utils::utf8_to_str_type; | ||
| use arrow::array::{ | ||
| Array, ArrayRef, GenericStringArray, GenericStringBuilder, NullBufferBuilder, | ||
| OffsetSizeTrait, StringBuilder, StringViewArray, new_null_array, | ||
| OffsetSizeTrait, StringViewArray, StringViewBuilder, new_null_array, | ||
| }; | ||
| use arrow::buffer::{Buffer, ScalarBuffer}; | ||
| use arrow::datatypes::DataType; | ||
|
|
@@ -331,6 +332,22 @@ fn string_trim<T: OffsetSizeTrait, Tr: Trimmer>(args: &[ArrayRef]) -> Result<Arr | |
| } | ||
| } | ||
|
|
||
| pub(crate) fn case_conversion_return_type( | ||
| arg_type: &DataType, | ||
| name: &str, | ||
| ) -> Result<DataType> { | ||
| match arg_type { | ||
| DataType::Utf8View | DataType::BinaryView => Ok(DataType::Utf8View), | ||
|
Contributor
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 believe |
||
| DataType::Dictionary(_, value_type) | ||
| if **value_type == DataType::Utf8View | ||
| || **value_type == DataType::BinaryView => | ||
| { | ||
| Ok(DataType::Utf8View) | ||
| } | ||
| _ => utf8_to_str_type(arg_type, name), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn to_lower(args: &[ColumnarValue], name: &str) -> Result<ColumnarValue> { | ||
| case_conversion(args, |string| string.to_lowercase(), name) | ||
| } | ||
|
|
@@ -358,10 +375,8 @@ where | |
| >(array, op)?)), | ||
| DataType::Utf8View => { | ||
| let string_array = as_string_view_array(array)?; | ||
| let mut string_builder = StringBuilder::with_capacity( | ||
| string_array.len(), | ||
| string_array.get_array_memory_size(), | ||
| ); | ||
| let mut string_builder = | ||
| StringViewBuilder::with_capacity(string_array.len()); | ||
|
|
||
| for str in string_array.iter() { | ||
| if let Some(str) = str { | ||
|
|
@@ -386,7 +401,7 @@ where | |
| } | ||
| ScalarValue::Utf8View(a) => { | ||
| let result = a.as_ref().map(|x| op(x)); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Utf8(result))) | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(result))) | ||
| } | ||
| other => exec_err!("Unsupported data type {other:?} for function {name}"), | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,7 @@ | |
| use arrow::datatypes::DataType; | ||
| use std::any::Any; | ||
|
|
||
| use crate::string::common::to_lower; | ||
| use crate::utils::utf8_to_str_type; | ||
| use crate::string::common::{case_conversion_return_type, to_lower}; | ||
| use datafusion_common::Result; | ||
| use datafusion_common::types::logical_string; | ||
| use datafusion_expr::{ | ||
|
|
@@ -82,7 +81,7 @@ impl ScalarUDFImpl for LowerFunc { | |
| } | ||
|
|
||
| fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
| utf8_to_str_type(&arg_types[0], "lower") | ||
| case_conversion_return_type(&arg_types[0], "lower") | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
|
|
@@ -97,8 +96,7 @@ impl ScalarUDFImpl for LowerFunc { | |
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use arrow::array::{Array, ArrayRef, StringArray}; | ||
| use arrow::datatypes::DataType::Utf8; | ||
| use arrow::array::{Array, ArrayRef, StringArray, StringViewArray}; | ||
| use arrow::datatypes::Field; | ||
| use datafusion_common::config::ConfigOptions; | ||
| use std::sync::Arc; | ||
|
|
@@ -111,7 +109,7 @@ mod tests { | |
| number_rows: input.len(), | ||
| args: vec![ColumnarValue::Array(input)], | ||
| arg_fields, | ||
| return_field: Field::new("f", Utf8, true).into(), | ||
| return_field: Field::new("f", expected.data_type().clone(), true).into(), | ||
| config_options: Arc::new(ConfigOptions::default()), | ||
| }; | ||
|
|
||
|
|
@@ -197,4 +195,31 @@ mod tests { | |
|
|
||
| to_lower(input, expected) | ||
| } | ||
|
|
||
| #[test] | ||
| fn lower_utf8view() -> Result<()> { | ||
| let input = Arc::new(StringViewArray::from(vec![ | ||
| Some("ARROW"), | ||
| None, | ||
| Some("TSCHÜSS"), | ||
| ])) as ArrayRef; | ||
|
|
||
| let expected = Arc::new(StringViewArray::from(vec![ | ||
| Some("arrow"), | ||
| None, | ||
| Some("tschüss"), | ||
| ])) as ArrayRef; | ||
|
|
||
| to_lower(input, expected) | ||
| } | ||
|
|
||
| #[test] | ||
| fn lower_return_type_dictionary_utf8view() -> Result<()> { | ||
|
Contributor
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. For symmetry, should we add an analogous test case for |
||
| let return_type = LowerFunc::new().return_type(&[DataType::Dictionary( | ||
| Box::new(DataType::Int32), | ||
| Box::new(DataType::Utf8View), | ||
| )])?; | ||
| assert_eq!(return_type, DataType::Utf8View); | ||
| Ok(()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -445,6 +445,21 @@ SELECT upper(arrow_cast('árvore ação αβγ', 'Dictionary(Int32, Utf8)')) | |
| ---- | ||
| ÁRVORE AÇÃO ΑΒΓ | ||
|
|
||
| query T | ||
| SELECT arrow_typeof(upper('foo')) | ||
| ---- | ||
| Utf8 | ||
|
|
||
| query T | ||
| SELECT arrow_typeof(upper(arrow_cast('foo', 'LargeUtf8'))) | ||
| ---- | ||
| LargeUtf8 | ||
|
|
||
| query T | ||
| SELECT arrow_typeof(upper(arrow_cast('foo', 'Utf8View'))) | ||
|
Contributor
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. Add SLT tests for dictionary with a Utf8View value type?
Contributor
Author
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'm not sure about the new tests i added, can you take a look. |
||
| ---- | ||
| Utf8View | ||
|
|
||
| query T | ||
| SELECT btrim(' foo ') | ||
| ---- | ||
|
|
@@ -500,6 +515,21 @@ SELECT lower(arrow_cast('ÁRVORE AÇÃO ΑΒΓ', 'Dictionary(Int32, Utf8)')) | |
| ---- | ||
| árvore ação αβγ | ||
|
|
||
| query T | ||
| SELECT arrow_typeof(lower('FOObar')) | ||
| ---- | ||
| Utf8 | ||
|
|
||
| query T | ||
| SELECT arrow_typeof(lower(arrow_cast('FOObar', 'LargeUtf8'))) | ||
| ---- | ||
| LargeUtf8 | ||
|
|
||
| query T | ||
| SELECT arrow_typeof(lower(arrow_cast('FOObar', 'Utf8View'))) | ||
| ---- | ||
| Utf8View | ||
|
|
||
| query T | ||
| SELECT ltrim(' foo') | ||
| ---- | ||
|
|
||
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.
This doesn't seem specific to case conversion; we probably need a more generic facility for finding the return return type for a function that takes a string. But we can handle that 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.
I believe we can remove this function entirely in favour of just
Ok(arg_types[0].clone())in the return type implementation; it does mean omitting dictionary handling but it seems the actual invoke code for lower/upper functions don't handle dictionaries anyway?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.
Good idea.