mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-10-13 15:32:03 +00:00
Fix precedence for the NOT operator (2/4)
I checked the docs of a few of the most popular RDBMSes, and it seems there's consensus that the precedence of `NOT` is higher than `AND`, but lower than `IS NULL`. Postgresql[1], Oracle[2] and MySQL[3] docs say that explicitly. T-SQL docs[4] do mention it's higher than `AND`, and while they don't explicitly mention IS NULL, this snippet: select * from (select 1 as a)x where (not x.a) is null ...is a parsing error, while the following works like IS NOT NULL: select * from (select 1 as a)x where not x.a is null sqlite doesn't seem to mention `NOT` precedence, but I assume it works similarly. [1] https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-OPERATORS [2] https://docs.oracle.com/cd/B19306_01/server.102/b14200/conditions001.htm#i1034834 [3] https://dev.mysql.com/doc/refman/8.0/en/operator-precedence.html [4] https://docs.microsoft.com/en-us/sql/t-sql/language-elements/operator-precedence-transact-sql?view=sql-server-2017
This commit is contained in:
parent
29db619792
commit
82dc581639
2 changed files with 32 additions and 5 deletions
|
@ -182,6 +182,30 @@ fn parse_is_not_null() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_not_precedence() {
|
||||
use self::ASTNode::*;
|
||||
// NOT has higher precedence than OR/AND, so the following must parse as (NOT true) OR true
|
||||
let sql = "NOT true OR true";
|
||||
match verified_expr(sql) {
|
||||
SQLBinaryExpr {
|
||||
op: SQLOperator::Or,
|
||||
..
|
||||
} => assert!(true),
|
||||
_ => assert!(false),
|
||||
};
|
||||
|
||||
// But NOT has lower precedence than comparison operators, so the following parses as NOT (a IS NULL)
|
||||
let sql = "NOT a IS NULL";
|
||||
match verified_expr(sql) {
|
||||
SQLUnary {
|
||||
operator: SQLOperator::Not,
|
||||
..
|
||||
} => assert!(true),
|
||||
_ => assert!(false),
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_like() {
|
||||
let sql = String::from("SELECT * FROM customers WHERE name LIKE '%a'");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue