mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-03 22:08:16 +00:00
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:
parent
d842f495db
commit
846c52f450
3 changed files with 44 additions and 18 deletions
|
@ -1459,7 +1459,7 @@ fn parse_literal_interval() {
|
|||
assert_eq!(
|
||||
&Expr::Value(Value::Interval {
|
||||
value: "1-1".into(),
|
||||
leading_field: DateTimeField::Year,
|
||||
leading_field: Some(DateTimeField::Year),
|
||||
leading_precision: None,
|
||||
last_field: Some(DateTimeField::Month),
|
||||
fractional_seconds_precision: None,
|
||||
|
@ -1472,7 +1472,7 @@ fn parse_literal_interval() {
|
|||
assert_eq!(
|
||||
&Expr::Value(Value::Interval {
|
||||
value: "01:01.01".into(),
|
||||
leading_field: DateTimeField::Minute,
|
||||
leading_field: Some(DateTimeField::Minute),
|
||||
leading_precision: Some(5),
|
||||
last_field: Some(DateTimeField::Second),
|
||||
fractional_seconds_precision: Some(5),
|
||||
|
@ -1485,7 +1485,7 @@ fn parse_literal_interval() {
|
|||
assert_eq!(
|
||||
&Expr::Value(Value::Interval {
|
||||
value: "1".into(),
|
||||
leading_field: DateTimeField::Second,
|
||||
leading_field: Some(DateTimeField::Second),
|
||||
leading_precision: Some(5),
|
||||
last_field: None,
|
||||
fractional_seconds_precision: Some(4),
|
||||
|
@ -1498,7 +1498,7 @@ fn parse_literal_interval() {
|
|||
assert_eq!(
|
||||
&Expr::Value(Value::Interval {
|
||||
value: "10".into(),
|
||||
leading_field: DateTimeField::Hour,
|
||||
leading_field: Some(DateTimeField::Hour),
|
||||
leading_precision: None,
|
||||
last_field: None,
|
||||
fractional_seconds_precision: None,
|
||||
|
@ -1511,7 +1511,7 @@ fn parse_literal_interval() {
|
|||
assert_eq!(
|
||||
&Expr::Value(Value::Interval {
|
||||
value: "10".into(),
|
||||
leading_field: DateTimeField::Hour,
|
||||
leading_field: Some(DateTimeField::Hour),
|
||||
leading_precision: Some(1),
|
||||
last_field: None,
|
||||
fractional_seconds_precision: None,
|
||||
|
@ -1519,6 +1519,19 @@ fn parse_literal_interval() {
|
|||
expr_from_projection(only(&select.projection)),
|
||||
);
|
||||
|
||||
let sql = "SELECT INTERVAL '1 DAY'";
|
||||
let select = verified_only_select(sql);
|
||||
assert_eq!(
|
||||
&Expr::Value(Value::Interval {
|
||||
value: "1 DAY".into(),
|
||||
leading_field: None,
|
||||
leading_precision: None,
|
||||
last_field: None,
|
||||
fractional_seconds_precision: None,
|
||||
}),
|
||||
expr_from_projection(only(&select.projection)),
|
||||
);
|
||||
|
||||
let result = parse_sql_statements("SELECT INTERVAL '1' SECOND TO SECOND");
|
||||
assert_eq!(
|
||||
ParserError::ParserError("Expected end of statement, found: SECOND".to_string()),
|
||||
|
@ -1544,6 +1557,12 @@ fn parse_literal_interval() {
|
|||
verified_only_select("SELECT INTERVAL '1' HOUR TO MINUTE");
|
||||
verified_only_select("SELECT INTERVAL '1' HOUR TO SECOND");
|
||||
verified_only_select("SELECT INTERVAL '1' MINUTE TO SECOND");
|
||||
verified_only_select("SELECT INTERVAL '1 YEAR'");
|
||||
verified_only_select("SELECT INTERVAL '1 YEAR' AS one_year");
|
||||
one_statement_parses_to(
|
||||
"SELECT INTERVAL '1 YEAR' one_year",
|
||||
"SELECT INTERVAL '1 YEAR' AS one_year",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue