Skip to content

Commit

Permalink
Support for INTERVAL inside window frames (#655)
Browse files Browse the repository at this point in the history
* Add support to for INTERVAL inside window queries

* Remove the unnecessary ancillary struct Interval

* Convert Window Frame Bound to Expr

* remove unnecessary changes

* remove unnecessary changes

Co-authored-by: Mehmet Ozan Kabak <ozankabak@gmail.com>
  • Loading branch information
mustafasrepo and ozankabak committed Oct 15, 2022
1 parent cacdf33 commit 427bec4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/ast/mod.rs
Expand Up @@ -906,9 +906,9 @@ pub enum WindowFrameBound {
/// `CURRENT ROW`
CurrentRow,
/// `<N> PRECEDING` or `UNBOUNDED PRECEDING`
Preceding(Option<u64>),
Preceding(Option<Box<Expr>>),
/// `<N> FOLLOWING` or `UNBOUNDED FOLLOWING`.
Following(Option<u64>),
Following(Option<Box<Expr>>),
}

impl fmt::Display for WindowFrameBound {
Expand Down
6 changes: 4 additions & 2 deletions src/parser.rs
Expand Up @@ -623,7 +623,6 @@ impl<'a> Parser<'a> {
} else {
None
};

Ok(Expr::Function(Function {
name,
args,
Expand Down Expand Up @@ -685,7 +684,10 @@ impl<'a> Parser<'a> {
let rows = if self.parse_keyword(Keyword::UNBOUNDED) {
None
} else {
Some(self.parse_literal_uint()?)
Some(Box::new(match self.peek_token() {
Token::SingleQuotedString(_) => self.parse_interval()?,
_ => self.parse_expr()?,
}))
};
if self.parse_keyword(Keyword::PRECEDING) {
Ok(WindowFrameBound::Preceding(rows))
Expand Down
6 changes: 5 additions & 1 deletion tests/sqlparser_common.rs
Expand Up @@ -2940,13 +2940,17 @@ fn parse_window_functions() {
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), \
avg(bar) OVER (ORDER BY a \
RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING), \
sum(bar) OVER (ORDER BY a \
RANGE BETWEEN INTERVAL '1' DAY PRECEDING AND INTERVAL '1 MONTH' FOLLOWING), \
COUNT(*) OVER (ORDER BY a \
RANGE BETWEEN INTERVAL '1 DAY' PRECEDING AND INTERVAL '1 DAY' FOLLOWING), \
max(baz) OVER (ORDER BY a \
ROWS UNBOUNDED PRECEDING), \
sum(qux) OVER (ORDER BY a \
GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING) \
FROM foo";
let select = verified_only_select(sql);
assert_eq!(5, select.projection.len());
assert_eq!(7, select.projection.len());
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("row_number")]),
Expand Down

0 comments on commit 427bec4

Please sign in to comment.