diff --git a/datafusion/functions/src/datetime/common.rs b/datafusion/functions/src/datetime/common.rs index f0d136c4633f..e888241d2552 100644 --- a/datafusion/functions/src/datetime/common.rs +++ b/datafusion/functions/src/datetime/common.rs @@ -38,26 +38,27 @@ pub(crate) fn string_to_timestamp_nanos_shim(s: &str) -> Result { string_to_timestamp_nanos(s).map_err(|e| e.into()) } -pub(crate) fn validate_data_types( - args: &[ColumnarValue], - name: &str, -) -> Option> { +/// Checks that all the arguments from the second are of type [Utf8] or [LargeUtf8] +/// +/// [Utf8]: DataType::Utf8 +/// [LargeUtf8]: DataType::LargeUtf8 +pub(crate) fn validate_data_types(args: &[ColumnarValue], name: &str) -> Result<()> { for (idx, a) in args.iter().skip(1).enumerate() { match a.data_type() { DataType::Utf8 | DataType::LargeUtf8 => { // all good } _ => { - return Some(exec_err!( + return exec_err!( "{name} function unsupported data type at index {}: {}", idx + 1, a.data_type() - )); + ); } } } - None + Ok(()) } /// Accepts a string and parses it using the [`chrono::format::strftime`] specifiers diff --git a/datafusion/functions/src/datetime/to_date.rs b/datafusion/functions/src/datetime/to_date.rs index e1b842680ca4..3756566d4204 100644 --- a/datafusion/functions/src/datetime/to_date.rs +++ b/datafusion/functions/src/datetime/to_date.rs @@ -95,9 +95,7 @@ impl ScalarUDFImpl for ToDateFunc { // validate that any args after the first one are Utf8 if args.len() > 1 { - if let Some(value) = validate_data_types(args, "to_date") { - return value; - } + validate_data_types(args, "to_date")?; } match args[0].data_type() { diff --git a/datafusion/functions/src/datetime/to_timestamp.rs b/datafusion/functions/src/datetime/to_timestamp.rs index cd8593337c7a..2eb21c5cdaec 100644 --- a/datafusion/functions/src/datetime/to_timestamp.rs +++ b/datafusion/functions/src/datetime/to_timestamp.rs @@ -127,9 +127,7 @@ impl ScalarUDFImpl for ToTimestampFunc { // validate that any args after the first one are Utf8 if args.len() > 1 { - if let Some(value) = validate_data_types(args, "to_timestamp") { - return value; - } + validate_data_types(args, "to_timestamp")?; } match args[0].data_type() { @@ -179,9 +177,7 @@ impl ScalarUDFImpl for ToTimestampSecondsFunc { // validate that any args after the first one are Utf8 if args.len() > 1 { - if let Some(value) = validate_data_types(args, "to_timestamp_seconds") { - return value; - } + validate_data_types(args, "to_timestamp")?; } match args[0].data_type() { @@ -228,9 +224,7 @@ impl ScalarUDFImpl for ToTimestampMillisFunc { // validate that any args after the first one are Utf8 if args.len() > 1 { - if let Some(value) = validate_data_types(args, "to_timestamp_millis") { - return value; - } + validate_data_types(args, "to_timestamp")?; } match args[0].data_type() { @@ -277,9 +271,7 @@ impl ScalarUDFImpl for ToTimestampMicrosFunc { // validate that any args after the first one are Utf8 if args.len() > 1 { - if let Some(value) = validate_data_types(args, "to_timestamp_micros") { - return value; - } + validate_data_types(args, "to_timestamp")?; } match args[0].data_type() { @@ -326,9 +318,7 @@ impl ScalarUDFImpl for ToTimestampNanosFunc { // validate that any args after the first one are Utf8 if args.len() > 1 { - if let Some(value) = validate_data_types(args, "to_timestamp_nanos") { - return value; - } + validate_data_types(args, "to_timestamp")?; } match args[0].data_type() { @@ -391,8 +381,6 @@ mod tests { use datafusion_common::{assert_contains, DataFusionError, ScalarValue}; use datafusion_expr::ScalarFunctionImplementation; - use crate::datetime::common::string_to_datetime_formatted; - use super::*; fn to_timestamp(args: &[ColumnarValue]) -> Result {