Skip to content

Commit fc81bf1

Browse files
authored
chore: simplify the return type of validate_data_types() (#9491)
Signed-off-by: Ruihang Xia <[email protected]>
1 parent 0c0fce3 commit fc81bf1

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

datafusion/functions/src/datetime/common.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,27 @@ pub(crate) fn string_to_timestamp_nanos_shim(s: &str) -> Result<i64> {
3838
string_to_timestamp_nanos(s).map_err(|e| e.into())
3939
}
4040

41-
pub(crate) fn validate_data_types(
42-
args: &[ColumnarValue],
43-
name: &str,
44-
) -> Option<Result<ColumnarValue>> {
41+
/// Checks that all the arguments from the second are of type [Utf8] or [LargeUtf8]
42+
///
43+
/// [Utf8]: DataType::Utf8
44+
/// [LargeUtf8]: DataType::LargeUtf8
45+
pub(crate) fn validate_data_types(args: &[ColumnarValue], name: &str) -> Result<()> {
4546
for (idx, a) in args.iter().skip(1).enumerate() {
4647
match a.data_type() {
4748
DataType::Utf8 | DataType::LargeUtf8 => {
4849
// all good
4950
}
5051
_ => {
51-
return Some(exec_err!(
52+
return exec_err!(
5253
"{name} function unsupported data type at index {}: {}",
5354
idx + 1,
5455
a.data_type()
55-
));
56+
);
5657
}
5758
}
5859
}
5960

60-
None
61+
Ok(())
6162
}
6263

6364
/// Accepts a string and parses it using the [`chrono::format::strftime`] specifiers

datafusion/functions/src/datetime/to_date.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ impl ScalarUDFImpl for ToDateFunc {
9595

9696
// validate that any args after the first one are Utf8
9797
if args.len() > 1 {
98-
if let Some(value) = validate_data_types(args, "to_date") {
99-
return value;
100-
}
98+
validate_data_types(args, "to_date")?;
10199
}
102100

103101
match args[0].data_type() {

datafusion/functions/src/datetime/to_timestamp.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ impl ScalarUDFImpl for ToTimestampFunc {
127127

128128
// validate that any args after the first one are Utf8
129129
if args.len() > 1 {
130-
if let Some(value) = validate_data_types(args, "to_timestamp") {
131-
return value;
132-
}
130+
validate_data_types(args, "to_timestamp")?;
133131
}
134132

135133
match args[0].data_type() {
@@ -179,9 +177,7 @@ impl ScalarUDFImpl for ToTimestampSecondsFunc {
179177

180178
// validate that any args after the first one are Utf8
181179
if args.len() > 1 {
182-
if let Some(value) = validate_data_types(args, "to_timestamp_seconds") {
183-
return value;
184-
}
180+
validate_data_types(args, "to_timestamp")?;
185181
}
186182

187183
match args[0].data_type() {
@@ -228,9 +224,7 @@ impl ScalarUDFImpl for ToTimestampMillisFunc {
228224

229225
// validate that any args after the first one are Utf8
230226
if args.len() > 1 {
231-
if let Some(value) = validate_data_types(args, "to_timestamp_millis") {
232-
return value;
233-
}
227+
validate_data_types(args, "to_timestamp")?;
234228
}
235229

236230
match args[0].data_type() {
@@ -277,9 +271,7 @@ impl ScalarUDFImpl for ToTimestampMicrosFunc {
277271

278272
// validate that any args after the first one are Utf8
279273
if args.len() > 1 {
280-
if let Some(value) = validate_data_types(args, "to_timestamp_micros") {
281-
return value;
282-
}
274+
validate_data_types(args, "to_timestamp")?;
283275
}
284276

285277
match args[0].data_type() {
@@ -326,9 +318,7 @@ impl ScalarUDFImpl for ToTimestampNanosFunc {
326318

327319
// validate that any args after the first one are Utf8
328320
if args.len() > 1 {
329-
if let Some(value) = validate_data_types(args, "to_timestamp_nanos") {
330-
return value;
331-
}
321+
validate_data_types(args, "to_timestamp")?;
332322
}
333323

334324
match args[0].data_type() {
@@ -391,8 +381,6 @@ mod tests {
391381
use datafusion_common::{assert_contains, DataFusionError, ScalarValue};
392382
use datafusion_expr::ScalarFunctionImplementation;
393383

394-
use crate::datetime::common::string_to_datetime_formatted;
395-
396384
use super::*;
397385

398386
fn to_timestamp(args: &[ColumnarValue]) -> Result<ColumnarValue> {

0 commit comments

Comments
 (0)