Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for timezone-aware time types. #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 23 additions & 6 deletions src/types/date_time.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::fmt;
use chrono::{DateTime, Utc};

use chrono::naive::{NaiveDate, NaiveDateTime, NaiveTime};
use serde::de::{self, DeserializeSeed, Deserializer, Visitor};

use super::{Context, Presto, PrestoTy};

macro_rules! gen_date_time {
($ty:ty, $seed:ident, $pty:expr, $format:expr, $empty:expr, $expect:expr) => {
($ty:ty, $parse:expr, $seed:ident, $pty:expr, $format:expr, $empty:expr, $expect:expr, $tzmap:expr) => {
impl Presto for $ty {
type ValueType<'a> = String;
type Seed<'a, 'de> = $seed;
Expand Down Expand Up @@ -41,9 +42,9 @@ macro_rules! gen_date_time {
where
E: de::Error,
{
<$ty>::parse_from_str(v, $format).map_err(|e| {
$parse(v, $format).map_err(|e| {
de::Error::custom(format!("deserialize {} failed, reason: {}", $expect, e))
})
}).map($tzmap)
}
}

Expand All @@ -61,28 +62,44 @@ macro_rules! gen_date_time {

gen_date_time!(
NaiveDate,
NaiveDate::parse_from_str,
NaiveDateSeed,
PrestoTy::Date,
"%Y-%m-%d",
NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(),
"naive date"
"naive date",
|t| t
);
gen_date_time!(
NaiveDateTime,
NaiveDateTime::parse_from_str,
NaiveDateTimeSeed,
PrestoTy::Timestamp,
"%Y-%m-%d %H:%M:%S%.3f",
NaiveDate::from_ymd_opt(1970, 1, 1)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap(),
"naive time"
"naive time",
|t| t
);
gen_date_time!(
NaiveTime,
NaiveTime::parse_from_str,
NaiveTimeSeed,
PrestoTy::Time,
"%H:%M:%S%.3f",
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
"naive date time"
"naive date time",
|t| t
);
gen_date_time!(
DateTime<Utc>,
NaiveDateTime::parse_from_str,
TimestampWithTimeZoneSeed,
PrestoTy::TimestampWithTimeZone,
"%Y-%m-%d %H:%M:%S%.3f %Z",
DateTime::default(),
"date time with time zone",
|t| t.and_utc()
);
61 changes: 54 additions & 7 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
ClientTypeSignatureParameter, Column, NamedTypeSignature, RawPrestoTy, RowFieldName,
TypeSignature,
};
use crate::PrestoTy::{TimestampWithTimeZone, TimeWithTimeZone};

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

Check warning on line 53 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused imports: `TimeWithTimeZone`, `TimestampWithTimeZone`

//TODO: refine it
#[derive(Display, Debug)]
Expand Down Expand Up @@ -125,8 +126,10 @@
(Option(ty), provided) => extract(ty, provided),
(Boolean, Boolean) => Ok(vec![]),
(Date, Date) => Ok(vec![]),
(Time, Time) => Ok(vec![]),
(Timestamp, Timestamp) => Ok(vec![]),
(Time, Time | TimeWithPrecision(_)) => Ok(vec![]),
(TimeWithTimeZone, TimeWithTimeZone) => {Ok(vec![])},
(Timestamp, Timestamp | TimestampWithPrecision(_)) => Ok(vec![]),
(TimestampWithTimeZone, TimestampWithTimeZone | TimestampWithTimeZoneWithPrecision(_)) => Ok(vec![]),
(IntervalYearToMonth, IntervalYearToMonth) => Ok(vec![]),
(IntervalDayToSecond, IntervalDayToSecond) => Ok(vec![]),
(PrestoInt(_), PrestoInt(_)) => Ok(vec![]),
Expand Down Expand Up @@ -171,14 +174,18 @@

// TODO:
// VarBinary Json
// TimestampWithTimeZone TimeWithTimeZone
// HyperLogLog P4HyperLogLog
// QDigest
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PrestoTy {
Date,
Time,
TimeWithPrecision(usize),
TimeWithTimeZone,
Timestamp,
TimestampWithPrecision(usize),
TimestampWithTimeZone,
TimestampWithTimeZoneWithPrecision(usize),
Uuid,
IntervalYearToMonth,
IntervalDayToSecond,
Expand Down Expand Up @@ -301,8 +308,24 @@
PrestoTy::Tuple(tuple)
}
}
RawPrestoTy::IpAddress => PrestoTy::IpAddress,
RawPrestoTy::Uuid => PrestoTy::Uuid,
RawPrestoTy::TimestampWithTimeZone => {
match sig.arguments.as_slice() {
[] => PrestoTy::TimestampWithTimeZone,
[ClientTypeSignatureParameter::LongLiteral(s)] => {
PrestoTy::TimestampWithTimeZoneWithPrecision(*s as usize)
}
_ => return Err(Error::InvalidTypeSignature),
}
}
RawPrestoTy::TimeWithTimeZone if sig.arguments.len() == 1 => {
match sig.arguments.as_slice() {
[] => PrestoTy::TimeWithTimeZone,
[ClientTypeSignatureParameter::LongLiteral(s)] => {
PrestoTy::TimeWithPrecision(*s as usize)
}
_ => return Err(Error::InvalidTypeSignature),
}
}
_ => return Err(Error::InvalidTypeSignature),
};

Expand Down Expand Up @@ -337,7 +360,12 @@
],
Date => vec![],
Time => vec![],
TimeWithTimeZone => vec![],
TimeWithPrecision(s) => vec![ClientTypeSignatureParameter::LongLiteral(s as u64)],
Timestamp => vec![],
TimestampWithPrecision(s) => vec![ClientTypeSignatureParameter::LongLiteral(s as u64)],
TimestampWithTimeZone => vec![],
TimestampWithTimeZoneWithPrecision(s) => vec![ClientTypeSignatureParameter::LongLiteral(s as u64)],
IntervalYearToMonth => vec![],
IntervalDayToSecond => vec![],
Option(t) => return t.into_type_signature(),
Expand Down Expand Up @@ -381,7 +409,24 @@
Option(t) => t.full_type(),
Date => RawPrestoTy::Date.to_str().into(),
Time => RawPrestoTy::Time.to_str().into(),
TimeWithPrecision(p) => format!(
"{}({})",
RawPrestoTy::Time.to_str(),
p.to_string()
).into(),
TimestampWithPrecision(p) => format!(
"{}({})",
RawPrestoTy::Timestamp.to_str(),
p.to_string()
).into(),
TimestampWithTimeZoneWithPrecision(p) => format!(
"{}({})",
RawPrestoTy::TimestampWithTimeZone.to_str(),
p.to_string()
).into(),
TimeWithTimeZone => RawPrestoTy::TimeWithTimeZone.to_str().into(),
Timestamp => RawPrestoTy::Timestamp.to_str().into(),
TimestampWithTimeZone => RawPrestoTy::TimestampWithTimeZone.to_str().into(),
IntervalYearToMonth => RawPrestoTy::IntervalYearToMonth.to_str().into(),
IntervalDayToSecond => RawPrestoTy::IntervalDayToSecond.to_str().into(),
Boolean => RawPrestoTy::Boolean.to_str().into(),
Expand Down Expand Up @@ -421,8 +466,10 @@
match self {
Unknown => RawPrestoTy::Unknown,
Date => RawPrestoTy::Date,
Time => RawPrestoTy::Time,
Timestamp => RawPrestoTy::Timestamp,
Time | TimeWithPrecision(_) => RawPrestoTy::Time,
TimeWithTimeZone => RawPrestoTy::TimeWithTimeZone,
Timestamp | TimestampWithPrecision(_) => RawPrestoTy::Timestamp,
TimestampWithTimeZone | TimestampWithTimeZoneWithPrecision(_) => RawPrestoTy::TimestampWithTimeZone,
IntervalYearToMonth => RawPrestoTy::IntervalYearToMonth,
IntervalDayToSecond => RawPrestoTy::IntervalDayToSecond,
Decimal(_, _) => RawPrestoTy::Decimal,
Expand Down
Loading