Skip to content

Commit 6a48f44

Browse files
authored
Updated parse_infix(..) in mysql.rs and sqlite.rs to handle error rather than unwrap() (#2207)
1 parent 4b4a9d7 commit 6a48f44

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/dialect/mysql.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,15 @@ impl Dialect for MySqlDialect {
102102
) -> Option<Result<crate::ast::Expr, ParserError>> {
103103
// Parse DIV as an operator
104104
if parser.parse_keyword(Keyword::DIV) {
105+
let left = Box::new(expr.clone());
106+
let right = Box::new(match parser.parse_expr() {
107+
Ok(expr) => expr,
108+
Err(e) => return Some(Err(e)),
109+
});
105110
Some(Ok(Expr::BinaryOp {
106-
left: Box::new(expr.clone()),
111+
left,
107112
op: BinaryOperator::MyIntegerDivide,
108-
right: Box::new(parser.parse_expr().unwrap()),
113+
right,
109114
}))
110115
} else {
111116
None

tests/sqlparser_mysql.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,6 +3627,14 @@ fn parse_div_infix() {
36273627
mysql().verified_stmt(r#"SELECT 5 DIV 2"#);
36283628
}
36293629

3630+
#[test]
3631+
fn parse_div_infix_propagates_parse_error() {
3632+
let err = mysql()
3633+
.parse_sql_statements("SELECT 5 DIV")
3634+
.expect_err("expected an error");
3635+
assert_matches!(err, ParserError::ParserError(_));
3636+
}
3637+
36303638
#[test]
36313639
fn parse_drop_temporary_table() {
36323640
let sql = "DROP TEMPORARY TABLE foo";

0 commit comments

Comments
 (0)