Skip to content

Commit e7d9d22

Browse files
izveigormcheshkov
authored andcommitted
fix: unary negation operator with operators: Mul, Div and Mod (apache#902)
Can drop this after rebase on commit 8877cba " fix: unary negation operator with operators: Mul, Div and Mod (apache#902)", first released in 0.35.0
1 parent f79fe83 commit e7d9d22

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
@@ -504,7 +504,7 @@ impl<'a> Parser<'a> {
504504
};
505505
Ok(Expr::UnaryOp {
506506
op,
507-
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
507+
expr: Box::new(self.parse_subexpr(Self::MUL_DIV_MOD_OP_PREC)?),
508508
})
509509
}
510510
tok @ Token::DoubleExclamationMark
@@ -1476,6 +1476,7 @@ impl<'a> Parser<'a> {
14761476
}
14771477

14781478
// use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a reference
1479+
const MUL_DIV_MOD_OP_PREC: u8 = 40;
14791480
const PLUS_MINUS_PREC: u8 = 30;
14801481
const XOR_PREC: u8 = 24;
14811482
const TIME_ZONE_PREC: u8 = 20;
@@ -1545,7 +1546,9 @@ impl<'a> Parser<'a> {
15451546
Token::Caret | Token::Sharp | Token::ShiftRight | Token::ShiftLeft => Ok(22),
15461547
Token::Ampersand => Ok(23),
15471548
Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
1548-
Token::Mul | Token::Div | Token::Mod | Token::StringConcat => Ok(40),
1549+
Token::Mul | Token::Div | Token::Mod | Token::StringConcat => {
1550+
Ok(Self::MUL_DIV_MOD_OP_PREC)
1551+
}
15491552
Token::DoubleColon => Ok(50),
15501553
Token::ExclamationMark => Ok(50),
15511554
Token::LBracket

tests/sqlparser_common.rs

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

676676
#[test]
677-
fn parse_unary_math() {
677+
fn parse_unary_math_with_plus() {
678678
use self::Expr::*;
679679
let sql = "- a + - b";
680680
assert_eq!(
@@ -693,6 +693,26 @@ fn parse_unary_math() {
693693
);
694694
}
695695

696+
#[test]
697+
fn parse_unary_math_with_multiply() {
698+
use self::Expr::*;
699+
let sql = "- a * - b";
700+
assert_eq!(
701+
BinaryOp {
702+
left: Box::new(UnaryOp {
703+
op: UnaryOperator::Minus,
704+
expr: Box::new(Identifier(Ident::new("a"))),
705+
}),
706+
op: BinaryOperator::Multiply,
707+
right: Box::new(UnaryOp {
708+
op: UnaryOperator::Minus,
709+
expr: Box::new(Identifier(Ident::new("b"))),
710+
}),
711+
},
712+
verified_expr(sql)
713+
);
714+
}
715+
696716
#[test]
697717
fn parse_is_null() {
698718
use self::Expr::*;

0 commit comments

Comments
 (0)