A loadable SQLite extension and rusqlite library for bijou64, Ink & Switch's bijective 64-bit variable-length integer encoding.
README.md

sqlite-bijou #

A loadable SQLite extension and rusqlite library for bijou64, Ink & Switch's bijective 64-bit variable-length integer encoding.

Usage #

cargo ext                          # builds target/release/libsqlite_bijou.{so,dylib}
cargo test                         # unit and property tests against bundled SQLite
cargo test -- --include-ignored    # also tests loading the compiled shared library

From the sqlite3 shell:

sqlite3 :memory: -cmd '.load ./target/release/libsqlite_bijou sqlite3_bijou_init'

From a Rust application using rusqlite, depend on sqlite-bijou with default features and call:

sqlite_bijou::register(&conn)?;

The extension feature enables rusqlite/loadable_extension, routing SQLite calls through the API table passed to sqlite3_bijou_init. It must stay off for cargo test and standalone binaries, which link SQLite directly.

SQL reference #

Scalar codecs and composite keys #

Function Returns Description
bijou64(x) BLOB Encode an unsigned integer (0..=INT64_MAX from SQL).
bijou64s(x) BLOB Encode a signed integer (i64) using zigzag encoding.
bijou64_decode(b [, off]) INTEGER Decode an unsigned integer at byte offset off (default 0).
bijou64s_decode(b [, off]) INTEGER Decode a zigzag-encoded signed integer at byte offset off.
bijou64_len(b [, off]) INTEGER Encoded byte length of the value at off, read from the tag byte alone.
bijou64_pack(a, b, ...) BLOB Concatenate unsigned values into a tuple-ordered composite key.
bijou64_count(b) INTEGER Number of values in a packed blob (walks tag bytes only).
bijou64_subtree_end(k) BLOB Copy of k with its last element incremented by 1; the subtree under k is [k, bijou64_subtree_end(k)). NULL for the empty root key x''.

Packed lists #

Function Returns Description
bijou64_group(x) / bijou64s_group(x) BLOB Aggregate a column of integers into one packed blob; NULL rows are skipped.
bijou64_each(b [, signed]) rows Table-valued: (idx, off, len, value) per element; signed = 1 decodes zigzag.

Delta-encoded sorted sets (posting lists) #

Strictly ascending integers are stored as the first value followed by x[i] - x[i-1] - 1 for each subsequent element.

Function Returns Description
bijou64_delta_group(x) BLOB Aggregate strictly ascending integers into a delta blob (errors otherwise).
bijou64_intersect(a, b, ...) BLOB Merge-intersect delta lists, starting from the shortest; returns a delta list.
bijou64_delta_contains(b, x) INTEGER 1 if x is in delta list b, else 0; stops at the first value >= x.
bijou64_delta_each(b) rows Table-valued: (idx, off, len, value) per element.

Notices #

  • || always returns TEXT, and every TEXT sorts before every BLOB. Splice keys with bijou64_pack, or wrap in CAST(... AS BLOB), otherwise range scans silently miss rows.
  • bijou64s does not preserve numeric order. Zigzag interleaves negative and positive values (0, -1, 1, -2, 2, ...) to keep small magnitudes at 1 byte. Use it inside payloads, not as a sort key; for signed sort keys, flip the sign bit (x ^ (1 << 63)) and encode with bijou64.
  • Values above INT64_MAX. Functions that stay in BLOB space (bijou64_count, bijou64_len, bijou64_subtree_end, bijou64_intersect, and the off/len columns of *_each) handle the full u64 range. Functions returning SQL INTEGER (bijou64_decode, the value column) error instead of wrapping negative.

Benchmarks #

The scripts in examples/ reproduce everything below; each writes a *_results.txt. Python only generates workloads and orchestrates runs—all timed work happens inside SQLite.

Workload Baseline bijou64
1M-node tree, table + index size 108.9 MiB padded text path / 37.8 MiB adjacency list 50.7 MiB, 10.5 B mean key
1M-node tree, 100k-node subtree count (warm) 7.9 ms text path / 94 ms recursive CTE 7.3 ms
1M-node tree, same on cold storage 22 ms, 1,478 pages 16 ms, 769 pages
1M ids as one packed list 8.46 MB JSON, 117 ms scan 1.00 MB delta-encoded, 71 ms scan
5k tags over 1M items (3.86M pairs) 37.8 MiB junction table, 47 ms large∩large 7.0 MiB, 2.5 ms
Reorderable list keys, 20k inserts 3.8–18.3 B fractional indexing 3.0–8.6 B tuples
912k-row sync, 100 differing rows 135 KB fixed 16-byte keys 64 KB

Delta posting lists lose on skewed intersections (rare ∩ large) and single-item writes, which rewrite the whole blob; chunking into 128-item blobs keyed by (tag, first_item) fixes both at 6.9 MiB. examples/varint-cmp compares codecs against VARU64, SQLite4's varint and LEB128: bijou64 is within a few percent of SQLite4 on key size and is the only codec that is both bijective and order-preserving under concatenation.

License #

MIT OR Apache-2.0, at your option. See LICENSE-MIT and LICENSE-APACHE.