name: sql-db-architecture
description: Understand the toy relational database architecture, crate organization, data flow, and implementation patterns. Use when exploring the database codebase, understanding query execution, debugging storage/indexing, or planning new features. Keywords: sql-database, database, crate, storage, executor, planner, parser, catalog, WAL, buffer pool, index, architecture
sql-database Architecture Expert
Provides deep understanding of the toy relational database implementation, crate boundaries, and system design.
When to Activate
Use this skill when:
- Exploring how SQL queries flow through the system
- Understanding storage layer (heap files, slotted pages, WAL)
- Debugging parser → planner → executor pipeline
- Working with indexes (B+Tree, Hash, Bitmap, Trie)
- Navigating crate dependencies and boundaries
- Planning new features that span multiple crates
System Overview
sql-database is a minimal RDBMS in Rust implementing:
- SQL subset: CREATE/DROP TABLE/INDEX, INSERT, SELECT, UPDATE, DELETE
- Storage: slotted pages + buffer pool + WAL (redo-only)
- Execution: Volcano model with SeqScan/IndexScan operators
- Indexes: B+Tree (sled), Hash, Bitmap (roaring), Trie
Instructions
1. Identify the Query
When user asks about functionality, map to crates:
"How does SELECT work?"
→ parser → planner → executor → storage/index → buffer
"Where are tables created?"
→ catalog (metadata) → storage (heap allocation) → WAL (logging)
"How are indexes used?"
→ planner (index selection) → executor (IndexScan) → index crate
2. Crate Responsibility Map
Use this mapping to locate relevant code:
| Crate |
Responsibility |
Key Types |
common |
Shared types, errors |
Row, RecordBatch, DbError, ColumnId |
types |
SQL types & values |
SqlType, Value |
expr |
Expression AST + eval |
Expr, BinaryOp, UnaryOp |
parser |
SQL → AST |
sqlparser-rs adapter |
planner |
AST → logical → physical |
Plan nodes, optimization rules |
executor |
Volcano operators |
Exec trait, SeqScan, Filter, Project |
storage |
Heap table, tuples |
HeapTable, RecordId, slotted pages |
buffer |
Page cache (LRU) |
Pager, PageId, TableId |
wal |
Write-ahead log |
WalRecord, append/replay |
catalog |
Schema metadata |
TableMeta, IndexMeta, TableSchema |
index |
All index types |
Index trait, BTree/Hash/Bitmap/Trie |
repl |
CLI shell |
rustyline, tabled rendering |
testsupport |
Test fixtures |
run_sql_script, snapshots |
3. Data Flow Patterns
Query Execution (SELECT):
SQL string
→ parser (sqlparser-rs → AST)
→ planner (logical plan → physical plan with index selection)
→ executor (Volcano operators: Scan → Filter → Project)
→ storage/index (fetch rows via RecordId)
→ buffer (page cache lookup/fetch)
→ REPL (format RecordBatch with tabled)
Write Path (INSERT/UPDATE/DELETE):
SQL string
→ parser → planner → executor
→ WAL (append WalRecord, fsync)
→ storage (heap table modification)
→ index (maintain all indexes on affected columns)
→ buffer (mark pages dirty)
Recovery:
Startup
→ WAL replay (read WalRecord log)
→ storage (re-apply operations)
→ catalog (restore table/index metadata)
4. Code Navigation Strategy
Finding implementations:
- Trait definitions: Search crate root (e.g.,
executor/lib.rs for Exec)
- Concrete operators: Look in
<crate>/src/<operator>.rs
- Tests: Adjacent
.rs files or tests/ directory
- Integration:
testsupport/ for end-to-end scripts
Dependency order (from CLAUDE.md):
common ← types ← expr
↓
parser
↓
catalog
↓
buffer ← storage ← wal
↓
planner
↓
index
↓
executor
↓
repl
5. Key Design Principles
From the design doc:
- Tiny interfaces - Prefer small traits (
Exec, Index, HeapTable, Pager)
- No implicit coercion - Same-type comparisons only (v1)
- WAL-first writes - (1) append WAL (2) fsync (3) apply to storage
- Rule-based planning - Predicate pushdown + index selection
- Single writer - No transactions/MVCC in v1
- Snapshot tests - Use
insta for query output verification
6. Response Format
When explaining architecture:
Component Overview:
- Purpose in 1 sentence
- Key types/traits
- Dependencies (what it calls)
- Dependents (what calls it)
Code References:
- Use
crate/path/file.rs:line format
- Link related components
- Show trait → impl relationships
Examples:
- Provide concrete SQL queries
- Show expected output format
- Reference existing tests when available
Project Context
- Language: Rust (workspace with 13 crates)
- Build:
cargo check/test/fmt/clippy
- Coverage:
scripts/coverage.sh (llvm-cov)
- Testing: Unit tests inline, integration in
tests/, snapshots with insta
- Dependencies: Pinned in workspace
Cargo.toml with workspace = true
Common Questions
"How does index selection work?"
→ Check planner/ for rules matching WHERE predicates to available indexes
"Where is Row serialized?"
→ storage/ uses bincode for tuple layout in slotted pages
"How does the buffer pool work?"
→ buffer/ implements LRU cache over PageId, backed by file segments
"What SQL is supported?"
→ See SKILL.md header or parser/ for DDL/DML subset
"How to add a new operator?"
→ Implement Exec trait in executor/, wire into planner physical node generation
Tool Usage
- Grep: Find trait implementations, error types, specific SQL keywords
- Read: Examine crate root
lib.rs, trait definitions, test fixtures
- Glob: Locate all operators (
executor/**/*.rs), test files (**/tests/*.rs)
For detailed API signatures and implementation notes, see REFERENCE.md.
1---2name: sql-db-architecture3description: Understand the toy relational database architecture, crate organization, data flow, and implementation patterns. Use when exploring the database codebase, understanding query execution, debugging stor4---5
6---
7name: sql-db-architecture
8description: Understand the toy relational database architecture, crate organization, data flow, and implementation patterns. Use when exploring the database codebase, understanding query execution, debugging storage/indexing, or planning new features. Keywords: sql-database, database, crate, storage, executor, planner, parser, catalog, WAL, buffer pool, index, architecture
9---
10
11# sql-database Architecture Expert
12
13Provides deep understanding of the toy relational database implementation, crate boundaries, and system design.
14
15## When to Activate
16
17Use this skill when:
18- Exploring how SQL queries flow through the system
19- Understanding storage layer (heap files, slotted pages, WAL)
20- Debugging parser → planner → executor pipeline
21- Working with indexes (B+Tree, Hash, Bitmap, Trie)
22- Navigating crate dependencies and boundaries
23- Planning new features that span multiple crates
24
25## System Overview
26
27sql-database is a minimal RDBMS in Rust implementing:
28- SQL subset: CREATE/DROP TABLE/INDEX, INSERT, SELECT, UPDATE, DELETE
29- Storage: slotted pages + buffer pool + WAL (redo-only)
30- Execution: Volcano model with SeqScan/IndexScan operators
31- Indexes: B+Tree (sled), Hash, Bitmap (roaring), Trie
32
33## Instructions
34
35### 1. Identify the Query
36
37When user asks about functionality, map to crates:
38
39**"How does SELECT work?"**
40→ parser → planner → executor → storage/index → buffer
41
42**"Where are tables created?"**
43→ catalog (metadata) → storage (heap allocation) → WAL (logging)
44
45**"How are indexes used?"**
46→ planner (index selection) → executor (IndexScan) → index crate
47
48### 2. Crate Responsibility Map
49
50Use this mapping to locate relevant code:
51
52| Crate | Responsibility | Key Types |
53|-------|---------------|-----------|
54| `common` | Shared types, errors | `Row`, `RecordBatch`, `DbError`, `ColumnId` |
55| `types` | SQL types & values | `SqlType`, `Value` |
56| `expr` | Expression AST + eval | `Expr`, `BinaryOp`, `UnaryOp` |
57| `parser` | SQL → AST | sqlparser-rs adapter |
58| `planner` | AST → logical → physical | Plan nodes, optimization rules |
59| `executor` | Volcano operators | `Exec` trait, SeqScan, Filter, Project |
60| `storage` | Heap table, tuples | `HeapTable`, `RecordId`, slotted pages |
61| `buffer` | Page cache (LRU) | `Pager`, `PageId`, `TableId` |
62| `wal` | Write-ahead log | `WalRecord`, append/replay |
63| `catalog` | Schema metadata | `TableMeta`, `IndexMeta`, `TableSchema` |
64| `index` | All index types | `Index` trait, BTree/Hash/Bitmap/Trie |
65| `repl` | CLI shell | rustyline, tabled rendering |
66| `testsupport` | Test fixtures | `run_sql_script`, snapshots |
67
68### 3. Data Flow Patterns
69
70**Query Execution (SELECT):**
71```
72SQL string
73 → parser (sqlparser-rs → AST)
74 → planner (logical plan → physical plan with index selection)
75 → executor (Volcano operators: Scan → Filter → Project)
76 → storage/index (fetch rows via RecordId)
77 → buffer (page cache lookup/fetch)
78 → REPL (format RecordBatch with tabled)
79```
80
81**Write Path (INSERT/UPDATE/DELETE):**
82```
83SQL string
84 → parser → planner → executor
85 → WAL (append WalRecord, fsync)
86 → storage (heap table modification)
87 → index (maintain all indexes on affected columns)
88 → buffer (mark pages dirty)
89```
90
91**Recovery:**
92```
93Startup
94 → WAL replay (read WalRecord log)
95 → storage (re-apply operations)
96 → catalog (restore table/index metadata)
97```
98
99### 4. Code Navigation Strategy
100
101**Finding implementations:**
102- **Trait definitions**: Search crate root (e.g., `executor/lib.rs` for `Exec`)
103- **Concrete operators**: Look in `<crate>/src/<operator>.rs`
104- **Tests**: Adjacent `.rs` files or `tests/` directory
105- **Integration**: `testsupport/` for end-to-end scripts
106
107**Dependency order** (from CLAUDE.md):
108```
109common ← types ← expr
110 ↓
111 parser
112 ↓
113 catalog
114 ↓
115 buffer ← storage ← wal
116 ↓
117 planner
118 ↓
119 index
120 ↓
121 executor
122 ↓
123 repl
124```
125
126### 5. Key Design Principles
127
128From the design doc:
129
1301. **Tiny interfaces** - Prefer small traits (`Exec`, `Index`, `HeapTable`, `Pager`)
1312. **No implicit coercion** - Same-type comparisons only (v1)
1323. **WAL-first writes** - (1) append WAL (2) fsync (3) apply to storage
1334. **Rule-based planning** - Predicate pushdown + index selection
1345. **Single writer** - No transactions/MVCC in v1
1356. **Snapshot tests** - Use `insta` for query output verification
136
137### 6. Response Format
138
139When explaining architecture:
140
141**Component Overview:**
142- Purpose in 1 sentence
143- Key types/traits
144- Dependencies (what it calls)
145- Dependents (what calls it)
146
147**Code References:**
148- Use `crate/path/file.rs:line` format
149- Link related components
150- Show trait → impl relationships
151
152**Examples:**
153- Provide concrete SQL queries
154- Show expected output format
155- Reference existing tests when available
156
157## Project Context
158
159- **Language**: Rust (workspace with 13 crates)
160- **Build**: `cargo check/test/fmt/clippy`
161- **Coverage**: `scripts/coverage.sh` (llvm-cov)
162- **Testing**: Unit tests inline, integration in `tests/`, snapshots with `insta`
163- **Dependencies**: Pinned in workspace `Cargo.toml` with `workspace = true`
164
165## Common Questions
166
167**"How does index selection work?"**
168→ Check `planner/` for rules matching `WHERE` predicates to available indexes
169
170**"Where is Row serialized?"**
171→ `storage/` uses `bincode` for tuple layout in slotted pages
172
173**"How does the buffer pool work?"**
174→ `buffer/` implements LRU cache over `PageId`, backed by file segments
175
176**"What SQL is supported?"**
177→ See SKILL.md header or `parser/` for DDL/DML subset
178
179**"How to add a new operator?"**
180→ Implement `Exec` trait in `executor/`, wire into planner physical node generation
181
182## Tool Usage
183
184- **Grep**: Find trait implementations, error types, specific SQL keywords
185- **Read**: Examine crate root `lib.rs`, trait definitions, test fixtures
186- **Glob**: Locate all operators (`executor/**/*.rs`), test files (`**/tests/*.rs`)
187
188---
189
190For detailed API signatures and implementation notes, see REFERENCE.md.