Skip to content

Commit

Permalink
chore: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mesejo committed Sep 10, 2024
1 parent db7cc31 commit c73baf2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
22 changes: 13 additions & 9 deletions datafusion/functions/src/datetime/to_local_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use arrow::datatypes::{

use chrono::{DateTime, MappedLocalTime, Offset, TimeDelta, TimeZone, Utc};
use datafusion_common::cast::as_primitive_array;
use datafusion_common::{exec_err, DataFusionError, Result, ScalarValue};
use datafusion_common::{exec_err, plan_err, DataFusionError, Result, ScalarValue};
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};

/// A UDF function that converts a timezone-aware timestamp to local time (with no offset or
Expand Down Expand Up @@ -330,20 +330,24 @@ impl ScalarUDFImpl for ToLocalTimeFunc {

fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
if arg_types.len() != 1 {
return Err(DataFusionError::Execution(format!(
return plan_err!(
"to_local_time function requires 1 argument, got {:?}",
arg_types.len()
)));
);
}

match &arg_types[0] {
Timestamp(Nanosecond, timezone) => Ok(vec![Timestamp(Nanosecond, timezone.clone())]),
Timestamp(Microsecond, timezone) => Ok(vec![Timestamp(Microsecond, timezone.clone())]),
Timestamp(Millisecond, timezone) => Ok(vec![Timestamp(Millisecond, timezone.clone())]),
Timestamp(Nanosecond, timezone) => {
Ok(vec![Timestamp(Nanosecond, timezone.clone())])
}
Timestamp(Microsecond, timezone) => {
Ok(vec![Timestamp(Microsecond, timezone.clone())])
}
Timestamp(Millisecond, timezone) => {
Ok(vec![Timestamp(Millisecond, timezone.clone())])
}
Timestamp(Second, timezone) => Ok(vec![Timestamp(Second, timezone.clone())]),
_ => Err(DataFusionError::Execution(format!(
"The to_local_time function can only accept timestamp as the arg, got {:?}", arg_types[0]
)))
_ => plan_err!("The to_local_time function can only accept timestamp as the arg, got {:?}", arg_types[0])
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions datafusion/sqllogictest/test_files/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,19 @@ CREATE TABLE foo2(c1 double, c2 double) AS VALUES
(2, 5),
(3, 6);

statement ok
SELECT COALESCE(column1, column2) FROM VALUES (null, 1.2);
query T
SELECT arrow_typeof(COALESCE(column1, column2)) FROM VALUES (null, 1.2);
----
Float64

# multiple rows and columns with null need type coercion
statement ok
SELECT column1, column2, column3 FROM VALUES
query TTT
select arrow_typeof(column1), arrow_typeof(column2), arrow_typeof(column3) from (SELECT column1, column2, column3 FROM VALUES
(null, 2, 'a'),
(1.1, null, 'b'),
(2, 5, null);
(2, 5, null)) LIMIT 1;
----
Float64 Int64 Utf8


# foo distinct
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -2858,7 +2858,7 @@ statement error
select to_local_time('2024-04-01T00:00:20Z'::timestamp, 'some string');

# invalid argument data type
statement error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Execution\("The to_local_time function can only accept timestamp as the arg, got Utf8"\)
statement error DataFusion error: Error during planning: Execution error: User\-defined coercion failed with Plan\("The to_local_time function can only accept timestamp as the arg, got Utf8"\)
select to_local_time('2024-04-01T00:00:20Z');

# invalid timezone
Expand Down

0 comments on commit c73baf2

Please sign in to comment.