Skip to content

Commit 01fab57

Browse files
committed
chore: better messaging
1 parent 8322811 commit 01fab57

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

datafusion/functions/src/unicode/substr.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,42 @@ impl ScalarUDFImpl for SubstrFunc {
8181
}
8282

8383
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
84-
match arg_types {
85-
[first, second] => match (first, second) {
86-
(
87-
DataType::LargeUtf8 | DataType::Utf8View | DataType::Utf8,
88-
DataType::Int64 | DataType::Int32,
89-
) => Ok(vec![first.to_owned(), DataType::Int64]),
90-
_ => Err(DataFusionError::Execution(format!(
91-
"The {} function can only accept strings, but got {:?}.",
92-
self.name(),
93-
arg_types
94-
))),
95-
},
96-
[first, second, third] => match (first, second, third) {
97-
(
98-
DataType::LargeUtf8 | DataType::Utf8View | DataType::Utf8,
99-
DataType::Int64 | DataType::Int32,
100-
DataType::Int64 | DataType::Int32,
101-
) => Ok(vec![first.to_owned(), DataType::Int64, DataType::Int64]),
102-
_ => Err(DataFusionError::Execution(format!(
103-
"The {} function can only accept strings, but got {:?}.",
104-
self.name(),
105-
arg_types
106-
))),
107-
},
108-
_ => Err(DataFusionError::Execution(format!(
109-
"The {} function can only accept strings, but got {:?}",
84+
if ![DataType::LargeUtf8, DataType::Utf8View, DataType::Utf8]
85+
.contains(&arg_types[0])
86+
{
87+
return Err(DataFusionError::Execution(format!(
88+
"The first argument of the {} function can only be a string, but got {:?}.",
11089
self.name(),
111-
arg_types
112-
))),
90+
arg_types[0]
91+
)));
92+
}
93+
94+
if ![DataType::Int64, DataType::Int32].contains(&arg_types[1]) {
95+
return Err(DataFusionError::Execution(format!(
96+
"The second argument of the {} function can only be an integer, but got {:?}.",
97+
self.name(),
98+
arg_types[1]
99+
)));
100+
}
101+
102+
if arg_types.len() == 3
103+
&& ![DataType::Int64, DataType::Int32].contains(&arg_types[2])
104+
{
105+
return Err(DataFusionError::Execution(format!(
106+
"The third argument of the {} function can only be an integer, but got {:?}.",
107+
self.name(),
108+
arg_types[2]
109+
)));
110+
}
111+
112+
if arg_types.len() == 2 {
113+
Ok(vec![arg_types[0].to_owned(), DataType::Int64])
114+
} else {
115+
Ok(vec![
116+
arg_types[0].to_owned(),
117+
DataType::Int64,
118+
DataType::Int64,
119+
])
113120
}
114121
}
115122
}

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 Execution\("The substr function can only accept strings, but got \[Int64, Int64]\."\)
500+
statement error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Execution\("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 Execution\("The substr function can only accept strings, but got \[Int64, Int64, Int64\]\."\)
503+
statement error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Execution\("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

0 commit comments

Comments
 (0)