Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support timestamp(n) type
Browse files Browse the repository at this point in the history
findepi committed Nov 2, 2024
1 parent 752561a commit 555e05f
Showing 2 changed files with 47 additions and 4 deletions.
16 changes: 12 additions & 4 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
@@ -454,7 +454,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
SQLDataType::Char(_)
| SQLDataType::Text
| SQLDataType::String(_) => Ok(DataType::Utf8),
SQLDataType::Timestamp(None, tz_info) => {
SQLDataType::Timestamp(precision, tz_info)
if precision.is_none() || [0, 3, 6, 9].contains(&precision.unwrap()) => {
let tz = if matches!(tz_info, TimezoneInfo::Tz)
|| matches!(tz_info, TimezoneInfo::WithTimeZone)
{
@@ -466,7 +467,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
// Timestamp Without Time zone
None
};
Ok(DataType::Timestamp(TimeUnit::Nanosecond, tz.map(Into::into)))
let precision = match precision {
Some(0) => TimeUnit::Second,
Some(3) => TimeUnit::Millisecond,
Some(6) => TimeUnit::Microsecond,
None | Some(9) => TimeUnit::Nanosecond,
_ => unreachable!(),
};
Ok(DataType::Timestamp(precision, tz.map(Into::into)))
}
SQLDataType::Date => Ok(DataType::Date32),
SQLDataType::Time(None, tz_info) => {
@@ -535,8 +543,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
| SQLDataType::CharVarying(_)
| SQLDataType::CharacterLargeObject(_)
| SQLDataType::CharLargeObject(_)
// Precision is not supported
| SQLDataType::Timestamp(Some(_), _)
// Unsupported precision
| SQLDataType::Timestamp(_, _)
// Precision is not supported
| SQLDataType::Time(Some(_), _)
| SQLDataType::Dec(_)
35 changes: 35 additions & 0 deletions datafusion/sqllogictest/test_files/timestamps.slt
Original file line number Diff line number Diff line change
@@ -402,6 +402,41 @@ SELECT COUNT(*) FROM ts_data_secs where ts > from_unixtime(1599566400)
----
2

query P rowsort
SELECT ts FROM ts_data_nanos;
----
2020-09-08T11:42:29.190855123
2020-09-08T12:42:29.190855123
2020-09-08T13:42:29.190855123

query P rowsort
SELECT CAST(ts AS timestamp(0)) FROM ts_data_nanos;
----
2020-09-08T11:42:29
2020-09-08T12:42:29
2020-09-08T13:42:29

query P rowsort
SELECT CAST(ts AS timestamp(3)) FROM ts_data_nanos;
----
2020-09-08T11:42:29.190
2020-09-08T12:42:29.190
2020-09-08T13:42:29.190

query P rowsort
SELECT CAST(ts AS timestamp(6)) FROM ts_data_nanos;
----
2020-09-08T11:42:29.190855
2020-09-08T12:42:29.190855
2020-09-08T13:42:29.190855

query P rowsort
SELECT CAST(ts AS timestamp(9)) FROM ts_data_nanos;
----
2020-09-08T11:42:29.190855123
2020-09-08T12:42:29.190855123
2020-09-08T13:42:29.190855123


# count_distinct_timestamps
query P rowsort

0 comments on commit 555e05f

Please sign in to comment.