Skip to content

Commit 56fc202

Browse files
committed
bq: add in more date/time parts
1 parent 343cb64 commit 56fc202

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/ast/value.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub enum DateTimeField {
132132
YearOfWeekIso,
133133
Quarter,
134134
Month,
135-
Week,
135+
Week(Option<String>),
136136
WeekIso,
137137
Day,
138138
DayOfWeek,
@@ -142,6 +142,8 @@ pub enum DateTimeField {
142142
Minute,
143143
Second,
144144
Epoch,
145+
// https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#extract_2
146+
Other(&'static str),
145147
Literal(String),
146148
}
147149

@@ -153,7 +155,8 @@ impl fmt::Display for DateTimeField {
153155
DateTimeField::YearOfWeekIso => "YEAROFWEEKISO",
154156
DateTimeField::Quarter => "QUARTER",
155157
DateTimeField::Month => "MONTH",
156-
DateTimeField::Week => "WEEK",
158+
DateTimeField::Week(None) => "WEEK",
159+
DateTimeField::Week(Some(ref weekday)) => return write!(f, "WEEK({})", weekday),
157160
DateTimeField::WeekIso => "WEEKISO",
158161
DateTimeField::Day => "DAY",
159162
DateTimeField::DayOfWeek => "DAYOFWEEK",
@@ -163,6 +166,7 @@ impl fmt::Display for DateTimeField {
163166
DateTimeField::Minute => "MINUTE",
164167
DateTimeField::Second => "SECOND",
165168
DateTimeField::Epoch => "EPOCH",
169+
DateTimeField::Other(s) => return write!(f, "{}", s),
166170
DateTimeField::Literal(ref s) => return write!(f, "'{}'", s),
167171
})
168172
}

src/dialect/keywords.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ define_keywords!(
221221
FOREIGN,
222222
FRAME_ROW,
223223
FREE,
224+
FRIDAY,
224225
FROM,
225226
FULL,
226227
FUNCTION,
@@ -254,6 +255,8 @@ define_keywords!(
254255
INTO,
255256
IS,
256257
ISOLATION,
258+
ISOWEEK,
259+
ISOYEAR,
257260
JOIN,
258261
JSONFILE,
259262
KEY,
@@ -283,6 +286,8 @@ define_keywords!(
283286
MEMBER,
284287
MERGE,
285288
METHOD,
289+
MICROSECOND,
290+
MILLISECOND,
286291
MIN,
287292
MINUS,
288293
MINUTE,
@@ -291,6 +296,7 @@ define_keywords!(
291296
MODIFIES,
292297
MODULE,
293298
MON,
299+
MONDAY,
294300
MONS,
295301
MONTH,
296302
MONTHS,
@@ -393,6 +399,7 @@ define_keywords!(
393399
ROWID,
394400
ROWS,
395401
ROW_NUMBER,
402+
SATURDAY,
396403
SAVEPOINT,
397404
SCHEMA,
398405
SCOPE,
@@ -428,6 +435,7 @@ define_keywords!(
428435
SUBSTRING_REGEX,
429436
SUCCEEDS,
430437
SUM,
438+
SUNDAY,
431439
SYMMETRIC,
432440
SYSTEM,
433441
SYSTEM_TIME,
@@ -437,6 +445,7 @@ define_keywords!(
437445
TEXT,
438446
TEXTFILE,
439447
THEN,
448+
THURSDAY,
440449
TIES,
441450
TIME,
442451
TIMESTAMP,
@@ -456,6 +465,7 @@ define_keywords!(
456465
TRUE,
457466
TRUNCATE,
458467
TRY_CAST,
468+
TUESDAY,
459469
UESCAPE,
460470
UNBOUNDED,
461471
UNCOMMITTED,
@@ -481,6 +491,7 @@ define_keywords!(
481491
VIEW,
482492
VIRTUAL,
483493
W,
494+
WEDNESDAY,
484495
WEEK,
485496
WEEKDAY,
486497
WEEKOFYEAR,

src/parser.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,12 +668,27 @@ impl<'a> Parser<'a> {
668668
Keyword::MONTH | Keyword::MM | Keyword::MON | Keyword::MONS | Keyword::MONTHS => {
669669
Ok(DateTimeField::Month)
670670
}
671+
Keyword::WEEK
672+
if dialect_of!(self is BigQueryDialect)
673+
&& self.consume_token(&Token::LParen) =>
674+
{
675+
use Keyword::*;
676+
let weekday = self.expect_one_of_keywords(&[
677+
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,
678+
])?;
679+
self.expect_token(&Token::RParen)?;
680+
Ok(DateTimeField::Week(Some(format!("{:?}", weekday))))
681+
}
671682
Keyword::WEEK
672683
| Keyword::W
673684
| Keyword::WK
674685
| Keyword::WEEKOFYEAR
675686
| Keyword::WOY
676-
| Keyword::WY => Ok(DateTimeField::Week),
687+
| Keyword::WY => Ok(DateTimeField::Week(None)),
688+
Keyword::ISOWEEK => Ok(DateTimeField::Other("ISOWEEK")),
689+
Keyword::ISOYEAR => Ok(DateTimeField::Other("ISOYEAR")),
690+
Keyword::MICROSECOND => Ok(DateTimeField::Other("MICROSECOND")),
691+
Keyword::MILLISECOND => Ok(DateTimeField::Other("MILLISECOND")),
677692
Keyword::WEEKISO
678693
| Keyword::WEEK_ISO
679694
| Keyword::WEEKOFYEARISO
@@ -739,11 +754,15 @@ impl<'a> Parser<'a> {
739754
Token::Word(kw)
740755
if [
741756
Keyword::YEAR,
757+
Keyword::QUARTER,
742758
Keyword::MONTH,
759+
Keyword::WEEK,
743760
Keyword::DAY,
744761
Keyword::HOUR,
745762
Keyword::MINUTE,
746763
Keyword::SECOND,
764+
Keyword::MILLISECOND,
765+
Keyword::MICROSECOND,
747766
]
748767
.iter()
749768
.any(|d| kw.keyword == *d) =>

0 commit comments

Comments
 (0)