Skip to content

SQLite: Support NOT GLOB and NOT MATCH - #3043

Open
LucaCappelletti94 wants to merge 1 commit into
mainfrom
sqlite-not-glob
Open

LucaCappelletti94 wants to merge 1 commit into
mainfrom
sqlite-not-glob

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

SQLiteDialect parses a GLOB b and a MATCH b but rejects their negated forms, which SQLite accepts.

CREATE INDEX i ON t(id) WHERE id NOT GLOB 'h-*';
-- Expected: IN or BETWEEN after NOT, found: GLOB

The SQLite infix hook now also reads NOT GLOB and NOT MATCH as the new BinaryOperator::NotGlob and BinaryOperator::NotMatch, at the precedence the plain operators already have.

@LucaCappelletti94 LucaCappelletti94 added enhancement New feature or request api change fuzzer A bug found by the fuzzer SQLite labels Nov 24, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.56%. Comparing base (ba850fa) to head (851a228).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3043   +/-   ##
=======================================
  Coverage   83.56%   83.56%           
=======================================
  Files          42       42           
  Lines       35689    35693    +4     
  Branches    35689    35693    +4     
=======================================
+ Hits        29822    29828    +6     
  Misses       2429     2429           
+ Partials     3438     3436    -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zommiommy

Copy link
Copy Markdown

there's a precedence issue:

SELECT 'a' NOT GLOB 'a' = 1
-- this PR:  'a' NOT GLOB ('a' = 1)
-- SQLite:   ('a' NOT GLOB 'a') = 1   -- sqlite3 3.53.3 returns 0; the right-grouped form returns 1

in SQLite's docs (https://www.sqlite.org/lang_expr.html#operators_and_parse_affecting_attributes), =, ==, <>, !=, IS [NOT] [DISTINCT FROM], BETWEEN, IN, MATCH, LIKE, REGEXP and
GLOB share one cell, so they have the same precedence and are left-associative.
note 5 says NOT keeps the bare operator's precedence.
SQLiteDialect maps Precedence::Like to 19 and Eq/Between to 20, so they are not on the same level.

#[test]
fn parse_negated_glob_and_match_binds_like_comparison() {
   for (operator, expected) in [
       ("NOT GLOB", BinaryOperator::NotGlob),
       ("NOT MATCH", BinaryOperator::NotMatch),
   ] {
       assert_eq!(
           sqlite().verified_expr(&format!("a {operator} 'p' = 1")),
           Expr::BinaryOp {
               left: Box::new(Expr::BinaryOp {
                   left: Box::new(Expr::Identifier(Ident::new("a"))),
                   op: expected,
                   right: Box::new(Expr::Value(
                       (Value::SingleQuotedString("p".to_string())).with_empty_span()
                   )),
               }),
               op: BinaryOperator::Eq,
               right: Box::new(Expr::value(number("1"))),
           }
       );
   }
}

GLOB / MATCH already have this problem on main

a nit: NotGlob/NotMatch should be inserted at the end; otherwise, it will BinaryOperator's serialized versions. serde formats like bincode use the enum variant position as discriminant, so changing the order of variants breaks file compatibility. idk if this is important for this crate tho.

@LucaCappelletti94

Copy link
Copy Markdown
Contributor Author

@zommiommy the misgrouping comes from the preexistent SQLiteDialect::prec_value, which ranks Precedence::Like at 19 below Eq and Between at 20. GLOB, MATCH, REGEXP, LIKE and their NOT forms all take that level, so a GLOB 'p' = 1 and a LIKE 'p' = 1 misgroup on main as well.

I will fix the level in a separate PR from main, with your test extended to all of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api change enhancement New feature or request fuzzer A bug found by the fuzzer SQLite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants