Skip to content

Commit

Permalink
add with/without time zone (sqlparser-rs#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingkuo authored and ovr committed Mar 21, 2023
1 parent a65390c commit a71d038
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/ast/data_type.rs
Expand Up @@ -53,8 +53,10 @@ pub enum DataType {
Date,
/// Time
Time,
/// Timestamp
/// Timestamp [Without Time Zone]
Timestamp,
/// Timestamp With Time Zone
TimestampTz,
/// Interval
Interval,
/// Regclass used in postgresql serial
Expand Down Expand Up @@ -100,6 +102,7 @@ impl fmt::Display for DataType {
DataType::Date => write!(f, "DATE"),
DataType::Time => write!(f, "TIME"),
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
2 changes: 2 additions & 0 deletions src/dialect/keywords.rs
Expand Up @@ -445,6 +445,8 @@ define_keywords!(
TIES,
TIME,
TIMESTAMP,
TIMESTAMPTZ,
TIMEZONE,
TIMEZONE_HOUR,
TIMEZONE_MINUTE,
TO,
Expand Down
11 changes: 8 additions & 3 deletions src/parser.rs
Expand Up @@ -2024,12 +2024,17 @@ impl<'a> Parser<'a> {
Keyword::UUID => Ok(DataType::Uuid),
Keyword::DATE => Ok(DataType::Date),
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
2 changes: 1 addition & 1 deletion src/tokenizer.rs
Expand Up @@ -616,7 +616,7 @@ impl<'a> Tokenizer<'a> {
'r' => s.push('\r'),
't' => s.push('\t'),
'Z' => s.push('\x1a'),
x => s.push(x)
x => s.push(x),
}
}
}
Expand Down
33 changes: 28 additions & 5 deletions tests/sqlparser_common.rs
Expand Up @@ -1893,7 +1893,7 @@ fn parse_literal_time() {
}

#[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 @@ -1903,6 +1903,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 Expand Up @@ -2715,10 +2738,10 @@ fn parse_scalar_subqueries() {
assert_matches!(
verified_expr(sql),
Expr::BinaryOp {
op: BinaryOperator::Plus, ..
//left: box Subquery { .. },
//right: box Subquery { .. },
}
op: BinaryOperator::Plus,
.. //left: box Subquery { .. },
//right: box Subquery { .. },
}
);
}

Expand Down

0 comments on commit a71d038

Please sign in to comment.