diff --git a/fuzz/fuzz_seeds/sqlite_3070_like_glob_precedence.sql b/fuzz/fuzz_seeds/sqlite_3070_like_glob_precedence.sql new file mode 100644 index 0000000000..92bf5c0e70 --- /dev/null +++ b/fuzz/fuzz_seeds/sqlite_3070_like_glob_precedence.sql @@ -0,0 +1 @@ +SELECT a GLOB b = c, a NOT LIKE b ESCAPE c <> d diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs index d97fc93197..c65aa6b80e 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -134,8 +134,8 @@ impl Dialect for SQLiteDialect { Precedence::Ampersand => 24, Precedence::Caret => 23, Precedence::Pipe | Precedence::Colon | Precedence::PgOther => 22, - Precedence::Between | Precedence::Eq => 20, - Precedence::Like => 19, + // `parse.y` puts LIKE, GLOB, MATCH and REGEXP on the level of `=` + Precedence::Between | Precedence::Eq | Precedence::Like => 20, Precedence::Is => 17, Precedence::UnaryNot => 15, Precedence::And => 10, @@ -228,6 +228,7 @@ impl Dialect for SQLiteDialect { | Keyword::VALUES | Keyword::FROM | Keyword::INTO + | Keyword::ESCAPE ) } diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 541511d949..5c396d9da3 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -2239,7 +2239,7 @@ fn parse_pg_other_operator_precedence() { all_dialects().verified_expr("t.a -> 'k' = b") ); - // `LIKE` sits below `=` in the precedence table, so cover that boundary too. + // `->` also binds tighter than `LIKE`. assert_eq!( Expr::Like { negated: false, diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index e947ffb7c2..7afbabe2da 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -2228,3 +2228,124 @@ fn parse_is_null_test_precedence() { let without_is_operator = all_dialects_where(|d| !d.supports_is_operator()); assert_eq!(top_operator(&without_is_operator, "a IS NULL + 1"), Plus); } + +#[test] +fn parse_like_glob_match_regexp_group_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 like = |negated: bool, expr: Expr, pattern: Expr, escape_char: Option| Expr::Like { + negated, + any: false, + expr: Box::new(expr), + pattern: Box::new(pattern), + escape_char: escape_char.map(Box::new), + }; + let not_regexp = |expr: Expr, pattern: Expr| Expr::RLike { + negated: true, + expr: Box::new(expr), + pattern: Box::new(pattern), + regexp: true, + }; + use BinaryOperator::{Eq, Glob, Lt, Match, NotEq, Regexp}; + + for (sql, expected) in [ + ( + "a LIKE b = c", + binary(like(false, id("a"), id("b"), None), Eq, id("c")), + ), + ( + "a = b LIKE c", + like(false, binary(id("a"), Eq, id("b")), id("c"), None), + ), + ( + "a NOT LIKE b <> c", + binary(like(true, id("a"), id("b"), None), NotEq, id("c")), + ), + ( + "a <> b NOT LIKE c", + like(true, binary(id("a"), NotEq, id("b")), id("c"), None), + ), + ( + "a LIKE b ESCAPE c = d", + binary(like(false, id("a"), id("b"), Some(id("c"))), Eq, id("d")), + ), + ( + "a NOT LIKE b ESCAPE c <> d", + binary(like(true, id("a"), id("b"), Some(id("c"))), NotEq, id("d")), + ), + ( + "a LIKE b IN (c)", + Expr::In { + expr: Box::new(like(false, id("a"), id("b"), None)), + target: InTarget::List(vec![id("c")]), + negated: false, + global: false, + }, + ), + ( + "a LIKE b BETWEEN c AND d", + Expr::Between { + expr: Box::new(like(false, id("a"), id("b"), None)), + negated: false, + low: Box::new(id("c")), + high: Box::new(id("d")), + }, + ), + ( + "a GLOB b = c", + binary(binary(id("a"), Glob, id("b")), Eq, id("c")), + ), + ( + "a = b GLOB c", + binary(binary(id("a"), Eq, id("b")), Glob, id("c")), + ), + ( + "a MATCH b <> c", + binary(binary(id("a"), Match, id("b")), NotEq, id("c")), + ), + ( + "a <> b MATCH c", + binary(binary(id("a"), NotEq, id("b")), Match, id("c")), + ), + ( + "a REGEXP b = c", + binary(binary(id("a"), Regexp, id("b")), Eq, id("c")), + ), + ( + "a = b REGEXP c", + binary(binary(id("a"), Eq, id("b")), Regexp, id("c")), + ), + ( + "a NOT REGEXP b = c", + binary(not_regexp(id("a"), id("b")), Eq, id("c")), + ), + ( + "a = b NOT REGEXP c", + not_regexp(binary(id("a"), Eq, id("b")), id("c")), + ), + ( + "a GLOB b < c", + binary(id("a"), Glob, binary(id("b"), Lt, id("c"))), + ), + ] { + assert_eq!(sqlite().verified_expr(sql), expected, "{sql}"); + } +} + +// sqlite3: near "ESCAPE": syntax error +#[test] +fn parse_unquoted_escape_is_not_an_alias() { + for sql in [ + "SELECT 1 ESCAPE", + "SELECT 'a' LIKE 'b' = 0 ESCAPE", + "SELECT 'a' LIKE 'b' IN (0) ESCAPE", + ] { + let parsed = sqlite().parse_sql_statements(sql); + assert!(parsed.is_err(), "{sql} parsed as {parsed:?}"); + } + sqlite().verified_stmt(r#"SELECT 'a' LIKE 'b' = 0 AS "ESCAPE""#); +}