Skip to content

Commit

Permalink
Merge 81732f3 into 231370a
Browse files Browse the repository at this point in the history
  • Loading branch information
MazterQyou committed Aug 10, 2022
2 parents 231370a + 81732f3 commit 67228f0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4402,13 +4402,13 @@ impl<'a> Parser<'a> {
if self.parse_keyword(Keyword::ALL) {
Ok(None)
} else {
Ok(Some(Expr::Value(self.parse_number_value()?)))
Ok(Some(self.parse_expr()?))
}
}

/// Parse an OFFSET clause
pub fn parse_offset(&mut self) -> Result<Offset, ParserError> {
let value = Expr::Value(self.parse_number_value()?);
let value = self.parse_expr()?;
let rows = if self.parse_keyword(Keyword::ROW) {
OffsetRows::Row
} else if self.parse_keyword(Keyword::ROWS) {
Expand Down
37 changes: 37 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5010,6 +5010,20 @@ fn test_placeholder() {
right: Box::new(Expr::Value(Value::Placeholder("$Id1".into())))
})
);

let sql = "SELECT * FROM student LIMIT $1 OFFSET $2";
let ast = dialects.verified_query(sql);
assert_eq!(
ast.limit,
Some(Expr::Value(Value::Placeholder("$1".into())))
);
assert_eq!(
ast.offset,
Some(Offset {
value: Expr::Value(Value::Placeholder("$2".into())),
rows: OffsetRows::None,
}),
);
}

#[test]
Expand Down Expand Up @@ -5058,6 +5072,29 @@ fn parse_offset_and_limit() {
// different order is OK
one_statement_parses_to("SELECT foo FROM bar OFFSET 2 LIMIT 2", sql);

// expressions are allowed
let sql = "SELECT foo FROM bar LIMIT 1 + 2 OFFSET 3 * 4";
let ast = verified_query(sql);
assert_eq!(
ast.limit,
Some(Expr::BinaryOp {
left: Box::new(Expr::Value(number("1"))),
op: BinaryOperator::Plus,
right: Box::new(Expr::Value(number("2"))),
}),
);
assert_eq!(
ast.offset,
Some(Offset {
value: Expr::BinaryOp {
left: Box::new(Expr::Value(number("3"))),
op: BinaryOperator::Multiply,
right: Box::new(Expr::Value(number("4"))),
},
rows: OffsetRows::None,
}),
);

// Can't repeat OFFSET / LIMIT
let res = parse_sql_statements("SELECT foo FROM bar OFFSET 2 OFFSET 2");
assert_eq!(
Expand Down

0 comments on commit 67228f0

Please sign in to comment.