Skip to content
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

feat: support upper and lower for stringview #12138

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion datafusion/functions/src/string/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::Arc;
use arrow::array::{
new_null_array, Array, ArrayAccessor, ArrayDataBuilder, ArrayIter, ArrayRef,
GenericStringArray, GenericStringBuilder, OffsetSizeTrait, StringArray,
StringViewArray,
StringBuilder, StringViewArray,
};
use arrow::buffer::{Buffer, MutableBuffer, NullBuffer};
use arrow::datatypes::DataType;
Expand Down Expand Up @@ -214,6 +214,23 @@ where
i64,
_,
>(array, op)?)),
DataType::Utf8View => {
let string_array = as_string_view_array(array)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure happy with this implementation, but it seems to work. I tried something akin to:

let result = string_array.iter().map(|string| string.map(op));

but that seems to require a clone of op which isn't allowed based on the existing function definition, and seems worth avoiding.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this implemenation looks reasonable to me. Thanks @tshauck

let mut string_builder = StringBuilder::with_capacity(
string_array.len(),
string_array.get_array_memory_size(),
);

for str in string_array.iter() {
if let Some(str) = str {
string_builder.append_value(op(str));
} else {
string_builder.append_null();
}
}

Ok(ColumnarValue::Array(Arc::new(string_builder.finish())))
}
other => exec_err!("Unsupported data type {other:?} for function {name}"),
},
ColumnarValue::Scalar(scalar) => match scalar {
Expand All @@ -225,6 +242,10 @@ where
let result = a.as_ref().map(|x| op(x));
Ok(ColumnarValue::Scalar(ScalarValue::LargeUtf8(result)))
}
ScalarValue::Utf8View(a) => {
let result = a.as_ref().map(|x| op(x));
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(result)))
}
other => exec_err!("Unsupported data type {other:?} for function {name}"),
},
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/functions/src/string/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl LowerFunc {
Self {
signature: Signature::uniform(
1,
vec![Utf8, LargeUtf8],
vec![Utf8, LargeUtf8, Utf8View],
Volatility::Immutable,
),
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/functions/src/string/upper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl UpperFunc {
Self {
signature: Signature::uniform(
1,
vec![Utf8, LargeUtf8],
vec![Utf8, LargeUtf8, Utf8View],
Volatility::Immutable,
),
}
Expand Down
34 changes: 29 additions & 5 deletions datafusion/sqllogictest/test_files/string_view.slt
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,6 @@ Xiangpeng
Raphael
NULL



### Initcap

query TT
Expand Down Expand Up @@ -501,7 +499,7 @@ SELECT
INITCAP(column1_large_utf8_lower) as c3
FROM test_lowercase;
----
Andrew Andrew Andrew
Andrew Andrew Andrew
Xiangpeng Xiangpeng Xiangpeng
Raphael Raphael Raphael
NULL NULL NULL
Expand Down Expand Up @@ -828,16 +826,42 @@ logical_plan
02)--TableScan: test projection=[column1_utf8view, column2_utf8view]

## Ensure no casts for LOWER
## TODO https://github.com/apache/datafusion/issues/11855
query TT
EXPLAIN SELECT
LOWER(column1_utf8view) as c1
FROM test;
----
logical_plan
01)Projection: lower(CAST(test.column1_utf8view AS Utf8)) AS c1
01)Projection: lower(test.column1_utf8view) AS c1
02)--TableScan: test projection=[column1_utf8view]

query T
SELECT LOWER(column1_utf8view) as c1
FROM test;
----
andrew
xiangpeng
raphael
NULL

## Ensure no casts for UPPER
query TT
EXPLAIN SELECT
UPPER(column1_utf8view) as c1
FROM test;
----
logical_plan
01)Projection: upper(test.column1_utf8view) AS c1
02)--TableScan: test projection=[column1_utf8view]

query T
SELECT UPPER(column1_utf8view) as c1
FROM test;
----
ANDREW
XIANGPENG
RAPHAEL
NULL

## Ensure no casts for LPAD
query TT
Expand Down