Skip to content

Commit

Permalink
feat(query): support interval 'string'
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason committed Dec 23, 2024
1 parent 0e12f28 commit 74c064e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/query/ast/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,19 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
},
);

let interval_expr = map(
rule! {
INTERVAL ~ #consumed(literal_string)
},
|(_, (span, date))| ExprElement::Cast {
expr: Box::new(Expr::Literal {
span: transform_span(span.tokens),
value: Literal::String(date),
}),
target_type: TypeName::Interval,
},
);

let is_distinct_from = map(
rule! {
IS ~ NOT? ~ DISTINCT ~ FROM
Expand Down Expand Up @@ -1360,6 +1373,7 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
| #date_expr : "`DATE <str_literal>`"
| #timestamp_expr : "`TIMESTAMP <str_literal>`"
| #interval : "`INTERVAL ... (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW)`"
| #interval_expr : "`INTERVAL <str_literal>`"
| #extract : "`EXTRACT((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | WEEK) FROM ...)`"
| #date_part : "`DATE_PART((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | WEEK), ...)`"

Expand Down
1 change: 1 addition & 0 deletions src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ fn test_expr() {
r#"MAP_FILTER({1:1,2:2,3:4}, (k, v) -> k > v)"#,
r#"MAP_TRANSFORM_KEYS({1:10,2:20,3:30}, (k, v) -> k + 1)"#,
r#"MAP_TRANSFORM_VALUES({1:10,2:20,3:30}, (k, v) -> v + 1)"#,
r#"INTERVAL '1 YEAR'"#,
];

for case in cases {
Expand Down
22 changes: 22 additions & 0 deletions src/query/ast/tests/it/testdata/expr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5034,3 +5034,25 @@ FunctionCall {
}


---------- Input ----------
INTERVAL '1 YEAR'
---------- Output ---------
CAST('1 YEAR' AS INTERVAL)
---------- AST ------------
Cast {
span: Some(
0..17,
),
expr: Literal {
span: Some(
9..17,
),
value: String(
"1 YEAR",
),
},
target_type: Interval,
pg_style: false,
}


Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
onlyif http
query T
select interval '1 month 1 hour 1 microsecond ago'
----
-1 month -1:00:00.000001

onlyif http
statement ok
create or replace table t(c1 interval, c2 interval);
Expand Down

0 comments on commit 74c064e

Please sign in to comment.