SQLite: Parse `IS NULL` against `+` as a comparison
diff --git a/fuzz/fuzz_seeds/sqlite_2840_is_null_precedence.sql b/fuzz/fuzz_seeds/sqlite_2840_is_null_precedence.sql new file mode 100644 index 0000000..8acb025 --- /dev/null +++ b/fuzz/fuzz_seeds/sqlite_2840_is_null_precedence.sql
@@ -0,0 +1 @@ +SELECT a IS NULL + 1;
diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9f2ef33..2e653c7 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs
@@ -4217,7 +4217,55 @@ } else if let Token::Word(w) = &tok.token { match w.keyword { Keyword::IS => { - if self.parse_keyword(Keyword::NULL) { + if self.parse_keywords(&[Keyword::DISTINCT, Keyword::FROM]) { + // The right operand binds no more loosely than `IS` + // itself, so that e.g. `a IS DISTINCT FROM b AND c` + // parses as `(a IS DISTINCT FROM b) AND c`. + let expr2 = self.parse_subexpr(precedence)?; + Ok(Expr::IsDistinctFrom(Box::new(expr), Box::new(expr2))) + } else if self.parse_keywords(&[Keyword::NOT, Keyword::DISTINCT, Keyword::FROM]) + { + let expr2 = self.parse_subexpr(precedence)?; + Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2))) + } else if self.dialect.supports_is_operator() { + let op = if self.parse_keyword(Keyword::NOT) { + BinaryOperator::IsNot + } else { + BinaryOperator::Is + }; + // SQLite gives `IS` the same precedence as `=` (https://www.sqlite.org/lang_expr.html). + let right = self.parse_subexpr(self.dialect.prec_value(Precedence::Eq))?; + let expr = Box::new(expr); + let literal = match &right { + Expr::Value(ValueWithSpan { value, .. }) => Some(value), + _ => None, + }; + Ok(match (&op, literal) { + (BinaryOperator::Is, Some(Value::Null)) => Expr::IsNull { + expr, + syntax: IsNullSyntax::IsNull, + }, + (BinaryOperator::IsNot, Some(Value::Null)) => Expr::IsNotNull { + expr, + syntax: IsNotNullSyntax::IsNotNull, + }, + (BinaryOperator::Is, Some(Value::Boolean(true))) => Expr::IsTrue(expr), + (BinaryOperator::IsNot, Some(Value::Boolean(true))) => { + Expr::IsNotTrue(expr) + } + (BinaryOperator::Is, Some(Value::Boolean(false))) => { + Expr::IsFalse(expr) + } + (BinaryOperator::IsNot, Some(Value::Boolean(false))) => { + Expr::IsNotFalse(expr) + } + _ => Expr::BinaryOp { + left: expr, + op, + right: Box::new(right), + }, + }) + } else if self.parse_keyword(Keyword::NULL) { Ok(Expr::IsNull { expr: Box::new(expr), syntax: IsNullSyntax::IsNull, @@ -4235,30 +4283,6 @@ Ok(Expr::IsFalse(Box::new(expr))) } else if self.parse_keywords(&[Keyword::NOT, Keyword::FALSE]) { Ok(Expr::IsNotFalse(Box::new(expr))) - } else if self.parse_keywords(&[Keyword::DISTINCT, Keyword::FROM]) { - // The right operand binds no more loosely than `IS` - // itself, so that e.g. `a IS DISTINCT FROM b AND c` - // parses as `(a IS DISTINCT FROM b) AND c`. - let expr2 = self.parse_subexpr(precedence)?; - Ok(Expr::IsDistinctFrom(Box::new(expr), Box::new(expr2))) - } else if self.parse_keywords(&[Keyword::NOT, Keyword::DISTINCT, Keyword::FROM]) - { - let expr2 = self.parse_subexpr(precedence)?; - Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2))) - } else if self.dialect.supports_is_operator() { - let op = if self.parse_keyword(Keyword::NOT) { - BinaryOperator::IsNot - } else { - BinaryOperator::Is - }; - // SQLite gives `IS` the same precedence as `=` (https://www.sqlite.org/lang_expr.html). - Ok(Expr::BinaryOp { - left: Box::new(expr), - op, - right: Box::new( - self.parse_subexpr(self.dialect.prec_value(Precedence::Eq))?, - ), - }) } else if self.parse_keywords(&[Keyword::UNKNOWN]) { Ok(Expr::IsUnknown(Box::new(expr))) } else if self.parse_keywords(&[Keyword::NOT, Keyword::UNKNOWN]) {
diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index b17ad59..e947ffb 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs
@@ -2192,3 +2192,39 @@ ); } } + +#[test] +fn parse_is_null_test_precedence() { + fn top_operator(dialects: &TestedDialects, sql: &str) -> BinaryOperator { + match dialects.verified_expr(sql) { + Expr::BinaryOp { op, .. } => op, + other => panic!("{sql} parsed as {other:?}"), + } + } + use BinaryOperator::{Is, IsNot, Plus}; + + // `IS [NOT]` binds looser than `+`, while the postfix tests apply to what precedes them. + for (sql, expected) in [ + ("a IS NULL + 1", Is), + ("a IS NOT NULL + 1", IsNot), + ("a IS true + 1", Is), + ("a IS NOT false + 1", IsNot), + ("a IS (NULL)", Is), + ("a NOT NULL + 1", Plus), + ("a NOTNULL + 1", Plus), + ("a ISNULL + 1", Plus), + ] { + assert_eq!(top_operator(&sqlite(), sql), expected, "{sql}"); + } + assert!(matches!( + sqlite().verified_expr("a IS NULL"), + Expr::IsNull { .. } + )); + assert!(matches!( + sqlite().verified_expr("a IS NOT TRUE"), + Expr::IsNotTrue(_) + )); + + let without_is_operator = all_dialects_where(|d| !d.supports_is_operator()); + assert_eq!(top_operator(&without_is_operator, "a IS NULL + 1"), Plus); +}