Skip to content

Commit f79fe83

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 862e7a6 commit f79fe83

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
@@ -2863,59 +2863,6 @@ fn parse_literal_interval() {
28632863
);
28642864
}
28652865

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

tests/sqlparser_postgres.rs

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

0 commit comments

Comments
 (0)