diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs index c65aa6b80e..83f4959aff 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -134,9 +134,8 @@ impl Dialect for SQLiteDialect { Precedence::Ampersand => 24, Precedence::Caret => 23, Precedence::Pipe | Precedence::Colon | Precedence::PgOther => 22, - // `parse.y` puts LIKE, GLOB, MATCH and REGEXP on the level of `=` - Precedence::Between | Precedence::Eq | Precedence::Like => 20, - Precedence::Is => 17, + // `parse.y` puts IS, LIKE, GLOB, MATCH and REGEXP on the level of `=` + Precedence::Between | Precedence::Eq | Precedence::Like | Precedence::Is => 20, Precedence::UnaryNot => 15, Precedence::And => 10, Precedence::Or => 5, diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 5c396d9da3..193af404a1 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -2170,6 +2170,7 @@ fn parse_is_distinct_from_precedence() { verified_expr("a IS DISTINCT FROM b + 1") ); + // SQLite puts `IS` on the level of `=`, see `parse_is_groups_with_equality`. assert_eq!( IsDistinctFrom( Box::new(Identifier(Ident::new("a"))), @@ -2179,7 +2180,7 @@ fn parse_is_distinct_from_precedence() { right: Box::new(Identifier(Ident::new("c"))), }), ), - verified_expr("a IS DISTINCT FROM b = c") + all_dialects_except(|d| d.is::()).verified_expr("a IS DISTINCT FROM b = c") ); // `NOT` binds more loosely than `IS`, so it applies to the whole comparison. diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index 7afbabe2da..cb54b1dc7d 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -2349,3 +2349,84 @@ fn parse_unquoted_escape_is_not_an_alias() { } sqlite().verified_stmt(r#"SELECT 'a' LIKE 'b' = 0 AS "ESCAPE""#); } + +#[test] +fn parse_is_groups_with_equality() { + let id = |name: &str| Expr::Identifier(Ident::new(name)); + let binary = |left: Expr, op: BinaryOperator, right: Expr| Expr::BinaryOp { + left: Box::new(left), + op, + right: Box::new(right), + }; + let distinct = |left: Expr, right: Expr| Expr::IsDistinctFrom(Box::new(left), Box::new(right)); + let not_distinct = + |left: Expr, right: Expr| Expr::IsNotDistinctFrom(Box::new(left), Box::new(right)); + use BinaryOperator::{Eq, Lt, NotEq}; + + for (sql, expected) in [ + ( + "a IS DISTINCT FROM b = c", + binary(distinct(id("a"), id("b")), Eq, id("c")), + ), + ( + "a = b IS DISTINCT FROM c", + distinct(binary(id("a"), Eq, id("b")), id("c")), + ), + ( + "a IS NOT DISTINCT FROM b <> c", + binary(not_distinct(id("a"), id("b")), NotEq, id("c")), + ), + ( + "a <> b IS NOT DISTINCT FROM c", + not_distinct(binary(id("a"), NotEq, id("b")), id("c")), + ), + ( + "a IS DISTINCT FROM b BETWEEN c AND d", + Expr::Between { + expr: Box::new(distinct(id("a"), id("b"))), + negated: false, + low: Box::new(id("c")), + high: Box::new(id("d")), + }, + ), + ( + "a IS DISTINCT FROM b IN (c)", + Expr::In { + expr: Box::new(distinct(id("a"), id("b"))), + target: InTarget::List(vec![id("c")]), + negated: false, + global: false, + }, + ), + ( + "a IS DISTINCT FROM b < c", + distinct(id("a"), binary(id("b"), Lt, id("c"))), + ), + ( + "a LIKE b IS NULL", + Expr::IsNull { + expr: Box::new(Expr::Like { + negated: false, + any: false, + expr: Box::new(id("a")), + pattern: Box::new(id("b")), + escape_char: None, + }), + syntax: IsNullSyntax::IsNull, + }, + ), + ( + "a IS NULL = b", + binary( + Expr::IsNull { + expr: Box::new(id("a")), + syntax: IsNullSyntax::IsNull, + }, + Eq, + id("b"), + ), + ), + ] { + assert_eq!(sqlite().verified_expr(sql), expected, "{sql}"); + } +}