SQLite Forum

SQLite doesn't use indexes for bitwise clauses?
Login
REPRO: Create a table with an index covering a not-null integer. `EXPLAIN QUERY PLAN SELECT * FROM table WHERE...` reports the index is used for `WHERE n=?` and `WHERE n>?` but not `WHERE n & ? != 0`

This is with SQLite 3.34.1

```
sqlite> .schema resource
CREATE TABLE Resource(_ResourceID INTEGER PRIMARY KEY NOT NULL,_Revision INTEGER NOT NULL DEFAULT 1,_WorkId INTEGER NOT NULL DEFAULT 0,Package INTEGER NOT NULL,"Index" INTEGER NOT NULL,Language TEXT NOT NULL COLLATE NOCASE);
CREATE UNIQUE INDEX IDX_Resource_Package_Index__WorkId ON Resource(Package, "Index", _WorkId);
CREATE INDEX IDX_Resource__WorkId ON Resource(_WorkId) WHERE _WorkId<>0;
sqlite>
sqlite>
sqlite>
sqlite> explain query plan select * from resource where package=?;
QUERY PLAN
`--SEARCH TABLE resource USING INDEX IDX_Resource_Package_Index__WorkId (Package=?)
sqlite>
sqlite>
sqlite> explain query plan select * from resource where package>?;
QUERY PLAN
`--SEARCH TABLE resource USING INDEX IDX_Resource_Package_Index__WorkId (Package>?)
sqlite>
sqlite>
sqlite> explain query plan select * from resource where package & ? != 0;
QUERY PLAN
`--SCAN TABLE resource
sqlite>
sqlite>
sqlite> explain query plan select * from resource where package & ? = 0;
QUERY PLAN
`--SCAN TABLE resource
```

We use bitwise logic in multiple places. Usually in the form `x & ? != 0` or `x & ? = 0`, and less often testing against non-zero e.g. `x & ? = 0x1408`

I was little surprised the indexes aren't leveraged for bitwise comparisons. Is there a reason why not or is it just an oversight that can be addressed in vNext?