Skip to main content

sqlrite/sql/pager/
mod.rs

1//! On-disk persistence for a `Database`, using fixed-size paged files.
2//!
3//! The file is a sequence of 4 KiB pages. Page 0 holds the header
4//! (magic, version, page count, schema-root pointer). Every other page carries
5//! a small per-page header (type tag + next-page pointer + payload length)
6//! followed by a payload of up to 4089 bytes.
7//!
8//! **Storage strategy (format version 2, Phase 3c.5).**
9//!
10//! - Each `Table`'s rows live as **cells** in a chain of `TableLeaf` pages.
11//!   Cell layout and slot directory are in `cell.rs` / `table_page.rs`;
12//!   cells that exceed the inline threshold spill into an overflow chain
13//!   via `overflow.rs`.
14//! - The schema catalog is itself a regular table named `sqlrite_master`,
15//!   with one row per user table:
16//!       `(name TEXT PRIMARY KEY, sql TEXT NOT NULL,
17//!         rootpage INTEGER NOT NULL, last_rowid INTEGER NOT NULL)`
18//!   This is the SQLite-style approach: the schema of `sqlrite_master`
19//!   itself is hardcoded into the engine so the open path can bootstrap.
20//! - Page 0's `schema_root_page` field points at the first leaf of
21//!   `sqlrite_master`.
22//!
23//! **Format version.** Version 2 is not compatible with files produced by
24//! earlier commits. Opening a v1 file returns a clean error — users on
25//! old files have to regenerate them from CREATE/INSERT, as there's no
26//! production data to migrate yet.
27
28// Data-layer modules. Not every helper in these modules is used by save/open
29// yet — some exist for tests, some for future maintenance operations.
30// Module-level #[allow(dead_code)] keeps the build quiet without dotting
31// the modules with per-item attributes.
32#[allow(dead_code)]
33pub mod allocator;
34#[allow(dead_code)]
35pub mod cell;
36pub mod file;
37#[allow(dead_code)]
38pub mod freelist;
39#[allow(dead_code)]
40pub mod fts_cell;
41pub mod header;
42#[allow(dead_code)]
43pub mod hnsw_cell;
44#[allow(dead_code)]
45pub mod index_cell;
46#[allow(dead_code)]
47pub mod interior_page;
48pub mod overflow;
49pub mod page;
50pub mod pager;
51#[allow(dead_code)]
52pub mod table_page;
53#[allow(dead_code)]
54pub mod varint;
55#[allow(dead_code)]
56pub mod wal;
57
58use std::collections::{BTreeMap, HashMap};
59use std::path::Path;
60use std::sync::{Arc, Mutex};
61
62use crate::sql::dialect::SqlriteDialect;
63use sqlparser::parser::Parser;
64
65use crate::error::{Result, SQLRiteError};
66use crate::sql::db::database::Database;
67use crate::sql::db::secondary_index::{IndexOrigin, SecondaryIndex};
68use crate::sql::db::table::{Column, DataType, Row, Table, Value};
69use crate::sql::hnsw::DistanceMetric;
70use crate::sql::pager::cell::Cell;
71use crate::sql::pager::header::DbHeader;
72use crate::sql::pager::index_cell::IndexCell;
73use crate::sql::pager::interior_page::{InteriorCell, InteriorPage};
74use crate::sql::pager::overflow::{
75    OVERFLOW_THRESHOLD, OverflowRef, PagedEntry, read_overflow_chain, write_overflow_chain,
76};
77use crate::sql::pager::page::{PAGE_HEADER_SIZE, PAGE_SIZE, PAYLOAD_PER_PAGE, PageType};
78use crate::sql::pager::pager::Pager;
79use crate::sql::pager::table_page::TablePage;
80use crate::sql::parser::create::CreateQuery;
81
82// Re-export so callers can spell `sql::pager::AccessMode` without
83// reaching into the `pager::pager::pager` submodule path.
84pub use crate::sql::pager::pager::AccessMode;
85
86/// Name of the internal catalog table. Reserved — user CREATEs of this
87/// name must be rejected upstream.
88pub const MASTER_TABLE_NAME: &str = "sqlrite_master";
89
90/// Opens a database file in read-write mode. Shorthand for
91/// [`open_database_with_mode`] with [`AccessMode::ReadWrite`].
92pub fn open_database(path: &Path, db_name: String) -> Result<Database> {
93    open_database_with_mode(path, db_name, AccessMode::ReadWrite)
94}
95
96/// Opens a database file in read-only mode. Acquires a shared OS-level
97/// advisory lock, so other read-only openers coexist but any writer is
98/// excluded. Attempts to mutate the returned `Database` (e.g. an
99/// `INSERT`, or a `save_database` call against it) bottom out in a
100/// `cannot commit: database is opened read-only` error from the Pager.
101pub fn open_database_read_only(path: &Path, db_name: String) -> Result<Database> {
102    open_database_with_mode(path, db_name, AccessMode::ReadOnly)
103}
104
105/// Opens a database file and reconstructs the in-memory `Database`,
106/// leaving the long-lived `Pager` attached for subsequent auto-save
107/// (read-write) or consistent-snapshot reads (read-only).
108pub fn open_database_with_mode(path: &Path, db_name: String, mode: AccessMode) -> Result<Database> {
109    let pager = Pager::open_with_mode(path, mode)?;
110
111    // 1. Load sqlrite_master from the tree at header.schema_root_page.
112    let mut master = build_empty_master_table();
113    load_table_rows(&pager, &mut master, pager.header().schema_root_page)?;
114
115    // 2. Two passes over master rows: first build every user table, then
116    //    attach secondary indexes. Indexes need their base table to exist
117    //    before we can populate them. Auto-indexes are created at table
118    //    build time so we only have to load explicit indexes from disk
119    //    (but we also reload the auto-index CONTENT because Table::new
120    //    built it empty).
121    let mut db = Database::new(db_name);
122    let mut index_rows: Vec<IndexCatalogRow> = Vec::new();
123
124    for rowid in master.rowids() {
125        let ty = take_text(&master, "type", rowid)?;
126        let name = take_text(&master, "name", rowid)?;
127        let sql = take_text(&master, "sql", rowid)?;
128        let rootpage = take_integer(&master, "rootpage", rowid)? as u32;
129        let last_rowid = take_integer(&master, "last_rowid", rowid)?;
130
131        match ty.as_str() {
132            "table" => {
133                let (parsed_name, columns) = parse_create_sql(&sql)?;
134                if parsed_name != name {
135                    return Err(SQLRiteError::Internal(format!(
136                        "sqlrite_master row '{name}' carries SQL for '{parsed_name}' — corrupt catalog?"
137                    )));
138                }
139                let mut table = build_empty_table(&name, columns, last_rowid);
140                if rootpage != 0 {
141                    load_table_rows(&pager, &mut table, rootpage)?;
142                }
143                if last_rowid > table.last_rowid {
144                    table.last_rowid = last_rowid;
145                }
146                db.tables.insert(name, table);
147            }
148            "index" => {
149                index_rows.push(IndexCatalogRow {
150                    name,
151                    sql,
152                    rootpage,
153                });
154            }
155            other => {
156                return Err(SQLRiteError::Internal(format!(
157                    "sqlrite_master row '{name}' has unknown type '{other}'"
158                )));
159            }
160        }
161    }
162
163    // Second pass: attach each index to its table. HNSW indexes
164    // (Phase 7d.2) take a different code path because their persisted
165    // form is just the CREATE INDEX SQL — the graph itself isn't
166    // persisted yet (Phase 7d.3). Detect HNSW via the SQL's USING clause
167    // and route to a graph-rebuild instead of the B-Tree-cell load.
168    //
169    // Phase 8b — same shape for FTS indexes. The posting lists aren't
170    // persisted yet (Phase 8c), so we replay the CREATE INDEX SQL on
171    // open and let `execute_create_index` walk current rows.
172    for row in index_rows {
173        if create_index_sql_uses_hnsw(&row.sql) {
174            rebuild_hnsw_index(&mut db, &pager, &row)?;
175        } else if create_index_sql_uses_fts(&row.sql) {
176            rebuild_fts_index(&mut db, &pager, &row)?;
177        } else {
178            attach_index(&mut db, &pager, row)?;
179        }
180    }
181
182    // Phase 11.9 — replay any MVCC commit batches recovered from
183    // the WAL into the freshly-built `MvStore`, and seed the
184    // `MvccClock` past the highest persisted timestamp. Without
185    // this step the in-memory MVCC state would always start blank
186    // on reopen — fine for legacy single-session workloads, but a
187    // correctness gap once `BEGIN CONCURRENT` is in play (a
188    // second process could hand out a `begin_ts` below an
189    // already-committed version's `end`, breaking the visibility
190    // rule).
191    //
192    // The clock seed is the larger of (header.clock_high_water,
193    // max(commit_ts among replayed batches)) so a crash between
194    // commits and the next checkpoint — where the header's
195    // high-water lags reality — still produces a clock that
196    // doesn't regress.
197    replay_mvcc_into_db(&mut db, &pager)?;
198
199    db.source_path = Some(path.to_path_buf());
200    db.pager = Some(pager);
201    Ok(db)
202}
203
204/// Phase 11.9 — drains every MVCC commit batch the Pager recovered
205/// from the WAL into `db.mv_store`, and advances `db.mvcc_clock`
206/// to at least the highest observed timestamp.
207///
208/// Batches are replayed in WAL order, which matches commit order
209/// (the WAL appends sequentially). Each record's `commit_ts`
210/// becomes the version's `begin`, with the previous latest
211/// version's `end` capped at the same timestamp — identical to
212/// the live-commit path's `MvStore::push_committed`.
213fn replay_mvcc_into_db(db: &mut Database, pager: &Pager) -> Result<()> {
214    use crate::mvcc::RowVersion;
215
216    let mut clock_seed = pager.clock_high_water();
217    for batch in pager.recovered_mvcc_commits() {
218        if batch.commit_ts > clock_seed {
219            clock_seed = batch.commit_ts;
220        }
221        for rec in &batch.records {
222            let version = RowVersion::committed(batch.commit_ts, rec.payload.clone());
223            db.mv_store
224                .push_committed(rec.row.clone(), version)
225                .map_err(|e| {
226                    SQLRiteError::Internal(format!(
227                        "WAL MVCC replay: push_committed failed for {}/{}: {e}",
228                        rec.row.table, rec.row.rowid,
229                    ))
230                })?;
231        }
232    }
233    if clock_seed > 0 {
234        db.mvcc_clock.observe(clock_seed);
235    }
236    Ok(())
237}
238
239/// Catalog row for a secondary index — deferred until after every table is
240/// loaded so the index's base table exists by the time we populate it.
241struct IndexCatalogRow {
242    name: String,
243    sql: String,
244    rootpage: u32,
245}
246
247/// Persists `db` to disk. Diff-pager skips writing pages whose bytes
248/// haven't changed; the [`PageAllocator`] preserves per-table page
249/// numbers across saves so unchanged tables produce zero dirty frames.
250///
251/// Pages that were live before this save but aren't restaged this round
252/// (e.g., the leaves of a dropped table) move onto a persisted free
253/// list rooted at `header.freelist_head`; subsequent saves draw from
254/// the freelist before extending the file. `VACUUM` (see
255/// [`vacuum_database`]) compacts the file by ignoring the freelist and
256/// allocating linearly from page 1.
257///
258/// [`PageAllocator`]: crate::sql::pager::allocator::PageAllocator
259pub fn save_database(db: &mut Database, path: &Path) -> Result<()> {
260    save_database_with_mode(db, path, /*compact=*/ false)
261}
262
263/// Reclaims space by rewriting every live B-Tree contiguously from
264/// page 1, with no freelist. Equivalent to `save_database` but ignores
265/// the existing freelist and per-table preferred pools — every page is
266/// allocated by extending the high-water mark — so the resulting file
267/// is tightly packed and the freelist is empty.
268///
269/// Used by the SQL-level `VACUUM;` statement.
270pub fn vacuum_database(db: &mut Database, path: &Path) -> Result<()> {
271    save_database_with_mode(db, path, /*compact=*/ true)
272}
273
274/// Shared save core. `compact = false` is the normal save path (uses
275/// the existing freelist + per-table preferred pools). `compact = true`
276/// is the VACUUM path (empty freelist, empty preferred pools, linear
277/// allocation from page 1).
278fn save_database_with_mode(db: &mut Database, path: &Path, compact: bool) -> Result<()> {
279    // Phase 7d.3 — rebuild any HNSW index that DELETE / UPDATE-on-vector
280    // marked dirty. Done up front under the &mut Database borrow we
281    // already hold, before the immutable iteration loops below need
282    // their own borrow.
283    rebuild_dirty_hnsw_indexes(db);
284    // Phase 8b — same drill for FTS indexes flagged by DELETE / UPDATE.
285    rebuild_dirty_fts_indexes(db);
286
287    let same_path = db.source_path.as_deref() == Some(path);
288    let mut pager = if same_path {
289        match db.pager.take() {
290            Some(p) => p,
291            None if path.exists() => Pager::open(path)?,
292            None => Pager::create(path)?,
293        }
294    } else if path.exists() {
295        Pager::open(path)?
296    } else {
297        Pager::create(path)?
298    };
299
300    // Snapshot what was live BEFORE we reset staged. Used to compute the
301    // newly-freed set after staging completes. Page 0 (the header) is
302    // never on the freelist — it's always live.
303    let old_header = pager.header();
304    let old_live: std::collections::HashSet<u32> = (1..old_header.page_count).collect();
305
306    // Read the previously-persisted freelist so its leaf pages can be
307    // reused as preferred allocations and its trunk pages don't leak.
308    let (old_free_leaves, old_free_trunks) = if compact || old_header.freelist_head == 0 {
309        (Vec::new(), Vec::new())
310    } else {
311        crate::sql::pager::freelist::read_freelist(&pager, old_header.freelist_head)?
312    };
313
314    // Snapshot the previous rootpages of each table/index so we can
315    // seed per-table preferred pools (the unchanged-table case stages
316    // byte-identical pages → diff pager skips every write for it).
317    let old_rootpages = if compact {
318        HashMap::new()
319    } else {
320        read_old_rootpages(&pager, old_header.schema_root_page)?
321    };
322
323    // SQLR-1 — snapshot every prior B-Tree's page set NOW, before any
324    // staging starts. `Pager::read_page` shadows on-disk bytes with the
325    // current `staged` buffer, so if we deferred these walks until each
326    // object's turn in the staging loop, a *new* index added in this
327    // save would extend past the old high-water and overwrite the
328    // pages of any later-staged object whose old root sits in that
329    // range — including `sqlrite_master`, which is always staged last.
330    // The follow-up walk would then read the wrong B-Tree's bytes and
331    // either hand the allocator a bogus preferred pool or panic
332    // dispatching cells (a table-cell decoder vs. an index leaf, the
333    // shape of the original SQLR-1 panic). Walking up front pins each
334    // map to the committed bytes that were on disk before this save
335    // touched anything.
336    let old_preferred_pages: HashMap<(String, String), Vec<u32>> = if compact {
337        HashMap::new()
338    } else {
339        let mut map: HashMap<(String, String), Vec<u32>> = HashMap::new();
340        for ((kind, name), &root) in &old_rootpages {
341            // Tables can carry overflow chains; index/HNSW/FTS leaves
342            // never overflow in the current encoding, so the cheaper
343            // walk suffices for them.
344            let follow = kind == "table";
345            let pages = collect_pages_for_btree(&pager, root, follow)?;
346            map.insert((kind.clone(), name.clone()), pages);
347        }
348        map
349    };
350    let old_master_pages: Vec<u32> = if compact || old_header.schema_root_page == 0 {
351        Vec::new()
352    } else {
353        collect_pages_for_btree(
354            &pager,
355            old_header.schema_root_page,
356            /*follow_overflow=*/ true,
357        )?
358    };
359
360    pager.clear_staged();
361
362    // Allocator: in normal mode, seed with the old freelist; in compact
363    // mode, start empty so allocation extends linearly from page 1.
364    use std::collections::VecDeque;
365    let initial_freelist: VecDeque<u32> = if compact {
366        VecDeque::new()
367    } else {
368        crate::sql::pager::freelist::freelist_to_deque(old_free_leaves.clone())
369    };
370    let mut alloc = crate::sql::pager::allocator::PageAllocator::new(initial_freelist, 1);
371
372    // 1. Stage each user table's B-Tree, collecting master-row info.
373    //    `kind` is "table" or "index" — master has one row per each.
374    let mut master_rows: Vec<CatalogEntry> = Vec::new();
375
376    let mut table_names: Vec<&String> = db.tables.keys().collect();
377    table_names.sort();
378    for name in table_names {
379        if name == MASTER_TABLE_NAME {
380            return Err(SQLRiteError::Internal(format!(
381                "user table cannot be named '{MASTER_TABLE_NAME}' (reserved)"
382            )));
383        }
384        if !compact {
385            if let Some(prev) = old_preferred_pages.get(&("table".to_string(), name.to_string())) {
386                alloc.set_preferred(prev.clone());
387            }
388        }
389        let table = &db.tables[name];
390        let rootpage = stage_table_btree(&mut pager, table, &mut alloc)?;
391        alloc.finish_preferred();
392        master_rows.push(CatalogEntry {
393            kind: "table".into(),
394            name: name.clone(),
395            sql: table_to_create_sql(table),
396            rootpage,
397            last_rowid: table.last_rowid,
398        });
399    }
400
401    // 2. Stage each secondary index's B-Tree. Indexes persist in a
402    //    deterministic order: sorted by (owning_table, index_name).
403    let mut index_entries: Vec<(&Table, &SecondaryIndex)> = Vec::new();
404    for table in db.tables.values() {
405        for idx in &table.secondary_indexes {
406            index_entries.push((table, idx));
407        }
408    }
409    index_entries
410        .sort_by(|(ta, ia), (tb, ib)| ta.tb_name.cmp(&tb.tb_name).then(ia.name.cmp(&ib.name)));
411    for (_table, idx) in index_entries {
412        if !compact {
413            if let Some(prev) =
414                old_preferred_pages.get(&("index".to_string(), idx.name.to_string()))
415            {
416                alloc.set_preferred(prev.clone());
417            }
418        }
419        let rootpage = stage_index_btree(&mut pager, idx, &mut alloc)?;
420        alloc.finish_preferred();
421        master_rows.push(CatalogEntry {
422            kind: "index".into(),
423            name: idx.name.clone(),
424            sql: idx.synthesized_sql(),
425            rootpage,
426            last_rowid: 0,
427        });
428    }
429
430    // 2b. Phase 7d.3: persist HNSW indexes as their own cell-encoded
431    //     page trees, with the rootpage recorded in sqlrite_master.
432    //     Reopen loads the graph back from cells (fast, exact match)
433    //     instead of rebuilding from rows.
434    //
435    //     Dirty indexes (set by DELETE / UPDATE-on-vector-col) are
436    //     rebuilt from current rows BEFORE staging, so the on-disk
437    //     graph reflects the current row set.
438    let mut hnsw_entries: Vec<(&Table, &crate::sql::db::table::HnswIndexEntry)> = Vec::new();
439    for table in db.tables.values() {
440        for entry in &table.hnsw_indexes {
441            hnsw_entries.push((table, entry));
442        }
443    }
444    hnsw_entries
445        .sort_by(|(ta, ea), (tb, eb)| ta.tb_name.cmp(&tb.tb_name).then(ea.name.cmp(&eb.name)));
446    for (table, entry) in hnsw_entries {
447        if !compact {
448            if let Some(prev) =
449                old_preferred_pages.get(&("index".to_string(), entry.name.to_string()))
450            {
451                alloc.set_preferred(prev.clone());
452            }
453        }
454        let rootpage = stage_hnsw_btree(&mut pager, &entry.index, &mut alloc)?;
455        alloc.finish_preferred();
456        master_rows.push(CatalogEntry {
457            kind: "index".into(),
458            name: entry.name.clone(),
459            sql: synthesize_hnsw_create_index_sql(
460                &entry.name,
461                &table.tb_name,
462                &entry.column_name,
463                entry.metric,
464            ),
465            rootpage,
466            last_rowid: 0,
467        });
468    }
469
470    // 2c. Phase 8c — persist FTS posting lists as their own
471    //     cell-encoded page trees, with the rootpage recorded in
472    //     sqlrite_master. Reopen loads the postings back from cells
473    //     (fast, exact match) instead of re-tokenizing rows.
474    //
475    //     Dirty indexes (set by DELETE / UPDATE-on-text-col) are
476    //     rebuilt from current rows BEFORE staging by
477    //     `rebuild_dirty_fts_indexes`, so the on-disk tree reflects
478    //     the current row set.
479    let mut fts_entries: Vec<(&Table, &crate::sql::db::table::FtsIndexEntry)> = Vec::new();
480    for table in db.tables.values() {
481        for entry in &table.fts_indexes {
482            fts_entries.push((table, entry));
483        }
484    }
485    fts_entries
486        .sort_by(|(ta, ea), (tb, eb)| ta.tb_name.cmp(&tb.tb_name).then(ea.name.cmp(&eb.name)));
487    let any_fts = !fts_entries.is_empty();
488    for (table, entry) in fts_entries {
489        if !compact {
490            if let Some(prev) =
491                old_preferred_pages.get(&("index".to_string(), entry.name.to_string()))
492            {
493                alloc.set_preferred(prev.clone());
494            }
495        }
496        let rootpage = stage_fts_btree(&mut pager, &entry.index, &mut alloc)?;
497        alloc.finish_preferred();
498        master_rows.push(CatalogEntry {
499            kind: "index".into(),
500            name: entry.name.clone(),
501            sql: format!(
502                "CREATE INDEX {} ON {} USING fts ({})",
503                entry.name, table.tb_name, entry.column_name
504            ),
505            rootpage,
506            last_rowid: 0,
507        });
508    }
509
510    // 3. Build an in-memory sqlrite_master with one row per table or index,
511    //    then stage it via the same tree-build path. Seed master's
512    //    preferred pool with the previous master tree's pages so the
513    //    catalog page numbers stay stable across saves whenever the
514    //    catalog content didn't change.
515    let mut master = build_empty_master_table();
516    for (i, entry) in master_rows.into_iter().enumerate() {
517        let rowid = (i as i64) + 1;
518        master.restore_row(
519            rowid,
520            vec![
521                Some(Value::Text(entry.kind)),
522                Some(Value::Text(entry.name)),
523                Some(Value::Text(entry.sql)),
524                Some(Value::Integer(entry.rootpage as i64)),
525                Some(Value::Integer(entry.last_rowid)),
526            ],
527        )?;
528    }
529    if !compact && !old_master_pages.is_empty() {
530        // Use the page list snapshotted before any staging touched
531        // disk; re-walking here would read whatever a new index
532        // already restaged on top of master's old root (SQLR-1).
533        alloc.set_preferred(old_master_pages.clone());
534    }
535    let master_root = stage_table_btree(&mut pager, &master, &mut alloc)?;
536    alloc.finish_preferred();
537
538    // 4. Compute newly-freed pages: the previously-live set minus what
539    //    we just restaged. The previous freelist's trunk pages get
540    //    re-encoded too — they're in `old_live`, weren't restaged, so
541    //    the filter naturally moves them to the new freelist.
542    //
543    // In `compact` mode (VACUUM), we *discard* newly_freed instead of
544    // routing it onto the new freelist. The whole point of VACUUM is
545    // to let the file truncate to the new high-water mark, so any page
546    // past it gets dropped at the next checkpoint.
547    if !compact {
548        let used = alloc.used().clone();
549        let mut newly_freed: Vec<u32> = old_live
550            .iter()
551            .copied()
552            .filter(|p| !used.contains(p))
553            .collect();
554        let _ = &old_free_trunks; // silenced — handled by the old_live filter
555        alloc.add_to_freelist(newly_freed.drain(..));
556    }
557
558    // 5. Encode the new freelist into trunk pages. `stage_freelist`
559    //    consumes some of the free pages AS the trunk pages themselves —
560    //    a trunk is just a free page borrowed for metadata. Pages that
561    //    were on the freelist but become trunks no longer need to be
562    //    "extension" pages; the high-water mark from the staging loop
563    //    above is already correct.
564    let new_free_pages = alloc.drain_freelist();
565    let new_freelist_head =
566        crate::sql::pager::freelist::stage_freelist(&mut pager, new_free_pages)?;
567
568    // 6. Pick the format version. v6 is on demand: only bumps when the
569    //    new freelist is non-empty. FTS-bearing files keep their v5
570    //    promotion; v6 is a strict superset (v6 readers handle v4/v5/v6).
571    use crate::sql::pager::header::{FORMAT_VERSION_V5, FORMAT_VERSION_V6};
572    let format_version = if new_freelist_head != 0 {
573        FORMAT_VERSION_V6
574    } else if any_fts {
575        // Preserve a v6 file at v6 (don't downgrade) but otherwise
576        // bump v4 → v5 for FTS like Phase 8c does.
577        std::cmp::max(FORMAT_VERSION_V5, old_header.format_version)
578    } else {
579        // Preserve whatever the file already was.
580        old_header.format_version
581    };
582
583    pager.commit(DbHeader {
584        page_count: alloc.high_water(),
585        schema_root_page: master_root,
586        format_version,
587        freelist_head: new_freelist_head,
588    })?;
589
590    if same_path {
591        db.pager = Some(pager);
592    }
593    Ok(())
594}
595
596/// Build material for a single row in sqlrite_master.
597struct CatalogEntry {
598    kind: String, // "table" or "index"
599    name: String,
600    sql: String,
601    rootpage: u32,
602    last_rowid: i64,
603}
604
605// -------------------------------------------------------------------------
606// sqlrite_master — hardcoded catalog table schema
607
608fn build_empty_master_table() -> Table {
609    // Phase 3e: `type` is the first column, matching SQLite's convention.
610    // It distinguishes `'table'` rows from `'index'` rows.
611    let columns = vec![
612        Column::new("type".into(), "text".into(), false, true, false),
613        Column::new("name".into(), "text".into(), true, true, true),
614        Column::new("sql".into(), "text".into(), false, true, false),
615        Column::new("rootpage".into(), "integer".into(), false, true, false),
616        Column::new("last_rowid".into(), "integer".into(), false, true, false),
617    ];
618    build_empty_table(MASTER_TABLE_NAME, columns, 0)
619}
620
621/// Reads a required Text column from a known-good catalog row.
622fn take_text(table: &Table, col: &str, rowid: i64) -> Result<String> {
623    match table.get_value(col, rowid) {
624        Some(Value::Text(s)) => Ok(s),
625        other => Err(SQLRiteError::Internal(format!(
626            "sqlrite_master column '{col}' at rowid {rowid}: expected Text, got {other:?}"
627        ))),
628    }
629}
630
631/// Reads a required Integer column from a known-good catalog row.
632fn take_integer(table: &Table, col: &str, rowid: i64) -> Result<i64> {
633    match table.get_value(col, rowid) {
634        Some(Value::Integer(v)) => Ok(v),
635        other => Err(SQLRiteError::Internal(format!(
636            "sqlrite_master column '{col}' at rowid {rowid}: expected Integer, got {other:?}"
637        ))),
638    }
639}
640
641// -------------------------------------------------------------------------
642// CREATE-TABLE SQL synthesis and re-parsing
643
644/// Synthesizes a CREATE TABLE SQL string that recreates the table's schema.
645/// Deterministic: same schema → same SQL, so diffing commits stay stable.
646fn table_to_create_sql(table: &Table) -> String {
647    let mut parts = Vec::with_capacity(table.columns.len());
648    for c in &table.columns {
649        // Render the SQL type literally so the round-trip through
650        // CREATE TABLE re-parsing recreates the same schema. Vector
651        // carries its dimension inline.
652        let ty: String = match &c.datatype {
653            DataType::Integer => "INTEGER".to_string(),
654            DataType::Text => "TEXT".to_string(),
655            DataType::Real => "REAL".to_string(),
656            DataType::Bool => "BOOLEAN".to_string(),
657            DataType::Vector(dim) => format!("VECTOR({dim})"),
658            DataType::Json => "JSON".to_string(),
659            DataType::None | DataType::Invalid => "TEXT".to_string(),
660        };
661        let mut piece = format!("{} {}", c.column_name, ty);
662        if c.is_pk {
663            piece.push_str(" PRIMARY KEY");
664        } else {
665            if c.is_unique {
666                piece.push_str(" UNIQUE");
667            }
668            if c.not_null {
669                piece.push_str(" NOT NULL");
670            }
671        }
672        if let Some(default) = &c.default {
673            piece.push_str(" DEFAULT ");
674            piece.push_str(&render_default_literal(default));
675        }
676        parts.push(piece);
677    }
678    format!("CREATE TABLE {} ({});", table.tb_name, parts.join(", "))
679}
680
681/// Renders a DEFAULT value back to SQL-literal form so the synthesized
682/// CREATE TABLE round-trips through `parse_create_sql`. Text values get
683/// single-quoted with single-quote doubling for escaping. Vector defaults
684/// are not currently expressible at CREATE TABLE time, so we render them
685/// as their bracket-array form (matches the INSERT literal grammar).
686fn render_default_literal(value: &Value) -> String {
687    match value {
688        Value::Integer(i) => i.to_string(),
689        Value::Real(f) => f.to_string(),
690        Value::Bool(b) => {
691            if *b {
692                "TRUE".to_string()
693            } else {
694                "FALSE".to_string()
695            }
696        }
697        Value::Text(s) => format!("'{}'", s.replace('\'', "''")),
698        Value::Null => "NULL".to_string(),
699        Value::Vector(_) => value.to_display_string(),
700    }
701}
702
703/// Reverses `table_to_create_sql`: feeds the SQL back through `sqlparser`
704/// and produces our internal column list. Returns `(table_name, columns)`.
705fn parse_create_sql(sql: &str) -> Result<(String, Vec<Column>)> {
706    let dialect = SqlriteDialect::new();
707    let mut ast = Parser::parse_sql(&dialect, sql).map_err(SQLRiteError::from)?;
708    let stmt = ast.pop().ok_or_else(|| {
709        SQLRiteError::Internal("sqlrite_master row held an empty SQL string".to_string())
710    })?;
711    let create = CreateQuery::new(&stmt)?;
712    let columns = create
713        .columns
714        .into_iter()
715        .map(|pc| {
716            Column::with_default(
717                pc.name,
718                pc.datatype,
719                pc.is_pk,
720                pc.not_null,
721                pc.is_unique,
722                pc.default,
723            )
724        })
725        .collect();
726    Ok((create.table_name, columns))
727}
728
729// -------------------------------------------------------------------------
730// In-memory table (re)construction
731
732/// Builds an empty in-memory `Table` given the declared columns.
733fn build_empty_table(name: &str, columns: Vec<Column>, last_rowid: i64) -> Table {
734    let rows: Arc<Mutex<HashMap<String, Row>>> = Arc::new(Mutex::new(HashMap::new()));
735    let mut secondary_indexes: Vec<SecondaryIndex> = Vec::new();
736    {
737        let mut map = rows.lock().expect("rows mutex poisoned");
738        for col in &columns {
739            // Mirror the dispatch in `Table::new` so the reconstructed
740            // table has the same shape it'd have if it were built fresh
741            // from SQL. Phase 7a adds the Vector arm — without it,
742            // VECTOR columns silently restore as Row::None and every
743            // restore_row hits a "storage None vs value Some(Vector(...))"
744            // type mismatch.
745            let row = match &col.datatype {
746                DataType::Integer => Row::Integer(BTreeMap::new()),
747                DataType::Text => Row::Text(BTreeMap::new()),
748                DataType::Real => Row::Real(BTreeMap::new()),
749                DataType::Bool => Row::Bool(BTreeMap::new()),
750                DataType::Vector(_dim) => Row::Vector(BTreeMap::new()),
751                // JSON columns reuse Text storage — see Table::new and
752                // Phase 7e's scope-correction note.
753                DataType::Json => Row::Text(BTreeMap::new()),
754                DataType::None | DataType::Invalid => Row::None,
755            };
756            map.insert(col.column_name.clone(), row);
757
758            // Auto-create UNIQUE/PK indexes so the restored table has the
759            // same shape Table::new would have built from fresh SQL.
760            if (col.is_pk || col.is_unique)
761                && matches!(col.datatype, DataType::Integer | DataType::Text)
762            {
763                if let Ok(idx) = SecondaryIndex::new(
764                    SecondaryIndex::auto_name(name, &col.column_name),
765                    name.to_string(),
766                    col.column_name.clone(),
767                    &col.datatype,
768                    true,
769                    IndexOrigin::Auto,
770                ) {
771                    secondary_indexes.push(idx);
772                }
773            }
774        }
775    }
776
777    let primary_key = columns
778        .iter()
779        .find(|c| c.is_pk)
780        .map(|c| c.column_name.clone())
781        .unwrap_or_else(|| "-1".to_string());
782
783    Table {
784        tb_name: name.to_string(),
785        columns,
786        rows,
787        secondary_indexes,
788        // HNSW indexes (Phase 7d.2) are reconstructed on open by re-
789        // executing each `CREATE INDEX … USING hnsw` SQL stored in
790        // `sqlrite_master`. This builder produces the empty shell;
791        // `replay_create_index_for_hnsw` (in this same module) walks
792        // sqlrite_master after every table is loaded and rebuilds the
793        // graph from current row data. Persistence of the graph itself
794        // (avoiding the on-open rebuild cost) is Phase 7d.3.
795        hnsw_indexes: Vec::new(),
796        // FTS indexes (Phase 8b) follow the same pattern — the
797        // CREATE INDEX … USING fts SQL is the source of truth on open
798        // and the in-memory posting list gets rebuilt from current
799        // rows. Cell-encoded persistence of the postings is Phase 8c.
800        fts_indexes: Vec::new(),
801        last_rowid,
802        primary_key,
803    }
804}
805
806// -------------------------------------------------------------------------
807// Leaf-chain read / write
808
809/// Walks a table's B-Tree from `root_page`, following the leftmost-child
810/// chain down to the first leaf, then iterating leaves via their sibling
811/// `next_page` pointers. Every cell is decoded and replayed into `table`.
812///
813/// Open-path note: we eagerly materialize the entire table into `Table`'s
814/// in-memory maps. Phase 5 will introduce a `Cursor` that hits the pager
815/// on demand so queries can stream through the tree without a full upfront
816/// load.
817/// Re-parses `CREATE INDEX` SQL from sqlrite_master and restores the
818/// index on its base table by walking the tree of index cells at
819/// `rootpage`. The base table is expected to already be in `db.tables`.
820fn attach_index(db: &mut Database, pager: &Pager, row: IndexCatalogRow) -> Result<()> {
821    let (table_name, column_name, is_unique) = parse_create_index_sql(&row.sql)?;
822
823    let table = db.get_table_mut(table_name.clone()).map_err(|_| {
824        SQLRiteError::Internal(format!(
825            "index '{}' references unknown table '{table_name}' (sqlrite_master out of sync?)",
826            row.name
827        ))
828    })?;
829    let datatype = table
830        .columns
831        .iter()
832        .find(|c| c.column_name == column_name)
833        .map(|c| clone_datatype(&c.datatype))
834        .ok_or_else(|| {
835            SQLRiteError::Internal(format!(
836                "index '{}' references unknown column '{column_name}' on '{table_name}'",
837                row.name
838            ))
839        })?;
840
841    // An auto-index on this column may already exist (built by
842    // build_empty_table for UNIQUE/PK columns). If the names match, reuse
843    // the slot instead of adding a duplicate entry.
844    let existing_slot = table
845        .secondary_indexes
846        .iter()
847        .position(|i| i.name == row.name);
848    let idx = match existing_slot {
849        Some(i) => {
850            // Drain any entries that may have been populated during table
851            // restore_row calls — we're about to repopulate from the
852            // persisted tree.
853            table.secondary_indexes.remove(i)
854        }
855        None => SecondaryIndex::new(
856            row.name.clone(),
857            table_name.clone(),
858            column_name.clone(),
859            &datatype,
860            is_unique,
861            IndexOrigin::Explicit,
862        )?,
863    };
864    let mut idx = idx;
865    // Wipe any stale entries from the auto path so the load is idempotent.
866    let is_unique_flag = idx.is_unique;
867    let origin = idx.origin;
868    idx = SecondaryIndex::new(
869        idx.name,
870        idx.table_name,
871        idx.column_name,
872        &datatype,
873        is_unique_flag,
874        origin,
875    )?;
876
877    // Populate from the index tree's cells.
878    load_index_rows(pager, &mut idx, row.rootpage)?;
879
880    table.secondary_indexes.push(idx);
881    Ok(())
882}
883
884/// Walks the leaves of an index B-Tree rooted at `root_page` and inserts
885/// every `(value, rowid)` pair into `idx`.
886fn load_index_rows(pager: &Pager, idx: &mut SecondaryIndex, root_page: u32) -> Result<()> {
887    if root_page == 0 {
888        return Ok(());
889    }
890    let first_leaf = find_leftmost_leaf(pager, root_page)?;
891    let mut current = first_leaf;
892    while current != 0 {
893        let page_buf = pager
894            .read_page(current)
895            .ok_or_else(|| SQLRiteError::Internal(format!("missing index leaf page {current}")))?;
896        if page_buf[0] != PageType::TableLeaf as u8 {
897            return Err(SQLRiteError::Internal(format!(
898                "page {current} tagged {} but expected TableLeaf (index)",
899                page_buf[0]
900            )));
901        }
902        let next_leaf = u32::from_le_bytes(page_buf[1..5].try_into().unwrap());
903        let payload: &[u8; PAYLOAD_PER_PAGE] = (&page_buf[PAGE_HEADER_SIZE..])
904            .try_into()
905            .map_err(|_| SQLRiteError::Internal("index leaf payload size".to_string()))?;
906        let leaf = TablePage::from_bytes(payload);
907
908        for slot in 0..leaf.slot_count() {
909            // Slots on an index page hold KIND_INDEX cells; decode directly.
910            let offset = leaf.slot_offset_raw(slot)?;
911            let (ic, _) = IndexCell::decode(leaf.as_bytes(), offset)?;
912            idx.insert(&ic.value, ic.rowid)?;
913        }
914        current = next_leaf;
915    }
916    Ok(())
917}
918
919/// Minimal recognizer for the synthesized-or-user `CREATE INDEX` SQL we
920/// store in sqlrite_master. Returns `(table_name, column_name, is_unique)`.
921///
922/// Uses sqlparser so user-supplied SQL with extra whitespace, case, etc.
923/// still works; the only shape we accept is single-column indexes.
924fn parse_create_index_sql(sql: &str) -> Result<(String, String, bool)> {
925    use sqlparser::ast::{CreateIndex, Expr, Statement};
926
927    let dialect = SqlriteDialect::new();
928    let mut ast = Parser::parse_sql(&dialect, sql).map_err(SQLRiteError::from)?;
929    let Some(Statement::CreateIndex(CreateIndex {
930        table_name,
931        columns,
932        unique,
933        ..
934    })) = ast.pop()
935    else {
936        return Err(SQLRiteError::Internal(format!(
937            "sqlrite_master index row's SQL isn't a CREATE INDEX: {sql}"
938        )));
939    };
940    if columns.len() != 1 {
941        return Err(SQLRiteError::NotImplemented(
942            "multi-column indexes aren't supported yet".to_string(),
943        ));
944    }
945    let col = match &columns[0].column.expr {
946        Expr::Identifier(ident) => ident.value.clone(),
947        Expr::CompoundIdentifier(parts) => {
948            parts.last().map(|p| p.value.clone()).unwrap_or_default()
949        }
950        other => {
951            return Err(SQLRiteError::Internal(format!(
952                "unsupported indexed column expression: {other:?}"
953            )));
954        }
955    };
956    Ok((table_name.to_string(), col, unique))
957}
958
959/// True iff a CREATE INDEX SQL string uses `USING hnsw` (case-insensitive).
960/// Used by the open path to route HNSW indexes to the graph-rebuild path
961/// instead of the standard B-Tree cell-load. Pre-Phase-7d.2 indexes
962/// don't have a USING clause, so they all return false and continue
963/// taking the existing path.
964fn create_index_sql_uses_hnsw(sql: &str) -> bool {
965    use sqlparser::ast::{CreateIndex, IndexType, Statement};
966
967    let dialect = SqlriteDialect::new();
968    let Ok(mut ast) = Parser::parse_sql(&dialect, sql) else {
969        return false;
970    };
971    let Some(Statement::CreateIndex(CreateIndex { using, .. })) = ast.pop() else {
972        return false;
973    };
974    matches!(using, Some(IndexType::Custom(ident)) if ident.value.eq_ignore_ascii_case("hnsw"))
975}
976
977/// Phase 8b — peeks at a CREATE INDEX SQL to detect `USING fts(...)`.
978/// Mirrors [`create_index_sql_uses_hnsw`].
979fn create_index_sql_uses_fts(sql: &str) -> bool {
980    use sqlparser::ast::{CreateIndex, IndexType, Statement};
981
982    let dialect = SqlriteDialect::new();
983    let Ok(mut ast) = Parser::parse_sql(&dialect, sql) else {
984        return false;
985    };
986    let Some(Statement::CreateIndex(CreateIndex { using, .. })) = ast.pop() else {
987        return false;
988    };
989    matches!(using, Some(IndexType::Custom(ident)) if ident.value.eq_ignore_ascii_case("fts"))
990}
991
992/// Phase 8c — loads (or rebuilds) an FTS index on database open. Two
993/// paths mirror [`rebuild_hnsw_index`]:
994///
995///   - **rootpage != 0** (Phase 8c default): the posting list is
996///     persisted as cell-encoded pages. Read every cell directly via
997///     [`load_fts_postings`] and reconstruct the index — no
998///     re-tokenization, exact bit-for-bit reproduction.
999///
1000///   - **rootpage == 0** (compatibility): no on-disk postings, e.g.
1001///     for files saved by Phase 8b before persistence landed. Replay
1002///     the CREATE INDEX SQL through `execute_create_index`, which
1003///     walks the table's current rows and tokenizes them fresh.
1004fn rebuild_fts_index(db: &mut Database, pager: &Pager, row: &IndexCatalogRow) -> Result<()> {
1005    use crate::sql::db::table::FtsIndexEntry;
1006    use crate::sql::executor::execute_create_index;
1007    use crate::sql::fts::PostingList;
1008    use sqlparser::ast::Statement;
1009
1010    let dialect = SqlriteDialect::new();
1011    let mut ast = Parser::parse_sql(&dialect, &row.sql).map_err(SQLRiteError::from)?;
1012    let Some(stmt @ Statement::CreateIndex(_)) = ast.pop() else {
1013        return Err(SQLRiteError::Internal(format!(
1014            "sqlrite_master FTS row's SQL isn't a CREATE INDEX: {}",
1015            row.sql
1016        )));
1017    };
1018
1019    if row.rootpage == 0 {
1020        // Compatibility path — no persisted postings; replay rows.
1021        execute_create_index(&stmt, db)?;
1022        return Ok(());
1023    }
1024
1025    let (doc_lengths, postings) = load_fts_postings(pager, row.rootpage)?;
1026    let index = PostingList::from_persisted_postings(doc_lengths, postings);
1027    let (tbl_name, col_name) = parse_fts_create_index_sql(&row.sql)?;
1028    let table_mut = db.get_table_mut(tbl_name.clone()).map_err(|_| {
1029        SQLRiteError::Internal(format!(
1030            "FTS index '{}' references unknown table '{tbl_name}'",
1031            row.name
1032        ))
1033    })?;
1034    table_mut.fts_indexes.push(FtsIndexEntry {
1035        name: row.name.clone(),
1036        column_name: col_name,
1037        index,
1038        needs_rebuild: false,
1039    });
1040    Ok(())
1041}
1042
1043/// Pulls (table_name, column_name) out of a `CREATE INDEX … USING fts(col)`
1044/// SQL string. Same shape as `parse_hnsw_create_index_sql`.
1045fn parse_fts_create_index_sql(sql: &str) -> Result<(String, String)> {
1046    use sqlparser::ast::{CreateIndex, Expr, Statement};
1047
1048    let dialect = SqlriteDialect::new();
1049    let mut ast = Parser::parse_sql(&dialect, sql).map_err(SQLRiteError::from)?;
1050    let Some(Statement::CreateIndex(CreateIndex {
1051        table_name,
1052        columns,
1053        ..
1054    })) = ast.pop()
1055    else {
1056        return Err(SQLRiteError::Internal(format!(
1057            "sqlrite_master FTS row's SQL isn't a CREATE INDEX: {sql}"
1058        )));
1059    };
1060    if columns.len() != 1 {
1061        return Err(SQLRiteError::NotImplemented(
1062            "multi-column FTS indexes aren't supported yet".to_string(),
1063        ));
1064    }
1065    let col = match &columns[0].column.expr {
1066        Expr::Identifier(ident) => ident.value.clone(),
1067        Expr::CompoundIdentifier(parts) => {
1068            parts.last().map(|p| p.value.clone()).unwrap_or_default()
1069        }
1070        other => {
1071            return Err(SQLRiteError::Internal(format!(
1072                "FTS CREATE INDEX has unexpected column expr: {other:?}"
1073            )));
1074        }
1075    };
1076    Ok((table_name.to_string(), col))
1077}
1078
1079/// Loads (or rebuilds) an HNSW index on database open. Two paths:
1080///
1081///   - **rootpage != 0** (Phase 7d.3 default): the graph is persisted
1082///     as cell-encoded pages. Read every node directly via
1083///     `load_hnsw_nodes` and reconstruct the index — fast, zero
1084///     algorithm runs, exact bit-for-bit reproduction of what was saved.
1085///
1086///   - **rootpage == 0** (compatibility): no on-disk graph, e.g. for
1087///     files saved by Phase 7d.2 before persistence landed. Replay the
1088///     CREATE INDEX SQL through `execute_create_index`, which walks the
1089///     table's current rows and populates a fresh graph. Slower but
1090///     correctness-equivalent on the first save with the new code.
1091fn rebuild_hnsw_index(db: &mut Database, pager: &Pager, row: &IndexCatalogRow) -> Result<()> {
1092    use crate::sql::db::table::HnswIndexEntry;
1093    use crate::sql::executor::execute_create_index;
1094    use crate::sql::hnsw::HnswIndex;
1095    use sqlparser::ast::Statement;
1096
1097    let dialect = SqlriteDialect::new();
1098    let mut ast = Parser::parse_sql(&dialect, &row.sql).map_err(SQLRiteError::from)?;
1099    let Some(stmt @ Statement::CreateIndex(_)) = ast.pop() else {
1100        return Err(SQLRiteError::Internal(format!(
1101            "sqlrite_master HNSW row's SQL isn't a CREATE INDEX: {}",
1102            row.sql
1103        )));
1104    };
1105
1106    if row.rootpage == 0 {
1107        // Compatibility path — no persisted graph; walk current rows.
1108        execute_create_index(&stmt, db)?;
1109        return Ok(());
1110    }
1111
1112    // Persistence path — read the cell tree, deserialize. The metric
1113    // travels through the synthesized CREATE INDEX SQL stored in
1114    // `sqlrite_master`; pre-SQLR-28 rows omit the WITH clause and
1115    // decode as L2, which matches what those graphs were built with.
1116    let (tbl_name, col_name, metric) = parse_hnsw_create_index_sql(&row.sql)?;
1117    let nodes = load_hnsw_nodes(pager, row.rootpage)?;
1118    let index = HnswIndex::from_persisted_nodes(metric, 0xC0FFEE, nodes);
1119
1120    // Parse the CREATE INDEX to know which table + column to attach to
1121    // — same shape as the row-walk path; we just don't execute it.
1122    let table_mut = db.get_table_mut(tbl_name.clone()).map_err(|_| {
1123        SQLRiteError::Internal(format!(
1124            "HNSW index '{}' references unknown table '{tbl_name}'",
1125            row.name
1126        ))
1127    })?;
1128    table_mut.hnsw_indexes.push(HnswIndexEntry {
1129        name: row.name.clone(),
1130        column_name: col_name,
1131        metric,
1132        index,
1133        needs_rebuild: false,
1134    });
1135    Ok(())
1136}
1137
1138/// Phase 7d.3 — Phase-7d.3-side helper: walk every leaf in the HNSW
1139/// page tree at `root_page` and decode each cell as a node. Returns
1140/// the (node_id, layers) tuples in slot-order (already ascending by
1141/// node_id since they were staged that way). The caller hands them to
1142/// `HnswIndex::from_persisted_nodes`.
1143fn load_hnsw_nodes(pager: &Pager, root_page: u32) -> Result<Vec<(i64, Vec<Vec<i64>>)>> {
1144    use crate::sql::pager::hnsw_cell::HnswNodeCell;
1145
1146    let mut nodes: Vec<(i64, Vec<Vec<i64>>)> = Vec::new();
1147    let first_leaf = find_leftmost_leaf(pager, root_page)?;
1148    let mut current = first_leaf;
1149    while current != 0 {
1150        let page_buf = pager
1151            .read_page(current)
1152            .ok_or_else(|| SQLRiteError::Internal(format!("missing HNSW leaf page {current}")))?;
1153        if page_buf[0] != PageType::TableLeaf as u8 {
1154            return Err(SQLRiteError::Internal(format!(
1155                "page {current} tagged {} but expected TableLeaf (HNSW)",
1156                page_buf[0]
1157            )));
1158        }
1159        let next_leaf = u32::from_le_bytes(page_buf[1..5].try_into().unwrap());
1160        let payload: &[u8; PAYLOAD_PER_PAGE] = (&page_buf[PAGE_HEADER_SIZE..])
1161            .try_into()
1162            .map_err(|_| SQLRiteError::Internal("HNSW leaf payload size".to_string()))?;
1163        let leaf = TablePage::from_bytes(payload);
1164        for slot in 0..leaf.slot_count() {
1165            let offset = leaf.slot_offset_raw(slot)?;
1166            let (cell, _) = HnswNodeCell::decode(leaf.as_bytes(), offset)?;
1167            nodes.push((cell.node_id, cell.layers));
1168        }
1169        current = next_leaf;
1170    }
1171    Ok(nodes)
1172}
1173
1174/// Pulls `(table_name, column_name, metric)` out of a CREATE INDEX
1175/// SQL string of the form `CREATE INDEX … USING hnsw (col) [WITH
1176/// (metric = '<m>')]`. Used by the persistence path on open to know
1177/// where to attach the loaded graph and which distance metric to
1178/// rebuild it under. Pre-SQLR-28 rows omit the WITH clause and
1179/// default to L2.
1180fn parse_hnsw_create_index_sql(sql: &str) -> Result<(String, String, DistanceMetric)> {
1181    use crate::sql::hnsw::DistanceMetric;
1182    use sqlparser::ast::{BinaryOperator, CreateIndex, Expr, Statement, Value as AstValue};
1183
1184    let dialect = SqlriteDialect::new();
1185    let mut ast = Parser::parse_sql(&dialect, sql).map_err(SQLRiteError::from)?;
1186    let Some(Statement::CreateIndex(CreateIndex {
1187        table_name,
1188        columns,
1189        with,
1190        ..
1191    })) = ast.pop()
1192    else {
1193        return Err(SQLRiteError::Internal(format!(
1194            "sqlrite_master HNSW row's SQL isn't a CREATE INDEX: {sql}"
1195        )));
1196    };
1197    if columns.len() != 1 {
1198        return Err(SQLRiteError::NotImplemented(
1199            "multi-column HNSW indexes aren't supported yet".to_string(),
1200        ));
1201    }
1202    let col = match &columns[0].column.expr {
1203        Expr::Identifier(ident) => ident.value.clone(),
1204        Expr::CompoundIdentifier(parts) => {
1205            parts.last().map(|p| p.value.clone()).unwrap_or_default()
1206        }
1207        other => {
1208            return Err(SQLRiteError::Internal(format!(
1209                "unsupported HNSW indexed column expression: {other:?}"
1210            )));
1211        }
1212    };
1213
1214    // Pull the metric off the parsed WITH (...) bag. The user-facing
1215    // CREATE INDEX path validates this in detail (typo'd metric names,
1216    // unknown keys, etc.); here on the persistence read-path we trust
1217    // what we previously wrote and surface a clean Internal error if
1218    // it ever doesn't match.
1219    let mut metric = DistanceMetric::L2;
1220    for opt in &with {
1221        if let Expr::BinaryOp { left, op, right } = opt {
1222            if matches!(op, BinaryOperator::Eq) {
1223                if let (Expr::Identifier(key), Expr::Value(v)) = (left.as_ref(), right.as_ref())
1224                    && key.value.eq_ignore_ascii_case("metric")
1225                {
1226                    if let AstValue::SingleQuotedString(s) | AstValue::DoubleQuotedString(s) =
1227                        &v.value
1228                    {
1229                        metric = DistanceMetric::from_sql_name(s).ok_or_else(|| {
1230                            SQLRiteError::Internal(format!(
1231                                "sqlrite_master HNSW row carries unknown metric '{s}'"
1232                            ))
1233                        })?;
1234                    }
1235                }
1236            }
1237        }
1238    }
1239
1240    Ok((table_name.to_string(), col, metric))
1241}
1242
1243/// Phase 7d.3 — rebuilds in-place any HnswIndexEntry whose
1244/// `needs_rebuild` flag is set (DELETE / UPDATE-on-vector marked it).
1245/// Walks the table's current Vec<f32> column storage and runs the
1246/// HNSW algorithm fresh. Called at the top of `save_database` before
1247/// any immutable borrows of `db` start.
1248///
1249/// Cost: O(N · ef_construction · log N) per dirty index. Fine for
1250/// small tables, expensive for ≥100k-row tables — matches the
1251/// trade-off SQLite makes for FTS5: dirtying-and-rebuilding is the
1252/// MVP, more sophisticated incremental delete strategies (soft-delete
1253/// + tombstones, neighbor reconnection) are future polish.
1254fn rebuild_dirty_hnsw_indexes(db: &mut Database) {
1255    use crate::sql::hnsw::HnswIndex;
1256
1257    for table in db.tables.values_mut() {
1258        // Snapshot which (index_name, column, metric) triples need
1259        // rebuilding, before we go grabbing column data — keeps the
1260        // borrow structure simple. The per-entry metric matters here:
1261        // rebuilding a cosine-built graph as L2 (or vice versa) would
1262        // silently corrupt the topology and break the SQLR-28 probe.
1263        let dirty: Vec<(String, String, DistanceMetric)> = table
1264            .hnsw_indexes
1265            .iter()
1266            .filter(|e| e.needs_rebuild)
1267            .map(|e| (e.name.clone(), e.column_name.clone(), e.metric))
1268            .collect();
1269        if dirty.is_empty() {
1270            continue;
1271        }
1272
1273        for (idx_name, col_name, metric) in dirty {
1274            // Snapshot every (rowid, vec) for this column.
1275            let mut vectors: Vec<(i64, Vec<f32>)> = Vec::new();
1276            {
1277                let row_data = table.rows.lock().expect("rows mutex poisoned");
1278                if let Some(Row::Vector(map)) = row_data.get(&col_name) {
1279                    for (id, v) in map.iter() {
1280                        vectors.push((*id, v.clone()));
1281                    }
1282                }
1283            }
1284            // Pre-build a HashMap for the get_vec closure so we don't
1285            // pay O(N) lookup per insert call.
1286            let snapshot: std::collections::HashMap<i64, Vec<f32>> =
1287                vectors.iter().cloned().collect();
1288
1289            let mut new_idx = HnswIndex::new(metric, 0xC0FFEE);
1290            // Sort by id so the rebuild is deterministic across runs.
1291            vectors.sort_by_key(|(id, _)| *id);
1292            for (id, v) in &vectors {
1293                new_idx.insert(*id, v, |q| snapshot.get(&q).cloned().unwrap_or_default());
1294            }
1295
1296            // Replace the entry's index + clear the dirty flag.
1297            if let Some(entry) = table.hnsw_indexes.iter_mut().find(|e| e.name == idx_name) {
1298                entry.index = new_idx;
1299                entry.needs_rebuild = false;
1300            }
1301        }
1302    }
1303}
1304
1305/// Synthesises the CREATE INDEX SQL stored back into `sqlrite_master`
1306/// for an HNSW index. The metric travels through the SQL via an
1307/// optional `WITH (metric = '<m>')` clause; L2 indexes omit the clause
1308/// for byte-identical round-trip with pre-SQLR-28 catalogs.
1309fn synthesize_hnsw_create_index_sql(
1310    index_name: &str,
1311    table_name: &str,
1312    column_name: &str,
1313    metric: DistanceMetric,
1314) -> String {
1315    if matches!(metric, DistanceMetric::L2) {
1316        format!("CREATE INDEX {index_name} ON {table_name} USING hnsw ({column_name})")
1317    } else {
1318        format!(
1319            "CREATE INDEX {index_name} ON {table_name} USING hnsw ({column_name}) WITH (metric = '{}')",
1320            metric.sql_name()
1321        )
1322    }
1323}
1324
1325/// Phase 8b — rebuild every FTS index a DELETE / UPDATE-on-text-col
1326/// marked dirty. Mirrors [`rebuild_dirty_hnsw_indexes`]; runs at save
1327/// time under `&mut Database`. Cheap on a clean DB (the `dirty` snapshot
1328/// is empty so the per-table loop short-circuits).
1329fn rebuild_dirty_fts_indexes(db: &mut Database) {
1330    use crate::sql::fts::PostingList;
1331
1332    for table in db.tables.values_mut() {
1333        let dirty: Vec<(String, String)> = table
1334            .fts_indexes
1335            .iter()
1336            .filter(|e| e.needs_rebuild)
1337            .map(|e| (e.name.clone(), e.column_name.clone()))
1338            .collect();
1339        if dirty.is_empty() {
1340            continue;
1341        }
1342
1343        for (idx_name, col_name) in dirty {
1344            // Snapshot every (rowid, text) pair for this column under
1345            // the row mutex, then drop the lock before re-tokenizing.
1346            let mut docs: Vec<(i64, String)> = Vec::new();
1347            {
1348                let row_data = table.rows.lock().expect("rows mutex poisoned");
1349                if let Some(Row::Text(map)) = row_data.get(&col_name) {
1350                    for (id, v) in map.iter() {
1351                        // "Null" sentinel is the parser's
1352                        // null-marker for TEXT cells; skip those —
1353                        // they'd round-trip as the literal string
1354                        // "Null" otherwise. Aligns with insert_row's
1355                        // typed_value gate.
1356                        if v != "Null" {
1357                            docs.push((*id, v.clone()));
1358                        }
1359                    }
1360                }
1361            }
1362
1363            let mut new_idx = PostingList::new();
1364            // Sort by id so the rebuild is deterministic across runs
1365            // (the BTreeMap inside PostingList is order-stable, but
1366            // doc-length aggregation order doesn't matter — sorting
1367            // here is purely for reproducibility on inspection).
1368            docs.sort_by_key(|(id, _)| *id);
1369            for (id, text) in &docs {
1370                new_idx.insert(*id, text);
1371            }
1372
1373            if let Some(entry) = table.fts_indexes.iter_mut().find(|e| e.name == idx_name) {
1374                entry.index = new_idx;
1375                entry.needs_rebuild = false;
1376            }
1377        }
1378    }
1379}
1380
1381/// Cheap clone helper — `DataType` doesn't derive `Clone` elsewhere.
1382fn clone_datatype(dt: &DataType) -> DataType {
1383    match dt {
1384        DataType::Integer => DataType::Integer,
1385        DataType::Text => DataType::Text,
1386        DataType::Real => DataType::Real,
1387        DataType::Bool => DataType::Bool,
1388        DataType::Vector(dim) => DataType::Vector(*dim),
1389        DataType::Json => DataType::Json,
1390        DataType::None => DataType::None,
1391        DataType::Invalid => DataType::Invalid,
1392    }
1393}
1394
1395/// Stages an index's B-Tree at `start_page`. Each leaf cell is a
1396/// `KIND_INDEX` entry carrying `(original_rowid, value)`. Returns
1397/// `(root_page, next_free_page)`.
1398///
1399/// The tree's shape matches a regular table's — leaves chained via
1400/// `next_page`, optional interior layer above. `Cell::peek_rowid` works
1401/// uniformly for index cells (same prefix as local cells), so the
1402/// existing slot directory and binary search carry over.
1403fn stage_index_btree(
1404    pager: &mut Pager,
1405    idx: &SecondaryIndex,
1406    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1407) -> Result<u32> {
1408    // Build the leaves.
1409    let leaves = stage_index_leaves(pager, idx, alloc)?;
1410    if leaves.len() == 1 {
1411        return Ok(leaves[0].0);
1412    }
1413    let mut level: Vec<(u32, i64)> = leaves;
1414    while level.len() > 1 {
1415        level = stage_interior_level(pager, &level, alloc)?;
1416    }
1417    Ok(level[0].0)
1418}
1419
1420/// Packs the index's (value, rowid) entries into a sibling-chained run
1421/// of `TableLeaf` pages. Iteration order matches `SecondaryIndex::iter_entries`
1422/// (ascending value; rowids in insertion order within a value), which is
1423/// also ascending by the "cell rowid" carried in each IndexCell (the
1424/// original row's rowid) — so Cell::peek_rowid + the slot directory's
1425/// rowid ordering stays consistent.
1426fn stage_index_leaves(
1427    pager: &mut Pager,
1428    idx: &SecondaryIndex,
1429    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1430) -> Result<Vec<(u32, i64)>> {
1431    let mut leaves: Vec<(u32, i64)> = Vec::new();
1432    let mut current_leaf = TablePage::empty();
1433    let mut current_leaf_page = alloc.allocate();
1434    let mut current_max_rowid: Option<i64> = None;
1435
1436    // Sort the entries by original rowid so the in-page slot directory,
1437    // which binary-searches by rowid, stays valid. (iter_entries orders by
1438    // value; we reorder here for B-Tree correctness.)
1439    let mut entries: Vec<(Value, i64)> = idx.iter_entries().collect();
1440    entries.sort_by_key(|(_, r)| *r);
1441
1442    for (value, rowid) in entries {
1443        let cell = IndexCell::new(rowid, value);
1444        let entry_bytes = cell.encode()?;
1445
1446        if !current_leaf.would_fit(entry_bytes.len()) {
1447            let next_leaf_page_num = alloc.allocate();
1448            emit_leaf(pager, current_leaf_page, &current_leaf, next_leaf_page_num);
1449            leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1450            current_leaf = TablePage::empty();
1451            current_leaf_page = next_leaf_page_num;
1452
1453            if !current_leaf.would_fit(entry_bytes.len()) {
1454                return Err(SQLRiteError::Internal(format!(
1455                    "index entry of {} bytes exceeds empty-page capacity {}",
1456                    entry_bytes.len(),
1457                    current_leaf.free_space()
1458                )));
1459            }
1460        }
1461        current_leaf.insert_entry(rowid, &entry_bytes)?;
1462        current_max_rowid = Some(rowid);
1463    }
1464
1465    emit_leaf(pager, current_leaf_page, &current_leaf, 0);
1466    leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1467    Ok(leaves)
1468}
1469
1470/// Phase 7d.3 — stages an HNSW index's page tree at `start_page`.
1471/// Each leaf cell is a `KIND_HNSW` entry carrying one node's
1472/// (node_id, layers). Returns `(root_page, next_free_page)`.
1473///
1474/// Tree shape is identical to `stage_index_btree` — chained leaves +
1475/// optional interior layers. The slot directory binary-searches by
1476/// node_id (which is the cell's "rowid" in `Cell::peek_rowid` terms),
1477/// so reads can locate any node in O(log N) once 7d.4-or-later
1478/// optimizes the load path to lazy-fetch instead of read-all.
1479/// Today, `load_hnsw_nodes` reads the entire tree on open.
1480fn stage_hnsw_btree(
1481    pager: &mut Pager,
1482    idx: &crate::sql::hnsw::HnswIndex,
1483    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1484) -> Result<u32> {
1485    let leaves = stage_hnsw_leaves(pager, idx, alloc)?;
1486    if leaves.len() == 1 {
1487        return Ok(leaves[0].0);
1488    }
1489    let mut level: Vec<(u32, i64)> = leaves;
1490    while level.len() > 1 {
1491        level = stage_interior_level(pager, &level, alloc)?;
1492    }
1493    Ok(level[0].0)
1494}
1495
1496/// Phase 8c — stage one FTS index as a `TableLeaf`-shaped B-Tree.
1497/// Mirrors `stage_hnsw_btree` (sibling-chained leaves, optional interior
1498/// levels). Returns `(root_page, next_free_page)`. Each leaf is filled
1499/// with `KIND_FTS_POSTING` cells: one sidecar cell holding the
1500/// doc-lengths map, then one cell per term in lexicographic order.
1501fn stage_fts_btree(
1502    pager: &mut Pager,
1503    idx: &crate::sql::fts::PostingList,
1504    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1505) -> Result<u32> {
1506    let leaves = stage_fts_leaves(pager, idx, alloc)?;
1507    if leaves.len() == 1 {
1508        return Ok(leaves[0].0);
1509    }
1510    let mut level: Vec<(u32, i64)> = leaves;
1511    while level.len() > 1 {
1512        level = stage_interior_level(pager, &level, alloc)?;
1513    }
1514    Ok(level[0].0)
1515}
1516
1517/// Packs FTS posting cells into a sibling-chained run of `TableLeaf`
1518/// pages. Cell layout: a single doc-lengths sidecar at `cell_id = 1`,
1519/// followed by one cell per term in lexicographic order with
1520/// `cell_id = 2..=N + 1`. Sequential ids keep the slot directory's
1521/// rowid ordering valid (the `cell_id` field is what `peek_rowid`
1522/// returns).
1523fn stage_fts_leaves(
1524    pager: &mut Pager,
1525    idx: &crate::sql::fts::PostingList,
1526    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1527) -> Result<Vec<(u32, i64)>> {
1528    use crate::sql::pager::fts_cell::FtsPostingCell;
1529
1530    let mut leaves: Vec<(u32, i64)> = Vec::new();
1531    let mut current_leaf = TablePage::empty();
1532    let mut current_leaf_page = alloc.allocate();
1533    let mut current_max_rowid: Option<i64> = None;
1534
1535    // Build the cell sequence: sidecar first, then per-term cells. The
1536    // sidecar always exists (even on an empty index) so reload sees a
1537    // canonical "this index was persisted" marker in slot 0.
1538    let mut cell_id: i64 = 1;
1539    let mut cells: Vec<FtsPostingCell> = Vec::new();
1540    cells.push(FtsPostingCell::doc_lengths(
1541        cell_id,
1542        idx.serialize_doc_lengths(),
1543    ));
1544    for (term, entries) in idx.serialize_postings() {
1545        cell_id += 1;
1546        cells.push(FtsPostingCell::posting(cell_id, term, entries));
1547    }
1548
1549    for cell in cells {
1550        let entry_bytes = cell.encode()?;
1551
1552        if !current_leaf.would_fit(entry_bytes.len()) {
1553            let next_leaf_page_num = alloc.allocate();
1554            emit_leaf(pager, current_leaf_page, &current_leaf, next_leaf_page_num);
1555            leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1556            current_leaf = TablePage::empty();
1557            current_leaf_page = next_leaf_page_num;
1558
1559            if !current_leaf.would_fit(entry_bytes.len()) {
1560                // A single posting cell exceeds page capacity. Phase
1561                // 8c MVP doesn't chain via overflow cells (the plan
1562                // notes this as a stretch goal); surface a clear
1563                // error so users know which term tripped it.
1564                return Err(SQLRiteError::Internal(format!(
1565                    "FTS posting cell {} of {} bytes exceeds empty-page capacity {} \
1566                     (term too long or too many postings; overflow chaining is Phase 8.1)",
1567                    cell.cell_id,
1568                    entry_bytes.len(),
1569                    current_leaf.free_space()
1570                )));
1571            }
1572        }
1573        current_leaf.insert_entry(cell.cell_id, &entry_bytes)?;
1574        current_max_rowid = Some(cell.cell_id);
1575    }
1576
1577    emit_leaf(pager, current_leaf_page, &current_leaf, 0);
1578    leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1579    Ok(leaves)
1580}
1581
1582/// (rowid, value) pairs as decoded from a single FTS cell — value is
1583/// either term frequency (posting cell) or doc length (sidecar cell).
1584type FtsEntries = Vec<(i64, u32)>;
1585/// (term, posting list) pairs as decoded from non-sidecar FTS cells.
1586type FtsPostings = Vec<(String, FtsEntries)>;
1587
1588/// Phase 8c — read every cell of an FTS index from `root_page` back
1589/// into the `(doc_lengths, postings)` shape `PostingList::from_persisted_postings`
1590/// expects. Mirrors `load_hnsw_nodes`: leftmost-leaf descent, walk the
1591/// sibling chain, decode each slot.
1592fn load_fts_postings(pager: &Pager, root_page: u32) -> Result<(FtsEntries, FtsPostings)> {
1593    use crate::sql::pager::fts_cell::FtsPostingCell;
1594
1595    let mut doc_lengths: Vec<(i64, u32)> = Vec::new();
1596    let mut postings: Vec<(String, Vec<(i64, u32)>)> = Vec::new();
1597    let mut saw_sidecar = false;
1598
1599    let first_leaf = find_leftmost_leaf(pager, root_page)?;
1600    let mut current = first_leaf;
1601    while current != 0 {
1602        let page_buf = pager
1603            .read_page(current)
1604            .ok_or_else(|| SQLRiteError::Internal(format!("missing FTS leaf page {current}")))?;
1605        if page_buf[0] != PageType::TableLeaf as u8 {
1606            return Err(SQLRiteError::Internal(format!(
1607                "page {current} tagged {} but expected TableLeaf (FTS)",
1608                page_buf[0]
1609            )));
1610        }
1611        let next_leaf = u32::from_le_bytes(page_buf[1..5].try_into().unwrap());
1612        let payload: &[u8; PAYLOAD_PER_PAGE] = (&page_buf[PAGE_HEADER_SIZE..])
1613            .try_into()
1614            .map_err(|_| SQLRiteError::Internal("FTS leaf payload size".to_string()))?;
1615        let leaf = TablePage::from_bytes(payload);
1616        for slot in 0..leaf.slot_count() {
1617            let offset = leaf.slot_offset_raw(slot)?;
1618            let (cell, _) = FtsPostingCell::decode(leaf.as_bytes(), offset)?;
1619            if cell.is_doc_lengths() {
1620                if saw_sidecar {
1621                    return Err(SQLRiteError::Internal(
1622                        "FTS index has more than one doc-lengths sidecar cell".to_string(),
1623                    ));
1624                }
1625                saw_sidecar = true;
1626                doc_lengths = cell.entries;
1627            } else {
1628                postings.push((cell.term, cell.entries));
1629            }
1630        }
1631        current = next_leaf;
1632    }
1633
1634    if !saw_sidecar {
1635        return Err(SQLRiteError::Internal(
1636            "FTS index missing doc-lengths sidecar cell — corrupt or truncated tree".to_string(),
1637        ));
1638    }
1639    Ok((doc_lengths, postings))
1640}
1641
1642/// Packs HNSW nodes into a sibling-chained run of `TableLeaf` pages.
1643/// `serialize_nodes` already returns nodes in ascending node_id order,
1644/// so the slot directory's rowid ordering stays valid.
1645fn stage_hnsw_leaves(
1646    pager: &mut Pager,
1647    idx: &crate::sql::hnsw::HnswIndex,
1648    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1649) -> Result<Vec<(u32, i64)>> {
1650    use crate::sql::pager::hnsw_cell::HnswNodeCell;
1651
1652    let mut leaves: Vec<(u32, i64)> = Vec::new();
1653    let mut current_leaf = TablePage::empty();
1654    let mut current_leaf_page = alloc.allocate();
1655    let mut current_max_rowid: Option<i64> = None;
1656
1657    let serialized = idx.serialize_nodes();
1658
1659    // Empty index → emit a single empty leaf page so the rootpage
1660    // pointer in sqlrite_master stays nonzero (== "graph is persisted,
1661    // it just happens to be empty"). load_hnsw_nodes is fine with an
1662    // empty leaf — slot_count() returns 0.
1663    for (node_id, layers) in serialized {
1664        let cell = HnswNodeCell::new(node_id, layers);
1665        let entry_bytes = cell.encode()?;
1666
1667        if !current_leaf.would_fit(entry_bytes.len()) {
1668            let next_leaf_page_num = alloc.allocate();
1669            emit_leaf(pager, current_leaf_page, &current_leaf, next_leaf_page_num);
1670            leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1671            current_leaf = TablePage::empty();
1672            current_leaf_page = next_leaf_page_num;
1673
1674            if !current_leaf.would_fit(entry_bytes.len()) {
1675                return Err(SQLRiteError::Internal(format!(
1676                    "HNSW node {node_id} cell of {} bytes exceeds empty-page capacity {}",
1677                    entry_bytes.len(),
1678                    current_leaf.free_space()
1679                )));
1680            }
1681        }
1682        current_leaf.insert_entry(node_id, &entry_bytes)?;
1683        current_max_rowid = Some(node_id);
1684    }
1685
1686    emit_leaf(pager, current_leaf_page, &current_leaf, 0);
1687    leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1688    Ok(leaves)
1689}
1690
1691fn load_table_rows(pager: &Pager, table: &mut Table, root_page: u32) -> Result<()> {
1692    let first_leaf = find_leftmost_leaf(pager, root_page)?;
1693    let mut current = first_leaf;
1694    while current != 0 {
1695        let page_buf = pager
1696            .read_page(current)
1697            .ok_or_else(|| SQLRiteError::Internal(format!("missing leaf page {current}")))?;
1698        if page_buf[0] != PageType::TableLeaf as u8 {
1699            return Err(SQLRiteError::Internal(format!(
1700                "page {current} tagged {} but expected TableLeaf",
1701                page_buf[0]
1702            )));
1703        }
1704        let next_leaf = u32::from_le_bytes(page_buf[1..5].try_into().unwrap());
1705        let payload: &[u8; PAYLOAD_PER_PAGE] = (&page_buf[PAGE_HEADER_SIZE..])
1706            .try_into()
1707            .map_err(|_| SQLRiteError::Internal("leaf payload slice size".to_string()))?;
1708        let leaf = TablePage::from_bytes(payload);
1709
1710        for slot in 0..leaf.slot_count() {
1711            let entry = leaf.entry_at(slot)?;
1712            let cell = match entry {
1713                PagedEntry::Local(c) => c,
1714                PagedEntry::Overflow(r) => {
1715                    let body_bytes =
1716                        read_overflow_chain(pager, r.first_overflow_page, r.total_body_len)?;
1717                    let (c, _) = Cell::decode(&body_bytes, 0)?;
1718                    c
1719                }
1720            };
1721            table.restore_row(cell.rowid, cell.values)?;
1722        }
1723        current = next_leaf;
1724    }
1725    Ok(())
1726}
1727
1728/// Walks every page reachable from `root_page` and returns their page
1729/// numbers. Includes `root_page`, every interior page, every leaf, and
1730/// — when `follow_overflow` is true — every overflow page chained off
1731/// table-leaf cells. Used by `save_database` to seed each table's
1732/// per-table preferred pool and to compute the newly-freed set.
1733///
1734/// `follow_overflow = true` for table B-Trees (cells may carry
1735/// `OverflowRef`s pointing at chained overflow pages); `false` for
1736/// secondary-index, HNSW, and FTS B-Trees, which never overflow in the
1737/// current encoding.
1738fn collect_pages_for_btree(
1739    pager: &Pager,
1740    root_page: u32,
1741    follow_overflow: bool,
1742) -> Result<Vec<u32>> {
1743    if root_page == 0 {
1744        return Ok(Vec::new());
1745    }
1746    let mut pages: Vec<u32> = Vec::new();
1747    let mut stack: Vec<u32> = vec![root_page];
1748
1749    while let Some(p) = stack.pop() {
1750        let buf = pager.read_page(p).ok_or_else(|| {
1751            SQLRiteError::Internal(format!(
1752                "collect_pages: missing page {p} (rooted at {root_page})"
1753            ))
1754        })?;
1755        pages.push(p);
1756        match buf[0] {
1757            t if t == PageType::InteriorNode as u8 => {
1758                let payload: &[u8; PAYLOAD_PER_PAGE] =
1759                    (&buf[PAGE_HEADER_SIZE..]).try_into().map_err(|_| {
1760                        SQLRiteError::Internal("interior payload slice size".to_string())
1761                    })?;
1762                let interior = InteriorPage::from_bytes(payload);
1763                // Push every divider's child + the rightmost child.
1764                for slot in 0..interior.slot_count() {
1765                    let cell = interior.cell_at(slot)?;
1766                    stack.push(cell.child_page);
1767                }
1768                stack.push(interior.rightmost_child());
1769            }
1770            t if t == PageType::TableLeaf as u8 => {
1771                if follow_overflow {
1772                    let payload: &[u8; PAYLOAD_PER_PAGE] =
1773                        (&buf[PAGE_HEADER_SIZE..]).try_into().map_err(|_| {
1774                            SQLRiteError::Internal("leaf payload slice size".to_string())
1775                        })?;
1776                    let leaf = TablePage::from_bytes(payload);
1777                    for slot in 0..leaf.slot_count() {
1778                        match leaf.entry_at(slot)? {
1779                            PagedEntry::Local(_) => {}
1780                            PagedEntry::Overflow(r) => {
1781                                let mut cur = r.first_overflow_page;
1782                                while cur != 0 {
1783                                    pages.push(cur);
1784                                    let ob = pager.read_page(cur).ok_or_else(|| {
1785                                        SQLRiteError::Internal(format!(
1786                                            "collect_pages: missing overflow page {cur}"
1787                                        ))
1788                                    })?;
1789                                    if ob[0] != PageType::Overflow as u8 {
1790                                        return Err(SQLRiteError::Internal(format!(
1791                                            "collect_pages: page {cur} expected Overflow, got tag {}",
1792                                            ob[0]
1793                                        )));
1794                                    }
1795                                    cur = u32::from_le_bytes(ob[1..5].try_into().unwrap());
1796                                }
1797                            }
1798                        }
1799                    }
1800                }
1801            }
1802            other => {
1803                return Err(SQLRiteError::Internal(format!(
1804                    "collect_pages: unexpected page type {other} at page {p}"
1805                )));
1806            }
1807        }
1808    }
1809    Ok(pages)
1810}
1811
1812/// Reads the previously-persisted `sqlrite_master` and returns a map from
1813/// `(kind, name)` to that object's rootpage. Used by `save_database` to
1814/// seed each table/index's per-table preferred pool with the pages it
1815/// occupied last time round.
1816///
1817/// `kind` is `"table"` or `"index"` (the catalog already disambiguates
1818/// the three index families via the SQL string, but for page-collection
1819/// purposes a "table" tree must follow overflow refs while an "index"
1820/// tree never does — that's the only distinction we need here).
1821fn read_old_rootpages(pager: &Pager, schema_root: u32) -> Result<HashMap<(String, String), u32>> {
1822    let mut out: HashMap<(String, String), u32> = HashMap::new();
1823    if schema_root == 0 {
1824        return Ok(out);
1825    }
1826    let mut master = build_empty_master_table();
1827    load_table_rows(pager, &mut master, schema_root)?;
1828    for rowid in master.rowids() {
1829        let kind = take_text(&master, "type", rowid)?;
1830        let name = take_text(&master, "name", rowid)?;
1831        let rootpage = take_integer(&master, "rootpage", rowid)? as u32;
1832        out.insert((kind, name), rootpage);
1833    }
1834    Ok(out)
1835}
1836
1837/// Descends from `root_page` through `InteriorNode` pages, always taking
1838/// the leftmost child, until a `TableLeaf` is reached. Returns that leaf's
1839/// page number. A root that's already a leaf is returned as-is.
1840fn find_leftmost_leaf(pager: &Pager, root_page: u32) -> Result<u32> {
1841    let mut current = root_page;
1842    loop {
1843        let page_buf = pager.read_page(current).ok_or_else(|| {
1844            SQLRiteError::Internal(format!("missing page {current} during tree descent"))
1845        })?;
1846        match page_buf[0] {
1847            t if t == PageType::TableLeaf as u8 => return Ok(current),
1848            t if t == PageType::InteriorNode as u8 => {
1849                let payload: &[u8; PAYLOAD_PER_PAGE] =
1850                    (&page_buf[PAGE_HEADER_SIZE..]).try_into().map_err(|_| {
1851                        SQLRiteError::Internal("interior payload slice size".to_string())
1852                    })?;
1853                let interior = InteriorPage::from_bytes(payload);
1854                current = interior.leftmost_child()?;
1855            }
1856            other => {
1857                return Err(SQLRiteError::Internal(format!(
1858                    "unexpected page type {other} during tree descent at page {current}"
1859                )));
1860            }
1861        }
1862    }
1863}
1864
1865/// Stages a table's B-Tree, drawing every page number from `alloc`.
1866/// Returns the root page (the topmost interior page, or the single leaf
1867/// when the table fits in one page).
1868///
1869/// Builds bottom-up: pack rows into `TableLeaf` pages chained via
1870/// `next_page`, then if more than one leaf, recursively wrap them in
1871/// `InteriorNode` levels until one root remains.
1872///
1873/// Deterministic: same rows + same allocator handouts → byte-identical
1874/// pages at the same numbers, so the diff pager skips unchanged tables.
1875fn stage_table_btree(
1876    pager: &mut Pager,
1877    table: &Table,
1878    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1879) -> Result<u32> {
1880    let leaves = stage_leaves(pager, table, alloc)?;
1881    if leaves.len() == 1 {
1882        return Ok(leaves[0].0);
1883    }
1884    let mut level: Vec<(u32, i64)> = leaves;
1885    while level.len() > 1 {
1886        level = stage_interior_level(pager, &level, alloc)?;
1887    }
1888    Ok(level[0].0)
1889}
1890
1891/// Packs the table's rows into a sibling-linked chain of `TableLeaf` pages.
1892/// Returns each leaf's `(page_number, max_rowid)` for use by the next
1893/// interior level. Allocates leaf and overflow pages from `alloc`.
1894fn stage_leaves(
1895    pager: &mut Pager,
1896    table: &Table,
1897    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1898) -> Result<Vec<(u32, i64)>> {
1899    let mut leaves: Vec<(u32, i64)> = Vec::new();
1900    let mut current_leaf = TablePage::empty();
1901    let mut current_leaf_page = alloc.allocate();
1902    let mut current_max_rowid: Option<i64> = None;
1903
1904    for rowid in table.rowids() {
1905        let entry_bytes = build_row_entry(pager, table, rowid, alloc)?;
1906
1907        if !current_leaf.would_fit(entry_bytes.len()) {
1908            // The new leaf goes at whatever the allocator hands out
1909            // next. Commit the current leaf with that as its sibling
1910            // pointer.
1911            let next_leaf_page_num = alloc.allocate();
1912            emit_leaf(pager, current_leaf_page, &current_leaf, next_leaf_page_num);
1913            leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1914            current_leaf = TablePage::empty();
1915            current_leaf_page = next_leaf_page_num;
1916            // current_max_rowid is reassigned by the insert below; no need
1917            // to zero it out here.
1918
1919            if !current_leaf.would_fit(entry_bytes.len()) {
1920                return Err(SQLRiteError::Internal(format!(
1921                    "entry of {} bytes exceeds empty-page capacity {}",
1922                    entry_bytes.len(),
1923                    current_leaf.free_space()
1924                )));
1925            }
1926        }
1927        current_leaf.insert_entry(rowid, &entry_bytes)?;
1928        current_max_rowid = Some(rowid);
1929    }
1930
1931    // Final leaf: sibling next_page = 0 (end of chain).
1932    emit_leaf(pager, current_leaf_page, &current_leaf, 0);
1933    leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1934    Ok(leaves)
1935}
1936
1937/// Encodes a single row's on-leaf entry — either the local cell bytes, or
1938/// an `OverflowRef` pointing at a freshly-allocated overflow chain if the
1939/// encoded cell exceeded the inline threshold. Allocates any overflow
1940/// pages from `alloc`.
1941fn build_row_entry(
1942    pager: &mut Pager,
1943    table: &Table,
1944    rowid: i64,
1945    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1946) -> Result<Vec<u8>> {
1947    let values = table.extract_row(rowid);
1948    let local_cell = Cell::new(rowid, values);
1949    let local_bytes = local_cell.encode()?;
1950    if local_bytes.len() > OVERFLOW_THRESHOLD {
1951        let overflow_start = write_overflow_chain(pager, &local_bytes, alloc)?;
1952        Ok(OverflowRef {
1953            rowid,
1954            total_body_len: local_bytes.len() as u64,
1955            first_overflow_page: overflow_start,
1956        }
1957        .encode())
1958    } else {
1959        Ok(local_bytes)
1960    }
1961}
1962
1963/// Builds one level of `InteriorNode` pages above the given children.
1964/// Each interior packs as many dividers as will fit; the last child
1965/// assigned to an interior becomes its `rightmost_child`. Returns the
1966/// emitted interior pages as `(page_number, max_rowid_in_subtree)`.
1967fn stage_interior_level(
1968    pager: &mut Pager,
1969    children: &[(u32, i64)],
1970    alloc: &mut crate::sql::pager::allocator::PageAllocator,
1971) -> Result<Vec<(u32, i64)>> {
1972    let mut next_level: Vec<(u32, i64)> = Vec::new();
1973    let mut idx = 0usize;
1974
1975    while idx < children.len() {
1976        let interior_page_num = alloc.allocate();
1977
1978        // Seed the interior with the first unassigned child as its
1979        // rightmost. As we add more children, the previous rightmost
1980        // graduates to being a divider and the new arrival takes over
1981        // as rightmost.
1982        let (mut rightmost_child_page, mut rightmost_child_max) = children[idx];
1983        idx += 1;
1984        let mut interior = InteriorPage::empty(rightmost_child_page);
1985
1986        while idx < children.len() {
1987            let new_divider_cell = InteriorCell {
1988                divider_rowid: rightmost_child_max,
1989                child_page: rightmost_child_page,
1990            };
1991            let new_divider_bytes = new_divider_cell.encode();
1992            if !interior.would_fit(new_divider_bytes.len()) {
1993                break;
1994            }
1995            interior.insert_divider(rightmost_child_max, rightmost_child_page)?;
1996            let (next_child_page, next_child_max) = children[idx];
1997            interior.set_rightmost_child(next_child_page);
1998            rightmost_child_page = next_child_page;
1999            rightmost_child_max = next_child_max;
2000            idx += 1;
2001        }
2002
2003        emit_interior(pager, interior_page_num, &interior);
2004        next_level.push((interior_page_num, rightmost_child_max));
2005    }
2006
2007    Ok(next_level)
2008}
2009
2010/// Wraps a `TablePage` in the 7-byte page header and hands it to the pager.
2011fn emit_leaf(pager: &mut Pager, page_num: u32, leaf: &TablePage, next_leaf: u32) {
2012    let mut buf = [0u8; PAGE_SIZE];
2013    buf[0] = PageType::TableLeaf as u8;
2014    buf[1..5].copy_from_slice(&next_leaf.to_le_bytes());
2015    // For leaf pages the legacy `payload_len` field isn't used — the slot
2016    // directory self-describes. Zero it by convention.
2017    buf[5..7].copy_from_slice(&0u16.to_le_bytes());
2018    buf[PAGE_HEADER_SIZE..].copy_from_slice(leaf.as_bytes());
2019    pager.stage_page(page_num, buf);
2020}
2021
2022/// Wraps an `InteriorPage` in the 7-byte page header. Interior pages
2023/// don't use `next_page` (there's no sibling chain between interiors);
2024/// `payload_len` is also unused (the slot directory self-describes).
2025fn emit_interior(pager: &mut Pager, page_num: u32, interior: &InteriorPage) {
2026    let mut buf = [0u8; PAGE_SIZE];
2027    buf[0] = PageType::InteriorNode as u8;
2028    buf[1..5].copy_from_slice(&0u32.to_le_bytes());
2029    buf[5..7].copy_from_slice(&0u16.to_le_bytes());
2030    buf[PAGE_HEADER_SIZE..].copy_from_slice(interior.as_bytes());
2031    pager.stage_page(page_num, buf);
2032}
2033
2034#[cfg(test)]
2035mod tests {
2036    use super::*;
2037    use crate::sql::pager::freelist::MIN_PAGES_FOR_AUTO_VACUUM;
2038    use crate::sql::process_command;
2039
2040    fn seed_db() -> Database {
2041        let mut db = Database::new("test".to_string());
2042        process_command(
2043            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, age INTEGER);",
2044            &mut db,
2045        )
2046        .unwrap();
2047        process_command(
2048            "INSERT INTO users (name, age) VALUES ('alice', 30);",
2049            &mut db,
2050        )
2051        .unwrap();
2052        process_command("INSERT INTO users (name, age) VALUES ('bob', 25);", &mut db).unwrap();
2053        process_command(
2054            "CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT);",
2055            &mut db,
2056        )
2057        .unwrap();
2058        process_command("INSERT INTO notes (body) VALUES ('hello');", &mut db).unwrap();
2059        db
2060    }
2061
2062    fn tmp_path(name: &str) -> std::path::PathBuf {
2063        let mut p = std::env::temp_dir();
2064        let pid = std::process::id();
2065        let nanos = std::time::SystemTime::now()
2066            .duration_since(std::time::UNIX_EPOCH)
2067            .map(|d| d.as_nanos())
2068            .unwrap_or(0);
2069        p.push(format!("sqlrite-{pid}-{nanos}-{name}.sqlrite"));
2070        p
2071    }
2072
2073    /// Phase 4c: every .sqlrite has a `-wal` sidecar now. Delete both so
2074    /// `/tmp` doesn't accumulate orphan WALs across test runs.
2075    fn cleanup(path: &std::path::Path) {
2076        let _ = std::fs::remove_file(path);
2077        let mut wal = path.as_os_str().to_owned();
2078        wal.push("-wal");
2079        let _ = std::fs::remove_file(std::path::PathBuf::from(wal));
2080    }
2081
2082    #[test]
2083    fn round_trip_preserves_schema_and_data() {
2084        let path = tmp_path("roundtrip");
2085        let mut db = seed_db();
2086        save_database(&mut db, &path).expect("save");
2087
2088        let loaded = open_database(&path, "test".to_string()).expect("open");
2089        assert_eq!(loaded.tables.len(), 2);
2090
2091        let users = loaded.get_table("users".to_string()).expect("users table");
2092        assert_eq!(users.columns.len(), 3);
2093        let rowids = users.rowids();
2094        assert_eq!(rowids.len(), 2);
2095        let names: Vec<String> = rowids
2096            .iter()
2097            .filter_map(|r| match users.get_value("name", *r) {
2098                Some(Value::Text(s)) => Some(s),
2099                _ => None,
2100            })
2101            .collect();
2102        assert!(names.contains(&"alice".to_string()));
2103        assert!(names.contains(&"bob".to_string()));
2104
2105        let notes = loaded.get_table("notes".to_string()).expect("notes table");
2106        assert_eq!(notes.rowids().len(), 1);
2107
2108        cleanup(&path);
2109    }
2110
2111    // -----------------------------------------------------------------
2112    // Phase 7a — VECTOR(N) save / reopen round-trip
2113    // -----------------------------------------------------------------
2114
2115    #[test]
2116    fn round_trip_preserves_vector_column() {
2117        let path = tmp_path("vec_roundtrip");
2118
2119        // Build, populate, save.
2120        {
2121            let mut db = Database::new("test".to_string());
2122            process_command(
2123                "CREATE TABLE docs (id INTEGER PRIMARY KEY, embedding VECTOR(3));",
2124                &mut db,
2125            )
2126            .unwrap();
2127            process_command(
2128                "INSERT INTO docs (embedding) VALUES ([0.1, 0.2, 0.3]);",
2129                &mut db,
2130            )
2131            .unwrap();
2132            process_command(
2133                "INSERT INTO docs (embedding) VALUES ([1.5, -2.0, 3.5]);",
2134                &mut db,
2135            )
2136            .unwrap();
2137            save_database(&mut db, &path).expect("save");
2138        } // db drops → its exclusive lock releases before reopen.
2139
2140        // Reopen and verify schema + data both round-tripped.
2141        let loaded = open_database(&path, "test".to_string()).expect("open");
2142        let docs = loaded.get_table("docs".to_string()).expect("docs table");
2143
2144        // Schema preserved: column is still VECTOR(3).
2145        let embedding_col = docs
2146            .columns
2147            .iter()
2148            .find(|c| c.column_name == "embedding")
2149            .expect("embedding column");
2150        assert!(
2151            matches!(embedding_col.datatype, DataType::Vector(3)),
2152            "expected DataType::Vector(3) after round-trip, got {:?}",
2153            embedding_col.datatype
2154        );
2155
2156        // Data preserved: both vectors still readable bit-for-bit.
2157        let mut rows: Vec<Vec<f32>> = docs
2158            .rowids()
2159            .iter()
2160            .filter_map(|r| match docs.get_value("embedding", *r) {
2161                Some(Value::Vector(v)) => Some(v),
2162                _ => None,
2163            })
2164            .collect();
2165        rows.sort_by(|a, b| a[0].partial_cmp(&b[0]).unwrap());
2166        assert_eq!(rows.len(), 2);
2167        assert_eq!(rows[0], vec![0.1f32, 0.2, 0.3]);
2168        assert_eq!(rows[1], vec![1.5f32, -2.0, 3.5]);
2169
2170        cleanup(&path);
2171    }
2172
2173    #[test]
2174    fn round_trip_preserves_json_column() {
2175        // Phase 7e — JSON columns are stored as Text under the hood with
2176        // INSERT-time validation. Save + reopen should preserve the
2177        // schema (DataType::Json) and the underlying text bytes; a
2178        // post-reopen json_extract should still resolve paths correctly.
2179        let path = tmp_path("json_roundtrip");
2180
2181        {
2182            let mut db = Database::new("test".to_string());
2183            process_command(
2184                "CREATE TABLE docs (id INTEGER PRIMARY KEY, payload JSON);",
2185                &mut db,
2186            )
2187            .unwrap();
2188            process_command(
2189                r#"INSERT INTO docs (payload) VALUES ('{"name": "alice", "tags": ["rust","sql"]}');"#,
2190                &mut db,
2191            )
2192            .unwrap();
2193            save_database(&mut db, &path).expect("save");
2194        }
2195
2196        let mut loaded = open_database(&path, "test".to_string()).expect("open");
2197        let docs = loaded.get_table("docs".to_string()).expect("docs");
2198
2199        // Schema: column declared as JSON, restored with the same type.
2200        let payload_col = docs
2201            .columns
2202            .iter()
2203            .find(|c| c.column_name == "payload")
2204            .unwrap();
2205        assert!(
2206            matches!(payload_col.datatype, DataType::Json),
2207            "expected DataType::Json, got {:?}",
2208            payload_col.datatype
2209        );
2210
2211        // json_extract works against the reopened data — exercises the
2212        // full Text-storage + serde_json::from_str path post-reopen.
2213        let resp = process_command(
2214            r#"SELECT id FROM docs WHERE json_extract(payload, '$.name') = 'alice';"#,
2215            &mut loaded,
2216        )
2217        .expect("select via json_extract after reopen");
2218        assert!(resp.contains("1 row returned"), "got: {resp}");
2219
2220        cleanup(&path);
2221    }
2222
2223    #[test]
2224    fn round_trip_rebuilds_hnsw_index_from_create_sql() {
2225        // Phase 7d.3: HNSW indexes now persist their graph as cell-encoded
2226        // pages. After save+reopen the index entry reattaches with the
2227        // same column + same node count, loaded directly from disk
2228        // instead of re-walking rows.
2229        let path = tmp_path("hnsw_roundtrip");
2230
2231        // Build, populate, index, save.
2232        {
2233            let mut db = Database::new("test".to_string());
2234            process_command(
2235                "CREATE TABLE docs (id INTEGER PRIMARY KEY, e VECTOR(2));",
2236                &mut db,
2237            )
2238            .unwrap();
2239            for v in &[
2240                "[1.0, 0.0]",
2241                "[2.0, 0.0]",
2242                "[0.0, 3.0]",
2243                "[1.0, 4.0]",
2244                "[10.0, 10.0]",
2245            ] {
2246                process_command(&format!("INSERT INTO docs (e) VALUES ({v});"), &mut db).unwrap();
2247            }
2248            process_command("CREATE INDEX ix_e ON docs USING hnsw (e);", &mut db).unwrap();
2249            save_database(&mut db, &path).expect("save");
2250        } // db drops → exclusive lock releases.
2251
2252        // Reopen and verify the index reattached, with the same name +
2253        // column + populated graph.
2254        let mut loaded = open_database(&path, "test".to_string()).expect("open");
2255        {
2256            let table = loaded.get_table("docs".to_string()).expect("docs");
2257            assert_eq!(table.hnsw_indexes.len(), 1, "HNSW index should reattach");
2258            let entry = &table.hnsw_indexes[0];
2259            assert_eq!(entry.name, "ix_e");
2260            assert_eq!(entry.column_name, "e");
2261            assert_eq!(entry.index.len(), 5, "loaded graph should hold all 5 rows");
2262            assert!(
2263                !entry.needs_rebuild,
2264                "fresh load should not be marked dirty"
2265            );
2266        }
2267
2268        // Quick functional check: KNN query through the loaded index
2269        // returns results.
2270        let resp = process_command(
2271            "SELECT id FROM docs ORDER BY vec_distance_l2(e, [1.0, 0.0]) ASC LIMIT 3;",
2272            &mut loaded,
2273        )
2274        .unwrap();
2275        assert!(resp.contains("3 rows returned"), "got: {resp}");
2276
2277        cleanup(&path);
2278    }
2279
2280    /// SQLR-28 — the HNSW metric must round-trip across save+reopen.
2281    /// Without this, the SQL re-synthesised into `sqlrite_master`
2282    /// would drop the metric and a cosine-built graph would reload
2283    /// as L2, silently breaking subsequent cosine probes.
2284    #[test]
2285    fn round_trip_preserves_hnsw_cosine_metric() {
2286        use crate::sql::hnsw::DistanceMetric;
2287        let path = tmp_path("hnsw_metric_roundtrip");
2288
2289        {
2290            let mut db = Database::new("test".to_string());
2291            process_command(
2292                "CREATE TABLE docs (id INTEGER PRIMARY KEY, e VECTOR(2));",
2293                &mut db,
2294            )
2295            .unwrap();
2296            for v in &["[1.0, 0.0]", "[0.0, 1.0]", "[0.7071, 0.7071]"] {
2297                process_command(&format!("INSERT INTO docs (e) VALUES ({v});"), &mut db).unwrap();
2298            }
2299            process_command(
2300                "CREATE INDEX ix_cos ON docs USING hnsw (e) WITH (metric = 'cosine');",
2301                &mut db,
2302            )
2303            .unwrap();
2304            save_database(&mut db, &path).expect("save");
2305        }
2306
2307        let mut loaded = open_database(&path, "test".to_string()).expect("open");
2308        {
2309            let table = loaded.get_table("docs".to_string()).expect("docs");
2310            assert_eq!(table.hnsw_indexes.len(), 1);
2311            assert_eq!(
2312                table.hnsw_indexes[0].metric,
2313                DistanceMetric::Cosine,
2314                "metric should round-trip through CREATE INDEX SQL"
2315            );
2316            assert_eq!(table.hnsw_indexes[0].index.distance, DistanceMetric::Cosine);
2317        }
2318
2319        // Cosine probe still finds the self-vector after reopen — the
2320        // optimizer's metric gate should match the loaded entry's
2321        // metric, so this should hit the graph shortcut.
2322        let resp = process_command(
2323            "SELECT id FROM docs ORDER BY vec_distance_cosine(e, [1.0, 0.0]) ASC LIMIT 1;",
2324            &mut loaded,
2325        )
2326        .unwrap();
2327        assert!(resp.contains("1 row returned"), "got: {resp}");
2328
2329        cleanup(&path);
2330    }
2331
2332    #[test]
2333    fn round_trip_rebuilds_fts_index_from_create_sql() {
2334        // Phase 8c: FTS indexes now persist their posting lists as
2335        // cell-encoded pages. After save+reopen the index entry
2336        // reattaches with the same column + same posting count, loaded
2337        // directly from disk (no re-tokenization).
2338        let path = tmp_path("fts_roundtrip");
2339
2340        {
2341            let mut db = Database::new("test".to_string());
2342            process_command(
2343                "CREATE TABLE docs (id INTEGER PRIMARY KEY, body TEXT);",
2344                &mut db,
2345            )
2346            .unwrap();
2347            for body in &[
2348                "rust embedded database",
2349                "rust web framework",
2350                "go embedded systems",
2351                "python web framework",
2352                "rust rust embedded power",
2353            ] {
2354                process_command(
2355                    &format!("INSERT INTO docs (body) VALUES ('{body}');"),
2356                    &mut db,
2357                )
2358                .unwrap();
2359            }
2360            process_command("CREATE INDEX ix_body ON docs USING fts (body);", &mut db).unwrap();
2361            save_database(&mut db, &path).expect("save");
2362        } // db drops → exclusive lock releases.
2363
2364        let mut loaded = open_database(&path, "test".to_string()).expect("open");
2365        {
2366            let table = loaded.get_table("docs".to_string()).expect("docs");
2367            assert_eq!(table.fts_indexes.len(), 1, "FTS index should reattach");
2368            let entry = &table.fts_indexes[0];
2369            assert_eq!(entry.name, "ix_body");
2370            assert_eq!(entry.column_name, "body");
2371            assert_eq!(
2372                entry.index.len(),
2373                5,
2374                "rebuilt posting list should hold all 5 rows"
2375            );
2376            assert!(!entry.needs_rebuild);
2377        }
2378
2379        // Functional smoke: an FTS query through the reloaded index
2380        // returns the expected hit count.
2381        let resp = process_command(
2382            "SELECT id FROM docs WHERE fts_match(body, 'rust');",
2383            &mut loaded,
2384        )
2385        .unwrap();
2386        assert!(resp.contains("3 rows returned"), "got: {resp}");
2387
2388        cleanup(&path);
2389    }
2390
2391    #[test]
2392    fn delete_then_save_then_reopen_excludes_deleted_node_from_fts() {
2393        // Phase 8b — DELETE marks the FTS index dirty; save rebuilds it
2394        // from current rows; reopen replays the CREATE INDEX SQL against
2395        // the post-delete row set. The deleted rowid must not surface
2396        // in `fts_match` results post-reopen.
2397        let path = tmp_path("fts_delete_rebuild");
2398        let mut db = Database::new("test".to_string());
2399        process_command(
2400            "CREATE TABLE docs (id INTEGER PRIMARY KEY, body TEXT);",
2401            &mut db,
2402        )
2403        .unwrap();
2404        for body in &[
2405            "rust embedded",
2406            "rust framework",
2407            "go embedded",
2408            "python web",
2409        ] {
2410            process_command(
2411                &format!("INSERT INTO docs (body) VALUES ('{body}');"),
2412                &mut db,
2413            )
2414            .unwrap();
2415        }
2416        process_command("CREATE INDEX ix_body ON docs USING fts (body);", &mut db).unwrap();
2417
2418        // Delete row 1 ('rust embedded'); save (rebuild fires); reopen.
2419        process_command("DELETE FROM docs WHERE id = 1;", &mut db).unwrap();
2420        save_database(&mut db, &path).expect("save");
2421        drop(db);
2422
2423        let mut loaded = open_database(&path, "test".to_string()).expect("open");
2424        let resp = process_command(
2425            "SELECT id FROM docs WHERE fts_match(body, 'rust');",
2426            &mut loaded,
2427        )
2428        .unwrap();
2429        // Pre-delete: 2 rows ('rust embedded', 'rust framework') had
2430        // 'rust'. Post-delete: only id=2 remains.
2431        assert!(resp.contains("1 row returned"), "got: {resp}");
2432
2433        cleanup(&path);
2434    }
2435
2436    #[test]
2437    fn fts_roundtrip_uses_persistence_path_not_replay() {
2438        // Phase 8c — assert the reload didn't go through the
2439        // rootpage=0 replay shortcut. We do this by reading the
2440        // sqlrite_master row for the FTS index and confirming its
2441        // rootpage field is non-zero.
2442        let path = tmp_path("fts_persistence_path");
2443
2444        {
2445            let mut db = Database::new("test".to_string());
2446            process_command(
2447                "CREATE TABLE docs (id INTEGER PRIMARY KEY, body TEXT);",
2448                &mut db,
2449            )
2450            .unwrap();
2451            process_command(
2452                "INSERT INTO docs (body) VALUES ('rust embedded database');",
2453                &mut db,
2454            )
2455            .unwrap();
2456            process_command("CREATE INDEX ix_body ON docs USING fts (body);", &mut db).unwrap();
2457            save_database(&mut db, &path).expect("save");
2458        }
2459
2460        // Read raw sqlrite_master to find the FTS index row.
2461        let pager = Pager::open(&path).expect("open pager");
2462        let mut master = build_empty_master_table();
2463        load_table_rows(&pager, &mut master, pager.header().schema_root_page).unwrap();
2464        let mut found_rootpage: Option<u32> = None;
2465        for rowid in master.rowids() {
2466            let name = take_text(&master, "name", rowid).unwrap();
2467            if name == "ix_body" {
2468                let rp = take_integer(&master, "rootpage", rowid).unwrap();
2469                found_rootpage = Some(rp as u32);
2470            }
2471        }
2472        let rootpage = found_rootpage.expect("ix_body row in sqlrite_master");
2473        assert!(
2474            rootpage != 0,
2475            "Phase 8c FTS save should set rootpage != 0; got {rootpage}"
2476        );
2477
2478        cleanup(&path);
2479    }
2480
2481    #[test]
2482    fn save_without_fts_keeps_format_v4() {
2483        // Phase 8c on-demand bump — a database with zero FTS indexes
2484        // continues writing the v4 header. Existing v4 users must not
2485        // see their files silently promoted to v5 by an upgrade.
2486        use crate::sql::pager::header::FORMAT_VERSION_V4;
2487
2488        let path = tmp_path("fts_no_bump");
2489        let mut db = Database::new("test".to_string());
2490        process_command(
2491            "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER);",
2492            &mut db,
2493        )
2494        .unwrap();
2495        process_command("INSERT INTO t (n) VALUES (1);", &mut db).unwrap();
2496        save_database(&mut db, &path).unwrap();
2497        drop(db);
2498
2499        let pager = Pager::open(&path).expect("open");
2500        assert_eq!(
2501            pager.header().format_version,
2502            FORMAT_VERSION_V4,
2503            "no-FTS save should keep v4"
2504        );
2505        cleanup(&path);
2506    }
2507
2508    #[test]
2509    fn save_with_fts_bumps_to_v5() {
2510        // Phase 8c on-demand bump — first FTS-bearing save promotes
2511        // the file to v5. v5 readers handle both v4 and v5; v4
2512        // readers correctly refuse a v5 file.
2513        use crate::sql::pager::header::FORMAT_VERSION_V5;
2514
2515        let path = tmp_path("fts_bump_v5");
2516        let mut db = Database::new("test".to_string());
2517        process_command(
2518            "CREATE TABLE docs (id INTEGER PRIMARY KEY, body TEXT);",
2519            &mut db,
2520        )
2521        .unwrap();
2522        process_command("INSERT INTO docs (body) VALUES ('hello');", &mut db).unwrap();
2523        process_command("CREATE INDEX ix_body ON docs USING fts (body);", &mut db).unwrap();
2524        save_database(&mut db, &path).unwrap();
2525        drop(db);
2526
2527        let pager = Pager::open(&path).expect("open");
2528        assert_eq!(
2529            pager.header().format_version,
2530            FORMAT_VERSION_V5,
2531            "FTS save should promote to v5"
2532        );
2533        cleanup(&path);
2534    }
2535
2536    #[test]
2537    fn fts_persistence_handles_empty_and_zero_token_docs() {
2538        // Phase 8c — sidecar cell carries doc-lengths for every doc
2539        // including any with zero tokens (so total_docs is honest
2540        // post-reopen). Empty index also round-trips: a CREATE INDEX
2541        // on an empty table emits a single empty leaf with just the
2542        // (empty) sidecar.
2543        let path = tmp_path("fts_edges");
2544
2545        {
2546            let mut db = Database::new("test".to_string());
2547            process_command(
2548                "CREATE TABLE docs (id INTEGER PRIMARY KEY, body TEXT);",
2549                &mut db,
2550            )
2551            .unwrap();
2552            process_command("CREATE INDEX ix_body ON docs USING fts (body);", &mut db).unwrap();
2553            // Mix: real text, then a row that tokenizes to zero tokens
2554            // (only punctuation), then real again.
2555            process_command("INSERT INTO docs (body) VALUES ('rust embedded');", &mut db).unwrap();
2556            process_command("INSERT INTO docs (body) VALUES ('!!!---???');", &mut db).unwrap();
2557            process_command("INSERT INTO docs (body) VALUES ('go embedded');", &mut db).unwrap();
2558            save_database(&mut db, &path).unwrap();
2559        }
2560
2561        let loaded = open_database(&path, "test".to_string()).expect("open");
2562        let table = loaded.get_table("docs".to_string()).unwrap();
2563        let entry = &table.fts_indexes[0];
2564        // All three rows present — including the zero-token row,
2565        // which is critical for total_docs honesty in BM25.
2566        assert_eq!(entry.index.len(), 3);
2567        // 'embedded' appears in 2 rows after reload.
2568        let res = entry
2569            .index
2570            .query("embedded", &crate::sql::fts::Bm25Params::default());
2571        assert_eq!(res.len(), 2);
2572
2573        cleanup(&path);
2574    }
2575
2576    #[test]
2577    fn fts_persistence_round_trips_large_corpus() {
2578        // Phase 8c — exercise multi-leaf staging. ~500 docs with
2579        // single-token bodies generates enough cells to overflow a
2580        // single 4 KiB leaf (each posting cell averages ~8 bytes).
2581        let path = tmp_path("fts_large_corpus");
2582
2583        let mut expected_terms: std::collections::BTreeSet<String> =
2584            std::collections::BTreeSet::new();
2585        {
2586            let mut db = Database::new("test".to_string());
2587            process_command(
2588                "CREATE TABLE docs (id INTEGER PRIMARY KEY, body TEXT);",
2589                &mut db,
2590            )
2591            .unwrap();
2592            process_command("CREATE INDEX ix_body ON docs USING fts (body);", &mut db).unwrap();
2593            // 500 docs, each one a unique term — drives unique-term
2594            // count up so multiple leaves are required.
2595            for i in 0..500 {
2596                let term = format!("term{i:04}");
2597                process_command(
2598                    &format!("INSERT INTO docs (body) VALUES ('{term}');"),
2599                    &mut db,
2600                )
2601                .unwrap();
2602                expected_terms.insert(term);
2603            }
2604            save_database(&mut db, &path).unwrap();
2605        }
2606
2607        let loaded = open_database(&path, "test".to_string()).expect("open");
2608        let table = loaded.get_table("docs".to_string()).unwrap();
2609        let entry = &table.fts_indexes[0];
2610        assert_eq!(entry.index.len(), 500);
2611
2612        // Spot-check a handful of terms come back with their original
2613        // single-row posting list.
2614        for &i in &[0_i64, 137, 248, 391, 499] {
2615            let term = format!("term{i:04}");
2616            let res = entry
2617                .index
2618                .query(&term, &crate::sql::fts::Bm25Params::default());
2619            assert_eq!(res.len(), 1, "term {term} should match exactly 1 row");
2620            // PrimaryKey rowids start at 1; doc i was inserted at
2621            // rowid i+1.
2622            assert_eq!(res[0].0, i + 1);
2623        }
2624
2625        cleanup(&path);
2626    }
2627
2628    #[test]
2629    fn delete_then_save_then_reopen_excludes_deleted_node_from_hnsw() {
2630        // Phase 7d.3 — DELETE marks HNSW dirty; save rebuilds it from
2631        // current rows + serializes; reopen loads the post-delete graph.
2632        // After all that, the deleted rowid must NOT come back from a
2633        // KNN query.
2634        let path = tmp_path("hnsw_delete_rebuild");
2635        let mut db = Database::new("test".to_string());
2636        process_command(
2637            "CREATE TABLE docs (id INTEGER PRIMARY KEY, e VECTOR(2));",
2638            &mut db,
2639        )
2640        .unwrap();
2641        for v in &["[1.0, 0.0]", "[2.0, 0.0]", "[3.0, 0.0]", "[4.0, 0.0]"] {
2642            process_command(&format!("INSERT INTO docs (e) VALUES ({v});"), &mut db).unwrap();
2643        }
2644        process_command("CREATE INDEX ix_e ON docs USING hnsw (e);", &mut db).unwrap();
2645
2646        // Delete row 1 (the closest match to [0.5, 0.0]).
2647        process_command("DELETE FROM docs WHERE id = 1;", &mut db).unwrap();
2648        // Confirm it marked dirty.
2649        let dirty_before_save = db.tables["docs"].hnsw_indexes[0].needs_rebuild;
2650        assert!(dirty_before_save, "DELETE should mark dirty");
2651
2652        save_database(&mut db, &path).expect("save");
2653        // Confirm save cleared the dirty flag.
2654        let dirty_after_save = db.tables["docs"].hnsw_indexes[0].needs_rebuild;
2655        assert!(!dirty_after_save, "save should clear dirty");
2656        drop(db);
2657
2658        // Reopen, query for the closest match. Row 1 is gone; row 2
2659        // (id=2, vector [2.0, 0.0]) should now be the nearest.
2660        let loaded = open_database(&path, "test".to_string()).expect("open");
2661        let docs = loaded.get_table("docs".to_string()).expect("docs");
2662
2663        // Row 1 must not appear in any storage anymore.
2664        assert!(
2665            !docs.rowids().contains(&1),
2666            "deleted row 1 should not be in row storage"
2667        );
2668        assert_eq!(docs.rowids().len(), 3, "should have 3 surviving rows");
2669
2670        // The HNSW index must also have shed the deleted node.
2671        assert_eq!(
2672            docs.hnsw_indexes[0].index.len(),
2673            3,
2674            "HNSW graph should have shed the deleted node"
2675        );
2676
2677        cleanup(&path);
2678    }
2679
2680    #[test]
2681    fn round_trip_survives_writes_after_load() {
2682        let path = tmp_path("after_load");
2683        save_database(&mut seed_db(), &path).unwrap();
2684
2685        {
2686            let mut db = open_database(&path, "test".to_string()).unwrap();
2687            process_command(
2688                "INSERT INTO users (name, age) VALUES ('carol', 40);",
2689                &mut db,
2690            )
2691            .unwrap();
2692            save_database(&mut db, &path).unwrap();
2693        } // db drops → its exclusive lock releases before we reopen below.
2694
2695        let db2 = open_database(&path, "test".to_string()).unwrap();
2696        let users = db2.get_table("users".to_string()).unwrap();
2697        assert_eq!(users.rowids().len(), 3);
2698
2699        cleanup(&path);
2700    }
2701
2702    #[test]
2703    fn open_rejects_garbage_file() {
2704        let path = tmp_path("bad");
2705        std::fs::write(&path, b"not a sqlrite database, just bytes").unwrap();
2706        let result = open_database(&path, "x".to_string());
2707        assert!(result.is_err());
2708        cleanup(&path);
2709    }
2710
2711    #[test]
2712    fn many_small_rows_spread_across_leaves() {
2713        let path = tmp_path("many_rows");
2714        let mut db = Database::new("big".to_string());
2715        process_command(
2716            "CREATE TABLE things (id INTEGER PRIMARY KEY, data TEXT);",
2717            &mut db,
2718        )
2719        .unwrap();
2720        for i in 0..200 {
2721            let body = "x".repeat(200);
2722            let q = format!("INSERT INTO things (data) VALUES ('row-{i}-{body}');");
2723            process_command(&q, &mut db).unwrap();
2724        }
2725        save_database(&mut db, &path).unwrap();
2726        let loaded = open_database(&path, "big".to_string()).unwrap();
2727        let things = loaded.get_table("things".to_string()).unwrap();
2728        assert_eq!(things.rowids().len(), 200);
2729        cleanup(&path);
2730    }
2731
2732    #[test]
2733    fn huge_row_goes_through_overflow() {
2734        let path = tmp_path("overflow_row");
2735        let mut db = Database::new("big".to_string());
2736        process_command(
2737            "CREATE TABLE docs (id INTEGER PRIMARY KEY, body TEXT);",
2738            &mut db,
2739        )
2740        .unwrap();
2741        let body = "A".repeat(10_000);
2742        process_command(
2743            &format!("INSERT INTO docs (body) VALUES ('{body}');"),
2744            &mut db,
2745        )
2746        .unwrap();
2747        save_database(&mut db, &path).unwrap();
2748
2749        let loaded = open_database(&path, "big".to_string()).unwrap();
2750        let docs = loaded.get_table("docs".to_string()).unwrap();
2751        let rowids = docs.rowids();
2752        assert_eq!(rowids.len(), 1);
2753        let stored = docs.get_value("body", rowids[0]);
2754        match stored {
2755            Some(Value::Text(s)) => assert_eq!(s.len(), 10_000),
2756            other => panic!("expected Text, got {other:?}"),
2757        }
2758        cleanup(&path);
2759    }
2760
2761    #[test]
2762    fn create_sql_synthesis_round_trips() {
2763        // Build a table via CREATE, then verify table_to_create_sql +
2764        // parse_create_sql reproduce an equivalent column list.
2765        let mut db = Database::new("x".to_string());
2766        process_command(
2767            "CREATE TABLE t (id INTEGER PRIMARY KEY, tag TEXT UNIQUE, note TEXT NOT NULL);",
2768            &mut db,
2769        )
2770        .unwrap();
2771        let t = db.get_table("t".to_string()).unwrap();
2772        let sql = table_to_create_sql(t);
2773        let (name, cols) = parse_create_sql(&sql).unwrap();
2774        assert_eq!(name, "t");
2775        assert_eq!(cols.len(), 3);
2776        assert!(cols[0].is_pk);
2777        assert!(cols[1].is_unique);
2778        assert!(cols[2].not_null);
2779    }
2780
2781    #[test]
2782    fn sqlrite_master_is_not_exposed_as_a_user_table() {
2783        // After open, the public db.tables map should not list the master.
2784        let path = tmp_path("no_master");
2785        save_database(&mut seed_db(), &path).unwrap();
2786        let loaded = open_database(&path, "x".to_string()).unwrap();
2787        assert!(!loaded.tables.contains_key(MASTER_TABLE_NAME));
2788        cleanup(&path);
2789    }
2790
2791    #[test]
2792    fn multi_leaf_table_produces_an_interior_root() {
2793        // 200 fat rows force the table into multiple leaves, which means
2794        // save_database must build at least one InteriorNode above them.
2795        // The test verifies the round-trip works and confirms the root is
2796        // indeed an interior page (not a leaf) by reading the page type
2797        // directly out of the open pager.
2798        let path = tmp_path("multi_leaf_interior");
2799        let mut db = Database::new("big".to_string());
2800        process_command(
2801            "CREATE TABLE things (id INTEGER PRIMARY KEY, data TEXT);",
2802            &mut db,
2803        )
2804        .unwrap();
2805        for i in 0..200 {
2806            let body = "x".repeat(200);
2807            let q = format!("INSERT INTO things (data) VALUES ('row-{i}-{body}');");
2808            process_command(&q, &mut db).unwrap();
2809        }
2810        save_database(&mut db, &path).unwrap();
2811
2812        // Confirm the round-trip preserved all 200 rows.
2813        let loaded = open_database(&path, "big".to_string()).unwrap();
2814        let things = loaded.get_table("things".to_string()).unwrap();
2815        assert_eq!(things.rowids().len(), 200);
2816
2817        // Peek at `things`'s root page via the pager attached to the
2818        // loaded DB and check it's an InteriorNode, not a leaf.
2819        let pager = loaded
2820            .pager
2821            .as_ref()
2822            .expect("loaded DB should have a pager");
2823        // sqlrite_master's row for `things` holds its root page. Easiest
2824        // way to find it: walk the leaf chain by using find_leftmost_leaf
2825        // and then hop one level up. Simpler: read the master, scan for
2826        // the "things" row, look up rootpage.
2827        let mut master = build_empty_master_table();
2828        load_table_rows(pager, &mut master, pager.header().schema_root_page).unwrap();
2829        let things_root = master
2830            .rowids()
2831            .into_iter()
2832            .find_map(|r| match master.get_value("name", r) {
2833                Some(Value::Text(s)) if s == "things" => match master.get_value("rootpage", r) {
2834                    Some(Value::Integer(p)) => Some(p as u32),
2835                    _ => None,
2836                },
2837                _ => None,
2838            })
2839            .expect("things should appear in sqlrite_master");
2840        let root_buf = pager.read_page(things_root).unwrap();
2841        assert_eq!(
2842            root_buf[0],
2843            PageType::InteriorNode as u8,
2844            "expected a multi-leaf table to have an interior root, got tag {}",
2845            root_buf[0]
2846        );
2847
2848        cleanup(&path);
2849    }
2850
2851    #[test]
2852    fn explicit_index_persists_across_save_and_open() {
2853        let path = tmp_path("idx_persist");
2854        let mut db = Database::new("idx".to_string());
2855        process_command(
2856            "CREATE TABLE users (id INTEGER PRIMARY KEY, tag TEXT);",
2857            &mut db,
2858        )
2859        .unwrap();
2860        for i in 1..=5 {
2861            let tag = if i % 2 == 0 { "odd" } else { "even" };
2862            process_command(
2863                &format!("INSERT INTO users (tag) VALUES ('{tag}');"),
2864                &mut db,
2865            )
2866            .unwrap();
2867        }
2868        process_command("CREATE INDEX users_tag_idx ON users (tag);", &mut db).unwrap();
2869        save_database(&mut db, &path).unwrap();
2870
2871        let loaded = open_database(&path, "idx".to_string()).unwrap();
2872        let users = loaded.get_table("users".to_string()).unwrap();
2873        let idx = users
2874            .index_by_name("users_tag_idx")
2875            .expect("explicit index should survive save/open");
2876        assert_eq!(idx.column_name, "tag");
2877        assert!(!idx.is_unique);
2878        // 5 rows: rowids 2, 4 are "odd" (i % 2 == 0 when i is 2 or 4) — 2 entries;
2879        // rowids 1, 3, 5 are "even" (i % 2 != 0) — 3 entries.
2880        let even_rowids = idx.lookup(&Value::Text("even".into()));
2881        let odd_rowids = idx.lookup(&Value::Text("odd".into()));
2882        assert_eq!(even_rowids.len(), 3);
2883        assert_eq!(odd_rowids.len(), 2);
2884
2885        cleanup(&path);
2886    }
2887
2888    #[test]
2889    fn auto_indexes_for_unique_columns_survive_save_open() {
2890        let path = tmp_path("auto_idx_persist");
2891        let mut db = Database::new("a".to_string());
2892        process_command(
2893            "CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE);",
2894            &mut db,
2895        )
2896        .unwrap();
2897        process_command("INSERT INTO users (email) VALUES ('a@x');", &mut db).unwrap();
2898        process_command("INSERT INTO users (email) VALUES ('b@x');", &mut db).unwrap();
2899        save_database(&mut db, &path).unwrap();
2900
2901        let loaded = open_database(&path, "a".to_string()).unwrap();
2902        let users = loaded.get_table("users".to_string()).unwrap();
2903        // Every UNIQUE column auto-creates an index; the load path populated
2904        // it from the persisted entries.
2905        let auto_name = SecondaryIndex::auto_name("users", "email");
2906        let idx = users
2907            .index_by_name(&auto_name)
2908            .expect("auto index should be restored");
2909        assert!(idx.is_unique);
2910        assert_eq!(idx.lookup(&Value::Text("a@x".into())).len(), 1);
2911        assert_eq!(idx.lookup(&Value::Text("b@x".into())).len(), 1);
2912
2913        cleanup(&path);
2914    }
2915
2916    /// SQLR-1 — `CREATE INDEX` on a wide table must round-trip when the
2917    /// index B-tree grows past one leaf and needs an interior level.
2918    /// Before the fix, the post-DDL auto-save panicked with
2919    /// `Internal("unknown paged-entry kind tag 0x4 …")` because a
2920    /// table-cell decoder was being run against an index leaf
2921    /// (`KIND_INDEX = 0x04`).
2922    ///
2923    /// 5 000 rows mirror the original repro from the issue and exceed
2924    /// every leaf-fanout cliff for the small `(rowid, value)` cells in
2925    /// a TEXT-keyed secondary index.
2926    #[test]
2927    fn secondary_index_with_interior_level_round_trips() {
2928        let path = tmp_path("sqlr1_wide_index");
2929        let mut db = Database::new("idx".to_string());
2930        db.source_path = Some(path.clone());
2931
2932        process_command(
2933            "CREATE TABLE bloat (id INTEGER PRIMARY KEY, payload TEXT);",
2934            &mut db,
2935        )
2936        .unwrap();
2937        // BEGIN/COMMIT collapses 5 000 inserts into one save (matches
2938        // `auto_vacuum_setup` and the issue's repro shape).
2939        process_command("BEGIN;", &mut db).unwrap();
2940        for i in 0..5000 {
2941            process_command(
2942                &format!("INSERT INTO bloat (payload) VALUES ('p-{i:08}');"),
2943                &mut db,
2944            )
2945            .unwrap();
2946        }
2947        process_command("COMMIT;", &mut db).unwrap();
2948
2949        // The DDL that used to panic.
2950        process_command("CREATE INDEX idx_p ON bloat (payload);", &mut db).unwrap();
2951
2952        // Reopen and verify lookups, plus that the index tree actually
2953        // grew an interior layer (otherwise this test wouldn't cover the
2954        // regression).
2955        drop(db);
2956        let loaded = open_database(&path, "idx".to_string()).unwrap();
2957        let bloat = loaded.get_table("bloat".to_string()).unwrap();
2958        let idx = bloat
2959            .index_by_name("idx_p")
2960            .expect("idx_p should survive close/reopen");
2961        assert!(!idx.is_unique);
2962
2963        // Spot-check the keyspace: first, middle, last value each map
2964        // back to exactly the row that carried them.
2965        for &(probe_i, expected_rowid) in &[(0i64, 1i64), (2500, 2501), (4999, 5000)] {
2966            let value = Value::Text(format!("p-{probe_i:08}"));
2967            let hits = idx.lookup(&value);
2968            assert_eq!(
2969                hits,
2970                vec![expected_rowid],
2971                "lookup({value:?}) should yield rowid {expected_rowid}",
2972            );
2973        }
2974
2975        // Confirm the index tree is multi-level (the regression's
2976        // necessary condition) — root must be an `InteriorNode` and
2977        // `find_leftmost_leaf` must reach a `TableLeaf` through it.
2978        let pager = loaded.pager.as_ref().unwrap();
2979        let mut master = build_empty_master_table();
2980        load_table_rows(pager, &mut master, pager.header().schema_root_page).unwrap();
2981        let idx_root = master
2982            .rowids()
2983            .into_iter()
2984            .find_map(
2985                |r| match (master.get_value("name", r), master.get_value("type", r)) {
2986                    (Some(Value::Text(name)), Some(Value::Text(kind)))
2987                        if name == "idx_p" && kind == "index" =>
2988                    {
2989                        match master.get_value("rootpage", r) {
2990                            Some(Value::Integer(p)) => Some(p as u32),
2991                            _ => None,
2992                        }
2993                    }
2994                    _ => None,
2995                },
2996            )
2997            .expect("idx_p should appear in sqlrite_master");
2998        let root_buf = pager.read_page(idx_root).unwrap();
2999        assert_eq!(
3000            root_buf[0],
3001            PageType::InteriorNode as u8,
3002            "5 000-entry index must have an interior root — without one this test wouldn't cover SQLR-1",
3003        );
3004        let leaf = find_leftmost_leaf(pager, idx_root).unwrap();
3005        let leaf_buf = pager.read_page(leaf).unwrap();
3006        assert_eq!(leaf_buf[0], PageType::TableLeaf as u8);
3007
3008        cleanup(&path);
3009    }
3010
3011    /// SQLR-1 follow-on — the page-recycling path between two large
3012    /// versions of the same index name must not corrupt cell decoding.
3013    /// `DROP INDEX` returns its pages to the freelist; the next
3014    /// `CREATE INDEX` is free to reuse them. If the allocator hands an
3015    /// old index leaf to a *table* without zeroing it, an upstream
3016    /// table walk would see KIND_INDEX cells and panic.
3017    #[test]
3018    fn drop_then_recreate_wide_index_does_not_panic() {
3019        let path = tmp_path("sqlr1_drop_recreate");
3020        let mut db = Database::new("idx".to_string());
3021        db.source_path = Some(path.clone());
3022
3023        process_command(
3024            "CREATE TABLE bloat (id INTEGER PRIMARY KEY, payload TEXT);",
3025            &mut db,
3026        )
3027        .unwrap();
3028        process_command("BEGIN;", &mut db).unwrap();
3029        for i in 0..5000 {
3030            process_command(
3031                &format!("INSERT INTO bloat (payload) VALUES ('p-{i:08}');"),
3032                &mut db,
3033            )
3034            .unwrap();
3035        }
3036        process_command("COMMIT;", &mut db).unwrap();
3037
3038        process_command("CREATE INDEX idx_p ON bloat (payload);", &mut db).unwrap();
3039        process_command("DROP INDEX idx_p;", &mut db).unwrap();
3040        // Recreate from scratch — exercises the recycle path.
3041        process_command("CREATE INDEX idx_p ON bloat (payload);", &mut db).unwrap();
3042
3043        drop(db);
3044        let loaded = open_database(&path, "idx".to_string()).unwrap();
3045        let bloat = loaded.get_table("bloat".to_string()).unwrap();
3046        let idx = bloat
3047            .index_by_name("idx_p")
3048            .expect("idx_p should survive drop+recreate+reopen");
3049        assert_eq!(
3050            idx.lookup(&Value::Text("p-00002500".into())),
3051            vec![2501],
3052            "post-recycle lookup must still resolve correctly",
3053        );
3054
3055        cleanup(&path);
3056    }
3057
3058    #[test]
3059    fn deep_tree_round_trips() {
3060        // Force a 3-level tree by bypassing process_command (which prints
3061        // the full table on every INSERT, making large bulk loads O(N^2)
3062        // in I/O). We build the Table directly via restore_row.
3063        use crate::sql::db::table::Column as TableColumn;
3064
3065        let path = tmp_path("deep_tree");
3066        let mut db = Database::new("deep".to_string());
3067        let columns = vec![
3068            TableColumn::new("id".into(), "integer".into(), true, true, true),
3069            TableColumn::new("s".into(), "text".into(), false, true, false),
3070        ];
3071        let mut table = build_empty_table("t", columns, 0);
3072        // ~900-byte rows → ~4 rows per leaf. 6000 rows → ~1500 leaves,
3073        // which with interior fanout ~400 needs 2 interior levels (3-level
3074        // tree total, counting leaves).
3075        for i in 1..=6_000i64 {
3076            let body = "q".repeat(900);
3077            table
3078                .restore_row(
3079                    i,
3080                    vec![
3081                        Some(Value::Integer(i)),
3082                        Some(Value::Text(format!("r-{i}-{body}"))),
3083                    ],
3084                )
3085                .unwrap();
3086        }
3087        db.tables.insert("t".to_string(), table);
3088        save_database(&mut db, &path).unwrap();
3089
3090        let loaded = open_database(&path, "deep".to_string()).unwrap();
3091        let t = loaded.get_table("t".to_string()).unwrap();
3092        assert_eq!(t.rowids().len(), 6_000);
3093
3094        // Confirm the tree actually grew past 2 levels — i.e., the root's
3095        // leftmost child is itself an interior page, not a leaf.
3096        let pager = loaded.pager.as_ref().unwrap();
3097        let mut master = build_empty_master_table();
3098        load_table_rows(pager, &mut master, pager.header().schema_root_page).unwrap();
3099        let t_root = master
3100            .rowids()
3101            .into_iter()
3102            .find_map(|r| match master.get_value("name", r) {
3103                Some(Value::Text(s)) if s == "t" => match master.get_value("rootpage", r) {
3104                    Some(Value::Integer(p)) => Some(p as u32),
3105                    _ => None,
3106                },
3107                _ => None,
3108            })
3109            .expect("t in sqlrite_master");
3110        let root_buf = pager.read_page(t_root).unwrap();
3111        assert_eq!(root_buf[0], PageType::InteriorNode as u8);
3112        let root_payload: &[u8; PAYLOAD_PER_PAGE] =
3113            (&root_buf[PAGE_HEADER_SIZE..]).try_into().unwrap();
3114        let root_interior = InteriorPage::from_bytes(root_payload);
3115        let child = root_interior.leftmost_child().unwrap();
3116        let child_buf = pager.read_page(child).unwrap();
3117        assert_eq!(
3118            child_buf[0],
3119            PageType::InteriorNode as u8,
3120            "expected 3-level tree: root's leftmost child should also be InteriorNode",
3121        );
3122
3123        cleanup(&path);
3124    }
3125
3126    #[test]
3127    fn alter_rename_table_survives_save_and_reopen() {
3128        let path = tmp_path("alter_rename_table_roundtrip");
3129        let mut db = seed_db();
3130        save_database(&mut db, &path).expect("save");
3131
3132        process_command("ALTER TABLE users RENAME TO members;", &mut db).expect("rename");
3133        save_database(&mut db, &path).expect("save after rename");
3134
3135        let loaded = open_database(&path, "t".to_string()).expect("reopen");
3136        assert!(!loaded.contains_table("users".to_string()));
3137        assert!(loaded.contains_table("members".to_string()));
3138        let members = loaded.get_table("members".to_string()).unwrap();
3139        assert_eq!(members.rowids().len(), 2, "rows should survive");
3140        // Auto-indexes followed the rename.
3141        assert!(
3142            members
3143                .index_by_name("sqlrite_autoindex_members_id")
3144                .is_some()
3145        );
3146        assert!(
3147            members
3148                .index_by_name("sqlrite_autoindex_members_name")
3149                .is_some()
3150        );
3151
3152        cleanup(&path);
3153    }
3154
3155    #[test]
3156    fn alter_rename_column_survives_save_and_reopen() {
3157        let path = tmp_path("alter_rename_col_roundtrip");
3158        let mut db = seed_db();
3159        save_database(&mut db, &path).expect("save");
3160
3161        process_command(
3162            "ALTER TABLE users RENAME COLUMN name TO full_name;",
3163            &mut db,
3164        )
3165        .expect("rename column");
3166        save_database(&mut db, &path).expect("save after rename");
3167
3168        let loaded = open_database(&path, "t".to_string()).expect("reopen");
3169        let users = loaded.get_table("users".to_string()).unwrap();
3170        assert!(users.contains_column("full_name".to_string()));
3171        assert!(!users.contains_column("name".to_string()));
3172        // Verify a row's value survived the rename round-trip.
3173        let alice_rowid = users
3174            .rowids()
3175            .into_iter()
3176            .find(|r| users.get_value("full_name", *r) == Some(Value::Text("alice".to_string())))
3177            .expect("alice row should be findable under renamed column");
3178        assert_eq!(
3179            users.get_value("full_name", alice_rowid),
3180            Some(Value::Text("alice".to_string()))
3181        );
3182
3183        cleanup(&path);
3184    }
3185
3186    #[test]
3187    fn alter_add_column_with_default_survives_save_and_reopen() {
3188        let path = tmp_path("alter_add_default_roundtrip");
3189        let mut db = seed_db();
3190        save_database(&mut db, &path).expect("save");
3191
3192        process_command(
3193            "ALTER TABLE users ADD COLUMN status TEXT DEFAULT 'active';",
3194            &mut db,
3195        )
3196        .expect("add column");
3197        save_database(&mut db, &path).expect("save after add");
3198
3199        let loaded = open_database(&path, "t".to_string()).expect("reopen");
3200        let users = loaded.get_table("users".to_string()).unwrap();
3201        assert!(users.contains_column("status".to_string()));
3202        for rowid in users.rowids() {
3203            assert_eq!(
3204                users.get_value("status", rowid),
3205                Some(Value::Text("active".to_string())),
3206                "backfilled default should round-trip for rowid {rowid}"
3207            );
3208        }
3209        // The DEFAULT clause itself should still be on the column metadata
3210        // so a subsequent INSERT picks it up.
3211        let status_col = users
3212            .columns
3213            .iter()
3214            .find(|c| c.column_name == "status")
3215            .unwrap();
3216        assert_eq!(status_col.default, Some(Value::Text("active".to_string())));
3217
3218        cleanup(&path);
3219    }
3220
3221    #[test]
3222    fn alter_drop_column_survives_save_and_reopen() {
3223        let path = tmp_path("alter_drop_col_roundtrip");
3224        let mut db = seed_db();
3225        save_database(&mut db, &path).expect("save");
3226
3227        process_command("ALTER TABLE users DROP COLUMN age;", &mut db).expect("drop column");
3228        save_database(&mut db, &path).expect("save after drop");
3229
3230        let loaded = open_database(&path, "t".to_string()).expect("reopen");
3231        let users = loaded.get_table("users".to_string()).unwrap();
3232        assert!(!users.contains_column("age".to_string()));
3233        assert!(users.contains_column("name".to_string()));
3234
3235        cleanup(&path);
3236    }
3237
3238    #[test]
3239    fn drop_table_survives_save_and_reopen() {
3240        let path = tmp_path("drop_table_roundtrip");
3241        let mut db = seed_db();
3242        save_database(&mut db, &path).expect("save");
3243
3244        // Verify both tables landed.
3245        {
3246            let loaded = open_database(&path, "t".to_string()).expect("open");
3247            assert!(loaded.contains_table("users".to_string()));
3248            assert!(loaded.contains_table("notes".to_string()));
3249        }
3250
3251        process_command("DROP TABLE users;", &mut db).expect("drop users");
3252        save_database(&mut db, &path).expect("save after drop");
3253
3254        let loaded = open_database(&path, "t".to_string()).expect("reopen");
3255        assert!(
3256            !loaded.contains_table("users".to_string()),
3257            "dropped table should not resurface on reopen"
3258        );
3259        assert!(
3260            loaded.contains_table("notes".to_string()),
3261            "untouched table should survive"
3262        );
3263
3264        cleanup(&path);
3265    }
3266
3267    #[test]
3268    fn drop_index_survives_save_and_reopen() {
3269        let path = tmp_path("drop_index_roundtrip");
3270        let mut db = Database::new("t".to_string());
3271        process_command(
3272            "CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT);",
3273            &mut db,
3274        )
3275        .unwrap();
3276        process_command("CREATE INDEX notes_body_idx ON notes (body);", &mut db).unwrap();
3277        save_database(&mut db, &path).expect("save");
3278
3279        process_command("DROP INDEX notes_body_idx;", &mut db).unwrap();
3280        save_database(&mut db, &path).expect("save after drop");
3281
3282        let loaded = open_database(&path, "t".to_string()).expect("reopen");
3283        let notes = loaded.get_table("notes".to_string()).unwrap();
3284        assert!(
3285            notes.index_by_name("notes_body_idx").is_none(),
3286            "dropped index should not resurface on reopen"
3287        );
3288        // The auto-index for the PK should still be there.
3289        assert!(notes.index_by_name("sqlrite_autoindex_notes_id").is_some());
3290
3291        cleanup(&path);
3292    }
3293
3294    #[test]
3295    fn default_clause_survives_save_and_reopen() {
3296        let path = tmp_path("default_roundtrip");
3297        let mut db = Database::new("t".to_string());
3298
3299        process_command(
3300            "CREATE TABLE users (id INTEGER PRIMARY KEY, status TEXT DEFAULT 'active', score INTEGER DEFAULT 0);",
3301            &mut db,
3302        )
3303        .unwrap();
3304        save_database(&mut db, &path).expect("save");
3305
3306        let mut loaded = open_database(&path, "t".to_string()).expect("open");
3307
3308        // The reloaded column metadata should still carry the DEFAULT.
3309        let users = loaded.get_table("users".to_string()).expect("users table");
3310        let status_col = users
3311            .columns
3312            .iter()
3313            .find(|c| c.column_name == "status")
3314            .expect("status column");
3315        assert_eq!(
3316            status_col.default,
3317            Some(Value::Text("active".to_string())),
3318            "DEFAULT 'active' should round-trip"
3319        );
3320        let score_col = users
3321            .columns
3322            .iter()
3323            .find(|c| c.column_name == "score")
3324            .expect("score column");
3325        assert_eq!(
3326            score_col.default,
3327            Some(Value::Integer(0)),
3328            "DEFAULT 0 should round-trip"
3329        );
3330
3331        // Now exercise the runtime path: an INSERT that omits both DEFAULT
3332        // columns should pick them up from the reloaded schema.
3333        process_command("INSERT INTO users (id) VALUES (1);", &mut loaded).unwrap();
3334        let users = loaded.get_table("users".to_string()).unwrap();
3335        assert_eq!(
3336            users.get_value("status", 1),
3337            Some(Value::Text("active".to_string()))
3338        );
3339        assert_eq!(users.get_value("score", 1), Some(Value::Integer(0)));
3340
3341        cleanup(&path);
3342    }
3343
3344    // ---------------------------------------------------------------------
3345    // SQLR-6 — free-list + VACUUM tests
3346    // ---------------------------------------------------------------------
3347
3348    /// Drop a table; subsequent CREATE TABLE should reuse the freed pages
3349    /// rather than extending the file. The page_count after drop+create
3350    /// should be at most what it was after the original two tables —
3351    /// proving the new table landed on freelist pages.
3352    #[test]
3353    fn drop_table_freelist_persists_pages_for_reuse() {
3354        let path = tmp_path("freelist_reuse");
3355        let mut db = seed_db();
3356        db.source_path = Some(path.clone());
3357        save_database(&mut db, &path).expect("save");
3358        let pages_two_tables = db.pager.as_ref().unwrap().header().page_count;
3359
3360        // Drop one table; its pages go on the freelist.
3361        process_command("DROP TABLE users;", &mut db).expect("drop users");
3362        let pages_after_drop = db.pager.as_ref().unwrap().header().page_count;
3363        assert_eq!(
3364            pages_after_drop, pages_two_tables,
3365            "page_count should not shrink on drop — the freed pages persist on the freelist"
3366        );
3367        let head_after_drop = db.pager.as_ref().unwrap().header().freelist_head;
3368        assert!(
3369            head_after_drop != 0,
3370            "freelist_head must be non-zero after drop"
3371        );
3372
3373        // Re-create a similar-shaped table; should reuse freelist pages.
3374        process_command(
3375            "CREATE TABLE accounts (id INTEGER PRIMARY KEY, label TEXT NOT NULL UNIQUE);",
3376            &mut db,
3377        )
3378        .expect("create accounts");
3379        process_command("INSERT INTO accounts (label) VALUES ('a');", &mut db).unwrap();
3380        process_command("INSERT INTO accounts (label) VALUES ('b');", &mut db).unwrap();
3381        let pages_after_create = db.pager.as_ref().unwrap().header().page_count;
3382        assert!(
3383            pages_after_create <= pages_two_tables + 2,
3384            "creating a similar-sized table after a drop should mostly draw from the \
3385             freelist, not extend the file (got {pages_after_create} > {pages_two_tables} + 2)"
3386        );
3387
3388        cleanup(&path);
3389    }
3390
3391    /// `VACUUM;` after a drop must shrink the file and clear the freelist.
3392    #[test]
3393    fn drop_then_vacuum_shrinks_file() {
3394        let path = tmp_path("vacuum_shrinks");
3395        let mut db = seed_db();
3396        db.source_path = Some(path.clone());
3397        // Add a few more rows to make the dropped table bigger.
3398        for i in 0..20 {
3399            process_command(
3400                &format!("INSERT INTO users (name, age) VALUES ('user{i}', {i});"),
3401                &mut db,
3402            )
3403            .unwrap();
3404        }
3405        save_database(&mut db, &path).expect("save");
3406
3407        process_command("DROP TABLE users;", &mut db).expect("drop");
3408        let size_before_vacuum = std::fs::metadata(&path).unwrap().len();
3409        let pages_before_vacuum = db.pager.as_ref().unwrap().header().page_count;
3410        let head_before = db.pager.as_ref().unwrap().header().freelist_head;
3411        assert!(head_before != 0, "drop should populate the freelist");
3412
3413        // VACUUM (via process_command) checkpoints internally so the
3414        // file actually shrinks on disk before we observe its size.
3415        process_command("VACUUM;", &mut db).expect("vacuum");
3416
3417        let size_after = std::fs::metadata(&path).unwrap().len();
3418        let pages_after = db.pager.as_ref().unwrap().header().page_count;
3419        let head_after = db.pager.as_ref().unwrap().header().freelist_head;
3420        assert!(
3421            pages_after < pages_before_vacuum,
3422            "VACUUM must reduce page_count: was {pages_before_vacuum}, now {pages_after}"
3423        );
3424        assert_eq!(head_after, 0, "VACUUM must clear the freelist");
3425        assert!(
3426            size_after < size_before_vacuum,
3427            "VACUUM must shrink the file on disk: was {size_before_vacuum} bytes, now {size_after}"
3428        );
3429
3430        cleanup(&path);
3431    }
3432
3433    /// VACUUM on a non-empty multi-table DB must not lose any rows.
3434    #[test]
3435    fn vacuum_round_trips_data() {
3436        let path = tmp_path("vacuum_round_trip");
3437        let mut db = seed_db();
3438        db.source_path = Some(path.clone());
3439        save_database(&mut db, &path).expect("save");
3440        process_command("VACUUM;", &mut db).expect("vacuum");
3441
3442        // Re-open from disk to make sure the on-disk catalog round-trips.
3443        drop(db);
3444        let loaded = open_database(&path, "t".to_string()).expect("reopen after vacuum");
3445        assert!(loaded.contains_table("users".to_string()));
3446        assert!(loaded.contains_table("notes".to_string()));
3447        let users = loaded.get_table("users".to_string()).unwrap();
3448        // seed_db inserts two users.
3449        assert_eq!(users.rowids().len(), 2);
3450
3451        cleanup(&path);
3452    }
3453
3454    /// Format version is bumped to v6 only after a save that creates a
3455    /// non-empty freelist. VACUUM clears the freelist but doesn't
3456    /// downgrade — v6 is a strict superset, so once at v6 we stay.
3457    #[test]
3458    fn freelist_format_version_promotion() {
3459        use crate::sql::pager::header::{FORMAT_VERSION_BASELINE, FORMAT_VERSION_V6};
3460        let path = tmp_path("v6_promotion");
3461        let mut db = seed_db();
3462        db.source_path = Some(path.clone());
3463        save_database(&mut db, &path).expect("save");
3464        let v_after_save = db.pager.as_ref().unwrap().header().format_version;
3465        assert_eq!(
3466            v_after_save, FORMAT_VERSION_BASELINE,
3467            "fresh DB without drops should stay at the baseline version"
3468        );
3469
3470        process_command("DROP TABLE users;", &mut db).expect("drop");
3471        let v_after_drop = db.pager.as_ref().unwrap().header().format_version;
3472        assert_eq!(
3473            v_after_drop, FORMAT_VERSION_V6,
3474            "first save with a non-empty freelist must promote to V6"
3475        );
3476
3477        process_command("VACUUM;", &mut db).expect("vacuum");
3478        let v_after_vacuum = db.pager.as_ref().unwrap().header().format_version;
3479        assert_eq!(
3480            v_after_vacuum, FORMAT_VERSION_V6,
3481            "VACUUM must not downgrade — V6 is a strict superset"
3482        );
3483
3484        cleanup(&path);
3485    }
3486
3487    /// Freelist persists across reopen: drop, save, close, reopen,
3488    /// confirm the next CREATE TABLE re-uses pages from the persisted
3489    /// freelist (rather than extending the file).
3490    #[test]
3491    fn freelist_round_trip_through_reopen() {
3492        let path = tmp_path("freelist_reopen");
3493        let pages_two_tables;
3494        {
3495            let mut db = seed_db();
3496            db.source_path = Some(path.clone());
3497            save_database(&mut db, &path).expect("save");
3498            pages_two_tables = db.pager.as_ref().unwrap().header().page_count;
3499            process_command("DROP TABLE users;", &mut db).expect("drop");
3500            let head = db.pager.as_ref().unwrap().header().freelist_head;
3501            assert!(head != 0, "drop must populate the freelist");
3502        }
3503
3504        // Reopen from disk — the freelist must come back.
3505        let mut db = open_database(&path, "t".to_string()).expect("reopen");
3506        assert!(
3507            db.pager.as_ref().unwrap().header().freelist_head != 0,
3508            "freelist_head must survive close/reopen"
3509        );
3510
3511        process_command(
3512            "CREATE TABLE accounts (id INTEGER PRIMARY KEY, label TEXT NOT NULL UNIQUE);",
3513            &mut db,
3514        )
3515        .expect("create accounts");
3516        process_command("INSERT INTO accounts (label) VALUES ('reopened');", &mut db).unwrap();
3517        let pages_after_create = db.pager.as_ref().unwrap().header().page_count;
3518        assert!(
3519            pages_after_create <= pages_two_tables + 2,
3520            "post-reopen create should reuse freelist (got {pages_after_create} > \
3521             {pages_two_tables} + 2 — file extended instead of reusing)"
3522        );
3523
3524        cleanup(&path);
3525    }
3526
3527    /// VACUUM inside an explicit transaction must error before touching the
3528    /// disk. `BEGIN; VACUUM;` is the documented rejection path.
3529    #[test]
3530    fn vacuum_inside_transaction_is_rejected() {
3531        let path = tmp_path("vacuum_txn");
3532        let mut db = seed_db();
3533        db.source_path = Some(path.clone());
3534        save_database(&mut db, &path).expect("save");
3535
3536        process_command("BEGIN;", &mut db).expect("begin");
3537        let err = process_command("VACUUM;", &mut db).unwrap_err();
3538        assert!(
3539            format!("{err}").contains("VACUUM cannot run inside a transaction"),
3540            "expected in-transaction rejection, got: {err}"
3541        );
3542        // Roll back to leave the DB in a clean state.
3543        process_command("ROLLBACK;", &mut db).unwrap();
3544        cleanup(&path);
3545    }
3546
3547    /// VACUUM on an in-memory database is a documented no-op.
3548    #[test]
3549    fn vacuum_on_in_memory_database_is_noop() {
3550        let mut db = Database::new("mem".to_string());
3551        process_command("CREATE TABLE t (id INTEGER PRIMARY KEY);", &mut db).unwrap();
3552        let out = process_command("VACUUM;", &mut db).expect("vacuum no-op");
3553        assert!(
3554            out.to_lowercase().contains("no-op") || out.to_lowercase().contains("in-memory"),
3555            "expected no-op message for in-memory VACUUM, got: {out}"
3556        );
3557    }
3558
3559    /// Untouched tables shouldn't write any pages on the save that
3560    /// follows a DROP of an unrelated table. Confirms the per-table
3561    /// preferred pool keeps page numbers stable so the diff pager skips
3562    /// every byte-identical leaf.
3563    #[test]
3564    fn unchanged_table_pages_skip_diff_after_unrelated_drop() {
3565        // Need three tables so dropping one in the middle still leaves
3566        // an "unrelated" alphabetical neighbour. Layout pre-drop (sorted):
3567        //   accounts, notes, users
3568        // Drop `notes`. `accounts` and `users` should keep their pages.
3569        let path = tmp_path("diff_after_drop");
3570        let mut db = Database::new("t".to_string());
3571        db.source_path = Some(path.clone());
3572        process_command(
3573            "CREATE TABLE accounts (id INTEGER PRIMARY KEY, label TEXT);",
3574            &mut db,
3575        )
3576        .unwrap();
3577        process_command(
3578            "CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT);",
3579            &mut db,
3580        )
3581        .unwrap();
3582        process_command(
3583            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);",
3584            &mut db,
3585        )
3586        .unwrap();
3587        for i in 0..5 {
3588            process_command(
3589                &format!("INSERT INTO accounts (label) VALUES ('a{i}');"),
3590                &mut db,
3591            )
3592            .unwrap();
3593            process_command(
3594                &format!("INSERT INTO notes (body) VALUES ('n{i}');"),
3595                &mut db,
3596            )
3597            .unwrap();
3598            process_command(
3599                &format!("INSERT INTO users (name) VALUES ('u{i}');"),
3600                &mut db,
3601            )
3602            .unwrap();
3603        }
3604        save_database(&mut db, &path).expect("baseline save");
3605
3606        // Capture page bytes for `accounts` and `users` so we can
3607        // verify they don't change.
3608        let pager = db.pager.as_ref().unwrap();
3609        let acc_root = read_old_rootpages(pager, pager.header().schema_root_page)
3610            .unwrap()
3611            .get(&("table".to_string(), "accounts".to_string()))
3612            .copied()
3613            .unwrap();
3614        let users_root = read_old_rootpages(pager, pager.header().schema_root_page)
3615            .unwrap()
3616            .get(&("table".to_string(), "users".to_string()))
3617            .copied()
3618            .unwrap();
3619        let acc_bytes_before: Vec<u8> = pager.read_page(acc_root).unwrap().to_vec();
3620        let users_bytes_before: Vec<u8> = pager.read_page(users_root).unwrap().to_vec();
3621
3622        // Drop the middle table.
3623        process_command("DROP TABLE notes;", &mut db).expect("drop notes");
3624
3625        let pager = db.pager.as_ref().unwrap();
3626        // `accounts` and `users` should still live at the same pages
3627        // with byte-identical content.
3628        let acc_after = pager.read_page(acc_root).unwrap();
3629        let users_after = pager.read_page(users_root).unwrap();
3630        assert_eq!(
3631            &acc_after[..],
3632            &acc_bytes_before[..],
3633            "accounts root page must not be rewritten when an unrelated table is dropped"
3634        );
3635        assert_eq!(
3636            &users_after[..],
3637            &users_bytes_before[..],
3638            "users root page must not be rewritten when an unrelated table is dropped"
3639        );
3640
3641        cleanup(&path);
3642    }
3643
3644    // ---- SQLR-10: auto-VACUUM trigger after page-releasing DDL ----
3645
3646    /// Builds a file-backed DB with one small "keep" table and one
3647    /// large "bloat" table, sized so the post-drop freelist will
3648    /// comfortably cross the default 25% threshold and the
3649    /// `MIN_PAGES_FOR_AUTO_VACUUM` floor (16 pages). Used by the
3650    /// auto-VACUUM happy-path tests.
3651    fn auto_vacuum_setup(path: &std::path::Path) -> Database {
3652        let mut db = Database::new("av".to_string());
3653        db.source_path = Some(path.to_path_buf());
3654        process_command(
3655            "CREATE TABLE keep (id INTEGER PRIMARY KEY, n INTEGER);",
3656            &mut db,
3657        )
3658        .unwrap();
3659        process_command("INSERT INTO keep (n) VALUES (1);", &mut db).unwrap();
3660        process_command(
3661            "CREATE TABLE bloat (id INTEGER PRIMARY KEY, payload TEXT);",
3662            &mut db,
3663        )
3664        .unwrap();
3665        // Wrap the bulk insert in a transaction so we pay one save at
3666        // COMMIT instead of 5000 round-trips through auto-save.
3667        process_command("BEGIN;", &mut db).unwrap();
3668        for i in 0..5000 {
3669            process_command(
3670                &format!("INSERT INTO bloat (payload) VALUES ('p-{i:08}');"),
3671                &mut db,
3672            )
3673            .unwrap();
3674        }
3675        process_command("COMMIT;", &mut db).unwrap();
3676        db
3677    }
3678
3679    /// Default threshold (0.25) is engaged for fresh `Database`s and
3680    /// fires when a `DROP TABLE` orphans enough pages — file shrinks
3681    /// without anyone calling `VACUUM;`.
3682    #[test]
3683    fn auto_vacuum_default_threshold_triggers_on_drop_table() {
3684        let path = tmp_path("av_default_drop_table");
3685        let mut db = auto_vacuum_setup(&path);
3686        // Sanity: setup respects the shipped default.
3687        assert_eq!(db.auto_vacuum_threshold(), Some(0.25));
3688
3689        // Checkpoint before measuring `size_before` so the bloat actually
3690        // lives in the main file and not just the WAL — otherwise
3691        // `size_before` is the bare 2-page header and any post-vacuum
3692        // checkpoint will look like the file *grew*.
3693        if let Some(p) = db.pager.as_mut() {
3694            let _ = p.checkpoint();
3695        }
3696        let pages_before = db.pager.as_ref().unwrap().header().page_count;
3697        let size_before = std::fs::metadata(&path).unwrap().len();
3698        assert!(
3699            pages_before >= MIN_PAGES_FOR_AUTO_VACUUM,
3700            "setup should produce >= MIN_PAGES_FOR_AUTO_VACUUM ({MIN_PAGES_FOR_AUTO_VACUUM}) \
3701             pages so the floor doesn't suppress the trigger; got {pages_before}"
3702        );
3703
3704        // Drop the bloat table — freelist should pass 25% of page_count
3705        // and the auto-VACUUM hook should compact in place. Note: no
3706        // explicit `VACUUM;` statement is issued.
3707        process_command("DROP TABLE bloat;", &mut db).expect("drop");
3708
3709        let pages_after = db.pager.as_ref().unwrap().header().page_count;
3710        let head_after = db.pager.as_ref().unwrap().header().freelist_head;
3711        // Second checkpoint so the post-vacuum file shrinks on disk
3712        // (auto-VACUUM stages the compact through WAL just like manual
3713        // VACUUM does).
3714        if let Some(p) = db.pager.as_mut() {
3715            let _ = p.checkpoint();
3716        }
3717        let size_after = std::fs::metadata(&path).unwrap().len();
3718
3719        assert!(
3720            pages_after < pages_before,
3721            "auto-VACUUM must reduce page_count: was {pages_before}, now {pages_after}"
3722        );
3723        assert_eq!(head_after, 0, "auto-VACUUM must clear the freelist");
3724        assert!(
3725            size_after < size_before,
3726            "auto-VACUUM must shrink the file on disk: was {size_before}, now {size_after}"
3727        );
3728
3729        cleanup(&path);
3730    }
3731
3732    /// Setting the threshold to `None` disables the trigger entirely:
3733    /// the same workload that shrinks under the default leaves the file
3734    /// at its high-water mark.
3735    #[test]
3736    fn auto_vacuum_disabled_keeps_file_at_hwm() {
3737        let path = tmp_path("av_disabled");
3738        let mut db = auto_vacuum_setup(&path);
3739        db.set_auto_vacuum_threshold(None).expect("disable");
3740        assert_eq!(db.auto_vacuum_threshold(), None);
3741
3742        let pages_before = db.pager.as_ref().unwrap().header().page_count;
3743
3744        process_command("DROP TABLE bloat;", &mut db).expect("drop");
3745
3746        let pages_after = db.pager.as_ref().unwrap().header().page_count;
3747        let head_after = db.pager.as_ref().unwrap().header().freelist_head;
3748        assert_eq!(
3749            pages_after, pages_before,
3750            "with auto-VACUUM disabled, drop must keep page_count at the HWM"
3751        );
3752        assert!(
3753            head_after != 0,
3754            "drop must still populate the freelist (manual VACUUM would be needed to reclaim)"
3755        );
3756
3757        cleanup(&path);
3758    }
3759
3760    /// `DROP INDEX` is the second of three page-releasing DDL paths
3761    /// covered by SQLR-10. We bloat the freelist via a separate
3762    /// `DROP TABLE` first (with auto-VACUUM disabled so it doesn't
3763    /// compact early), then re-arm the trigger and drop a small index
3764    /// — the cumulative freelist crosses 25% on the index drop and
3765    /// auto-VACUUM fires.
3766    ///
3767    /// The detour around bloat is necessary because building a
3768    /// secondary index on a 5000-row column would need multi-level
3769    /// interior nodes, and the cell-decoder's interior-page support
3770    /// is a separate work item from SQLR-10.
3771    #[test]
3772    fn auto_vacuum_triggers_on_drop_index() {
3773        let path = tmp_path("av_drop_index");
3774        let mut db = auto_vacuum_setup(&path);
3775
3776        // Phase 1: drop the bloat table with auto-VACUUM disabled so
3777        // its pages land on the freelist without being reclaimed.
3778        db.set_auto_vacuum_threshold(None).expect("disable");
3779        process_command("DROP TABLE bloat;", &mut db).expect("drop bloat");
3780        let pages_after_bloat_drop = db.pager.as_ref().unwrap().header().page_count;
3781        let head_after_bloat_drop = db.pager.as_ref().unwrap().header().freelist_head;
3782        assert!(
3783            head_after_bloat_drop != 0,
3784            "bloat drop must populate the freelist (else later index drop won't trip the threshold)"
3785        );
3786
3787        // Phase 2: a small index on the surviving `keep` table. The
3788        // index reuses one page from the freelist (which is fine —
3789        // freelist still holds plenty more).
3790        process_command("CREATE INDEX idx_keep_n ON keep (n);", &mut db).expect("create idx");
3791
3792        // Phase 3: re-arm the trigger and drop the index. The freelist
3793        // is already heavily populated from phase 1; this drop just
3794        // adds the index page on top, keeping the ratio well above
3795        // 25%, so auto-VACUUM should fire.
3796        db.set_auto_vacuum_threshold(Some(0.25)).expect("re-arm");
3797        process_command("DROP INDEX idx_keep_n;", &mut db).expect("drop index");
3798
3799        let pages_after = db.pager.as_ref().unwrap().header().page_count;
3800        let head_after = db.pager.as_ref().unwrap().header().freelist_head;
3801        assert!(
3802            pages_after < pages_after_bloat_drop,
3803            "DROP INDEX should fire auto-VACUUM and reduce page_count: \
3804             was {pages_after_bloat_drop}, now {pages_after}"
3805        );
3806        assert_eq!(
3807            head_after, 0,
3808            "auto-VACUUM after DROP INDEX must clear the freelist"
3809        );
3810
3811        cleanup(&path);
3812    }
3813
3814    /// `ALTER TABLE … DROP COLUMN` releases pages too — the third path
3815    /// the SQLR-10 trigger covers.
3816    #[test]
3817    fn auto_vacuum_triggers_on_alter_drop_column() {
3818        let path = tmp_path("av_alter_drop_col");
3819        let mut db = auto_vacuum_setup(&path);
3820        let pages_before = db.pager.as_ref().unwrap().header().page_count;
3821
3822        // Drop the wide `payload` column — this rewrites every row in
3823        // `bloat` without the column, so the old leaf pages get freed.
3824        process_command("ALTER TABLE bloat DROP COLUMN payload;", &mut db).expect("alter drop");
3825
3826        let pages_after = db.pager.as_ref().unwrap().header().page_count;
3827        assert!(
3828            pages_after < pages_before,
3829            "ALTER TABLE DROP COLUMN should fire auto-VACUUM and reduce page_count: \
3830             was {pages_before}, now {pages_after}"
3831        );
3832        assert_eq!(db.pager.as_ref().unwrap().header().freelist_head, 0);
3833
3834        cleanup(&path);
3835    }
3836
3837    /// A high threshold (0.99) suppresses the trigger when the freelist
3838    /// ratio is well below it — the file stays at HWM.
3839    #[test]
3840    fn auto_vacuum_skips_below_threshold() {
3841        let path = tmp_path("av_below_threshold");
3842        let mut db = auto_vacuum_setup(&path);
3843        db.set_auto_vacuum_threshold(Some(0.99)).expect("set");
3844
3845        let pages_before = db.pager.as_ref().unwrap().header().page_count;
3846
3847        process_command("DROP TABLE bloat;", &mut db).expect("drop");
3848
3849        let pages_after = db.pager.as_ref().unwrap().header().page_count;
3850        assert_eq!(
3851            pages_after, pages_before,
3852            "freelist ratio after a single drop is far below 0.99 — \
3853             page_count must stay at the HWM"
3854        );
3855        assert!(
3856            db.pager.as_ref().unwrap().header().freelist_head != 0,
3857            "drop must still populate the freelist"
3858        );
3859
3860        cleanup(&path);
3861    }
3862
3863    /// Inside an explicit transaction, the page-releasing DDL doesn't
3864    /// flush to disk yet — the freelist isn't accurate, so the trigger
3865    /// must skip. The compact would also publish in-flight work out of
3866    /// band, which is exactly what the manual `VACUUM;` rejection
3867    /// inside a txn already prevents.
3868    #[test]
3869    fn auto_vacuum_skips_inside_transaction() {
3870        let path = tmp_path("av_in_txn");
3871        let mut db = auto_vacuum_setup(&path);
3872        let pages_before = db.pager.as_ref().unwrap().header().page_count;
3873
3874        process_command("BEGIN;", &mut db).expect("begin");
3875        process_command("DROP TABLE bloat;", &mut db).expect("drop in txn");
3876        // Mid-transaction: no save has occurred, so the on-disk
3877        // freelist_head must be unchanged and page_count must not have
3878        // shifted from a sneaky compact.
3879        let pages_mid = db.pager.as_ref().unwrap().header().page_count;
3880        assert_eq!(
3881            pages_mid, pages_before,
3882            "auto-VACUUM must not fire mid-transaction"
3883        );
3884
3885        process_command("ROLLBACK;", &mut db).expect("rollback");
3886        cleanup(&path);
3887    }
3888
3889    /// Tiny databases (under `MIN_PAGES_FOR_AUTO_VACUUM`) skip the
3890    /// trigger even if the ratio would otherwise qualify — the cost of
3891    /// rewriting a 64 KiB file isn't worth the few bytes reclaimed.
3892    #[test]
3893    fn auto_vacuum_skips_under_min_pages_floor() {
3894        let path = tmp_path("av_under_floor");
3895        let mut db = seed_db(); // small: just users + notes, ~5 pages
3896        db.source_path = Some(path.clone());
3897        save_database(&mut db, &path).expect("save");
3898        // Confirm we're below the floor so the test is meaningful.
3899        let pages_before = db.pager.as_ref().unwrap().header().page_count;
3900        assert!(
3901            pages_before < MIN_PAGES_FOR_AUTO_VACUUM,
3902            "test setup is too large: floor would not apply (got {pages_before} pages, \
3903             floor is {MIN_PAGES_FOR_AUTO_VACUUM})"
3904        );
3905
3906        process_command("DROP TABLE users;", &mut db).expect("drop");
3907
3908        let pages_after = db.pager.as_ref().unwrap().header().page_count;
3909        assert_eq!(
3910            pages_after, pages_before,
3911            "below MIN_PAGES_FOR_AUTO_VACUUM, drop must not trigger compaction"
3912        );
3913        assert!(
3914            db.pager.as_ref().unwrap().header().freelist_head != 0,
3915            "drop must still populate the freelist normally"
3916        );
3917
3918        cleanup(&path);
3919    }
3920
3921    /// Setter rejects NaN, infinities, and values outside `0.0..=1.0`
3922    /// rather than silently saturating.
3923    #[test]
3924    fn set_auto_vacuum_threshold_rejects_out_of_range() {
3925        let mut db = Database::new("t".to_string());
3926        for bad in [-0.01_f32, 1.01, f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
3927            let err = db.set_auto_vacuum_threshold(Some(bad)).unwrap_err();
3928            assert!(
3929                format!("{err}").contains("auto_vacuum_threshold"),
3930                "expected a typed range error for {bad}, got: {err}"
3931            );
3932        }
3933        // The default survives the rejected sets unchanged.
3934        assert_eq!(db.auto_vacuum_threshold(), Some(0.25));
3935        // And valid values land.
3936        db.set_auto_vacuum_threshold(Some(0.0)).unwrap();
3937        assert_eq!(db.auto_vacuum_threshold(), Some(0.0));
3938        db.set_auto_vacuum_threshold(Some(1.0)).unwrap();
3939        assert_eq!(db.auto_vacuum_threshold(), Some(1.0));
3940        db.set_auto_vacuum_threshold(None).unwrap();
3941        assert_eq!(db.auto_vacuum_threshold(), None);
3942    }
3943
3944    // ---------------------------------------------------------------
3945    // SQLR-13 — `PRAGMA auto_vacuum` SQL-level coverage. Mirrors the
3946    // SQLR-10 setter tests above, but routed through SQL so SDK / FFI
3947    // / MCP consumers (which can't reach the Rust setter directly)
3948    // get the same guarantees.
3949    // ---------------------------------------------------------------
3950
3951    /// `PRAGMA auto_vacuum = N;` set + `PRAGMA auto_vacuum;` read
3952    /// round-trip the threshold, observable via `auto_vacuum_threshold`.
3953    #[test]
3954    fn pragma_auto_vacuum_set_and_read_via_sql() {
3955        let mut db = Database::new("t".to_string());
3956
3957        let resp = process_command("PRAGMA auto_vacuum = 0.5;", &mut db).expect("set");
3958        assert!(
3959            resp.contains("PRAGMA"),
3960            "set form should produce a PRAGMA status, got: {resp}"
3961        );
3962        assert_eq!(db.auto_vacuum_threshold(), Some(0.5));
3963
3964        // Read form — status mentions a returned row.
3965        let resp = process_command("PRAGMA auto_vacuum;", &mut db).expect("read");
3966        assert!(resp.contains("1 row"), "expected a 1-row read, got: {resp}");
3967    }
3968
3969    /// `PRAGMA auto_vacuum = OFF;` (bare identifier — sqlparser's own
3970    /// pragma-value parser would reject this, the SQLR-13 dispatcher
3971    /// must accept it) and `= NONE;` both disable the trigger. So does
3972    /// the quoted form `'OFF'`.
3973    #[test]
3974    fn pragma_auto_vacuum_off_disables_trigger() {
3975        for raw in ["OFF", "off", "NONE", "none", "'OFF'", "'NONE'"] {
3976            let mut db = Database::new("t".to_string());
3977            assert_eq!(db.auto_vacuum_threshold(), Some(0.25));
3978
3979            let stmt = format!("PRAGMA auto_vacuum = {raw};");
3980            process_command(&stmt, &mut db)
3981                .unwrap_or_else(|e| panic!("`{stmt}` should disable: {e}"));
3982            assert_eq!(
3983                db.auto_vacuum_threshold(),
3984                None,
3985                "`{stmt}` should clear the threshold"
3986            );
3987        }
3988    }
3989
3990    /// Out-of-range numeric values surface as a typed error via the
3991    /// shared `set_auto_vacuum_threshold` validator — no silent
3992    /// saturation. Mirrors the SQLR-10 setter coverage.
3993    #[test]
3994    fn pragma_auto_vacuum_rejects_out_of_range_via_sql() {
3995        let mut db = Database::new("t".to_string());
3996        for bad in ["-0.01", "1.01", "1.5"] {
3997            let stmt = format!("PRAGMA auto_vacuum = {bad};");
3998            let err = process_command(&stmt, &mut db).unwrap_err();
3999            assert!(
4000                format!("{err}").contains("auto_vacuum_threshold"),
4001                "expected range error for `{stmt}`, got: {err}"
4002            );
4003        }
4004        // Default survives all the rejected sets.
4005        assert_eq!(db.auto_vacuum_threshold(), Some(0.25));
4006    }
4007
4008    /// Junk strings (anything that isn't a number or `OFF`/`NONE`) are
4009    /// rejected at parse time with a typed error, not silently treated
4010    /// as "disable".
4011    #[test]
4012    fn pragma_auto_vacuum_rejects_unknown_strings_via_sql() {
4013        let mut db = Database::new("t".to_string());
4014        let err = process_command("PRAGMA auto_vacuum = WAL;", &mut db).unwrap_err();
4015        assert!(
4016            format!("{err}").contains("OFF/NONE"),
4017            "expected OFF/NONE-style error, got: {err}"
4018        );
4019        // Default unaffected.
4020        assert_eq!(db.auto_vacuum_threshold(), Some(0.25));
4021    }
4022
4023    /// Pragmas SQLRite doesn't know about return `NotImplemented` —
4024    /// not a generic parser error. Future pragmas plug in here.
4025    /// (Phase 11.3 made `journal_mode` a recognised pragma; this
4026    /// test uses a name that's still unsupported.)
4027    #[test]
4028    fn pragma_unknown_returns_not_implemented() {
4029        let mut db = Database::new("t".to_string());
4030        let err = process_command("PRAGMA synchronous = NORMAL;", &mut db).unwrap_err();
4031        assert!(
4032            matches!(err, SQLRiteError::NotImplemented(_)),
4033            "unknown pragma must surface NotImplemented, got: {err:?}"
4034        );
4035    }
4036
4037    /// Setting the threshold via SQL must produce identical behavior to
4038    /// the Rust setter on the actual auto-VACUUM trigger: `= 0.99`
4039    /// suppresses, `= OFF` disables, default fires. Sanity-checks that
4040    /// `process_command_with_render`'s pre-parse step doesn't desync
4041    /// the in-memory state from the file.
4042    #[test]
4043    fn pragma_auto_vacuum_drives_real_trigger() {
4044        // Sub-case A — `PRAGMA auto_vacuum = OFF;` keeps file at HWM.
4045        {
4046            let path = tmp_path("av_pragma_off");
4047            let mut db = auto_vacuum_setup(&path);
4048            process_command("PRAGMA auto_vacuum = OFF;", &mut db).expect("disable via PRAGMA");
4049            assert_eq!(db.auto_vacuum_threshold(), None);
4050
4051            let pages_before = db.pager.as_ref().unwrap().header().page_count;
4052            process_command("DROP TABLE bloat;", &mut db).expect("drop");
4053            let pages_after = db.pager.as_ref().unwrap().header().page_count;
4054            assert_eq!(
4055                pages_after, pages_before,
4056                "PRAGMA-driven OFF must keep page_count at the HWM"
4057            );
4058            cleanup(&path);
4059        }
4060
4061        // Sub-case B — high threshold via PRAGMA suppresses the
4062        // trigger on a single drop.
4063        {
4064            let path = tmp_path("av_pragma_high");
4065            let mut db = auto_vacuum_setup(&path);
4066            process_command("PRAGMA auto_vacuum = 0.99;", &mut db).expect("set high");
4067            assert_eq!(db.auto_vacuum_threshold(), Some(0.99));
4068
4069            let pages_before = db.pager.as_ref().unwrap().header().page_count;
4070            process_command("DROP TABLE bloat;", &mut db).expect("drop");
4071            let pages_after = db.pager.as_ref().unwrap().header().page_count;
4072            assert_eq!(
4073                pages_after, pages_before,
4074                "high PRAGMA threshold must suppress the trigger"
4075            );
4076            cleanup(&path);
4077        }
4078
4079        // Sub-case C — re-arm via PRAGMA after disable: the trigger
4080        // fires again on the next page-releasing DDL.
4081        {
4082            let path = tmp_path("av_pragma_rearm");
4083            let mut db = auto_vacuum_setup(&path);
4084            process_command("PRAGMA auto_vacuum = OFF;", &mut db).unwrap();
4085            // Drop with the trigger off — pages land on the freelist
4086            // but the file stays at HWM.
4087            process_command("DROP TABLE bloat;", &mut db).unwrap();
4088            let pages_after_off_drop = db.pager.as_ref().unwrap().header().page_count;
4089            assert!(db.pager.as_ref().unwrap().header().freelist_head != 0);
4090
4091            // Re-arm via PRAGMA, then drop one more thing — the
4092            // accumulated freelist still exceeds 25%, so auto-VACUUM
4093            // fires.
4094            process_command("PRAGMA auto_vacuum = 0.25;", &mut db).expect("re-arm");
4095            process_command("CREATE INDEX idx_keep_n ON keep (n);", &mut db).unwrap();
4096            process_command("DROP INDEX idx_keep_n;", &mut db).expect("drop index");
4097
4098            let pages_after_rearm = db.pager.as_ref().unwrap().header().page_count;
4099            assert!(
4100                pages_after_rearm < pages_after_off_drop,
4101                "re-armed PRAGMA must let auto-VACUUM fire: was {pages_after_off_drop}, \
4102                 now {pages_after_rearm}"
4103            );
4104            assert_eq!(db.pager.as_ref().unwrap().header().freelist_head, 0);
4105            cleanup(&path);
4106        }
4107    }
4108
4109    /// VACUUM modifiers (FULL, REINDEX, table targets, …) are rejected
4110    /// with NotImplemented — only bare `VACUUM;` is supported.
4111    #[test]
4112    fn vacuum_modifiers_are_rejected() {
4113        let path = tmp_path("vacuum_modifiers");
4114        let mut db = seed_db();
4115        db.source_path = Some(path.clone());
4116        save_database(&mut db, &path).expect("save");
4117        for stmt in ["VACUUM FULL;", "VACUUM users;"] {
4118            let err = process_command(stmt, &mut db).unwrap_err();
4119            assert!(
4120                format!("{err}").contains("VACUUM modifiers"),
4121                "expected modifier rejection for `{stmt}`, got: {err}"
4122            );
4123        }
4124        cleanup(&path);
4125    }
4126}