Skip to content

Commit f31636d

Browse files
committed
Simplify parse_window_frame
It used to consume the `RParen` closing the encompassing `OVER (`, even when no window frame was parsed, which confused me a bit, even though I wrote it initially. After fixing that, I took the opportunity to reduce nesting and duplication a bit.
1 parent 9314371 commit f31636d

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

src/parser.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,13 @@ impl Parser {
294294
} else {
295295
vec![]
296296
};
297-
let window_frame = self.parse_window_frame()?;
297+
let window_frame = if !self.consume_token(&Token::RParen) {
298+
let window_frame = self.parse_window_frame()?;
299+
self.expect_token(&Token::RParen)?;
300+
Some(window_frame)
301+
} else {
302+
None
303+
};
298304

299305
Some(WindowSpec {
300306
partition_by,
@@ -313,35 +319,24 @@ impl Parser {
313319
}))
314320
}
315321

316-
pub fn parse_window_frame(&mut self) -> Result<Option<WindowFrame>, ParserError> {
317-
let window_frame = match self.peek_token() {
318-
Some(Token::Word(w)) => {
319-
let units = w.keyword.parse::<WindowFrameUnits>()?;
320-
self.next_token();
321-
if self.parse_keyword("BETWEEN") {
322-
let start_bound = self.parse_window_frame_bound()?;
323-
self.expect_keyword("AND")?;
324-
let end_bound = Some(self.parse_window_frame_bound()?);
325-
Some(WindowFrame {
326-
units,
327-
start_bound,
328-
end_bound,
329-
})
330-
} else {
331-
let start_bound = self.parse_window_frame_bound()?;
332-
let end_bound = None;
333-
Some(WindowFrame {
334-
units,
335-
start_bound,
336-
end_bound,
337-
})
338-
}
339-
}
340-
Some(Token::RParen) => None,
341-
unexpected => return self.expected("'ROWS', 'RANGE', 'GROUPS', or ')'", unexpected),
322+
pub fn parse_window_frame(&mut self) -> Result<WindowFrame, ParserError> {
323+
let units = match self.next_token() {
324+
Some(Token::Word(w)) => w.keyword.parse::<WindowFrameUnits>()?,
325+
unexpected => return self.expected("ROWS, RANGE, GROUPS", unexpected),
342326
};
343-
self.expect_token(&Token::RParen)?;
344-
Ok(window_frame)
327+
let (start_bound, end_bound) = if self.parse_keyword("BETWEEN") {
328+
let start_bound = self.parse_window_frame_bound()?;
329+
self.expect_keyword("AND")?;
330+
let end_bound = Some(self.parse_window_frame_bound()?);
331+
(start_bound, end_bound)
332+
} else {
333+
(self.parse_window_frame_bound()?, None)
334+
};
335+
Ok(WindowFrame {
336+
units,
337+
start_bound,
338+
end_bound,
339+
})
345340
}
346341

347342
/// "CURRENT ROW" | ( (<positive number> | "UNBOUNDED") ("PRECEDING" | FOLLOWING) )

0 commit comments

Comments
 (0)