shreyas@portfolio:~/projects/sqlite-clone-zig
$ cat projects/sqlite-clone-zig.md
# SQLite Clone (Zig) | Zig / database internals | 216 LOC
# repo: github.com/shreyasganesh0/sqlite-clone-zig
# tags: systems, databases, zig, file-format

Small (216 LOC) but focused: parse a real SQLite database file by reading bytes directly. No SQL execution; just the parts that storage-engine engineers actually think about.

What it does

  • Validates the 100-byte file header and extracts the page size.
  • Walks the B-tree of the schema page (page 1) via depth-first search using a stack of page-buffer structs.
  • Discriminates page types by reading the page-type byte: interior pages (0x05) push children onto the stack, leaf pages (0x0D) count their cells.
  • Decodes variable-length integers (varint) from cell records — the high bit is the “continue” flag, the lower 7 bits are data.
  • Reports table count by counting leaf cells in the schema page’s subtree.

Why this instead of “build a SQL query engine in Zig”

Because the file format and the B-tree walker are the load-bearing parts of any relational storage engine. SQL parsing is solved; query planning is its own discipline. What’s interesting at the storage layer is “given this byte stream and a page size, can I tell you what’s stored without anyone explaining the format to me.” That’s the skill that maps to CockroachDB, Turso, TigerBeetle, and Materialize.

The Zig part

Zig’s manual memory management and explicit endianness handling (std.mem.readInt(u32, ..., .big)) are useful here — you’re constantly working with packed, big-endian on-disk structures, and the cost of getting alignment or byte order wrong is silent corruption. Zig refuses to let you be sloppy in a way that’s harder to enforce in C.


← all projects · view on github →

perf: ·