Skip to content

Apache DataFusion SQL Query Engine

add Timestamp::Second as signature for ToTimestampSeconds; similar as other units; add test cases (#3004)

Wei-Ting Kuo55a12869f326
diff --git a/datafusion/core/tests/sql/timestamp.rs b/datafusion/core/tests/sql/timestamp.rs
index 8452fcf81f..8e293d035f 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 29158e234b..45cd99d570 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(),
),