Skip to content

Commit 8877cba

Browse files
authored
fix: unary negation operator with operators: Mul, Div and Mod (#902)
1 parent f72b5a5 commit 8877cba

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/parser.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl<'a> Parser<'a> {
799799
};
800800
Ok(Expr::UnaryOp {
801801
op,
802-
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
802+
expr: Box::new(self.parse_subexpr(Self::MUL_DIV_MOD_OP_PREC)?),
803803
})
804804
}
805805
tok @ Token::DoubleExclamationMark
@@ -1964,6 +1964,7 @@ impl<'a> Parser<'a> {
19641964
}
19651965

19661966
// use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a reference
1967+
const MUL_DIV_MOD_OP_PREC: u8 = 40;
19671968
const PLUS_MINUS_PREC: u8 = 30;
19681969
const XOR_PREC: u8 = 24;
19691970
const TIME_ZONE_PREC: u8 = 20;
@@ -1974,8 +1975,6 @@ impl<'a> Parser<'a> {
19741975
const AND_PREC: u8 = 10;
19751976
const OR_PREC: u8 = 5;
19761977

1977-
const DIV_OP_PREC: u8 = 40;
1978-
19791978
/// Get the precedence of the next token
19801979
pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
19811980
// allow the dialect to override precedence logic
@@ -2025,7 +2024,7 @@ impl<'a> Parser<'a> {
20252024
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::LIKE_PREC),
20262025
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(Self::LIKE_PREC),
20272026
Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(Self::BETWEEN_PREC),
2028-
Token::Word(w) if w.keyword == Keyword::DIV => Ok(Self::DIV_OP_PREC),
2027+
Token::Word(w) if w.keyword == Keyword::DIV => Ok(Self::MUL_DIV_MOD_OP_PREC),
20292028
Token::Eq
20302029
| Token::Lt
20312030
| Token::LtEq
@@ -2043,7 +2042,7 @@ impl<'a> Parser<'a> {
20432042
Token::Ampersand => Ok(23),
20442043
Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
20452044
Token::Mul | Token::Div | Token::DuckIntDiv | Token::Mod | Token::StringConcat => {
2046-
Ok(40)
2045+
Ok(Self::MUL_DIV_MOD_OP_PREC)
20472046
}
20482047
Token::DoubleColon => Ok(50),
20492048
Token::Colon => Ok(50),

tests/sqlparser_common.rs

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

10761076
#[test]
1077-
fn parse_unary_math() {
1077+
fn parse_unary_math_with_plus() {
10781078
use self::Expr::*;
10791079
let sql = "-a + -b";
10801080
assert_eq!(
@@ -1093,6 +1093,26 @@ fn parse_unary_math() {
10931093
);
10941094
}
10951095

1096+
#[test]
1097+
fn parse_unary_math_with_multiply() {
1098+
use self::Expr::*;
1099+
let sql = "-a * -b";
1100+
assert_eq!(
1101+
BinaryOp {
1102+
left: Box::new(UnaryOp {
1103+
op: UnaryOperator::Minus,
1104+
expr: Box::new(Identifier(Ident::new("a"))),
1105+
}),
1106+
op: BinaryOperator::Multiply,
1107+
right: Box::new(UnaryOp {
1108+
op: UnaryOperator::Minus,
1109+
expr: Box::new(Identifier(Ident::new("b"))),
1110+
}),
1111+
},
1112+
verified_expr(sql)
1113+
);
1114+
}
1115+
10961116
#[test]
10971117
fn parse_is_null() {
10981118
use self::Expr::*;

0 commit comments

Comments
 (0)