Skip to content

fix: unary negation operator with operators: Mul, Div and Mod #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,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 @@ -1959,6 +1959,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 All @@ -1969,8 +1970,6 @@ impl<'a> Parser<'a> {
const AND_PREC: u8 = 10;
const OR_PREC: u8 = 5;

const DIV_OP_PREC: u8 = 40;

/// Get the precedence of the next token
pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
// allow the dialect to override precedence logic
Expand Down Expand Up @@ -2020,7 +2019,7 @@ impl<'a> Parser<'a> {
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::LIKE_PREC),
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(Self::LIKE_PREC),
Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(Self::BETWEEN_PREC),
Token::Word(w) if w.keyword == Keyword::DIV => Ok(Self::DIV_OP_PREC),
Token::Word(w) if w.keyword == Keyword::DIV => Ok(Self::MUL_DIV_MOD_OP_PREC),
Token::Eq
| Token::Lt
| Token::LtEq
Expand All @@ -2038,7 +2037,7 @@ impl<'a> Parser<'a> {
Token::Ampersand => Ok(23),
Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
Token::Mul | Token::Div | Token::DuckIntDiv | Token::Mod | Token::StringConcat => {
Ok(40)
Ok(Self::MUL_DIV_MOD_OP_PREC)
}
Token::DoubleColon => Ok(50),
Token::Colon => Ok(50),
Expand Down
22 changes: 21 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,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 @@ -1093,6 +1093,26 @@ fn parse_unary_math() {
);
}

#[test]
fn parse_unary_math_with_multiply() {
use self::Expr::*;
let sql = "-a * -b";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this case without any of the code changes in this PR and it fails like the following (aka it is parased as -(a * -b)

---- parse_unary_math_with_multiply stdout ----
thread 'parse_unary_math_with_multiply' panicked at 'assertion failed: `(left == right)`
  left: `BinaryOp { left: UnaryOp { op: Minus, expr: Identifier(Ident { value: "a", quote_style: None }) }, op: Multiply, right: UnaryOp { op: Minus, expr: Identifier(Ident { value: "b", quote_style: None }) } }`,
 right: `UnaryOp { op: Minus, expr: BinaryOp { left: Identifier(Ident { value: "a", quote_style: None }), op: Multiply, right: UnaryOp { op: Minus, expr: Identifier(Ident { value: "b", quote_style: None }) } } }`', tests/sqlparser_common.rs:1100:5
stack backtrace:

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