From cc68a65e642c82d177b4e5e831feb28017668aec Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Tue, 1 Dec 2026 18:00:15 +0100 Subject: [PATCH] SQLite: Support NOT GLOB and NOT MATCH --- fuzz/fuzz_seeds/sqlite_2840_not_glob.sql | 1 + src/ast/operator.rs | 6 +++ src/dialect/sqlite.rs | 12 +++-- tests/sqlparser_sqlite.rs | 58 +++++++++++++++++++++++- 4 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 fuzz/fuzz_seeds/sqlite_2840_not_glob.sql diff --git a/fuzz/fuzz_seeds/sqlite_2840_not_glob.sql b/fuzz/fuzz_seeds/sqlite_2840_not_glob.sql new file mode 100644 index 0000000000..ef5549f014 --- /dev/null +++ b/fuzz/fuzz_seeds/sqlite_2840_not_glob.sql @@ -0,0 +1 @@ +SELECT a NOT GLOB 'x*', b NOT MATCH 'y' diff --git a/src/ast/operator.rs b/src/ast/operator.rs index f2c8454f34..1aa663e62d 100644 --- a/src/ast/operator.rs +++ b/src/ast/operator.rs @@ -147,6 +147,10 @@ pub enum BinaryOperator { /// GLOB operator, e.g. `a GLOB b` (SQLite-specific) /// See Glob, + /// NOT GLOB operator, e.g. `a NOT GLOB b` (SQLite-specific) + NotGlob, + /// NOT MATCH operator, e.g. `a NOT MATCH b` (SQLite-specific) + NotMatch, /// Support for custom operators (such as Postgres custom operators) Custom(String), /// Bitwise XOR, e.g. `a # b` (PostgreSQL-specific) @@ -365,6 +369,8 @@ impl fmt::Display for BinaryOperator { BinaryOperator::Match => f.write_str("MATCH"), BinaryOperator::Regexp => f.write_str("REGEXP"), BinaryOperator::Glob => f.write_str("GLOB"), + BinaryOperator::NotGlob => f.write_str("NOT GLOB"), + BinaryOperator::NotMatch => f.write_str("NOT MATCH"), BinaryOperator::Custom(s) => f.write_str(s), BinaryOperator::PGBitwiseXor => f.write_str("#"), BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"), diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs index c65aa6b80e..a6b4628f0e 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -92,12 +92,14 @@ impl Dialect for SQLiteDialect { ) -> Option> { // Parse MATCH, REGEXP and GLOB as operators // See - for (keyword, op) in [ - (Keyword::REGEXP, BinaryOperator::Regexp), - (Keyword::MATCH, BinaryOperator::Match), - (Keyword::GLOB, BinaryOperator::Glob), + for (keywords, op) in [ + (&[Keyword::REGEXP][..], BinaryOperator::Regexp), + (&[Keyword::MATCH], BinaryOperator::Match), + (&[Keyword::NOT, Keyword::MATCH], BinaryOperator::NotMatch), + (&[Keyword::GLOB], BinaryOperator::Glob), + (&[Keyword::NOT, Keyword::GLOB], BinaryOperator::NotGlob), ] { - if parser.parse_keyword(keyword) { + if parser.parse_keywords(keywords) { let left = Box::new(expr.clone()); let right = Box::new(match parser.parse_subexpr(precedence) { Ok(expr) => expr, diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index 7afbabe2da..2cc75b8477 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -2250,7 +2250,7 @@ fn parse_like_glob_match_regexp_group_with_equality() { pattern: Box::new(pattern), regexp: true, }; - use BinaryOperator::{Eq, Glob, Lt, Match, NotEq, Regexp}; + use BinaryOperator::{Eq, Glob, Lt, Match, NotEq, NotGlob, NotMatch, Regexp}; for (sql, expected) in [ ( @@ -2311,6 +2311,22 @@ fn parse_like_glob_match_regexp_group_with_equality() { "a <> b MATCH c", binary(binary(id("a"), NotEq, id("b")), Match, id("c")), ), + ( + "a NOT GLOB b = c", + binary(binary(id("a"), NotGlob, id("b")), Eq, id("c")), + ), + ( + "a = b NOT GLOB c", + binary(binary(id("a"), Eq, id("b")), NotGlob, id("c")), + ), + ( + "a NOT MATCH b <> c", + binary(binary(id("a"), NotMatch, id("b")), NotEq, id("c")), + ), + ( + "a <> b NOT MATCH c", + binary(binary(id("a"), NotEq, id("b")), NotMatch, id("c")), + ), ( "a REGEXP b = c", binary(binary(id("a"), Regexp, id("b")), Eq, id("c")), @@ -2349,3 +2365,43 @@ fn parse_unquoted_escape_is_not_an_alias() { } sqlite().verified_stmt(r#"SELECT 'a' LIKE 'b' = 0 AS "ESCAPE""#); } + +#[test] +fn parse_negated_glob_and_match() { + for (operator, expected) in [ + ("NOT GLOB", BinaryOperator::NotGlob), + ("NOT MATCH", BinaryOperator::NotMatch), + ] { + let where_operator = |sql: &str| match sqlite().verified_only_select(sql).selection { + Some(Expr::BinaryOp { op, .. }) => op, + selection => panic!("{selection:?}"), + }; + assert_eq!( + where_operator(&format!("SELECT 1 FROM t WHERE a {operator} 'p' AND b = 1")), + BinaryOperator::And + ); + assert_eq!( + where_operator(&format!("SELECT 1 FROM t WHERE a {operator} 'p' || 'q'")), + expected + ); + + assert_eq!( + sqlite().verified_expr(&format!("col {operator} 'pattern'")), + Expr::BinaryOp { + op: expected, + left: Box::new(Expr::Identifier(Ident::new("col"))), + right: Box::new(Expr::Value( + (Value::SingleQuotedString("pattern".to_string())).with_empty_span() + )) + } + ); + + assert_eq!( + sqlite().parse_sql_statements(&format!("SELECT 1 {operator}")), + Err(ParserError::ParserError( + "Expected: an expression, found: EOF".to_string() + )) + ); + } + sqlite().verified_stmt("CREATE INDEX i ON t(id) WHERE id NOT GLOB 'h-*'"); +}