Skip to content
This repository was archived by the owner on Dec 25, 2019. It is now read-only.
Merged
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
7 changes: 7 additions & 0 deletions src/ast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum Value {
Time(String),
/// `TIMESTAMP '...'` literals
Timestamp(String, ParsedTimestamp),
/// `TIMESTAMP WITH TIME ZONE` literals
TimestampTz(String, ParsedTimestamp),
/// INTERVAL literals, roughly in the following format:
///
/// ```text
Expand Down Expand Up @@ -80,6 +82,11 @@ impl fmt::Display for Value {
Value::Date(v, _) => write!(f, "DATE '{}'", escape_single_quote_string(v)),
Value::Time(v) => write!(f, "TIME '{}'", escape_single_quote_string(v)),
Value::Timestamp(v, _) => write!(f, "TIMESTAMP '{}'", escape_single_quote_string(v)),
Value::TimestampTz(v, _) => write!(
f,
"TIMESTAMP WITH TIME ZONE '{}'",
escape_single_quote_string(v)
),
Value::Interval(IntervalValue {
parsed: _,
value,
Expand Down
3 changes: 3 additions & 0 deletions src/ast/value/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ pub struct ParsedTimestamp {
pub minute: u8,
pub second: u8,
pub nano: u32,
pub timezone_offset_second: i64,
Comment thread
ruchirK marked this conversation as resolved.
}

/// All of the fields that can appear in a literal `DATE`, `TIMESTAMP` or `INTERVAL` string
Expand All @@ -297,6 +298,7 @@ pub struct ParsedDateTime {
pub minute: Option<u64>,
pub second: Option<u64>,
pub nano: Option<u32>,
pub timezone_offset_second: Option<i64>,
}

impl ParsedDateTime {
Expand All @@ -320,6 +322,7 @@ impl Default for ParsedDateTime {
minute: None,
second: None,
nano: None,
timezone_offset_second: None,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/dialect/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ define_keywords!(
TIES,
TIME,
TIMESTAMP,
TIMESTAMPTZ,
TIMEZONE_HOUR,
TIMEZONE_MINUTE,
TO,
Expand Down
81 changes: 76 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ impl Parser {
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
}),
"TIME" => Ok(Expr::Value(Value::Time(self.parse_literal_string()?))),
"TIMESTAMP" => Ok(Expr::Value(self.parse_timestamp()?)),
"TIMESTAMP" => self.parse_timestamp(),
"TIMESTAMPTZ" => self.parse_timestamptz(),
// Here `w` is a word, check if it's a part of a multi-part
// identifier, a function call, or a simple identifier:
_ => match self.peek_token() {
Expand Down Expand Up @@ -508,16 +509,46 @@ impl Parser {
}
}

fn parse_timestamp(&mut self) -> Result<Value, ParserError> {
fn parse_timestamp(&mut self) -> Result<Expr, ParserError> {
if self.parse_keyword("WITH") {
self.expect_keywords(&["TIME", "ZONE"])?;
return Ok(Expr::Value(self.parse_timestamp_inner(true)?));
} else if self.parse_keyword("WITHOUT") {
self.expect_keywords(&["TIME", "ZONE"])?;
}
Ok(Expr::Value(self.parse_timestamp_inner(false)?))
Comment thread
ruchirK marked this conversation as resolved.
}

fn parse_timestamptz(&mut self) -> Result<Expr, ParserError> {
Ok(Expr::Value(self.parse_timestamp_inner(true)?))
}

fn parse_timestamp_inner(&mut self, parse_timezone: bool) -> Result<Value, ParserError> {
use std::convert::TryInto;

let value = self.parse_literal_string()?;
let pdt = Self::parse_interval_string(&value, &DateTimeField::Year)?;
let pdt = Self::parse_timestamp_string(&value, parse_timezone)?;

match (
pdt.year, pdt.month, pdt.day, pdt.hour, pdt.minute, pdt.second, pdt.nano,
pdt.year,
pdt.month,
pdt.day,
pdt.hour,
pdt.minute,
pdt.second,
pdt.nano,
pdt.timezone_offset_second,
) {
(Some(year), Some(month), Some(day), Some(hour), Some(minute), Some(second), nano) => {
(
Some(year),
Some(month),
Some(day),
Some(hour),
Some(minute),
Some(second),
nano,
timezone_offset_second,
) => {
let p_err = |e: std::num::TryFromIntError, field: &str| {
ParserError::ParserError(format!(
"{} in date '{}' is invalid: {}",
Expand Down Expand Up @@ -555,6 +586,23 @@ impl Parser {
if second > 60 {
parser_err!("Second in timestamp '{}' cannot be > 60: {}", value, second)?;
}

if parse_timezone {
return Ok(Value::TimestampTz(
value,
ParsedTimestamp {
year,
month,
day,
hour,
minute,
second,
nano: nano.unwrap_or(0),
timezone_offset_second: timezone_offset_second.unwrap_or(0),
},
));
}

Ok(Value::Timestamp(
value,
ParsedTimestamp {
Expand All @@ -565,6 +613,7 @@ impl Parser {
minute,
second,
nano: nano.unwrap_or(0),
timezone_offset_second: 0,
},
))
}
Expand Down Expand Up @@ -773,6 +822,27 @@ impl Parser {
datetime::build_parsed_datetime(&toks, leading_field, value)
}

pub fn parse_timestamp_string(
value: &str,
parse_timezone: bool,
) -> Result<ParsedDateTime, ParserError> {
if value.is_empty() {
return Err(ParserError::ParserError(
"Timestamp string is empty!".to_string(),
));
}

let (ts_string, tz_string) = datetime::split_timestamp_string(value);

let mut pdt = Self::parse_interval_string(ts_string, &DateTimeField::Year)?;
if !parse_timezone || tz_string.is_empty() {
return Ok(pdt);
}

pdt.timezone_offset_second = Some(datetime::parse_timezone_offset_second(tz_string)?);
Ok(pdt)
}

/// Parses the parens following the `[ NOT ] IN` operator
pub fn parse_in(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
self.expect_token(&Token::LParen)?;
Expand Down Expand Up @@ -1554,6 +1624,7 @@ impl Parser {
}
Ok(DataType::Timestamp)
}
"TIMESTAMPTZ" => Ok(DataType::TimestampTz),
"TIME" => {
if self.parse_keyword("WITH") {
self.expect_keywords(&["TIME", "ZONE"])?;
Expand Down
Loading