Skip to content

Commit f4a1fe8

Browse files
izveigorserprex
authored andcommitted
fix: unary negation operator with operators: Mul, Div and Mod (apache#902)
1 parent 41e4997 commit f4a1fe8

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
@@ -803,7 +803,7 @@ impl<'a> Parser<'a> {
803803
};
804804
Ok(Expr::UnaryOp {
805805
op,
806-
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
806+
expr: Box::new(self.parse_subexpr(Self::MUL_DIV_MOD_OP_PREC)?),
807807
})
808808
}
809809
tok @ Token::DoubleExclamationMark
@@ -1968,6 +1968,7 @@ impl<'a> Parser<'a> {
19681968
}
19691969

19701970
// use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a reference
1971+
const MUL_DIV_MOD_OP_PREC: u8 = 40;
19711972
const PLUS_MINUS_PREC: u8 = 30;
19721973
const XOR_PREC: u8 = 24;
19731974
const TIME_ZONE_PREC: u8 = 20;
@@ -1978,8 +1979,6 @@ impl<'a> Parser<'a> {
19781979
const AND_PREC: u8 = 10;
19791980
const OR_PREC: u8 = 5;
19801981

1981-
const DIV_OP_PREC: u8 = 40;
1982-
19831982
/// Get the precedence of the next token
19841983
pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
19851984
// allow the dialect to override precedence logic
@@ -2029,7 +2028,7 @@ impl<'a> Parser<'a> {
20292028
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::LIKE_PREC),
20302029
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(Self::LIKE_PREC),
20312030
Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(Self::BETWEEN_PREC),
2032-
Token::Word(w) if w.keyword == Keyword::DIV => Ok(Self::DIV_OP_PREC),
2031+
Token::Word(w) if w.keyword == Keyword::DIV => Ok(Self::MUL_DIV_MOD_OP_PREC),
20332032
Token::Eq
20342033
| Token::Lt
20352034
| Token::LtEq
@@ -2047,7 +2046,7 @@ impl<'a> Parser<'a> {
20472046
Token::Ampersand => Ok(23),
20482047
Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
20492048
Token::Mul | Token::Div | Token::DuckIntDiv | Token::Mod | Token::StringConcat => {
2050-
Ok(40)
2049+
Ok(Self::MUL_DIV_MOD_OP_PREC)
20512050
}
20522051
Token::DoubleColon => Ok(50),
20532052
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)