Skip to content

sqlite: support custom collations via sqlite3_create_collation_v2 #63316

Open

Description

@IrosTheBeggar

What is the problem this feature will solve?

This affects the ORDER BY clause. Without this feature the word Ágætis gets put after the word Zebra. I'm building a music DB and this issue comes up a lot.

What is the feature you are proposing to solve the problem?

Expose sqlite's sqlite3_create_collation_v2 API in NodeJs.

What alternatives have you considered?

This sorting can be done in code, but it is much slower than having the feature available

Activity

  1. added
    feature request
    on May 15, 2026
  2. puneetdixit200 commented on May 16, 2026

    @puneetdixit200

    I’d like to work on this feature. I can start with tests for ORDER BY behavior using custom collations, then wire sqlite3_create_collation_v2 into the Node sqlite API in a way that matches existing callback/resource patterns. Please assign this to me.

  3. added
    sqlite
    on Jul 27, 2026
  4. trivikr commented on Jul 27, 2026

    @trivikr
    Member

    @puneetdixit200 Are you working on this? If yes, can you please link the pull request?

  5. shbernal commented on Sep 5, 2026

    @shbernal

    One more motivation for this. In my case the missing collation does not sort rows wrongly, it stops three tables being read at all.

    Anki's .apkg decks hold a SQLite file whose schema 18 declares six tables COLLATE unicase, a collation Anki registers from its own Rust and no one else has. Three of those tables are WITHOUT ROWID, so the collation is part of the b-tree key rather than something only ORDER BY reaches for. From node:sqlite those tables cannot be read at all.

    On v24.19.0, against a real schema-18 collection opened read-only:

    SELECT * FROM fields      -> ERR_SQLITE_ERROR  no such collation sequence: unicase
    SELECT * FROM templates   -> ERR_SQLITE_ERROR  no such collation sequence: unicase
    SELECT * FROM tags        -> ERR_SQLITE_ERROR  no query solution
    

    The three rowid tables read, until a query picks the index keyed on the collated column. decks has CREATE UNIQUE INDEX idx_decks_name ON decks (name), and name is COLLATE unicase, so:

    SELECT * FROM decks       -> ok, 2 rows
    SELECT count(*) FROM decks -> ERR_SQLITE_ERROR  no such collation sequence: unicase
    

    Which query fails is up to the planner, which makes this hard to work around by writing the SQL differently.

    What consumers do today is copy the file, then rewrite its schema:

    PRAGMA writable_schema=ON;
    UPDATE sqlite_master SET sql = replace(sql, ' COLLATE unicase', '') WHERE sql LIKE '%unicase%';
    PRAGMA writable_schema=RESET;

    RESET rather than OFF, because OFF leaves the already-parsed schema in memory and a later query that picks an index still asks for the collation it remembers. That failure is worse than an error: a covering-index scan comes back with fewer rows than the table holds and says nothing. So reading a file someone else wrote means taking a writable copy of it and editing sqlite_master by hand, on a database opened read-only in every other respect.

    sql.js cannot register one either. Its build exports create_function and create_aggregate and nothing for collations, so it needs the same rewrite.

    As for scope, DatabaseSync already has function, aggregate, enableDefensive, setAuthorizer and loadExtension, so a createCollation(name, comparator) sitting next to them follows a shape the class already has, and the comparator has no arguments beyond the two strings.

    I maintain a reader for these files (https://github.com/shbernal/anki-apkg-export) and am happy to test a patch against real decks if someone picks this up.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestsqlite

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions