Skip to content

Commit dca8d90

Browse files
izveigorMazterQyou
authored andcommitted
fix: unary negation operator with operators: Mul, Div and Mod (apache#902)
1 parent 2c1becf commit dca8d90

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/parser.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ impl<'a> Parser<'a> {
516516
};
517517
Ok(Expr::UnaryOp {
518518
op,
519-
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
519+
expr: Box::new(self.parse_subexpr(Self::MUL_DIV_MOD_OP_PREC)?),
520520
})
521521
}
522522
tok @ Token::DoubleExclamationMark
@@ -1485,6 +1485,7 @@ impl<'a> Parser<'a> {
14851485
}
14861486

14871487
// use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a reference
1488+
const MUL_DIV_MOD_OP_PREC: u8 = 40;
14881489
const PLUS_MINUS_PREC: u8 = 30;
14891490
const XOR_PREC: u8 = 24;
14901491
const TIME_ZONE_PREC: u8 = 20;
@@ -1554,7 +1555,9 @@ impl<'a> Parser<'a> {
15541555
Token::Caret | Token::Sharp | Token::ShiftRight | Token::ShiftLeft => Ok(22),
15551556
Token::Ampersand => Ok(23),
15561557
Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
1557-
Token::Mul | Token::Div | Token::Mod | Token::StringConcat => Ok(40),
1558+
Token::Mul | Token::Div | Token::Mod | Token::StringConcat => {
1559+
Ok(Self::MUL_DIV_MOD_OP_PREC)
1560+
}
15581561
Token::DoubleColon => Ok(50),
15591562
Token::ExclamationMark => Ok(50),
15601563
Token::LBracket => Ok(50),

tests/sqlparser_common.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ fn parse_compound_expr_2() {
683683
}
684684

685685
#[test]
686-
fn parse_unary_math() {
686+
fn parse_unary_math_with_plus() {
687687
use self::Expr::*;
688688
let sql = "- a + - b";
689689
assert_eq!(
@@ -702,6 +702,26 @@ fn parse_unary_math() {
702702
);
703703
}
704704

705+
#[test]
706+
fn parse_unary_math_with_multiply() {
707+
use self::Expr::*;
708+
let sql = "- a * - b";
709+
assert_eq!(
710+
BinaryOp {
711+
left: Box::new(UnaryOp {
712+
op: UnaryOperator::Minus,
713+
expr: Box::new(Identifier(Ident::new("a"))),
714+
}),
715+
op: BinaryOperator::Multiply,
716+
right: Box::new(UnaryOp {
717+
op: UnaryOperator::Minus,
718+
expr: Box::new(Identifier(Ident::new("b"))),
719+
}),
720+
},
721+
verified_expr(sql)
722+
);
723+
}
724+
705725
#[test]
706726
fn parse_is_null() {
707727
use self::Expr::*;

0 commit comments

Comments
 (0)