Skip to content

Commit 6954677

Browse files
committed
chore: better messaging
1 parent 8322811 commit 6954677

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

datafusion/functions/src/unicode/substr.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,36 +81,38 @@ 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+
85+
86+
if !vec![DataType::LargeUtf8, DataType::Utf8View, DataType::Utf8].contains(&arg_types[0]) {
87+
return Err(DataFusionError::Execution(format!(
88+
"The first argument of the {} function can only be a string, but got {:?}.",
89+
self.name(),
90+
arg_types[0]
91+
)));
92+
}
93+
94+
if !vec![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 && !vec![DataType::Int64, DataType::Int32].contains(&arg_types[2]) {
103+
return Err(DataFusionError::Execution(format!(
104+
"The third argument of the {} function can only be an integer, but got {:?}.",
110105
self.name(),
111-
arg_types
112-
))),
106+
arg_types[2]
107+
)));
108+
}
109+
110+
if arg_types.len() == 2 {
111+
Ok(vec![arg_types[0].to_owned(), DataType::Int64])
112+
} else {
113+
Ok(vec![arg_types[0].to_owned(), DataType::Int64, DataType::Int64])
113114
}
115+
114116
}
115117
}
116118

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)