Skip to content

Commit 2a4e7f7

Browse files
committed
Support binary / largebinary arguments to string functions
1 parent d9f414b commit 2a4e7f7

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

datafusion/expr/src/built_in_function.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,12 +1523,17 @@ impl FromStr for BuiltinScalarFunction {
15231523
}
15241524
}
15251525

1526+
/// Creates a function that returns a type based on the type of string to the
1527+
/// first argument
1528+
///
1529+
/// If the input type is `LargeUtf8` or `LargeBinary` the return type is `largeUtf8Type`,
1530+
/// If the input type is `Utf8` or `Binary` the return type is `utf8Type`,
15261531
macro_rules! make_utf8_to_return_type {
15271532
($FUNC:ident, $largeUtf8Type:expr, $utf8Type:expr) => {
15281533
fn $FUNC(arg_type: &DataType, name: &str) -> Result<DataType> {
15291534
Ok(match arg_type {
1530-
DataType::LargeUtf8 => $largeUtf8Type,
1531-
DataType::Utf8 => $utf8Type,
1535+
DataType::LargeUtf8 | DataType::LargeBinary => $largeUtf8Type,
1536+
DataType::Utf8 | DataType::Binary => $utf8Type,
15321537
DataType::Null => DataType::Null,
15331538
DataType::Dictionary(_, value_type) => match **value_type {
15341539
DataType::LargeUtf8 => $largeUtf8Type,
@@ -1554,7 +1559,9 @@ macro_rules! make_utf8_to_return_type {
15541559
};
15551560
}
15561561

1562+
/// `utf8_to_str_type` returns either a Utf8 or LargeUtf8 based on the input type size.
15571563
make_utf8_to_return_type!(utf8_to_str_type, DataType::LargeUtf8, DataType::Utf8);
1564+
/// `utf8_to_str_type` returns either a Int32 or Int64 based on the input type size.
15581565
make_utf8_to_return_type!(utf8_to_int_type, DataType::Int64, DataType::Int32);
15591566

15601567
fn utf8_or_binary_to_binary_type(arg_type: &DataType, name: &str) -> Result<DataType> {

datafusion/sqllogictest/test_files/binary.slt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,29 @@ SELECT largebinary FROM t where largebinary LIKE '%F';
226226

227227

228228
# character_length function
229-
# https://github.com/apache/arrow-datafusion/issues/7344
230-
query error DataFusion error: Error during planning: The "character_length" function can only accept strings, but got Binary\.
229+
query TITI
231230
SELECT
232231
cast(binary as varchar) as str,
233232
character_length(binary) as binary_len,
234233
cast(largebinary as varchar) as large_str,
235234
character_length(binary) as largebinary_len
236235
from t;
236+
----
237+
Foo 3 Foo 3
238+
NULL NULL NULL NULL
239+
Bar 3 Bar 3
240+
FooBar 6 FooBar 6
237241

238242
# regexp_replace
239-
# https://github.com/apache/arrow-datafusion/issues/7345
240-
query error DataFusion error: Error during planning: The "regexp_replace" function can only accept strings, but got Binary\.
243+
query TTTT
241244
SELECT
242245
cast(binary as varchar) as str,
243246
regexp_replace(binary, 'F', 'f') as binary_replaced,
244247
cast(largebinary as varchar) as large_str,
245248
regexp_replace(largebinary, 'F', 'f') as large_binary_replaced
246249
from t;
250+
----
251+
Foo foo Foo foo
252+
NULL NULL NULL NULL
253+
Bar Bar Bar Bar
254+
FooBar fooBar FooBar fooBar

0 commit comments

Comments
 (0)