Skip to content

Commit 525f4d7

Browse files
committed
Fix precedence of unary NOT
get_next_precedence deals with left-binding power, not right binding power. Therefore, when it encounters a standalone NOT operator (i.e., a "NOT" token that is not followed by "BETWEEN", "LIKE", or "IN"), it should return 0, because unary NOT is not an infix operator, it's a prefix operator, and therefore it has no left-binding power.
1 parent 1f87083 commit 525f4d7

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/sqlparser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,12 @@ impl Parser {
580580
// The precedence of NOT varies depending on keyword that
581581
// follows it. If it is followed by IN, BETWEEN, or LIKE,
582582
// it takes on the precedence of those tokens. Otherwise it
583-
// takes on UNARY_NOT_PREC.
583+
// is not an infix operator, and therefore has zero
584+
// precedence.
584585
Some(Token::SQLWord(k)) if k.keyword == "IN" => Ok(Self::BETWEEN_PREC),
585586
Some(Token::SQLWord(k)) if k.keyword == "BETWEEN" => Ok(Self::BETWEEN_PREC),
586587
Some(Token::SQLWord(k)) if k.keyword == "LIKE" => Ok(Self::BETWEEN_PREC),
587-
_ => Ok(Self::UNARY_NOT_PREC),
588+
_ => Ok(0),
588589
},
589590
Token::SQLWord(k) if k.keyword == "IS" => Ok(17),
590591
Token::SQLWord(k) if k.keyword == "IN" => Ok(Self::BETWEEN_PREC),

tests/sqlparser_common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn parse_not() {
352352
fn parse_invalid_infix_not() {
353353
let res = parse_sql_statements("SELECT c FROM t WHERE c NOT (");
354354
assert_eq!(
355-
ParserError::ParserError("Expected IN or BETWEEN after NOT, found: (".to_string()),
355+
ParserError::ParserError("Expected end of statement, found: NOT".to_string()),
356356
res.unwrap_err(),
357357
);
358358
}

0 commit comments

Comments
 (0)