Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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