Skip to content

Commit 75ec5e3

Browse files
committed
fix: encode function should work with strings and binary
closes apache#14055
1 parent 22b1873 commit 75ec5e3

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

datafusion/functions/src/encoding/inner.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,18 @@ impl ScalarUDFImpl for EncodeFunc {
8787
}
8888

8989
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
90-
Ok(arg_types[0].to_owned())
90+
use DataType::*;
91+
92+
Ok(match arg_types[0] {
93+
Utf8 => Utf8,
94+
LargeUtf8 => LargeUtf8,
95+
Binary => Utf8,
96+
LargeBinary => LargeUtf8,
97+
Null => Null,
98+
_ => {
99+
return plan_err!("The encode function can only accept Utf8 or Binary or Null.");
100+
}
101+
})
91102
}
92103

93104
fn invoke_batch(
@@ -112,12 +123,18 @@ impl ScalarUDFImpl for EncodeFunc {
112123
}
113124

114125
match arg_types[0] {
115-
DataType::Utf8 | DataType::Utf8View | DataType::Binary | DataType::Null => {
126+
DataType::Utf8 | DataType::Utf8View | DataType::Null => {
116127
Ok(vec![DataType::Utf8; 2])
117128
}
118-
DataType::LargeUtf8 | DataType::LargeBinary => {
129+
DataType::LargeUtf8 => {
119130
Ok(vec![DataType::LargeUtf8, DataType::Utf8])
120131
}
132+
DataType::Binary => {
133+
Ok(vec![DataType::Binary, DataType::Utf8])
134+
}
135+
DataType::LargeBinary => {
136+
Ok(vec![DataType::LargeBinary, DataType::Utf8])
137+
}
121138
_ => plan_err!(
122139
"1st argument should be Utf8 or Binary or Null, got {:?}",
123140
arg_types[0]

datafusion/sqllogictest/test_files/encoding.slt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ CREATE TABLE test(
2323
hex_field TEXT
2424
) as VALUES
2525
(0, 'abc', encode('abc', 'base64'), encode('abc', 'hex')),
26-
(1, 'qweqwe', encode('qweqwe', 'base64'), encode('qweqwe', 'hex')),
27-
(2, NULL, NULL, NULL)
26+
(1, 'qweqwe', encode('qweqwe', 'base64'), encode('qweqwe', 'hex')),
27+
(2, NULL, NULL, NULL),
28+
(3, X'8f50d3f60eae370ddbf85c86219c55108a350165', encode('abc', 'base64'), encode('abc', 'hex'))
2829
;
2930

3031
# errors
@@ -50,27 +51,31 @@ SELECT encode(bin_field, 'hex') FROM test ORDER BY num;
5051
616263
5152
717765717765
5253
NULL
54+
8f50d3f60eae370ddbf85c86219c55108a350165
5355

5456
query T
5557
SELECT arrow_cast(decode(base64_field, 'base64'), 'Utf8') FROM test ORDER BY num;
5658
----
5759
abc
5860
qweqwe
5961
NULL
62+
abc
6063

6164
query T
6265
SELECT arrow_cast(decode(hex_field, 'hex'), 'Utf8') FROM test ORDER BY num;
6366
----
6467
abc
6568
qweqwe
6669
NULL
70+
abc
6771

6872
query T
6973
select to_hex(num) from test ORDER BY num;
7074
----
7175
0
7276
1
7377
2
78+
3
7479

7580
# test for Utf8View support for encode
7681
statement ok
@@ -101,6 +106,4 @@ FROM test_utf8view;
101106
Andrew QW5kcmV3 416e64726577 X WA 58
102107
Xiangpeng WGlhbmdwZW5n 5869616e6770656e67 Xiangpeng WGlhbmdwZW5n 5869616e6770656e67
103108
Raphael UmFwaGFlbA 5261706861656c R Ug 52
104-
NULL NULL NULL R Ug 52
105-
106-
select encode(X'8f50d3f60eae370ddbf85c86219c55108a350165', 'hex');
109+
NULL NULL NULL R Ug 52

0 commit comments

Comments
 (0)