Skip to content

Support more DateTimeField variants #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024
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
6 changes: 3 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl fmt::Display for Interval {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = self.value.as_ref();
match (
self.leading_field,
&self.leading_field,
self.leading_precision,
self.fractional_seconds_precision,
) {
Expand All @@ -248,13 +248,13 @@ impl fmt::Display for Interval {
}
_ => {
write!(f, "INTERVAL {value}")?;
if let Some(leading_field) = self.leading_field {
if let Some(leading_field) = &self.leading_field {
write!(f, " {leading_field}")?;
}
if let Some(leading_precision) = self.leading_precision {
write!(f, " ({leading_precision})")?;
}
if let Some(last_field) = self.last_field {
if let Some(last_field) = &self.last_field {
write!(f, " TO {last_field}")?;
}
if let Some(fractional_seconds_precision) = self.fractional_seconds_precision {
Expand Down
114 changes: 75 additions & 39 deletions src/ast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

#[cfg(not(feature = "std"))]
use alloc::string::String;

#[cfg(not(feature = "std"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use the suggestion below I think you will not need these imports either

use alloc::format;

#[cfg(not(feature = "std"))]
use alloc::string::ToString;

use core::fmt;

#[cfg(feature = "bigdecimal")]
Expand All @@ -20,6 +27,7 @@ use bigdecimal::BigDecimal;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::ast::Ident;
#[cfg(feature = "visitor")]
use sqlparser_derive::{Visit, VisitMut};

Expand Down Expand Up @@ -109,17 +117,25 @@ impl fmt::Display for DollarQuotedString {
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum DateTimeField {
Year,
Month,
Week,
/// Week optionally followed by a WEEKDAY.
///
/// ```sql
/// WEEK(MONDAY)
/// ```
///
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#extract)
Week(Option<Ident>),
Day,
DayOfWeek,
DayOfYear,
Date,
Datetime,
Hour,
Minute,
Second,
Expand Down Expand Up @@ -148,47 +164,67 @@ pub enum DateTimeField {
TimezoneMinute,
TimezoneRegion,
NoDateTime,
/// Arbitrary abbreviation or custom date-time part.
///
/// ```sql
/// EXTRACT(q FROM CURRENT_TIMESTAMP)
/// ```
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/functions-date-time#supported-date-and-time-parts)
Custom(Ident),
}

impl fmt::Display for DateTimeField {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
DateTimeField::Year => "YEAR",
DateTimeField::Month => "MONTH",
DateTimeField::Week => "WEEK",
DateTimeField::Day => "DAY",
DateTimeField::DayOfWeek => "DAYOFWEEK",
DateTimeField::DayOfYear => "DAYOFYEAR",
DateTimeField::Date => "DATE",
DateTimeField::Hour => "HOUR",
DateTimeField::Minute => "MINUTE",
DateTimeField::Second => "SECOND",
DateTimeField::Century => "CENTURY",
DateTimeField::Decade => "DECADE",
DateTimeField::Dow => "DOW",
DateTimeField::Doy => "DOY",
DateTimeField::Epoch => "EPOCH",
DateTimeField::Isodow => "ISODOW",
DateTimeField::Isoyear => "ISOYEAR",
DateTimeField::IsoWeek => "ISOWEEK",
DateTimeField::Julian => "JULIAN",
DateTimeField::Microsecond => "MICROSECOND",
DateTimeField::Microseconds => "MICROSECONDS",
DateTimeField::Millenium => "MILLENIUM",
DateTimeField::Millennium => "MILLENNIUM",
DateTimeField::Millisecond => "MILLISECOND",
DateTimeField::Milliseconds => "MILLISECONDS",
DateTimeField::Nanosecond => "NANOSECOND",
DateTimeField::Nanoseconds => "NANOSECONDS",
DateTimeField::Quarter => "QUARTER",
DateTimeField::Time => "TIME",
DateTimeField::Timezone => "TIMEZONE",
DateTimeField::TimezoneAbbr => "TIMEZONE_ABBR",
DateTimeField::TimezoneHour => "TIMEZONE_HOUR",
DateTimeField::TimezoneMinute => "TIMEZONE_MINUTE",
DateTimeField::TimezoneRegion => "TIMEZONE_REGION",
DateTimeField::NoDateTime => "NODATETIME",
})
f.write_str(
match self {
DateTimeField::Year => "YEAR".to_string(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change to call to_string() will now cause a new string to be allocated for each type.

I think it would be better / more effiicent to use write! directly -- like

Suggested change
DateTimeField::Year => "YEAR".to_string(),
match self {
DateTimeField::Year => f.write_str("YEAR"),
DateTimeField::Month => f.write_str("MONTH"),
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that makes a lot more sense, thanks for the follow up fix too!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(for anyone following along it was #1209)

DateTimeField::Month => "MONTH".to_string(),
DateTimeField::Week(week_day) => {
format!(
"WEEK{}",
week_day
.as_ref()
.map(|w| format!("({w})"))
.unwrap_or_default()
)
}
DateTimeField::Day => "DAY".to_string(),
DateTimeField::DayOfWeek => "DAYOFWEEK".to_string(),
DateTimeField::DayOfYear => "DAYOFYEAR".to_string(),
DateTimeField::Date => "DATE".to_string(),
DateTimeField::Datetime => "DATETIME".to_string(),
DateTimeField::Hour => "HOUR".to_string(),
DateTimeField::Minute => "MINUTE".to_string(),
DateTimeField::Second => "SECOND".to_string(),
DateTimeField::Century => "CENTURY".to_string(),
DateTimeField::Decade => "DECADE".to_string(),
DateTimeField::Dow => "DOW".to_string(),
DateTimeField::Doy => "DOY".to_string(),
DateTimeField::Epoch => "EPOCH".to_string(),
DateTimeField::Isodow => "ISODOW".to_string(),
DateTimeField::Isoyear => "ISOYEAR".to_string(),
DateTimeField::IsoWeek => "ISOWEEK".to_string(),
DateTimeField::Julian => "JULIAN".to_string(),
DateTimeField::Microsecond => "MICROSECOND".to_string(),
DateTimeField::Microseconds => "MICROSECONDS".to_string(),
DateTimeField::Millenium => "MILLENIUM".to_string(),
DateTimeField::Millennium => "MILLENNIUM".to_string(),
DateTimeField::Millisecond => "MILLISECOND".to_string(),
DateTimeField::Milliseconds => "MILLISECONDS".to_string(),
DateTimeField::Nanosecond => "NANOSECOND".to_string(),
DateTimeField::Nanoseconds => "NANOSECONDS".to_string(),
DateTimeField::Quarter => "QUARTER".to_string(),
DateTimeField::Time => "TIME".to_string(),
DateTimeField::Timezone => "TIMEZONE".to_string(),
DateTimeField::TimezoneAbbr => "TIMEZONE_ABBR".to_string(),
DateTimeField::TimezoneHour => "TIMEZONE_HOUR".to_string(),
DateTimeField::TimezoneMinute => "TIMEZONE_MINUTE".to_string(),
DateTimeField::TimezoneRegion => "TIMEZONE_REGION".to_string(),
DateTimeField::NoDateTime => "NODATETIME".to_string(),
DateTimeField::Custom(custom) => format!("{custom}"),
}
.as_str(),
)
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,11 +1778,23 @@ impl<'a> Parser<'a> {
Token::Word(w) => match w.keyword {
Keyword::YEAR => Ok(DateTimeField::Year),
Keyword::MONTH => Ok(DateTimeField::Month),
Keyword::WEEK => Ok(DateTimeField::Week),
Keyword::WEEK => {
let week_day = if dialect_of!(self is BigQueryDialect | GenericDialect)
&& self.consume_token(&Token::LParen)
{
let week_day = self.parse_identifier(false)?;
self.expect_token(&Token::RParen)?;
Some(week_day)
} else {
None
};
Ok(DateTimeField::Week(week_day))
}
Keyword::DAY => Ok(DateTimeField::Day),
Keyword::DAYOFWEEK => Ok(DateTimeField::DayOfWeek),
Keyword::DAYOFYEAR => Ok(DateTimeField::DayOfYear),
Keyword::DATE => Ok(DateTimeField::Date),
Keyword::DATETIME => Ok(DateTimeField::Datetime),
Keyword::HOUR => Ok(DateTimeField::Hour),
Keyword::MINUTE => Ok(DateTimeField::Minute),
Keyword::SECOND => Ok(DateTimeField::Second),
Expand Down Expand Up @@ -1810,6 +1822,11 @@ impl<'a> Parser<'a> {
Keyword::TIMEZONE_HOUR => Ok(DateTimeField::TimezoneHour),
Keyword::TIMEZONE_MINUTE => Ok(DateTimeField::TimezoneMinute),
Keyword::TIMEZONE_REGION => Ok(DateTimeField::TimezoneRegion),
_ if dialect_of!(self is SnowflakeDialect | GenericDialect) => {
self.prev_token();
let custom = self.parse_identifier(false)?;
Ok(DateTimeField::Custom(custom))
}
_ => self.expected("date/time field", next_token),
},
_ => self.expected("date/time field", next_token),
Expand Down
13 changes: 13 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,19 @@ fn test_bigquery_trim() {
);
}

#[test]
fn parse_extract_weekday() {
let sql = "SELECT EXTRACT(WEEK(MONDAY) FROM d)";
let select = bigquery_and_generic().verified_only_select(sql);
assert_eq!(
&Expr::Extract {
field: DateTimeField::Week(Some(Ident::new("MONDAY"))),
expr: Box::new(Expr::Identifier(Ident::new("d"))),
},
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn test_select_as_struct() {
bigquery().verified_only_select("SELECT * FROM (SELECT AS VALUE STRUCT(123 AS a, false AS b))");
Expand Down
10 changes: 7 additions & 3 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ fn parse_extract() {
verified_stmt("SELECT EXTRACT(DAYOFWEEK FROM d)");
verified_stmt("SELECT EXTRACT(DAYOFYEAR FROM d)");
verified_stmt("SELECT EXTRACT(DATE FROM d)");
verified_stmt("SELECT EXTRACT(DATETIME FROM d)");
verified_stmt("SELECT EXTRACT(HOUR FROM d)");
verified_stmt("SELECT EXTRACT(MINUTE FROM d)");
verified_stmt("SELECT EXTRACT(SECOND FROM d)");
Expand Down Expand Up @@ -2299,7 +2300,8 @@ fn parse_extract() {
verified_stmt("SELECT EXTRACT(TIMEZONE_REGION FROM d)");
verified_stmt("SELECT EXTRACT(TIME FROM d)");

let res = parse_sql_statements("SELECT EXTRACT(JIFFY FROM d)");
let dialects = all_dialects_except(|d| d.is::<SnowflakeDialect>() || d.is::<GenericDialect>());
let res = dialects.parse_sql_statements("SELECT EXTRACT(JIFFY FROM d)");
assert_eq!(
ParserError::ParserError("Expected date/time field, found: JIFFY".to_string()),
res.unwrap_err()
Expand Down Expand Up @@ -2337,7 +2339,8 @@ fn parse_ceil_datetime() {
verified_stmt("SELECT CEIL(d TO SECOND) FROM df");
verified_stmt("SELECT CEIL(d TO MILLISECOND) FROM df");

let res = parse_sql_statements("SELECT CEIL(d TO JIFFY) FROM df");
let dialects = all_dialects_except(|d| d.is::<SnowflakeDialect>() || d.is::<GenericDialect>());
let res = dialects.parse_sql_statements("SELECT CEIL(d TO JIFFY) FROM df");
assert_eq!(
ParserError::ParserError("Expected date/time field, found: JIFFY".to_string()),
res.unwrap_err()
Expand All @@ -2363,7 +2366,8 @@ fn parse_floor_datetime() {
verified_stmt("SELECT FLOOR(d TO SECOND) FROM df");
verified_stmt("SELECT FLOOR(d TO MILLISECOND) FROM df");

let res = parse_sql_statements("SELECT FLOOR(d TO JIFFY) FROM df");
let dialects = all_dialects_except(|d| d.is::<SnowflakeDialect>() || d.is::<GenericDialect>());
let res = dialects.parse_sql_statements("SELECT FLOOR(d TO JIFFY) FROM df");
assert_eq!(
ParserError::ParserError("Expected date/time field, found: JIFFY".to_string()),
res.unwrap_err()
Expand Down
13 changes: 13 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,19 @@ fn parse_top() {
);
}

#[test]
fn parse_extract_custom_part() {
let sql = "SELECT EXTRACT(eod FROM d)";
let select = snowflake_and_generic().verified_only_select(sql);
assert_eq!(
&Expr::Extract {
field: DateTimeField::Custom(Ident::new("eod")),
expr: Box::new(Expr::Identifier(Ident::new("d"))),
},
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn parse_comma_outer_join() {
// compound identifiers
Expand Down