Skip to content

Commit

Permalink
chore: simplify the return type of validate_data_types() (apache#9491)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia authored Mar 8, 2024
1 parent 0c0fce3 commit fc81bf1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
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

0 comments on commit fc81bf1

Please sign in to comment.