Skip to content
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

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

Merged
merged 1 commit into from
Jan 13, 2024
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
7 changes: 5 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,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 @@ -1485,6 +1485,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 @@ -1554,7 +1555,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 => 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 @@ -683,7 +683,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 @@ -702,6 +702,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
Loading