Skip to content

chore: simplify the return type of validate_data_types() #9491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions datafusion/functions/src/datetime/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,27 @@ pub(crate) fn string_to_timestamp_nanos_shim(s: &str) -> Result<i64> {
string_to_timestamp_nanos(s).map_err(|e| e.into())
}

pub(crate) fn validate_data_types(
args: &[ColumnarValue],
name: &str,
) -> Option<Result<ColumnarValue>> {
/// 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
Expand Down
4 changes: 1 addition & 3 deletions datafusion/functions/src/datetime/to_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 5 additions & 17 deletions datafusion/functions/src/datetime/to_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<ColumnarValue> {
Expand Down