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 30ea60c58a..068475d530 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 cef2b554a7..05d33c29bc 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -2192,3 +2192,43 @@ fn parse_create_virtual_table_module_arguments_unbalanced_err() { ); } } + +#[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-*'"); +}