Skip to content

Commit

Permalink
add with/without time zone (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingkuo committed Aug 26, 2022
1 parent 72559e9 commit 95fbb55
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/ast/data_type.rs
Expand Up @@ -77,8 +77,10 @@ pub enum DataType {
Time,
/// Datetime
Datetime,
/// Timestamp
/// Timestamp [Without Time Zone]
Timestamp,
/// Timestamp With Time Zone
TimestampTz,
/// Interval
Interval,
/// Regclass used in postgresql serial
Expand Down Expand Up @@ -157,6 +159,7 @@ impl fmt::Display for DataType {
DataType::Time => write!(f, "TIME"),
DataType::Datetime => write!(f, "DATETIME"),
DataType::Timestamp => write!(f, "TIMESTAMP"),
DataType::TimestampTz => write!(f, "TIMESTAMPTZ"),
DataType::Interval => write!(f, "INTERVAL"),
DataType::Regclass => write!(f, "REGCLASS"),
DataType::Text => write!(f, "TEXT"),
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Expand Up @@ -505,6 +505,7 @@ define_keywords!(
TIES,
TIME,
TIMESTAMP,
TIMESTAMPTZ,
TIMEZONE,
TIMEZONE_HOUR,
TIMEZONE_MINUTE,
Expand Down
11 changes: 8 additions & 3 deletions src/parser.rs
Expand Up @@ -3119,12 +3119,17 @@ impl<'a> Parser<'a> {
Keyword::DATE => Ok(DataType::Date),
Keyword::DATETIME => Ok(DataType::Datetime),
Keyword::TIMESTAMP => {
// TBD: we throw away "with/without timezone" information
if self.parse_keyword(Keyword::WITH) || self.parse_keyword(Keyword::WITHOUT) {
if self.parse_keyword(Keyword::WITH) {
self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
Ok(DataType::TimestampTz)
} else if self.parse_keyword(Keyword::WITHOUT) {
self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
Ok(DataType::Timestamp)
} else {
Ok(DataType::Timestamp)
}
Ok(DataType::Timestamp)
}
Keyword::TIMESTAMPTZ => Ok(DataType::TimestampTz),
Keyword::TIME => {
// TBD: we throw away "with/without timezone" information
if self.parse_keyword(Keyword::WITH) || self.parse_keyword(Keyword::WITHOUT) {
Expand Down
25 changes: 24 additions & 1 deletion tests/sqlparser_common.rs
Expand Up @@ -2854,7 +2854,7 @@ fn parse_literal_datetime() {
}

#[test]
fn parse_literal_timestamp() {
fn parse_literal_timestamp_without_time_zone() {
let sql = "SELECT TIMESTAMP '1999-01-01 01:23:34'";
let select = verified_only_select(sql);
assert_eq!(
Expand All @@ -2864,6 +2864,29 @@ fn parse_literal_timestamp() {
},
expr_from_projection(only(&select.projection)),
);

one_statement_parses_to(
"SELECT TIMESTAMP WITHOUT TIME ZONE '1999-01-01 01:23:34'",
sql,
);
}

#[test]
fn parse_literal_timestamp_with_time_zone() {
let sql = "SELECT TIMESTAMPTZ '1999-01-01 01:23:34Z'";
let select = verified_only_select(sql);
assert_eq!(
&Expr::TypedString {
data_type: DataType::TimestampTz,
value: "1999-01-01 01:23:34Z".into()
},
expr_from_projection(only(&select.projection)),
);

one_statement_parses_to(
"SELECT TIMESTAMP WITH TIME ZONE '1999-01-01 01:23:34Z'",
sql,
);
}

#[test]
Expand Down

0 comments on commit 95fbb55

Please sign in to comment.