Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Dialect for SQLiteDialect {
&self,
parser: &mut crate::parser::Parser,
expr: &crate::ast::Expr,
_precedence: u8,
precedence: u8,
) -> Option<Result<crate::ast::Expr, ParserError>> {
// Parse MATCH, REGEXP and GLOB as operators
// See <https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators>
Expand All @@ -90,7 +90,7 @@ impl Dialect for SQLiteDialect {
] {
if parser.parse_keyword(keyword) {
let left = Box::new(expr.clone());
let right = Box::new(match parser.parse_expr() {
let right = Box::new(match parser.parse_subexpr(precedence) {
Ok(expr) => expr,
Err(e) => return Some(Err(e)),
});
Expand Down
31 changes: 31 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,37 @@ fn test_drop_trigger() {
}
}

#[test]
fn parse_pattern_operators_bind_at_like_precedence() {
fn where_operator(sql: &str) -> BinaryOperator {
let Statement::Query(query) = sqlite().verified_stmt(sql) else {
panic!("expected a query");
};
let SetExpr::Select(select) = *query.body else {
panic!("expected a select");
};
let Some(Expr::BinaryOp { op, .. }) = select.selection else {
panic!("expected a WHERE binary operator");
};
op
}

// Above AND, so the pattern does not swallow the rest of the expression.
for operator in ["REGEXP", "MATCH", "GLOB", "LIKE"] {
let sql = format!("SELECT 1 FROM t WHERE a {operator} 'p' AND b = 1");
assert_eq!(where_operator(&sql), BinaryOperator::And, "{operator}");
}
// Below string concatenation, so the pattern is not cut short either.
for (operator, expected) in [
("REGEXP", BinaryOperator::Regexp),
("MATCH", BinaryOperator::Match),
("GLOB", BinaryOperator::Glob),
] {
let sql = format!("SELECT 1 FROM t WHERE a {operator} 'p' || 'q'");
assert_eq!(where_operator(&sql), expected, "{operator}");
}
}

fn sqlite() -> TestedDialects {
TestedDialects::new(vec![Box::new(SQLiteDialect {})])
}
Expand Down
Loading