Skip to content

Commit c40a3b9

Browse files
MazterQyoumcheshkov
authored andcommitted
fix: Make interval summing dialect-specific
Recheck this after rebase on commit 57083a0 "Fix interval parsing logic and precedence (apache#705)", first released in 0.28.0
1 parent 5fc1434 commit c40a3b9

File tree

3 files changed

+59
-54
lines changed

3 files changed

+59
-54
lines changed

src/parser.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,12 @@ impl<'a> Parser<'a> {
10951095

10961096
// The first token in an interval is a string literal which specifies
10971097
// the duration of the interval.
1098-
let value = self.parse_subexpr(Self::PLUS_MINUS_PREC)?;
1098+
let value = if dialect_of!(self is PostgreSqlDialect | RedshiftSqlDialect | GenericDialect)
1099+
{
1100+
self.parse_subexpr(Self::PLUS_MINUS_PREC)
1101+
} else {
1102+
self.parse_expr()
1103+
}?;
10991104

11001105
// Following the string literal is a qualifier which indicates the units
11011106
// of the duration specified in the string literal.

tests/sqlparser_common.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,59 +2864,6 @@ fn parse_literal_interval() {
28642864
);
28652865
}
28662866

2867-
#[test]
2868-
fn parse_interval_math() {
2869-
let sql = "SELECT INTERVAL '1 DAY' + INTERVAL '2 DAY'";
2870-
let select = verified_only_select(sql);
2871-
assert_eq!(
2872-
&Expr::BinaryOp {
2873-
left: Box::new(Expr::Value(Value::Interval {
2874-
value: Box::new(Expr::Value(Value::SingleQuotedString("1 DAY".to_string()))),
2875-
leading_field: None,
2876-
leading_precision: None,
2877-
last_field: None,
2878-
fractional_seconds_precision: None,
2879-
})),
2880-
op: BinaryOperator::Plus,
2881-
right: Box::new(Expr::Value(Value::Interval {
2882-
value: Box::new(Expr::Value(Value::SingleQuotedString("2 DAY".to_string()))),
2883-
leading_field: None,
2884-
leading_precision: None,
2885-
last_field: None,
2886-
fractional_seconds_precision: None,
2887-
})),
2888-
},
2889-
expr_from_projection(only(&select.projection)),
2890-
);
2891-
2892-
let sql = "SELECT INTERVAL '1' || ' DAY' + INTERVAL '2 DAY'";
2893-
let select = verified_only_select(sql);
2894-
assert_eq!(
2895-
&Expr::BinaryOp {
2896-
left: Box::new(Expr::Value(Value::Interval {
2897-
value: Box::new(Expr::BinaryOp {
2898-
left: Box::new(Expr::Value(Value::SingleQuotedString("1".to_string()))),
2899-
op: BinaryOperator::StringConcat,
2900-
right: Box::new(Expr::Value(Value::SingleQuotedString(" DAY".to_string()))),
2901-
}),
2902-
leading_field: None,
2903-
leading_precision: None,
2904-
last_field: None,
2905-
fractional_seconds_precision: None,
2906-
})),
2907-
op: BinaryOperator::Plus,
2908-
right: Box::new(Expr::Value(Value::Interval {
2909-
value: Box::new(Expr::Value(Value::SingleQuotedString("2 DAY".to_string()))),
2910-
leading_field: None,
2911-
leading_precision: None,
2912-
last_field: None,
2913-
fractional_seconds_precision: None,
2914-
})),
2915-
},
2916-
expr_from_projection(only(&select.projection)),
2917-
);
2918-
}
2919-
29202867
#[test]
29212868
fn parse_at_timezone() {
29222869
let zero = Expr::Value(number("0"));

tests/sqlparser_postgres.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,3 +1683,56 @@ fn parse_fetch() {
16831683
pg_and_generic()
16841684
.verified_stmt("FETCH BACKWARD ALL IN \"SQL_CUR0x7fa44801bc00\" INTO \"new_table\"");
16851685
}
1686+
1687+
#[test]
1688+
fn parse_interval_math() {
1689+
let sql = "SELECT INTERVAL '1 DAY' + INTERVAL '2 DAY'";
1690+
let select = pg_and_generic().verified_only_select(sql);
1691+
assert_eq!(
1692+
&Expr::BinaryOp {
1693+
left: Box::new(Expr::Value(Value::Interval {
1694+
value: Box::new(Expr::Value(Value::SingleQuotedString("1 DAY".to_string()))),
1695+
leading_field: None,
1696+
leading_precision: None,
1697+
last_field: None,
1698+
fractional_seconds_precision: None,
1699+
})),
1700+
op: BinaryOperator::Plus,
1701+
right: Box::new(Expr::Value(Value::Interval {
1702+
value: Box::new(Expr::Value(Value::SingleQuotedString("2 DAY".to_string()))),
1703+
leading_field: None,
1704+
leading_precision: None,
1705+
last_field: None,
1706+
fractional_seconds_precision: None,
1707+
})),
1708+
},
1709+
expr_from_projection(only(&select.projection)),
1710+
);
1711+
1712+
let sql = "SELECT INTERVAL '1' || ' DAY' + INTERVAL '2 DAY'";
1713+
let select = pg_and_generic().verified_only_select(sql);
1714+
assert_eq!(
1715+
&Expr::BinaryOp {
1716+
left: Box::new(Expr::Value(Value::Interval {
1717+
value: Box::new(Expr::BinaryOp {
1718+
left: Box::new(Expr::Value(Value::SingleQuotedString("1".to_string()))),
1719+
op: BinaryOperator::StringConcat,
1720+
right: Box::new(Expr::Value(Value::SingleQuotedString(" DAY".to_string()))),
1721+
}),
1722+
leading_field: None,
1723+
leading_precision: None,
1724+
last_field: None,
1725+
fractional_seconds_precision: None,
1726+
})),
1727+
op: BinaryOperator::Plus,
1728+
right: Box::new(Expr::Value(Value::Interval {
1729+
value: Box::new(Expr::Value(Value::SingleQuotedString("2 DAY".to_string()))),
1730+
leading_field: None,
1731+
leading_precision: None,
1732+
last_field: None,
1733+
fractional_seconds_precision: None,
1734+
})),
1735+
},
1736+
expr_from_projection(only(&select.projection)),
1737+
);
1738+
}

0 commit comments

Comments
 (0)