Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/sqlite_3070_like_glob_precedence.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a GLOB b = c, a NOT LIKE b ESCAPE c <> d
5 changes: 3 additions & 2 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -228,6 +228,7 @@ impl Dialect for SQLiteDialect {
| Keyword::VALUES
| Keyword::FROM
| Keyword::INTO
| Keyword::ESCAPE
)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
121 changes: 121 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>| 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""#);
}
Loading