Skip to content

Commit 49b9351

Browse files
authored
Support timestamp(n) type (apache#13231)
1 parent 274b222 commit 49b9351

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

datafusion/sql/src/planner.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
454454
SQLDataType::Char(_)
455455
| SQLDataType::Text
456456
| SQLDataType::String(_) => Ok(DataType::Utf8),
457-
SQLDataType::Timestamp(None, tz_info) => {
457+
SQLDataType::Timestamp(precision, tz_info)
458+
if precision.is_none() || [0, 3, 6, 9].contains(&precision.unwrap()) => {
458459
let tz = if matches!(tz_info, TimezoneInfo::Tz)
459460
|| matches!(tz_info, TimezoneInfo::WithTimeZone)
460461
{
@@ -466,7 +467,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
466467
// Timestamp Without Time zone
467468
None
468469
};
469-
Ok(DataType::Timestamp(TimeUnit::Nanosecond, tz.map(Into::into)))
470+
let precision = match precision {
471+
Some(0) => TimeUnit::Second,
472+
Some(3) => TimeUnit::Millisecond,
473+
Some(6) => TimeUnit::Microsecond,
474+
None | Some(9) => TimeUnit::Nanosecond,
475+
_ => unreachable!(),
476+
};
477+
Ok(DataType::Timestamp(precision, tz.map(Into::into)))
470478
}
471479
SQLDataType::Date => Ok(DataType::Date32),
472480
SQLDataType::Time(None, tz_info) => {
@@ -535,8 +543,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
535543
| SQLDataType::CharVarying(_)
536544
| SQLDataType::CharacterLargeObject(_)
537545
| SQLDataType::CharLargeObject(_)
538-
// Precision is not supported
539-
| SQLDataType::Timestamp(Some(_), _)
546+
// Unsupported precision
547+
| SQLDataType::Timestamp(_, _)
540548
// Precision is not supported
541549
| SQLDataType::Time(Some(_), _)
542550
| SQLDataType::Dec(_)

datafusion/sqllogictest/test_files/timestamps.slt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,41 @@ SELECT COUNT(*) FROM ts_data_secs where ts > from_unixtime(1599566400)
425425
----
426426
2
427427

428+
query P rowsort
429+
SELECT ts FROM ts_data_nanos;
430+
----
431+
2020-09-08T11:42:29.190855123
432+
2020-09-08T12:42:29.190855123
433+
2020-09-08T13:42:29.190855123
434+
435+
query P rowsort
436+
SELECT CAST(ts AS timestamp(0)) FROM ts_data_nanos;
437+
----
438+
2020-09-08T11:42:29
439+
2020-09-08T12:42:29
440+
2020-09-08T13:42:29
441+
442+
query P rowsort
443+
SELECT CAST(ts AS timestamp(3)) FROM ts_data_nanos;
444+
----
445+
2020-09-08T11:42:29.190
446+
2020-09-08T12:42:29.190
447+
2020-09-08T13:42:29.190
448+
449+
query P rowsort
450+
SELECT CAST(ts AS timestamp(6)) FROM ts_data_nanos;
451+
----
452+
2020-09-08T11:42:29.190855
453+
2020-09-08T12:42:29.190855
454+
2020-09-08T13:42:29.190855
455+
456+
query P rowsort
457+
SELECT CAST(ts AS timestamp(9)) FROM ts_data_nanos;
458+
----
459+
2020-09-08T11:42:29.190855123
460+
2020-09-08T12:42:29.190855123
461+
2020-09-08T13:42:29.190855123
462+
428463

429464
# count_distinct_timestamps
430465
query P rowsort

0 commit comments

Comments
 (0)