Skip to content

Commit 222b7d1

Browse files
authored
allow DateTimeField::Custom with EXTRACT in Postgres (apache#1394)
1 parent 7282ce2 commit 222b7d1

File tree

6 files changed

+111
-6
lines changed

6 files changed

+111
-6
lines changed

src/dialect/generic.rs

+8
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ impl Dialect for GenericDialect {
7878
fn support_map_literal_syntax(&self) -> bool {
7979
true
8080
}
81+
82+
fn allow_extract_custom(&self) -> bool {
83+
true
84+
}
85+
86+
fn allow_extract_single_quotes(&self) -> bool {
87+
true
88+
}
8189
}

src/dialect/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,16 @@ pub trait Dialect: Debug + Any {
499499
fn describe_requires_table_keyword(&self) -> bool {
500500
false
501501
}
502+
503+
/// Returns true if this dialect allows the `EXTRACT` function to words other than [`Keyword`].
504+
fn allow_extract_custom(&self) -> bool {
505+
false
506+
}
507+
508+
/// Returns true if this dialect allows the `EXTRACT` function to use single quotes in the part being extracted.
509+
fn allow_extract_single_quotes(&self) -> bool {
510+
false
511+
}
502512
}
503513

504514
/// This represents the operators for which precedence must be defined

src/dialect/postgresql.rs

+8
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ impl Dialect for PostgreSqlDialect {
154154
Precedence::Or => OR_PREC,
155155
}
156156
}
157+
158+
fn allow_extract_custom(&self) -> bool {
159+
true
160+
}
161+
162+
fn allow_extract_single_quotes(&self) -> bool {
163+
true
164+
}
157165
}
158166

159167
pub fn parse_comment(parser: &mut Parser) -> Result<Statement, ParserError> {

src/dialect/snowflake.rs

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ impl Dialect for SnowflakeDialect {
158158
fn describe_requires_table_keyword(&self) -> bool {
159159
true
160160
}
161+
162+
fn allow_extract_custom(&self) -> bool {
163+
true
164+
}
165+
166+
fn allow_extract_single_quotes(&self) -> bool {
167+
true
168+
}
161169
}
162170

163171
/// Parse snowflake create table statement.

src/parser/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1970,15 +1970,14 @@ impl<'a> Parser<'a> {
19701970
Keyword::TIMEZONE_HOUR => Ok(DateTimeField::TimezoneHour),
19711971
Keyword::TIMEZONE_MINUTE => Ok(DateTimeField::TimezoneMinute),
19721972
Keyword::TIMEZONE_REGION => Ok(DateTimeField::TimezoneRegion),
1973-
_ if dialect_of!(self is SnowflakeDialect | GenericDialect) => {
1973+
_ if self.dialect.allow_extract_custom() => {
19741974
self.prev_token();
19751975
let custom = self.parse_identifier(false)?;
19761976
Ok(DateTimeField::Custom(custom))
19771977
}
19781978
_ => self.expected("date/time field", next_token),
19791979
},
1980-
Token::SingleQuotedString(_) if dialect_of!(self is SnowflakeDialect | GenericDialect) =>
1981-
{
1980+
Token::SingleQuotedString(_) if self.dialect.allow_extract_single_quotes() => {
19821981
self.prev_token();
19831982
let custom = self.parse_identifier(false)?;
19841983
Ok(DateTimeField::Custom(custom))

tests/sqlparser_common.rs

+75-3
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,7 @@ fn parse_extract() {
24742474
verified_stmt("SELECT EXTRACT(TIMEZONE_REGION FROM d)");
24752475
verified_stmt("SELECT EXTRACT(TIME FROM d)");
24762476

2477-
let dialects = all_dialects_except(|d| d.is::<SnowflakeDialect>() || d.is::<GenericDialect>());
2477+
let dialects = all_dialects_except(|d| d.allow_extract_custom());
24782478
let res = dialects.parse_sql_statements("SELECT EXTRACT(JIFFY FROM d)");
24792479
assert_eq!(
24802480
ParserError::ParserError("Expected: date/time field, found: JIFFY".to_string()),
@@ -2573,7 +2573,7 @@ fn parse_ceil_datetime() {
25732573
verified_stmt("SELECT CEIL(d TO SECOND) FROM df");
25742574
verified_stmt("SELECT CEIL(d TO MILLISECOND) FROM df");
25752575

2576-
let dialects = all_dialects_except(|d| d.is::<SnowflakeDialect>() || d.is::<GenericDialect>());
2576+
let dialects = all_dialects_except(|d| d.allow_extract_custom());
25772577
let res = dialects.parse_sql_statements("SELECT CEIL(d TO JIFFY) FROM df");
25782578
assert_eq!(
25792579
ParserError::ParserError("Expected: date/time field, found: JIFFY".to_string()),
@@ -2600,7 +2600,7 @@ fn parse_floor_datetime() {
26002600
verified_stmt("SELECT FLOOR(d TO SECOND) FROM df");
26012601
verified_stmt("SELECT FLOOR(d TO MILLISECOND) FROM df");
26022602

2603-
let dialects = all_dialects_except(|d| d.is::<SnowflakeDialect>() || d.is::<GenericDialect>());
2603+
let dialects = all_dialects_except(|d| d.allow_extract_custom());
26042604
let res = dialects.parse_sql_statements("SELECT FLOOR(d TO JIFFY) FROM df");
26052605
assert_eq!(
26062606
ParserError::ParserError("Expected: date/time field, found: JIFFY".to_string()),
@@ -10467,3 +10467,75 @@ fn test_group_by_nothing() {
1046710467
);
1046810468
}
1046910469
}
10470+
10471+
#[test]
10472+
fn test_extract_seconds_ok() {
10473+
let dialects = all_dialects_where(|d| d.allow_extract_custom());
10474+
let stmt = dialects.verified_expr("EXTRACT(seconds FROM '2 seconds'::INTERVAL)");
10475+
10476+
assert_eq!(
10477+
stmt,
10478+
Expr::Extract {
10479+
field: DateTimeField::Custom(Ident {
10480+
value: "seconds".to_string(),
10481+
quote_style: None,
10482+
}),
10483+
syntax: ExtractSyntax::From,
10484+
expr: Box::new(Expr::Cast {
10485+
kind: CastKind::DoubleColon,
10486+
expr: Box::new(Expr::Value(Value::SingleQuotedString(
10487+
"2 seconds".to_string()
10488+
))),
10489+
data_type: DataType::Interval,
10490+
format: None,
10491+
}),
10492+
}
10493+
)
10494+
}
10495+
10496+
#[test]
10497+
fn test_extract_seconds_single_quote_ok() {
10498+
let dialects = all_dialects_where(|d| d.allow_extract_custom());
10499+
let stmt = dialects.verified_expr(r#"EXTRACT('seconds' FROM '2 seconds'::INTERVAL)"#);
10500+
10501+
assert_eq!(
10502+
stmt,
10503+
Expr::Extract {
10504+
field: DateTimeField::Custom(Ident {
10505+
value: "seconds".to_string(),
10506+
quote_style: Some('\''),
10507+
}),
10508+
syntax: ExtractSyntax::From,
10509+
expr: Box::new(Expr::Cast {
10510+
kind: CastKind::DoubleColon,
10511+
expr: Box::new(Expr::Value(Value::SingleQuotedString(
10512+
"2 seconds".to_string()
10513+
))),
10514+
data_type: DataType::Interval,
10515+
format: None,
10516+
}),
10517+
}
10518+
)
10519+
}
10520+
10521+
#[test]
10522+
fn test_extract_seconds_err() {
10523+
let sql = "SELECT EXTRACT(seconds FROM '2 seconds'::INTERVAL)";
10524+
let dialects = all_dialects_except(|d| d.allow_extract_custom());
10525+
let err = dialects.parse_sql_statements(sql).unwrap_err();
10526+
assert_eq!(
10527+
err.to_string(),
10528+
"sql parser error: Expected: date/time field, found: seconds"
10529+
);
10530+
}
10531+
10532+
#[test]
10533+
fn test_extract_seconds_single_quote_err() {
10534+
let sql = r#"SELECT EXTRACT('seconds' FROM '2 seconds'::INTERVAL)"#;
10535+
let dialects = all_dialects_except(|d| d.allow_extract_single_quotes());
10536+
let err = dialects.parse_sql_statements(sql).unwrap_err();
10537+
assert_eq!(
10538+
err.to_string(),
10539+
"sql parser error: Expected: date/time field, found: 'seconds'"
10540+
);
10541+
}

0 commit comments

Comments
 (0)