Skip to content

Commit 9b3d6a4

Browse files
authored
Make lower and upper emit Utf8View for Utf8View input (#20616)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Part of #20585 ## Rationale for this change String UDFs should preserve string representation where feasible. lower and upper previously accepted Utf8View input but emitted Utf8, causing an unnecessary type downgrade. This aligns both with the expected behavior of returning the same string type as its primary input. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Updated lower and upper return type inference to emit Utf8View when input is Utf8View <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Yes <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent b51edff commit 9b3d6a4

4 files changed

Lines changed: 94 additions & 16 deletions

File tree

datafusion/functions/src/string/common.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::sync::Arc;
2222
use crate::strings::make_and_append_view;
2323
use arrow::array::{
2424
Array, ArrayRef, GenericStringArray, GenericStringBuilder, NullBufferBuilder,
25-
OffsetSizeTrait, StringBuilder, StringViewArray, new_null_array,
25+
OffsetSizeTrait, StringViewArray, StringViewBuilder, new_null_array,
2626
};
2727
use arrow::buffer::{Buffer, ScalarBuffer};
2828
use arrow::datatypes::DataType;
@@ -358,10 +358,8 @@ where
358358
>(array, op)?)),
359359
DataType::Utf8View => {
360360
let string_array = as_string_view_array(array)?;
361-
let mut string_builder = StringBuilder::with_capacity(
362-
string_array.len(),
363-
string_array.get_array_memory_size(),
364-
);
361+
let mut string_builder =
362+
StringViewBuilder::with_capacity(string_array.len());
365363

366364
for str in string_array.iter() {
367365
if let Some(str) = str {
@@ -386,7 +384,7 @@ where
386384
}
387385
ScalarValue::Utf8View(a) => {
388386
let result = a.as_ref().map(|x| op(x));
389-
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(result)))
387+
Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(result)))
390388
}
391389
other => exec_err!("Unsupported data type {other:?} for function {name}"),
392390
},

datafusion/functions/src/string/lower.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use arrow::datatypes::DataType;
1919
use std::any::Any;
2020

2121
use crate::string::common::to_lower;
22-
use crate::utils::utf8_to_str_type;
2322
use datafusion_common::Result;
2423
use datafusion_common::types::logical_string;
2524
use datafusion_expr::{
@@ -82,7 +81,7 @@ impl ScalarUDFImpl for LowerFunc {
8281
}
8382

8483
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
85-
utf8_to_str_type(&arg_types[0], "lower")
84+
Ok(arg_types[0].clone())
8685
}
8786

8887
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
@@ -97,8 +96,7 @@ impl ScalarUDFImpl for LowerFunc {
9796
#[cfg(test)]
9897
mod tests {
9998
use super::*;
100-
use arrow::array::{Array, ArrayRef, StringArray};
101-
use arrow::datatypes::DataType::Utf8;
99+
use arrow::array::{Array, ArrayRef, StringArray, StringViewArray};
102100
use arrow::datatypes::Field;
103101
use datafusion_common::config::ConfigOptions;
104102
use std::sync::Arc;
@@ -111,7 +109,7 @@ mod tests {
111109
number_rows: input.len(),
112110
args: vec![ColumnarValue::Array(input)],
113111
arg_fields,
114-
return_field: Field::new("f", Utf8, true).into(),
112+
return_field: Field::new("f", expected.data_type().clone(), true).into(),
115113
config_options: Arc::new(ConfigOptions::default()),
116114
};
117115

@@ -197,4 +195,21 @@ mod tests {
197195

198196
to_lower(input, expected)
199197
}
198+
199+
#[test]
200+
fn lower_utf8view() -> Result<()> {
201+
let input = Arc::new(StringViewArray::from(vec![
202+
Some("ARROW"),
203+
None,
204+
Some("TSCHÜSS"),
205+
])) as ArrayRef;
206+
207+
let expected = Arc::new(StringViewArray::from(vec![
208+
Some("arrow"),
209+
None,
210+
Some("tschüss"),
211+
])) as ArrayRef;
212+
213+
to_lower(input, expected)
214+
}
200215
}

datafusion/functions/src/string/upper.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License.
1717

1818
use crate::string::common::to_upper;
19-
use crate::utils::utf8_to_str_type;
2019
use arrow::datatypes::DataType;
2120
use datafusion_common::Result;
2221
use datafusion_common::types::logical_string;
@@ -81,7 +80,7 @@ impl ScalarUDFImpl for UpperFunc {
8180
}
8281

8382
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
84-
utf8_to_str_type(&arg_types[0], "upper")
83+
Ok(arg_types[0].clone())
8584
}
8685

8786
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
@@ -96,8 +95,7 @@ impl ScalarUDFImpl for UpperFunc {
9695
#[cfg(test)]
9796
mod tests {
9897
use super::*;
99-
use arrow::array::{Array, ArrayRef, StringArray};
100-
use arrow::datatypes::DataType::Utf8;
98+
use arrow::array::{Array, ArrayRef, StringArray, StringViewArray};
10199
use arrow::datatypes::Field;
102100
use datafusion_common::config::ConfigOptions;
103101
use std::sync::Arc;
@@ -110,7 +108,7 @@ mod tests {
110108
number_rows: input.len(),
111109
args: vec![ColumnarValue::Array(input)],
112110
arg_fields: vec![arg_field],
113-
return_field: Field::new("f", Utf8, true).into(),
111+
return_field: Field::new("f", expected.data_type().clone(), true).into(),
114112
config_options: Arc::new(ConfigOptions::default()),
115113
};
116114

@@ -196,4 +194,21 @@ mod tests {
196194

197195
to_upper(input, expected)
198196
}
197+
198+
#[test]
199+
fn upper_utf8view() -> Result<()> {
200+
let input = Arc::new(StringViewArray::from(vec![
201+
Some("arrow"),
202+
None,
203+
Some("tschüß"),
204+
])) as ArrayRef;
205+
206+
let expected = Arc::new(StringViewArray::from(vec![
207+
Some("ARROW"),
208+
None,
209+
Some("TSCHÜSS"),
210+
])) as ArrayRef;
211+
212+
to_upper(input, expected)
213+
}
199214
}

datafusion/sqllogictest/test_files/functions.slt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ SELECT upper(arrow_cast('foo', 'Dictionary(Int32, Utf8)'))
435435
----
436436
FOO
437437

438+
query T
439+
SELECT upper(arrow_cast(arrow_cast('foo', 'Dictionary(Int32, Utf8)'), 'Dictionary(Int32, Utf8View)'))
440+
----
441+
FOO
442+
438443
query T
439444
SELECT upper('árvore ação αβγ')
440445
----
@@ -445,6 +450,26 @@ SELECT upper(arrow_cast('árvore ação αβγ', 'Dictionary(Int32, Utf8)'))
445450
----
446451
ÁRVORE AÇÃO ΑΒΓ
447452

453+
query T
454+
SELECT arrow_typeof(upper('foo'))
455+
----
456+
Utf8
457+
458+
query T
459+
SELECT arrow_typeof(upper(arrow_cast('foo', 'LargeUtf8')))
460+
----
461+
LargeUtf8
462+
463+
query T
464+
SELECT arrow_typeof(upper(arrow_cast('foo', 'Utf8View')))
465+
----
466+
Utf8View
467+
468+
query T
469+
SELECT arrow_typeof(upper(arrow_cast(arrow_cast('foo', 'Dictionary(Int32, Utf8)'), 'Dictionary(Int32, Utf8View)')))
470+
----
471+
Utf8View
472+
448473
query T
449474
SELECT btrim(' foo ')
450475
----
@@ -490,6 +515,11 @@ SELECT lower(arrow_cast('FOObar', 'Dictionary(Int32, Utf8)'))
490515
----
491516
foobar
492517

518+
query T
519+
SELECT lower(arrow_cast(arrow_cast('FOObar', 'Dictionary(Int32, Utf8)'), 'Dictionary(Int32, Utf8View)'))
520+
----
521+
foobar
522+
493523
query T
494524
SELECT lower('ÁRVORE AÇÃO ΑΒΓ')
495525
----
@@ -500,6 +530,26 @@ SELECT lower(arrow_cast('ÁRVORE AÇÃO ΑΒΓ', 'Dictionary(Int32, Utf8)'))
500530
----
501531
árvore ação αβγ
502532

533+
query T
534+
SELECT arrow_typeof(lower('FOObar'))
535+
----
536+
Utf8
537+
538+
query T
539+
SELECT arrow_typeof(lower(arrow_cast('FOObar', 'LargeUtf8')))
540+
----
541+
LargeUtf8
542+
543+
query T
544+
SELECT arrow_typeof(lower(arrow_cast('FOObar', 'Utf8View')))
545+
----
546+
Utf8View
547+
548+
query T
549+
SELECT arrow_typeof(lower(arrow_cast(arrow_cast('FOObar', 'Dictionary(Int32, Utf8)'), 'Dictionary(Int32, Utf8View)')))
550+
----
551+
Utf8View
552+
503553
query T
504554
SELECT ltrim(' foo')
505555
----

0 commit comments

Comments
 (0)