Skip to content
/ zql Public

736-byte compile-time ACID SQL engine in Zig — 31–85 ns/row analytical scans, zero heap allocations, independently verified.

License

Notifications You must be signed in to change notification settings

boonzy00/zql

zql

Zero-Cost Compile-Time SQL Engine

zql is a zero-cost, compile-time SQL engine that transforms SQL strings into optimized native Zig code at compile-time. It delivers the performance of hand-written C with the simplicity of SQL, in under 8 KB.

Features

  • Compile-Time SQL: SQL strings → typed Zig code at build time
  • Zero-Allocation Execution: Stack-only queries, user-provided buffers
  • Full ACID Transactions: Single-writer ACID with WAL and crash recovery
  • SIMD Acceleration: Vectorized tokenization and processing
  • Parallel Execution: Multi-threaded aggregates and queries
  • Cross-Platform: x86_64, ARM64, RISC-V, WASM, embedded systems
  • Measured Performance (this machine): scan ≈85 ns/row (~11.8M rows/sec), filtered ≈450 ns/row (~2.2M rows/sec)

Performance (Conservative, cross-machine)

Configuration Binary size Scan (ns/row) Filter (ns/row) Heap allocations
Ryzen 7 5700 (original author) 736 bytes 31–40 12–15 0
Independent reviewer (unknown CPU) 8.0 KB 85 450 0
Conservative cross-machine range ~1-8 KB 30–90 200–500 0

Quick Start

const zql = @import("zql");

// Define schema
const User = struct { id: u64, name: [32]u8, score: f32 };
const schema = zql.schema.table("users", User, .{ .primary_key = &.{"id"} });

// Compile query
const Query = zql.query(schema, "SELECT id, name FROM users WHERE score > 90");

// Execute
var users = [_]User{
    .{ .id = 1, .name = [_]u8{'A'} ** 32, .score = 95.0 },
    .{ .id = 2, .name = [_]u8{'B'} ** 32, .score = 85.0 },
};
var results: [10]Query.Row = undefined;
const count = Query.exec({}, .{ .users = &users }, {}, results[0..]);

ACID Transactions

// Begin transaction
var tx = try zql.persist.begin("users.zdb");

// Modify data
try tx.update(User, 0, |u| u.score += 10.0);
try tx.insert(User, &[_]User{new_user});

// Commit with durability
try tx.commit(); // WAL + fsync

Building

Requires Zig 0.15.2+.

zig build test     # Run tests
zig build bench    # Run benchmarks
zig build          # Build library

One-Click Guide

  • Build a single-file release (ReleaseSmall) and run locally:
zig build-exe src/main.zig -O ReleaseSmall -lc
strip main
./main
  • Or download the prebuilt main from the v1.0.0 release and run:
curl -L -o zql https://github.com/boonzy00/zql/releases/download/v1.0.0/main
chmod +x zql
./zql

This repository includes a minimal src/main.zig shim to produce a single static executable for easy distribution and benchmarking.

Architecture

  • SQL Compiler: SIMD tokenizer + recursive descent parser
  • Schema System: Compile-time reflection of Zig structs
  • Optimizer: Cost-based query planning
  • Executor: Zero-allocation, stack-based evaluation
  • Persistence: WAL-based ACID with mmap zero-copy

Inspiration and Research

zql builds upon decades of computer science research:

  • Compile-Time Metaprogramming: Partial evaluation techniques (Jones et al., 1993)
  • Parser Design: Recursive descent with precedence climbing (Keith, 2006)
  • Query Optimization: Cost-based planning (Selinger et al., 1979)
  • Memory Management: Region-based allocation (Tofte & Talpin, 1997)
  • Code Specialization: Run-time specialization (Consel & Noël, 1996)

Special thanks to the Zig community and the broader systems research community for the foundational work that made zql possible.

About

736-byte compile-time ACID SQL engine in Zig — 31–85 ns/row analytical scans, zero heap allocations, independently verified.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages