From 55a12869f32644ed87d69d9b7617372db5103fb8 Mon Sep 17 00:00:00 2001 From: Wei-Ting Kuo Date: Tue, 2 Aug 2022 04:34:55 +0800 Subject: [PATCH] add Timestamp::Second as signature for ToTimestampSeconds; similar as other units; add test cases (#3004) --- datafusion/core/tests/sql/timestamp.rs | 81 ++++++++++++++++++++++++++ datafusion/expr/src/function.rs | 6 +- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/datafusion/core/tests/sql/timestamp.rs b/datafusion/core/tests/sql/timestamp.rs index 8452fcf81f42..8e293d035f6f 100644 --- a/datafusion/core/tests/sql/timestamp.rs +++ b/datafusion/core/tests/sql/timestamp.rs @@ -984,3 +984,84 @@ async fn sub_interval_day() -> Result<()> { Ok(()) } + +#[tokio::test] +async fn cast_to_timestamp_twice() -> Result<()> { + let ctx = SessionContext::new(); + + let sql = "select to_timestamp(a) from (select to_timestamp(1) as a)A;"; + let results = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+-------------------------------+", + "| totimestamp(a.a) |", + "+-------------------------------+", + "| 1970-01-01 00:00:00.000000001 |", + "+-------------------------------+", + ]; + + assert_batches_eq!(expected, &results); + + Ok(()) +} + +#[tokio::test] +async fn cast_to_timestamp_seconds_twice() -> Result<()> { + let ctx = SessionContext::new(); + + let sql = + "select to_timestamp_seconds(a) from (select to_timestamp_seconds(1) as a)A;"; + let results = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+-------------------------+", + "| totimestampseconds(a.a) |", + "+-------------------------+", + "| 1970-01-01 00:00:01 |", + "+-------------------------+", + ]; + + assert_batches_eq!(expected, &results); + + Ok(()) +} + +#[tokio::test] +async fn cast_to_timestamp_millis_twice() -> Result<()> { + let ctx = SessionContext::new(); + + let sql = "select to_timestamp_millis(a) from (select to_timestamp_millis(1) as a)A;"; + let results = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+-------------------------+", + "| totimestampmillis(a.a) |", + "+-------------------------+", + "| 1970-01-01 00:00:00.001 |", + "+-------------------------+", + ]; + + assert_batches_eq!(expected, &results); + + Ok(()) +} + +#[tokio::test] +async fn cast_to_timestamp_micros_twice() -> Result<()> { + let ctx = SessionContext::new(); + + let sql = "select to_timestamp_micros(a) from (select to_timestamp_micros(1) as a)A;"; + let results = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+----------------------------+", + "| totimestampmicros(a.a) |", + "+----------------------------+", + "| 1970-01-01 00:00:00.000001 |", + "+----------------------------+", + ]; + + assert_batches_eq!(expected, &results); + + Ok(()) +} diff --git a/datafusion/expr/src/function.rs b/datafusion/expr/src/function.rs index 29158e234b79..45cd99d5703e 100644 --- a/datafusion/expr/src/function.rs +++ b/datafusion/expr/src/function.rs @@ -350,8 +350,9 @@ pub fn signature(fun: &BuiltinScalarFunction) -> Signature { vec![ DataType::Utf8, DataType::Int64, - DataType::Timestamp(TimeUnit::Millisecond, None), + DataType::Timestamp(TimeUnit::Nanosecond, None), DataType::Timestamp(TimeUnit::Microsecond, None), + DataType::Timestamp(TimeUnit::Millisecond, None), DataType::Timestamp(TimeUnit::Second, None), ], fun.volatility(), @@ -363,6 +364,7 @@ pub fn signature(fun: &BuiltinScalarFunction) -> Signature { DataType::Int64, DataType::Timestamp(TimeUnit::Nanosecond, None), DataType::Timestamp(TimeUnit::Microsecond, None), + DataType::Timestamp(TimeUnit::Millisecond, None), DataType::Timestamp(TimeUnit::Second, None), ], fun.volatility(), @@ -373,6 +375,7 @@ pub fn signature(fun: &BuiltinScalarFunction) -> Signature { DataType::Utf8, DataType::Int64, DataType::Timestamp(TimeUnit::Nanosecond, None), + DataType::Timestamp(TimeUnit::Microsecond, None), DataType::Timestamp(TimeUnit::Millisecond, None), DataType::Timestamp(TimeUnit::Second, None), ], @@ -386,6 +389,7 @@ pub fn signature(fun: &BuiltinScalarFunction) -> Signature { DataType::Timestamp(TimeUnit::Nanosecond, None), DataType::Timestamp(TimeUnit::Microsecond, None), DataType::Timestamp(TimeUnit::Millisecond, None), + DataType::Timestamp(TimeUnit::Second, None), ], fun.volatility(), ),