Skip to content

Commit

Permalink
add Timestamp::Second as signature for ToTimestampSeconds; similar as…
Browse files Browse the repository at this point in the history
… other units; add test cases (#3004)
  • Loading branch information
waitingkuo authored Aug 1, 2022
1 parent c179102 commit 55a1286
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
81 changes: 81 additions & 0 deletions datafusion/core/tests/sql/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
6 changes: 5 additions & 1 deletion datafusion/expr/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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),
],
Expand All @@ -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(),
),
Expand Down

0 comments on commit 55a1286

Please sign in to comment.