Skip to content

Commit 1cc4344

Browse files
committed
chore: rebased, better error messaging in slt
1 parent 5477cf1 commit 1cc4344

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

datafusion/functions/src/unicode/substr.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ impl ScalarUDFImpl for SubstrFunc {
8181
}
8282

8383
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
84-
if ![DataType::LargeUtf8, DataType::Utf8View, DataType::Utf8]
85-
.contains(&arg_types[0])
84+
if ![
85+
DataType::LargeUtf8,
86+
DataType::Utf8View,
87+
DataType::Utf8,
88+
DataType::Null,
89+
]
90+
.contains(&arg_types[0])
8691
{
8792
return plan_err!(
8893
"The first argument of the {} function can only be a string, but got {:?}.",
@@ -91,7 +96,7 @@ impl ScalarUDFImpl for SubstrFunc {
9196
);
9297
}
9398

94-
if ![DataType::Int64, DataType::Int32].contains(&arg_types[1]) {
99+
if ![DataType::Int64, DataType::Int32, DataType::Null].contains(&arg_types[1]) {
95100
return plan_err!(
96101
"The second argument of the {} function can only be an integer, but got {:?}.",
97102
self.name(),
@@ -100,7 +105,7 @@ impl ScalarUDFImpl for SubstrFunc {
100105
}
101106

102107
if arg_types.len() == 3
103-
&& ![DataType::Int64, DataType::Int32].contains(&arg_types[2])
108+
&& ![DataType::Int64, DataType::Int32, DataType::Null].contains(&arg_types[2])
104109
{
105110
return plan_err!(
106111
"The third argument of the {} function can only be an integer, but got {:?}.",
@@ -109,11 +114,17 @@ impl ScalarUDFImpl for SubstrFunc {
109114
);
110115
}
111116

117+
let first_data_type = if arg_types[0] == DataType::Null {
118+
DataType::Utf8
119+
} else {
120+
arg_types[0].clone()
121+
};
122+
112123
if arg_types.len() == 2 {
113-
Ok(vec![arg_types[0].to_owned(), DataType::Int64])
124+
Ok(vec![first_data_type.to_owned(), DataType::Int64])
114125
} else {
115126
Ok(vec![
116-
arg_types[0].to_owned(),
127+
first_data_type.to_owned(),
117128
DataType::Int64,
118129
DataType::Int64,
119130
])

datafusion/sqllogictest/test_files/encoding.slt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ CREATE TABLE test(
2828
;
2929

3030
# errors
31-
query error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Plan
31+
query error 1st argument should be Utf8 or Binary or Null, got Int64
3232
select encode(12, 'hex');
3333

3434
query error DataFusion error: Error during planning: There is no built\-in encoding named 'non_encoding', currently supported encodings are: base64, hex
3535
select encode(bin_field, 'non_encoding') from test;
3636

37-
query error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Plan
37+
query error 1st argument should be Utf8 or Binary or Null, got Int64
3838
select decode(12, 'hex');
3939

4040
query error DataFusion error: Error during planning: There is no built\-in encoding named 'non_encoding', currently supported encodings are: base64, hex

datafusion/sqllogictest/test_files/functions.slt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,10 @@ SELECT substr('alphabet', 3, CAST(NULL AS int))
497497
----
498498
NULL
499499

500-
statement error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Plan
500+
statement error The first argument of the substr function can only be a string, but got Int64
501501
SELECT substr(1, 3)
502502

503-
statement error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Plan
503+
statement error The first argument of the substr function can only be a string, but got Int64
504504
SELECT substr(1, 3, 4)
505505

506506
query T

datafusion/sqllogictest/test_files/scalar.slt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ select position('' in '')
19071907
1
19081908

19091909

1910-
query error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Plan
1910+
query error POSITION function can only accept strings
19111911
select position(1 in 1)
19121912

19131913

datafusion/sqllogictest/test_files/string_functions/substr/substr_literal.slt.part

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ SELECT substr('Hello🌏世界', 5, 3)
123123
----
124124
o🌏世
125125

126-
statement error The SUBSTR function can only accept strings, but got Int64.
126+
statement error The first argument of the substr function can only be a string, but got Int64
127127
SELECT substr(1, 3)
128128

129-
statement error The SUBSTR function can only accept strings, but got Int64.
129+
statement error The first argument of the substr function can only be a string, but got Int64
130130
SELECT substr(1, 3, 4)
131131

132132
statement error Execution error: negative substring length not allowed

datafusion/sqllogictest/test_files/timestamps.slt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2858,7 +2858,7 @@ statement error
28582858
select to_local_time('2024-04-01T00:00:20Z'::timestamp, 'some string');
28592859

28602860
# invalid argument data type
2861-
statement error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Plan
2861+
statement error The to_local_time function can only accept Timestamp as the arg got Utf8
28622862
select to_local_time('2024-04-01T00:00:20Z');
28632863

28642864
# invalid timezone

0 commit comments

Comments
 (0)