Support expressions in LIMIT/OFFSET (#567)

This commit is contained in:
Alex Qyoun-ae 2022-08-11 14:50:18 +04:00 committed by GitHub
parent a9db6ed139
commit 8176561100
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View file

@ -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]
@ -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!(