Skip to content

Commit

Permalink
fix: unary negation operator with operators: Mul, Div and Mod (a…
Browse files Browse the repository at this point in the history
…pache#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
  • Loading branch information
izveigor authored and mcheshkov committed Sep 2, 2024
1 parent c40a3b9 commit 96d13ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ impl<'a> Parser<'a> {
};
Ok(Expr::UnaryOp {
op,
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
expr: Box::new(self.parse_subexpr(Self::MUL_DIV_MOD_OP_PREC)?),
})
}
tok @ Token::DoubleExclamationMark
Expand Down Expand Up @@ -1476,6 +1476,7 @@ impl<'a> Parser<'a> {
}

// use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a reference
const MUL_DIV_MOD_OP_PREC: u8 = 40;
const PLUS_MINUS_PREC: u8 = 30;
const XOR_PREC: u8 = 24;
const TIME_ZONE_PREC: u8 = 20;
Expand Down Expand Up @@ -1545,7 +1546,9 @@ impl<'a> Parser<'a> {
Token::Caret | Token::Sharp | Token::ShiftRight | Token::ShiftLeft => Ok(22),
Token::Ampersand => Ok(23),
Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
Token::Mul | Token::Div | Token::Mod | Token::StringConcat => Ok(40),
Token::Mul | Token::Div | Token::Mod | Token::StringConcat => {
Ok(Self::MUL_DIV_MOD_OP_PREC)
}
Token::DoubleColon => Ok(50),
Token::ExclamationMark => Ok(50),
Token::LBracket
Expand Down
22 changes: 21 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ fn parse_compound_expr_2() {
}

#[test]
fn parse_unary_math() {
fn parse_unary_math_with_plus() {
use self::Expr::*;
let sql = "- a + - b";
assert_eq!(
Expand All @@ -693,6 +693,26 @@ fn parse_unary_math() {
);
}

#[test]
fn parse_unary_math_with_multiply() {
use self::Expr::*;
let sql = "- a * - b";
assert_eq!(
BinaryOp {
left: Box::new(UnaryOp {
op: UnaryOperator::Minus,
expr: Box::new(Identifier(Ident::new("a"))),
}),
op: BinaryOperator::Multiply,
right: Box::new(UnaryOp {
op: UnaryOperator::Minus,
expr: Box::new(Identifier(Ident::new("b"))),
}),
},
verified_expr(sql)
);
}

#[test]
fn parse_is_null() {
use self::Expr::*;
Expand Down

0 comments on commit 96d13ea

Please sign in to comment.