Skip to content

Commit 13aff89

Browse files
committed
fix: Make interval summing dialect-specific
1 parent 84c33b6 commit 13aff89

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
@@ -1026,7 +1026,12 @@ impl<'a> Parser<'a> {
10261026

10271027
// The first token in an interval is a string literal which specifies
10281028
// the duration of the interval.
1029-
let value = self.parse_subexpr(Self::PLUS_MINUS_PREC)?;
1029+
let value = if dialect_of!(self is PostgreSqlDialect | RedshiftSqlDialect | GenericDialect)
1030+
{
1031+
self.parse_subexpr(Self::PLUS_MINUS_PREC)
1032+
} else {
1033+
self.parse_expr()
1034+
}?;
10301035

10311036
// Following the string literal is a qualifier which indicates the units
10321037
// 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
@@ -2708,59 +2708,6 @@ fn parse_literal_interval() {
27082708
);
27092709
}
27102710

2711-
#[test]
2712-
fn parse_interval_math() {
2713-
let sql = "SELECT INTERVAL '1 DAY' + INTERVAL '2 DAY'";
2714-
let select = verified_only_select(sql);
2715-
assert_eq!(
2716-
&Expr::BinaryOp {
2717-
left: Box::new(Expr::Value(Value::Interval {
2718-
value: Box::new(Expr::Value(Value::SingleQuotedString("1 DAY".to_string()))),
2719-
leading_field: None,
2720-
leading_precision: None,
2721-
last_field: None,
2722-
fractional_seconds_precision: None,
2723-
})),
2724-
op: BinaryOperator::Plus,
2725-
right: Box::new(Expr::Value(Value::Interval {
2726-
value: Box::new(Expr::Value(Value::SingleQuotedString("2 DAY".to_string()))),
2727-
leading_field: None,
2728-
leading_precision: None,
2729-
last_field: None,
2730-
fractional_seconds_precision: None,
2731-
})),
2732-
},
2733-
expr_from_projection(only(&select.projection)),
2734-
);
2735-
2736-
let sql = "SELECT INTERVAL '1' || ' DAY' + INTERVAL '2 DAY'";
2737-
let select = verified_only_select(sql);
2738-
assert_eq!(
2739-
&Expr::BinaryOp {
2740-
left: Box::new(Expr::Value(Value::Interval {
2741-
value: Box::new(Expr::BinaryOp {
2742-
left: Box::new(Expr::Value(Value::SingleQuotedString("1".to_string()))),
2743-
op: BinaryOperator::StringConcat,
2744-
right: Box::new(Expr::Value(Value::SingleQuotedString(" DAY".to_string()))),
2745-
}),
2746-
leading_field: None,
2747-
leading_precision: None,
2748-
last_field: None,
2749-
fractional_seconds_precision: None,
2750-
})),
2751-
op: BinaryOperator::Plus,
2752-
right: Box::new(Expr::Value(Value::Interval {
2753-
value: Box::new(Expr::Value(Value::SingleQuotedString("2 DAY".to_string()))),
2754-
leading_field: None,
2755-
leading_precision: None,
2756-
last_field: None,
2757-
fractional_seconds_precision: None,
2758-
})),
2759-
},
2760-
expr_from_projection(only(&select.projection)),
2761-
);
2762-
}
2763-
27642711
#[test]
27652712
fn parse_at_timezone() {
27662713
let zero = Expr::Value(number("0"));

tests/sqlparser_postgres.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,3 +1493,56 @@ fn parse_declare() {
14931493
.verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" NO SCROLL CURSOR FOR SELECT 1");
14941494
pg_and_generic().verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" BINARY INSENSITIVE SCROLL CURSOR WITH HOLD FOR SELECT * FROM table_name LIMIT 2222");
14951495
}
1496+
1497+
#[test]
1498+
fn parse_interval_math() {
1499+
let sql = "SELECT INTERVAL '1 DAY' + INTERVAL '2 DAY'";
1500+
let select = pg_and_generic().verified_only_select(sql);
1501+
assert_eq!(
1502+
&Expr::BinaryOp {
1503+
left: Box::new(Expr::Value(Value::Interval {
1504+
value: Box::new(Expr::Value(Value::SingleQuotedString("1 DAY".to_string()))),
1505+
leading_field: None,
1506+
leading_precision: None,
1507+
last_field: None,
1508+
fractional_seconds_precision: None,
1509+
})),
1510+
op: BinaryOperator::Plus,
1511+
right: Box::new(Expr::Value(Value::Interval {
1512+
value: Box::new(Expr::Value(Value::SingleQuotedString("2 DAY".to_string()))),
1513+
leading_field: None,
1514+
leading_precision: None,
1515+
last_field: None,
1516+
fractional_seconds_precision: None,
1517+
})),
1518+
},
1519+
expr_from_projection(only(&select.projection)),
1520+
);
1521+
1522+
let sql = "SELECT INTERVAL '1' || ' DAY' + INTERVAL '2 DAY'";
1523+
let select = pg_and_generic().verified_only_select(sql);
1524+
assert_eq!(
1525+
&Expr::BinaryOp {
1526+
left: Box::new(Expr::Value(Value::Interval {
1527+
value: Box::new(Expr::BinaryOp {
1528+
left: Box::new(Expr::Value(Value::SingleQuotedString("1".to_string()))),
1529+
op: BinaryOperator::StringConcat,
1530+
right: Box::new(Expr::Value(Value::SingleQuotedString(" DAY".to_string()))),
1531+
}),
1532+
leading_field: None,
1533+
leading_precision: None,
1534+
last_field: None,
1535+
fractional_seconds_precision: None,
1536+
})),
1537+
op: BinaryOperator::Plus,
1538+
right: Box::new(Expr::Value(Value::Interval {
1539+
value: Box::new(Expr::Value(Value::SingleQuotedString("2 DAY".to_string()))),
1540+
leading_field: None,
1541+
leading_precision: None,
1542+
last_field: None,
1543+
fractional_seconds_precision: None,
1544+
})),
1545+
},
1546+
expr_from_projection(only(&select.projection)),
1547+
);
1548+
}

0 commit comments

Comments
 (0)