allow DateTimeField::Custom with EXTRACT in Postgres (#1394)

This commit is contained in:
Samuel Colvin 2024-08-26 20:56:57 +01:00 committed by GitHub
parent 7282ce22f9
commit 222b7d127a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 111 additions and 6 deletions

View file

@ -78,4 +78,12 @@ impl Dialect for GenericDialect {
fn support_map_literal_syntax(&self) -> bool {
true
}
fn allow_extract_custom(&self) -> bool {
true
}
fn allow_extract_single_quotes(&self) -> bool {
true
}
}

View file

@ -499,6 +499,16 @@ pub trait Dialect: Debug + Any {
fn describe_requires_table_keyword(&self) -> bool {
false
}
/// Returns true if this dialect allows the `EXTRACT` function to words other than [`Keyword`].
fn allow_extract_custom(&self) -> bool {
false
}
/// Returns true if this dialect allows the `EXTRACT` function to use single quotes in the part being extracted.
fn allow_extract_single_quotes(&self) -> bool {
false
}
}
/// This represents the operators for which precedence must be defined

View file

@ -154,6 +154,14 @@ impl Dialect for PostgreSqlDialect {
Precedence::Or => OR_PREC,
}
}
fn allow_extract_custom(&self) -> bool {
true
}
fn allow_extract_single_quotes(&self) -> bool {
true
}
}
pub fn parse_comment(parser: &mut Parser) -> Result<Statement, ParserError> {

View file

@ -158,6 +158,14 @@ impl Dialect for SnowflakeDialect {
fn describe_requires_table_keyword(&self) -> bool {
true
}
fn allow_extract_custom(&self) -> bool {
true
}
fn allow_extract_single_quotes(&self) -> bool {
true
}
}
/// Parse snowflake create table statement.

View file

@ -1970,15 +1970,14 @@ 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) => {
_ if self.dialect.allow_extract_custom() => {
self.prev_token();
let custom = self.parse_identifier(false)?;
Ok(DateTimeField::Custom(custom))
}
_ => self.expected("date/time field", next_token),
},
Token::SingleQuotedString(_) if dialect_of!(self is SnowflakeDialect | GenericDialect) =>
{
Token::SingleQuotedString(_) if self.dialect.allow_extract_single_quotes() => {
self.prev_token();
let custom = self.parse_identifier(false)?;
Ok(DateTimeField::Custom(custom))