Allow omitting units after INTERVAL (#184)

Alter INTERVAL to support postgres syntax

This patch updates our INTERVAL implementation such that the Postgres
and Redshfit variation of the syntax is supported: namely that 'leading
field' is optional.

Fixes #177.
This commit is contained in:
Max Countryman 2020-06-09 23:32:13 -07:00 committed by GitHub
parent d842f495db
commit 846c52f450
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 18 deletions

View file

@ -526,12 +526,21 @@ impl Parser {
// Following the string literal is a qualifier which indicates the units
// of the duration specified in the string literal.
//
// Note that PostgreSQL allows omitting the qualifier, but we currently
// require at least the leading field, in accordance with the ANSI spec.
let leading_field = self.parse_date_time_field()?;
// Note that PostgreSQL allows omitting the qualifier, so we provide
// this more general implemenation.
let leading_field = match self.peek_token() {
Some(Token::Word(kw))
if ["YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND"]
.iter()
.any(|d| kw.keyword == *d) =>
{
Some(self.parse_date_time_field()?)
}
_ => None,
};
let (leading_precision, last_field, fsec_precision) =
if leading_field == DateTimeField::Second {
if leading_field == Some(DateTimeField::Second) {
// SQL mandates special syntax for `SECOND TO SECOND` literals.
// Instead of
// `SECOND [(<leading precision>)] TO SECOND[(<fractional seconds precision>)]`