Skip to content

jonwiggins/cqlite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cqlite

CI

A SQLite-compatible database engine built from scratch in Rust — entirely generated by AI.

This project is a demonstration of modern AI coding tools. Every line of code — the hand-written SQL tokenizer, recursive descent parser, B-tree storage engine, page cache, rollback journal, and query executor — was written by AI (Claude). cqlite exists to showcase what AI-assisted development can accomplish: a functional, file-compatible SQLite implementation built from first principles, without copying from the SQLite source code or using any existing SQL parsing libraries or database engines. The result passes 100% of sqllogictest select suites 1–3 (5,434 cases) and supports full ACID transactions with crash recovery.

Features

  • SQLite3 file format: read and write databases compatible with the official sqlite3 CLI
  • Full SQL support: SELECT, INSERT, UPDATE, DELETE, CREATE/DROP TABLE/INDEX, ALTER TABLE, transactions
  • Hand-written parser: tokenizer and recursive descent parser with no parser generators
  • B-tree storage: B-tree (indexes) and B+tree (tables) with page splitting, merging, and overflow pages
  • Query execution: joins (INNER/LEFT/RIGHT/CROSS/NATURAL), subqueries, CTEs (including recursive), aggregates, set operations
  • ACID transactions: rollback journal with crash recovery and hot journal detection
  • Automatic indexing: query planner selects indexes for equality conditions
  • AUTOINCREMENT: enforced via sqlite_sequence table, preventing rowid reuse
  • Date/time functions: date(), time(), datetime(), julianday(), unixepoch(), strftime() with modifiers
  • Interactive CLI: REPL with dot-commands (.tables, .schema, .mode, .dump, etc.)
  • 461+ tests passing including 5,434 sqllogictest cases at 100% pass rate

SQL coverage

Category Supported
DML SELECT, INSERT (VALUES/SELECT/DEFAULT/multi-row), UPDATE, DELETE
DDL CREATE/DROP TABLE, CREATE/DROP INDEX, ALTER TABLE (RENAME/ADD/DROP COLUMN)
Expressions Arithmetic, comparison, BETWEEN, IN, LIKE, GLOB, IS [NOT] NULL, CASE/WHEN, CAST, EXISTS
Joins INNER, LEFT, RIGHT, CROSS, NATURAL (nested loop)
Aggregates COUNT, SUM, AVG, MIN, MAX, GROUP_CONCAT with GROUP BY / HAVING
Set operations UNION, UNION ALL, INTERSECT, EXCEPT
Subqueries Scalar, IN, EXISTS (correlated), FROM clause
CTEs WITH, WITH RECURSIVE
Ordering ORDER BY (ASC/DESC, multi-column, column numbers, aliases), LIMIT, OFFSET, DISTINCT
Constraints NOT NULL, DEFAULT, PRIMARY KEY, UNIQUE, CHECK, AUTOINCREMENT
Conflict handling INSERT OR REPLACE/IGNORE/ABORT/FAIL/ROLLBACK
Transactions BEGIN (DEFERRED/IMMEDIATE/EXCLUSIVE), COMMIT, ROLLBACK, auto-commit
Introspection EXPLAIN, EXPLAIN QUERY PLAN, PRAGMA
Functions 30+ built-in scalar and aggregate functions
Date/time date, time, datetime, julianday, unixepoch, strftime (with modifiers)

Not yet implemented

  • Window functions, FULL OUTER joins, UPSERT, FOREIGN KEY enforcement
  • WAL mode, concurrent readers, cross-process file locking
  • Join reordering, index range scans, cost-based query planning

Getting started

Build

cargo build --release

Run

# Open or create a database
./target/release/rsqlite mydb.db

# In-memory database
./target/release/rsqlite

Example session

cqlite> CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE);
cqlite> INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
cqlite> INSERT INTO users VALUES (2, 'Bob', 'bob@example.com');
cqlite> SELECT * FROM users WHERE name LIKE 'A%';
1|Alice|alice@example.com
cqlite> .mode column
cqlite> .headers on
cqlite> SELECT * FROM users;
id  name   email
--  -----  -----------------
1   Alice  alice@example.com
2   Bob    bob@example.com

Testing

cargo test

The test suite includes unit tests across all modules, integration tests, and a sqllogictest harness.

Suite Cases Pass rate
Unit tests 371 100%
Integration tests 90 100%
sqllogictest (basic, select1–3) 5,434 100%

Development

Setup

./scripts/setup.sh

This installs a pre-commit hook that runs cargo fmt --check, cargo clippy, and cargo test before each commit.

Architecture

src/
├── main.rs          # CLI / REPL
├── lib.rs           # Public API
├── tokenizer.rs     # Hand-written SQL lexer
├── parser.rs        # Recursive descent parser
├── ast.rs           # AST node definitions
├── planner.rs       # Query planner + DDL/DML execution
├── vm.rs            # Query executor (Volcano model)
├── btree.rs         # B-tree / B+tree implementation
├── pager.rs         # Page cache and I/O layer
├── format.rs        # SQLite3 file format constants
├── schema.rs        # sqlite_master schema reader
├── record.rs        # Record serialization
├── varint.rs        # Variable-length integer encoding
├── types.rs         # Value types, affinity, coercion
├── functions.rs     # Built-in scalar functions
├── journal.rs       # Rollback journal
└── error.rs         # Error types

License

MIT

About

Lightweight SQLite implementation in Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors