Skip to main content

spg_storage/
lib.rs

1//! In-memory storage primitives.
2//!
3//! v0.3 is intentionally simple: a flat catalog of tables, each holding rows
4//! as `Vec<Value>` (positional, matching the table's `TableSchema`). No MVCC,
5//! no on-disk format — those land in later milestones.
6#![no_std]
7// v3.3.2 NEON path for l2_distance_sq (aarch64 only). Scoped allow:
8// `unsafe_code = "deny"` at workspace level stays in force for every
9// other crate.
10#![cfg_attr(target_arch = "aarch64", allow(unsafe_code))]
11
12extern crate alloc;
13
14pub mod bloom;
15pub mod halfvec;
16pub mod persistent;
17pub mod persistent_btree;
18pub mod quantize;
19pub mod row_locator;
20pub mod segment;
21
22pub use self::bloom::{BloomError, BloomFilter};
23pub use self::row_locator::{RowLocator, RowLocatorError};
24pub use self::segment::{
25    BRIN_SIDECAR_MAGIC, BrinSummary, OwnedSegment, SEGMENT_COMPRESS_ALGO_LZSS,
26    SEGMENT_COMPRESS_ALGO_NONE, SEGMENT_MAGIC, SEGMENT_MAGIC_V2, SEGMENT_PAGE_BYTES, SegmentError,
27    SegmentMeta, SegmentReader, derive_brin_summaries, encode_segment, wrap_v2_envelope,
28    wrap_v2_envelope_with_brin,
29};
30
31use alloc::boxed::Box;
32use alloc::collections::{BTreeMap, BTreeSet};
33use alloc::format;
34use alloc::string::String;
35use alloc::sync::Arc;
36use alloc::vec::Vec;
37use core::fmt;
38
39use self::persistent::PersistentVec;
40use self::persistent_btree::PersistentBTreeMap;
41
42/// In-cell encoding for `DataType::Vector`. Mirrors
43/// `spg_sql::ast::VecEncoding` — kept here so storage stays
44/// dep-free of `spg-sql`. The engine bridges between the two
45/// at DDL-execution time.
46///
47/// `F32` is the pre-v6 default: each cell holds a raw `Vec<f32>`.
48/// `Sq8` (v6.0.1) stores `Sq8Vector { min, max, bytes: Vec<u8> }`
49/// per cell; 4× compression vs `F32` with recall@10 ≥ 0.95 on
50/// natural embeddings (Gaussian / unit-sphere corpora).
51/// `F16` (v6.0.3, DDL keyword `HALF`) stores each element as
52/// IEEE-754 binary16; 2× compression and bit-exact dequantise.
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
54pub enum VecEncoding {
55    #[default]
56    F32,
57    Sq8,
58    F16,
59}
60
61impl fmt::Display for VecEncoding {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        match self {
64            Self::F32 => f.write_str("F32"),
65            Self::Sq8 => f.write_str("SQ8"),
66            Self::F16 => f.write_str("HALF"),
67        }
68    }
69}
70
71/// Runtime type tags. `Vector { dim, encoding }` / `Varchar(max)` /
72/// `Char(size)` are parameterised; the parameter travels with both
73/// the column schema and the on-wire serialised representation.
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum DataType {
76    /// 16-bit signed. Backed by `Value::SmallInt(i16)`; arithmetic that
77    /// would overflow surfaces as a type error at INSERT time.
78    SmallInt,
79    Int,    // 32-bit signed
80    BigInt, // 64-bit signed
81    Float,  // f64 (PG double precision)
82    Text,
83    /// `VARCHAR(n)` — same byte representation as `Text`, but INSERT
84    /// rejects values longer than `n` Unicode characters.
85    Varchar(u32),
86    /// `CHAR(n)` — same representation as `Text`, but INSERT right-pads
87    /// with U+0020 to exactly `n` Unicode characters (or rejects when
88    /// the input is already longer).
89    Char(u32),
90    Bool,
91    /// pgvector-style fixed-dimension vector. `encoding` selects
92    /// the in-cell representation (`F32` = pre-v6 raw f32 buffer;
93    /// `Sq8` = v6.0.1 8-bit scalar-quantised). The DDL grammar
94    /// surfaces encoding via the optional `USING <encoding>`
95    /// clause: `VECTOR(128) USING SQ8`.
96    Vector {
97        dim: u32,
98        encoding: VecEncoding,
99    },
100    /// `NUMERIC(precision, scale)` — exact fixed-point decimal stored as
101    /// a scaled `i128`. `precision` caps total decimal digits, `scale`
102    /// fixes digits after the decimal point. v1.12 supports up to
103    /// precision 38 (the i128-safe ceiling). `NUMERIC` and `NUMERIC(p)`
104    /// surface as `Numeric { precision: p, scale: 0 }`.
105    Numeric {
106        precision: u8,
107        scale: u8,
108    },
109    /// `DATE` — calendar date with day precision, stored as `i32` days
110    /// since the Unix epoch (1970-01-01).
111    Date,
112    /// `TIMESTAMP` (a.k.a. `MySQL` `DATETIME`) — instant with microsecond
113    /// precision, stored as `i64` microseconds since the Unix epoch.
114    Timestamp,
115    /// v7.9.2 `TIMESTAMPTZ` — bit-identical to `Timestamp` on disk
116    /// (i64 microseconds, UTC by convention). Carried as a distinct
117    /// type tag so the PG-wire layer can advertise OID 1184 (PG's
118    /// `timestamp with time zone`) and `sqlx`/`pgx`/JDBC clients
119    /// decode into their TZ-aware datetime types. The internal
120    /// semantics are unchanged: SPG never stored per-row offsets,
121    /// and neither did PG — `TIMESTAMPTZ` in PG is also UTC i64.
122    Timestamptz,
123    /// `INTERVAL` — calendar-aware span (months + microseconds). v2.11
124    /// supports INTERVAL only as a runtime intermediate (literals,
125    /// arithmetic results); on-disk encoding is rejected so this branch
126    /// can't appear in a `ColumnSchema`.
127    Interval,
128    /// v4.9: `JSON` — text-backed JSON document. We don't parse
129    /// the content (no path operators or jsonb functions yet) —
130    /// the column accepts any TEXT-compatible value and round-trips
131    /// it verbatim. PG OID 114 on the wire.
132    Json,
133    /// v7.9.0: `JSONB` — semantically identical to `Json` on
134    /// the storage side (same `Value::Json` cells, same
135    /// row codec), but advertised as PG OID 3802 on the wire
136    /// so `sqlx`-style clients that bind `jsonb` columns
137    /// decode correctly. mailrs migration blocker #3.
138    Jsonb,
139    /// v7.10.4: `BYTES` / `BYTEA` — variable-length raw binary.
140    /// Backed by `Value::Bytes(Vec<u8>)`. PG wire OID 17. Literal
141    /// forms accepted by parser/engine: PG hex form `'\xDEADBEEF'`
142    /// (case-insensitive hex pairs) and escape form
143    /// `'foo\\000bar'` (the latter decoded at coercion time when
144    /// the target column is BYTEA — TEXT columns leave the
145    /// backslash sequence verbatim).
146    Bytes,
147    /// v7.10.9: `TEXT[]` — single-dimension TEXT array. Elements
148    /// may be NULL (PG semantics). PG wire OID 1009. Literal
149    /// forms: `ARRAY['a', 'b', NULL]` and the PG external form
150    /// `'{a,b,NULL}'::TEXT[]`. Engine implements `= ANY(arr)`,
151    /// `<> ALL(arr)`, and 1-based indexing `arr[i]`. Catalog
152    /// FILE_VERSION 18+; older snapshots reject this DataType
153    /// (forward-only by design — TEXT[] columns aren't readable
154    /// on a pre-v7.10 binary).
155    TextArray,
156    /// v7.11.12: `INT[]` — single-dimension i32 array. PG wire
157    /// OID 1007 (_int4). Same `ARRAY[...]` / `'{1,2,3}'::INT[]`
158    /// literal surface as TEXT[]. Catalog FILE_VERSION 19+.
159    IntArray,
160    /// v7.11.12: `BIGINT[]` — single-dimension i64 array. PG
161    /// wire OID 1016 (_int8). Catalog FILE_VERSION 19+.
162    BigIntArray,
163    /// v7.12.0: PG `tsvector` — ordered, deduplicated set of
164    /// `(lexeme, positions, weight)` tuples. PG wire OID 3614.
165    /// Catalog FILE_VERSION 20+. Storage shape is row-codec
166    /// tag 22; the schema-agnostic `write_value` path emits tag
167    /// 18. Literal: `'foo:1 bar:2,3'::tsvector` (PG external
168    /// form). G-CRIT-3 entry — v7.12.0 only ships the type +
169    /// codec; matching `@@` lands in v7.12.2.
170    TsVector,
171    /// v7.12.0: PG `tsquery` — parse tree of lexemes joined by
172    /// `&` `|` `!` and phrase operators. PG wire OID 3615.
173    /// Catalog FILE_VERSION 20+.
174    TsQuery,
175}
176
177impl fmt::Display for DataType {
178    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179        match self {
180            Self::SmallInt => f.write_str("SMALLINT"),
181            Self::Int => f.write_str("INT"),
182            Self::BigInt => f.write_str("BIGINT"),
183            Self::Float => f.write_str("FLOAT"),
184            Self::Text => f.write_str("TEXT"),
185            Self::Varchar(n) => write!(f, "VARCHAR({n})"),
186            Self::Char(n) => write!(f, "CHAR({n})"),
187            Self::Bool => f.write_str("BOOL"),
188            Self::Vector { dim, encoding } => match encoding {
189                VecEncoding::F32 => write!(f, "VECTOR({dim})"),
190                VecEncoding::Sq8 => write!(f, "VECTOR({dim}) USING SQ8"),
191                VecEncoding::F16 => write!(f, "VECTOR({dim}) USING HALF"),
192            },
193            Self::Numeric { precision, scale } => {
194                if *scale == 0 {
195                    write!(f, "NUMERIC({precision})")
196                } else {
197                    write!(f, "NUMERIC({precision}, {scale})")
198                }
199            }
200            Self::Date => f.write_str("DATE"),
201            Self::Timestamp => f.write_str("TIMESTAMP"),
202            Self::Timestamptz => f.write_str("TIMESTAMPTZ"),
203            Self::Interval => f.write_str("INTERVAL"),
204            Self::Json => f.write_str("JSON"),
205            Self::Jsonb => f.write_str("JSONB"),
206            Self::Bytes => f.write_str("BYTEA"),
207            Self::TextArray => f.write_str("TEXT[]"),
208            Self::IntArray => f.write_str("INT[]"),
209            Self::BigIntArray => f.write_str("BIGINT[]"),
210            Self::TsVector => f.write_str("TSVECTOR"),
211            Self::TsQuery => f.write_str("TSQUERY"),
212        }
213    }
214}
215
216/// v7.12.0 — one entry in a `Value::TsVector`. The lexeme is the
217/// (already-tokenised + stemmed in v7.12.1+) word; `positions` is
218/// a strictly-ascending list of 1-based positions; `weight` is the
219/// PG weight letter (A=3, B=2, C=1, D=0) — v7.12.0 defaults every
220/// lexeme to D, the v7.12.2 ranking path consumes the weight.
221#[derive(Debug, Clone, PartialEq, Eq)]
222pub struct TsLexeme {
223    pub word: String,
224    pub positions: Vec<u16>,
225    pub weight: u8,
226}
227
228/// v7.12.0 — parse tree for a PG `tsquery`. v7.12.0 ships the
229/// type + codec only; the `to_tsquery` / `plainto_tsquery` lexer
230/// lands in v7.12.1 and the `@@` evaluator in v7.12.2.
231#[derive(Debug, Clone, PartialEq, Eq)]
232pub enum TsQueryAst {
233    /// Single lexeme term. The `weight_mask` is the PG-style
234    /// bitmask of accepted weights (`A=1<<3`, `B=1<<2`, `C=1<<1`,
235    /// `D=1<<0`); `0` = any weight. v7.12.0 always sets it to 0.
236    Term {
237        word: String,
238        weight_mask: u8,
239    },
240    And(Box<TsQueryAst>, Box<TsQueryAst>),
241    Or(Box<TsQueryAst>, Box<TsQueryAst>),
242    Not(Box<TsQueryAst>),
243    /// `phrase <distance> phrase`. v7.12.0 only persists this; the
244    /// match semantics arrive in v7.12.2 alongside `@@`.
245    Phrase {
246        left: Box<TsQueryAst>,
247        right: Box<TsQueryAst>,
248        distance: u16,
249    },
250}
251
252/// A row-cell value, including SQL `NULL`. `Float` uses `f64`; NaN compares
253/// non-equal to itself (PG behaviour) — `PartialEq` is derived so callers
254/// must opt into NaN-aware comparison if they need stronger guarantees.
255#[derive(Debug, Clone, PartialEq)]
256#[non_exhaustive]
257pub enum Value {
258    SmallInt(i16),
259    Int(i32),
260    BigInt(i64),
261    Float(f64),
262    Text(String),
263    Bool(bool),
264    Vector(Vec<f32>),
265    /// v6.0.1: 8-bit scalar-quantised vector cell. Lives in
266    /// columns declared `VECTOR(N) USING SQ8`. Layout per cell:
267    /// `Sq8Vector { min: f32, max: f32, bytes: Vec<u8> }` —
268    /// 4× compression vs `Vector(Vec<f32>)`. The wire layer
269    /// dequantises to `f32` on SELECT; INSERT path quantises
270    /// incoming `Vector(Vec<f32>)` cells into this variant.
271    Sq8Vector(crate::quantize::Sq8Vector),
272    /// v6.0.3: IEEE-754 binary16 vector cell. Lives in columns
273    /// declared `VECTOR(N) USING HALF`. Stores raw u16 LE bits
274    /// (2× compression vs `Vector(Vec<f32>)`). Wire / display
275    /// paths dequantise to f32 bit-exactly; INSERT path converts
276    /// incoming f32 vectors at the engine boundary.
277    HalfVector(crate::halfvec::HalfVector),
278    /// Exact fixed-point decimal. `scaled` holds the value as
279    /// `actual * 10^scale` so the storage type is always integral —
280    /// arithmetic never falls back to floating-point.
281    Numeric {
282        scaled: i128,
283        scale: u8,
284    },
285    /// Days since the Unix epoch (1970-01-01). Negative for earlier dates.
286    Date(i32),
287    /// Microseconds since the Unix epoch (1970-01-01T00:00:00Z).
288    Timestamp(i64),
289    /// Calendar span: `months` (variable-length) + `micros` (fixed-length).
290    /// Runtime-only — cannot appear in a stored row in v2.11.
291    Interval {
292        months: i32,
293        micros: i64,
294    },
295    /// v4.9 `JSON` — raw JSON text. No structural validation
296    /// happens at the storage layer; whatever the parser hands us
297    /// round-trips verbatim. Equality is byte-wise.
298    Json(String),
299    /// v7.10.4 `BYTEA` — raw binary blob. Equality is byte-wise.
300    /// Layout matches `Text`'s length-prefixed shape (`[u32 LE
301    /// len][bytes]`) under tag 18; the engine accepts PG hex
302    /// literals (`'\xDEADBEEF'`) and escape literals at the
303    /// coercion boundary.
304    Bytes(Vec<u8>),
305    /// v7.10.9 `TEXT[]` — single-dimension TEXT array with
306    /// optional NULL elements. Equality is element-wise. PG's
307    /// NULL-element comparison semantics: NULL ≠ NULL inside
308    /// arrays under `=`, so `[NULL] != [NULL]` (the engine
309    /// honours this).
310    TextArray(Vec<Option<String>>),
311    /// v7.11.12 `INT[]` — single-dimension i32 array with optional
312    /// NULL elements. Codec mirrors TextArray with i32 LE per
313    /// element instead of length-prefixed UTF-8.
314    IntArray(Vec<Option<i32>>),
315    /// v7.11.12 `BIGINT[]` — single-dimension i64 array with optional
316    /// NULL elements.
317    BigIntArray(Vec<Option<i64>>),
318    /// v7.12.0 `tsvector` — sorted-by-word, deduped lexeme set with
319    /// positions + weights. The engine enforces sort/dedup on
320    /// construction; consumers can rely on `lexemes.windows(2)`
321    /// being strictly ascending by `word`.
322    TsVector(Vec<TsLexeme>),
323    /// v7.12.0 `tsquery` — boolean / phrase parse tree over
324    /// lexemes. Engine builds via `to_tsquery` family.
325    TsQuery(TsQueryAst),
326    Null,
327}
328
329impl Value {
330    /// Type tag, or `None` for `NULL` (unknown at value level).
331    pub fn data_type(&self) -> Option<DataType> {
332        match self {
333            Self::SmallInt(_) => Some(DataType::SmallInt),
334            Self::Int(_) => Some(DataType::Int),
335            Self::BigInt(_) => Some(DataType::BigInt),
336            Self::Float(_) => Some(DataType::Float),
337            // `Text` covers both unbounded TEXT and bounded VARCHAR/CHAR
338            // — the constraint lives on the column schema, not the value.
339            Self::Text(_) => Some(DataType::Text),
340            Self::Bool(_) => Some(DataType::Bool),
341            Self::Vector(v) => Some(DataType::Vector {
342                dim: u32::try_from(v.len()).expect("vector dim ≤ u32"),
343                encoding: VecEncoding::F32,
344            }),
345            Self::Sq8Vector(q) => Some(DataType::Vector {
346                dim: u32::try_from(q.bytes.len()).expect("vector dim ≤ u32"),
347                encoding: VecEncoding::Sq8,
348            }),
349            Self::HalfVector(h) => Some(DataType::Vector {
350                dim: u32::try_from(h.dim()).expect("vector dim ≤ u32"),
351                encoding: VecEncoding::F16,
352            }),
353            // `Value::Numeric` doesn't carry its precision (the column
354            // schema does); we surface precision=0 as "unknown" and let
355            // the engine reconcile against the column type at coercion
356            // time.
357            Self::Numeric { scale, .. } => Some(DataType::Numeric {
358                precision: 0,
359                scale: *scale,
360            }),
361            Self::Date(_) => Some(DataType::Date),
362            Self::Timestamp(_) => Some(DataType::Timestamp),
363            Self::Interval { .. } => Some(DataType::Interval),
364            Self::Json(_) => Some(DataType::Json),
365            Self::Bytes(_) => Some(DataType::Bytes),
366            Self::TextArray(_) => Some(DataType::TextArray),
367            Self::IntArray(_) => Some(DataType::IntArray),
368            Self::BigIntArray(_) => Some(DataType::BigIntArray),
369            Self::TsVector(_) => Some(DataType::TsVector),
370            Self::TsQuery(_) => Some(DataType::TsQuery),
371            Self::Null => None,
372        }
373    }
374
375    pub const fn is_null(&self) -> bool {
376        matches!(self, Self::Null)
377    }
378}
379
380/// One table row — values are positional and must match
381/// `TableSchema.columns` in length and (modulo NULL) in `DataType`.
382#[derive(Debug, Clone, PartialEq)]
383pub struct Row {
384    pub values: Vec<Value>,
385}
386
387impl Row {
388    pub const fn new(values: Vec<Value>) -> Self {
389        Self { values }
390    }
391
392    pub fn len(&self) -> usize {
393        self.values.len()
394    }
395
396    pub fn is_empty(&self) -> bool {
397        self.values.is_empty()
398    }
399}
400
401#[derive(Debug, Clone, PartialEq)]
402pub struct ColumnSchema {
403    pub name: String,
404    pub ty: DataType,
405    pub nullable: bool,
406    /// Optional `DEFAULT` value, frozen at CREATE TABLE time. `None`
407    /// means "no default" (so omitted columns become NULL, or error
408    /// out when the column is NOT NULL). Literal defaults take this
409    /// path.
410    pub default: Option<Value>,
411    /// v7.9.21 — for DEFAULT expressions that need INSERT-time
412    /// evaluation (e.g. `DEFAULT now()`, `DEFAULT CURRENT_TIMESTAMP`),
413    /// the Display form of the expression. The engine re-parses
414    /// it on each INSERT default-fill, evaluates against an empty
415    /// row context, and coerces to the column type. mailrs G4.
416    /// Persisted in catalog FILE_VERSION 15+; older catalogs
417    /// deserialise with None.
418    pub runtime_default: Option<String>,
419    /// MySQL-style `AUTO_INCREMENT`. When set, an INSERT that leaves
420    /// this column unbound (or sets it to NULL) gets the next integer
421    /// computed from the column's current max + 1.
422    pub auto_increment: bool,
423}
424
425#[derive(Debug, Clone, PartialEq)]
426pub struct TableSchema {
427    pub name: String,
428    pub columns: Vec<ColumnSchema>,
429    /// v6.7.2 — per-table hot-tier byte budget override. `None`
430    /// falls through to the global `SPG_HOT_TIER_BYTES` setting;
431    /// `Some(n)` overrides it for this specific table. Set via
432    /// `ALTER TABLE t SET hot_tier_bytes = X`. Persisted in
433    /// catalog FILE_VERSION 11+.
434    pub hot_tier_bytes: Option<u64>,
435    /// v7.6.1 — FOREIGN KEY constraints declared on this table.
436    /// Engine maintains this in lock-step with `spg-sql`'s parser
437    /// AST; the storage layer carries the on-disk shape so a
438    /// catalog snapshot round-trips without external mapping.
439    /// Persisted in catalog FILE_VERSION 13+. Older catalogs
440    /// deserialise with an empty vec.
441    pub foreign_keys: Vec<ForeignKeyConstraint>,
442    /// v7.9.19 — composite UNIQUE / PRIMARY KEY constraints
443    /// declared at the table level. Each entry's leading column
444    /// has a BTree index (created via the constraint), and INSERT
445    /// path enforces the full-tuple uniqueness via a scan keyed
446    /// by the leading column. Persisted in catalog FILE_VERSION
447    /// 15+. Older catalogs (≤ 14) deserialise with an empty vec.
448    pub uniqueness_constraints: Vec<UniquenessConstraint>,
449    /// v7.13.0 — `CHECK (<expr>)` predicates declared on this
450    /// table. Both column-level inline `CHECK (…)` and
451    /// table-level `CHECK (…)` fold into this list. Each entry
452    /// is the AST Expr's `Display` form, re-parsed on every
453    /// INSERT/UPDATE and evaluated against the candidate row.
454    /// A false / NULL result rejects the mutation (PG semantics).
455    /// Persisted in catalog FILE_VERSION 23+. Older catalogs
456    /// deserialise with an empty vec.
457    pub checks: Vec<String>,
458}
459
460/// v7.9.19 — composite UNIQUE / PRIMARY KEY constraint persisted
461/// on the table schema. The leading column always has a BTree
462/// index (created at CREATE TABLE time); INSERT enforcement
463/// scans that index for collisions on the full column tuple.
464#[derive(Debug, Clone, PartialEq, Eq)]
465pub struct UniquenessConstraint {
466    /// `true` when this constraint was declared as `PRIMARY KEY`
467    /// (vs `UNIQUE`). Semantically PK implies NOT NULL on all
468    /// referenced columns; the engine enforces that at CREATE
469    /// TABLE time.
470    pub is_primary_key: bool,
471    /// Column positions on the parent table. ≥ 1 element. For
472    /// single-column UNIQUE this is exactly one position; the
473    /// BTree index alone enforces it.
474    pub columns: Vec<usize>,
475    /// v7.13.0 — `UNIQUE NULLS NOT DISTINCT` modifier
476    /// (mailrs round-5 G10; PG 15+ surface). When `true`, two
477    /// rows whose constrained columns are all NULL collide on
478    /// the constraint. Default (`false`) is the SQL-standard
479    /// `NULLS DISTINCT` behaviour where any NULL passes.
480    /// Persisted in catalog FILE_VERSION 23+.
481    pub nulls_not_distinct: bool,
482}
483
484/// v7.6.1 — Storage-layer mirror of `spg_sql::ast::ForeignKeyConstraint`.
485/// The engine's CREATE TABLE path translates between the two; keeping
486/// them separate preserves the no-deps boundary between
487/// `spg-storage` and `spg-sql`.
488#[derive(Debug, Clone, PartialEq, Eq)]
489pub struct ForeignKeyConstraint {
490    /// Optional user-supplied constraint name (`CONSTRAINT <name>`
491    /// prefix). Used by `ALTER TABLE DROP CONSTRAINT <name>` in
492    /// v7.6.8; ignored by enforcement.
493    pub name: Option<String>,
494    /// Positions of local columns in this table's column list.
495    /// Same arity as `parent_columns`.
496    pub local_columns: Vec<usize>,
497    /// Referenced parent table name.
498    pub parent_table: String,
499    /// Positions of parent columns in the parent's column list.
500    /// Engine resolves these at CREATE TABLE time (after the parent
501    /// schema is known) so enforcement paths can skip the name
502    /// lookup on every row.
503    pub parent_columns: Vec<usize>,
504    /// Referential action when a parent row is deleted.
505    pub on_delete: FkAction,
506    /// Referential action when a parent row's referenced columns
507    /// are updated.
508    pub on_update: FkAction,
509}
510
511/// v7.6.1 — referential action tag. Mirrors `spg_sql::ast::FkAction`.
512#[derive(Debug, Clone, Copy, PartialEq, Eq)]
513pub enum FkAction {
514    Restrict,
515    Cascade,
516    SetNull,
517    SetDefault,
518    NoAction,
519}
520
521impl FkAction {
522    /// On-disk tag byte (v13 catalog appendix).
523    pub const fn tag(self) -> u8 {
524        match self {
525            Self::Restrict => 0,
526            Self::Cascade => 1,
527            Self::SetNull => 2,
528            Self::SetDefault => 3,
529            Self::NoAction => 4,
530        }
531    }
532    pub const fn from_tag(b: u8) -> Option<Self> {
533        Some(match b {
534            0 => Self::Restrict,
535            1 => Self::Cascade,
536            2 => Self::SetNull,
537            3 => Self::SetDefault,
538            4 => Self::NoAction,
539            _ => return None,
540        })
541    }
542}
543
544impl TableSchema {
545    pub fn column_position(&self, name: &str) -> Option<usize> {
546        self.columns.iter().position(|c| c.name == name)
547    }
548}
549
550/// Key type accepted by secondary indices. Float / NULL / Vector values
551/// can't participate in a B-tree index — `f64` is only `PartialOrd`, NULL
552/// has SQL-three-valued semantics, and Vector belongs to the (future) HNSW
553/// path. Index lookups on those columns fall back to full scan.
554#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
555pub enum IndexKey {
556    Int(i64),
557    Text(String),
558    Bool(bool),
559}
560
561impl IndexKey {
562    pub fn from_value(v: &Value) -> Option<Self> {
563        match v {
564            Value::SmallInt(n) => Some(Self::Int(i64::from(*n))),
565            Value::Int(n) => Some(Self::Int(i64::from(*n))),
566            Value::BigInt(n) => Some(Self::Int(*n)),
567            Value::Text(s) => Some(Self::Text(s.clone())),
568            Value::Bool(b) => Some(Self::Bool(*b)),
569            // Date/Timestamp use their integer storage repr as the
570            // index key — same order semantics, same comparison.
571            Value::Date(d) => Some(Self::Int(i64::from(*d))),
572            Value::Timestamp(t) => Some(Self::Int(*t)),
573            // Numeric isn't (yet) indexable — exact-decimal index keys
574            // would need a stable scale-normalised representation.
575            // Interval isn't index-eligible either (and can't reach this
576            // path through column storage anyway).
577            Value::Null
578            | Value::Float(_)
579            | Value::Vector(_)
580            | Value::Sq8Vector(_)
581            | Value::HalfVector(_)
582            | Value::Numeric { .. }
583            | Value::Interval { .. }
584            | Value::Json(_)
585            | Value::Bytes(_)
586            | Value::TextArray(_)
587            | Value::IntArray(_)
588            | Value::BigIntArray(_)
589            | Value::TsVector(_)
590            | Value::TsQuery(_) => None,
591        }
592    }
593}
594
595/// A single-column secondary index. v2.0 carries either a B-tree map
596/// (the default — used for equality / range lookups on scalar columns)
597/// or a navigable-small-world graph (used for kNN over vector
598/// columns).
599#[derive(Debug, Clone)]
600pub struct Index {
601    pub name: String,
602    pub column_position: usize,
603    pub kind: IndexKind,
604    /// v6.8.0 — column positions of `INCLUDE (col1, col2, …)`
605    /// non-key columns. Carries the planner's "this query is
606    /// covered by the index" signal; lookup paths still resolve
607    /// via the `RowLocator` to fetch the row body, but EXPLAIN
608    /// surfaces the covered-scan annotation so operators can
609    /// confirm the planner sees the coverage.
610    ///
611    /// Empty `Vec` = no `INCLUDE` clause (the legacy shape). v12
612    /// catalog snapshots deserialise with an empty vec.
613    pub included_columns: Vec<usize>,
614    /// v6.8.1 — partial-index predicate stored as its canonical
615    /// Display form (the engine re-parses it on the maintenance
616    /// path). `None` = unconditional index (the legacy shape).
617    /// Persisted as `[u8 has_pred][u16 LE len][bytes]` on the
618    /// catalog snapshot (FILE_VERSION 12, appended after
619    /// `included_columns`).
620    pub partial_predicate: Option<String>,
621    /// v6.8.2 — expression-index key, stored as the expression's
622    /// canonical Display form. `None` = bare column-reference
623    /// index (the legacy shape). Persisted alongside
624    /// `partial_predicate` on the v12 catalog snapshot.
625    pub expression: Option<String>,
626    /// v7.9.29 — `CREATE UNIQUE INDEX …`. When true the engine
627    /// rejects INSERTs whose key already appears in this index
628    /// (combined with `partial_predicate` when present — only
629    /// rows matching the predicate enter the uniqueness check).
630    /// Catalog FILE_VERSION 16+; older snapshots deserialise
631    /// with `false`. mailrs K1.
632    pub is_unique: bool,
633    /// v7.9.29 — extra (non-leading) column positions for
634    /// multi-column indexes (`CREATE INDEX … (a, b, c)`). The
635    /// planner today still only uses the leading
636    /// `column_position` for index seeks, but UNIQUE INDEX
637    /// enforcement walks the full tuple so partial-unique
638    /// invariants like CalDAV `(calendar_id, uid,
639    /// recurrence_id)` are enforced correctly. Catalog
640    /// FILE_VERSION 16+; older snapshots deserialise empty.
641    pub extra_column_positions: Vec<usize>,
642}
643
644/// Default neighbor degree (M) for the NSW graph. Picked at construction
645/// time and persisted with the index.
646pub const NSW_DEFAULT_M: usize = 16;
647
648/// v5.2.2: outcome of a successful [`Catalog::freeze_oldest_to_cold`]
649/// call. The catalog state has already been mutated by the time this
650/// is returned (hot rows dropped + segment registered + Cold locators
651/// flipped). The caller's only remaining concern is `segment_bytes` —
652/// persist them to disk under `<db>.spg/segments/seg_<id>.spg` so a
653/// future restart can reload via the v5.1 `SPG_PRELOAD_COLD_SEGMENT`
654/// path. (v5.3's manifest will subsume this manual step.)
655#[derive(Debug, Clone)]
656pub struct FreezeReport {
657    /// Id allocated by [`Catalog::load_segment_bytes`] for the new
658    /// cold-tier segment. Stable across the call's success path.
659    pub segment_id: u32,
660    /// Number of rows that moved hot → cold. Equals the `max_rows`
661    /// the caller asked for (the API is strict on the count).
662    pub frozen_rows: usize,
663    /// Hot-tier bytes reclaimed by the freeze — the
664    /// [`Table::hot_bytes`] delta before vs after. Useful to feed
665    /// back into the freezer's budget check on the next tick.
666    pub bytes_freed: u64,
667    /// Encoded segment bytes, byte-identical to what
668    /// [`encode_segment`] produced. The catalog already owns a
669    /// copy inside `cold_segments`; this hand-off lets the caller
670    /// persist them without re-encoding.
671    pub segment_bytes: Vec<u8>,
672}
673
674/// v6.7.4 — read-only output of [`Catalog::prepare_freeze_slice`].
675/// Carries every row body + key in a contiguous hot-row range,
676/// already encoded and sorted by PK so the coordinator's merge
677/// step is a k-way merge over already-sorted streams.
678///
679/// `Vec<FreezeSlice>` from N independent workers feeds
680/// [`Catalog::commit_freeze_slices`], which concats + encodes the
681/// merged segment + atomically swaps the catalog state.
682#[derive(Debug, Clone)]
683pub struct FreezeSlice {
684    /// Hot-row index range this slice covered (half-open, in the
685    /// table's `rows: PersistentVec` ordering at call time). The
686    /// commit step uses this to compute the union range that
687    /// gets passed to [`Table::delete_rows`].
688    pub row_range: core::ops::Range<usize>,
689    /// `(pk_u64, encoded_row_body, IndexKey)` triples, sorted
690    /// ascending by `pk_u64`. Per-slice sort happens inside
691    /// `prepare_freeze_slice`; the coordinator does only a
692    /// k-way merge to reach the global PK ordering
693    /// [`encode_segment`] requires.
694    pub rows: Vec<(u64, Vec<u8>, IndexKey)>,
695}
696
697/// v6.7.3 — outcome of a [`Catalog::compact_cold_segments`] call.
698/// The catalog state has already been mutated when this is returned:
699/// the merged segment is loaded into `cold_segments`, the source
700/// segment slots are tombstoned (`None`), and every BTree-index
701/// `RowLocator::Cold` that previously pointed at a source now
702/// points at the merged segment. The caller's remaining job is to
703/// persist `merged_segment_bytes` under
704/// `<db>.spg/segments/seg_<merged_segment_id>.spg` and update the
705/// in-memory `segment_id → path` map (remove the source ids, add
706/// the merged id) so the next CHECKPOINT writes a manifest that
707/// no longer lists the retired sources.
708///
709/// On a no-op (fewer than 2 candidate segments under the threshold),
710/// `merged_segment_id` is `None` and `sources` is empty; the
711/// catalog was not mutated.
712#[derive(Debug, Clone)]
713pub struct CompactReport {
714    /// Source segment ids that were merged + tombstoned.
715    pub sources: Vec<u32>,
716    /// Id allocated for the merged segment. `None` on no-op.
717    pub merged_segment_id: Option<u32>,
718    /// Encoded merged-segment bytes (empty on no-op).
719    pub merged_segment_bytes: Vec<u8>,
720    /// Number of rows that landed in the merged segment.
721    pub merged_rows: usize,
722    /// `Σ source.num_rows − merged_rows`. Rows present in source
723    /// segment payloads but unreferenced by any live BTree
724    /// `Cold` locator — DELETE'd-but-still-frozen rows that
725    /// compaction GC'd during the merge.
726    pub deleted_rows_pruned: usize,
727    /// `Σ source.bytes() − merged.bytes()`. Estimate of on-disk
728    /// space the merge will reclaim once the source segment files
729    /// are GC'd. Saturating subtract — never negative.
730    pub bytes_reclaimed_estimate: u64,
731}
732
733#[derive(Debug, Clone)]
734pub enum IndexKind {
735    /// v4.40: structural-sharing B-tree over `IndexKey`. Replaces the v0.8
736    /// `BTreeMap<IndexKey, Vec<usize>>` — `Index::clone` is now an `Arc`
737    /// bump regardless of index size, so `Catalog::clone` inside the
738    /// v4.34 auto-commit wrap stays O(1) even for tables with secondary
739    /// indices (the case that bottlenecked v4.39 at 1M rows in the
740    /// sweep).
741    ///
742    /// v5.1: value type widened from `Vec<usize>` to `Vec<RowLocator>` so
743    /// a single key can point to a mix of hot-tier rows (`RowLocator::Hot`,
744    /// equivalent to the pre-v5 `usize` row index) and cold-tier rows
745    /// (`RowLocator::Cold { segment_id, page_offset }`) once the v5.2
746    /// freezer starts producing them. Pre-v5.2 only `Hot` entries appear
747    /// — the on-disk encoding stays at `FILE_VERSION` 8 (raw u64 row index)
748    /// because every locator round-trips through `RowLocator::from_legacy_v8_u64`
749    /// without information loss. `FILE_VERSION` 9 with tagged encoding lands
750    /// alongside the first freezer commit (v5.1 step 2b / v5.2).
751    BTree(PersistentBTreeMap<IndexKey, Vec<RowLocator>>),
752    /// Navigable-small-world graph for vector kNN search.
753    Nsw(NswGraph),
754    /// v6.7.1 — BRIN (Block Range INdex). Pure metadata: BRIN
755    /// indexes carry NO in-memory key→locator map. The (min,
756    /// max) summaries live in each cold-tier segment's v2
757    /// envelope sidecar; the BRIN entry in `Table.indices` only
758    /// records THAT a BRIN index exists on this column so the
759    /// segment encoder + planner can opt into the summary path.
760    Brin {
761        /// The cell type at `column_position` at CREATE INDEX time.
762        /// Used by the planner to type-check WHERE-clause range
763        /// predicates against the BRIN-indexed column.
764        column_type: DataType,
765    },
766    /// v7.12.3 — GIN inverted index over a `tsvector` column.
767    ///
768    /// Storage shape: `lexeme word → Vec<RowLocator>`. The posting
769    /// list per word is appended in row-order, so range scans are
770    /// O(matching rows) once the per-word lookup is done. Multi-
771    /// term queries intersect / union posting lists.
772    ///
773    /// `IndexKey::from_value(TsVector)` returns `None` — GIN doesn't
774    /// participate in `try_index_seek` (which is BTree-equality-keyed).
775    /// The engine consults this index through `try_gin_lookup` on
776    /// `WHERE col @@ tsquery` predicates instead.
777    ///
778    /// Backed by a `PersistentBTreeMap` so `Catalog::clone` (the
779    /// per-write snapshot) stays O(1) — same structural-sharing
780    /// invariant as BTree.
781    Gin(PersistentBTreeMap<alloc::string::String, Vec<RowLocator>>),
782}
783
784/// Multi-layer HNSW graph (v2.13). Each node is assigned a `top_level`;
785/// it appears in layers `0..=top_level`. Higher layers are sparser, so
786/// search starts from the entry at the top layer, greedy-descends to
787/// layer 0, and beam-searches there. Layer 0 keeps a larger neighbour
788/// budget (`m_max_0 = 2 * m` per the HNSW paper); upper layers cap at
789/// `m`. The struct name stays `NswGraph` so external users / on-disk
790/// callers don't have to track a rename — the algorithm changed, the
791/// data slot didn't.
792#[derive(Debug, Clone)]
793pub struct NswGraph {
794    /// Max neighbours per node on layers ≥ 1.
795    pub m: usize,
796    /// Max neighbours on layer 0 (the dense bottom layer). HNSW
797    /// convention: `m_max_0 = 2 * m`.
798    pub m_max_0: usize,
799    /// Entry point — the node that sits on the topmost layer. Search
800    /// always starts here.
801    pub entry: Option<usize>,
802    /// Top layer of the entry node (== `layers.len() - 1` when populated).
803    pub entry_level: u8,
804    /// `levels[i]` = top layer of node `i`. Nodes whose vector cell is
805    /// NULL / non-Vector have `levels[i] = 0` and no neighbour entries.
806    ///
807    /// v5.5.0: backed by `PersistentVec` so `NswGraph::clone` (and the
808    /// `Catalog::clone` on every group-commit write that contains it) is O(1)
809    /// structural-sharing instead of an O(N) element copy.
810    pub levels: PersistentVec<u8>,
811    /// `layers[l][i]` = neighbours of node `i` at layer `l`. Inner vec
812    /// is empty when node `i` doesn't reach layer `l`.
813    ///
814    /// v5.5.0: the per-node middle dimension (the O(N) one) is a
815    /// `PersistentVec`; the outer layer dimension stays a plain `Vec`
816    /// (layer count ≤ 8, so its clone is O(1) in practice) and the inner
817    /// neighbour list stays a `Vec` (bounded by `m_max_0`).
818    ///
819    /// v6.1.x: neighbour slot widened from `usize` (8 B on 64-bit) to
820    /// `u32` (4 B). Row indices are catalog-bounded by `u32::MAX` (4G
821    /// rows per table); the cast at the NSW boundary asserts this. At
822    /// 1M dim-128 SQ8, layer 0 adjacency alone shrinks by ~128 MiB
823    /// — the largest single contribution to the v6.0.5-measured
824    /// 624 MiB ambition gap. On-disk format already used u32 LE, so
825    /// this is a pure in-memory layout change; no `FILE_VERSION` bump.
826    pub layers: Vec<PersistentVec<Vec<u32>>>,
827}
828
829impl NswGraph {
830    fn new(m: usize) -> Self {
831        Self {
832            m,
833            m_max_0: m.saturating_mul(2),
834            entry: None,
835            entry_level: 0,
836            levels: PersistentVec::new(),
837            layers: alloc::vec![PersistentVec::new()],
838        }
839    }
840
841    /// Max-neighbour budget for layer `l`.
842    pub const fn cap_for_layer(&self, layer: u8) -> usize {
843        if layer == 0 { self.m_max_0 } else { self.m }
844    }
845}
846
847/// Deterministic level assignment, seeded on the row index so the same
848/// insert order reproduces the same topology. Distribution is roughly
849/// HNSW-flavoured with `mL ≈ 1/ln(M) ≈ 0.36` for M=16: each 4-bit
850/// chunk that comes up zero promotes the node one layer (so P(level ≥
851/// L) ≈ (1/16)^L).
852#[allow(clippy::verbose_bit_mask)] // clippy suggests trailing_zeros(); we need an explicit MAX cap and a stable distribution shape.
853pub fn nsw_assign_level(row_idx: usize) -> u8 {
854    const MAX_LEVEL: u8 = 7; // 7 ⇒ ~16^7 ≈ 2.7e8 expected nodes between promotions; ample.
855    // SplitMix-style mixer — cheap and seedable.
856    let mut x = (row_idx as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15);
857    x ^= x >> 30;
858    x = x.wrapping_mul(0xBF58_476D_1CE4_E5B9);
859    x ^= x >> 27;
860    x = x.wrapping_mul(0x94D0_49BB_1331_11EB);
861    x ^= x >> 31;
862    // Count contiguous low-end zero nibbles (4-bit chunks). Each zero
863    // nibble has probability 1/16, mirroring HNSW's `mL ≈ 1/ln(M)` for
864    // M=16. `trailing_zeros / 4` would lose the ordering when x = 0, so
865    // a plain loop with a cap is clearer.
866    let mut level: u8 = 0;
867    while x & 0xF == 0 && level < MAX_LEVEL {
868        level += 1;
869        x >>= 4;
870    }
871    level
872}
873
874impl Index {
875    fn new_btree(name: String, column_position: usize) -> Self {
876        Self {
877            name,
878            column_position,
879            kind: IndexKind::BTree(PersistentBTreeMap::new()),
880            included_columns: Vec::new(),
881            partial_predicate: None,
882            expression: None,
883            is_unique: false,
884            extra_column_positions: Vec::new(),
885        }
886    }
887
888    fn new_nsw(name: String, column_position: usize, m: usize) -> Self {
889        Self {
890            name,
891            column_position,
892            kind: IndexKind::Nsw(NswGraph::new(m)),
893            included_columns: Vec::new(),
894            partial_predicate: None,
895            expression: None,
896            is_unique: false,
897            extra_column_positions: Vec::new(),
898        }
899    }
900
901    /// v6.7.1 — BRIN index constructor. BRIN carries no in-memory
902    /// data; the `column_type` snapshot is used by the segment
903    /// encoder + planner for type-checking range predicates.
904    fn new_brin(name: String, column_position: usize, column_type: DataType) -> Self {
905        Self {
906            name,
907            column_position,
908            kind: IndexKind::Brin { column_type },
909            included_columns: Vec::new(),
910            partial_predicate: None,
911            expression: None,
912            is_unique: false,
913            extra_column_positions: Vec::new(),
914        }
915    }
916
917    /// v7.12.3 — GIN inverted-index constructor. Empty posting-list
918    /// map; caller (typically [`Table::add_gin_index`] or
919    /// [`Table::restore_gin_index`]) populates it from existing rows
920    /// or from a deserialised snapshot.
921    fn new_gin(name: String, column_position: usize) -> Self {
922        Self {
923            name,
924            column_position,
925            kind: IndexKind::Gin(PersistentBTreeMap::new()),
926            included_columns: Vec::new(),
927            partial_predicate: None,
928            expression: None,
929            is_unique: false,
930            extra_column_positions: Vec::new(),
931        }
932    }
933
934    /// Look up the locators stored under `key` (B-tree only). Returns
935    /// an empty slice when the key is absent or the index isn't a
936    /// BTree — callers can treat both cases uniformly.
937    ///
938    /// v5.1: return type widened from `&[usize]` to `&[RowLocator]`.
939    /// Pre-v5.2 callers can read the slice and `.as_hot().unwrap()`
940    /// each entry (no `Cold` variants exist until the freezer lands);
941    /// post-v5.2 callers dispatch hot vs. cold per locator.
942    pub fn lookup_eq(&self, key: &IndexKey) -> &[RowLocator] {
943        match &self.kind {
944            IndexKind::BTree(m) => m.get(key).map_or(&[][..], Vec::as_slice),
945            // BRIN / NSW / GIN have no IndexKey-keyed map; lookup is a no-op.
946            // GIN uses [`Index::gin_lookup_word`] instead.
947            IndexKind::Nsw(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => &[][..],
948        }
949    }
950
951    /// v7.12.3 — GIN posting-list lookup. Returns the row locators
952    /// whose `tsvector` cell contains `word`. Empty when the word is
953    /// absent from the index or this isn't a GIN index.
954    pub fn gin_lookup_word(&self, word: &str) -> &[RowLocator] {
955        match &self.kind {
956            IndexKind::Gin(m) => m.get(&String::from(word)).map_or(&[][..], Vec::as_slice),
957            IndexKind::BTree(_) | IndexKind::Nsw(_) | IndexKind::Brin { .. } => &[][..],
958        }
959    }
960
961    /// Borrow the NSW graph (if this is an NSW index). Callers that need
962    /// the graph for a kNN search go through here.
963    pub const fn nsw(&self) -> Option<&NswGraph> {
964        match &self.kind {
965            IndexKind::Nsw(g) => Some(g),
966            IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => None,
967        }
968    }
969
970    /// v6.7.1 — true when this index is a BRIN (block range) index.
971    /// Used by the segment encoder to opt into BRIN sidecar emission
972    /// at freeze time, and by the planner to opt into page-skipping
973    /// on range predicates.
974    pub const fn is_brin(&self) -> bool {
975        matches!(self.kind, IndexKind::Brin { .. })
976    }
977
978    /// v7.12.3 — true when this index is a GIN inverted index.
979    /// Used by the planner to opt into posting-list acceleration on
980    /// `WHERE col @@ tsquery` predicates.
981    pub const fn is_gin(&self) -> bool {
982        matches!(self.kind, IndexKind::Gin(_))
983    }
984}
985
986/// In-memory table: schema + a persistent row vector + secondary indices.
987///
988/// v4.39: `rows` is a [`PersistentVec`] (Bitmapped Vector Trie, 32-way) so
989/// `Table::clone()` is `O(1)` — the whole reason for v4.39's existence is
990/// to make `Catalog::clone()` cheap inside the v4.34 auto-commit wrap.
991///
992/// v5.2.1: `hot_bytes` tracks the encoded byte size of every row currently
993/// in [`Self::rows`], summed over rows. Updated incrementally by `insert`
994/// (+= encoded row size), `delete_rows` (-= removed rows' encoded sizes),
995/// and `update_row` (-= old size, += new size). The value is what the
996/// v5.2 freezer reads to decide when to demote cold rows — when the
997/// catalog-wide sum crosses `SPG_HOT_TIER_BYTES` (default 4 GiB) the
998/// freezer thread wakes. v5.2.1 ships measurement only; the freezer
999/// itself lands in v5.2.2. Stored as `u64` so a single field clone in
1000/// `Catalog::clone` stays at the O(1) invariant v4.39 built.
1001#[derive(Debug, Clone)]
1002pub struct Table {
1003    schema: TableSchema,
1004    rows: PersistentVec<Row>,
1005    indices: Vec<Index>,
1006    hot_bytes: u64,
1007    /// v6.7.0 — cached count of rows currently materialised in the
1008    /// cold tier via `RowLocator::Cold` entries across THIS table's
1009    /// indices. Populated by `ANALYZE` (walks every BTree index and
1010    /// counts Cold locators); the count survives until the next
1011    /// ANALYZE recomputes it. Surfaced via `spg_statistic.cold_row_count`
1012    /// and `spg_stat_segment.table_name`.
1013    ///
1014    /// Honest scope: this is a CACHED count, not a live one.
1015    /// Freezer / promote / DELETE don't currently update the cache
1016    /// incrementally — they invalidate it by setting the
1017    /// `cold_row_count_stale` flag, and the next ANALYZE re-walks.
1018    /// Incremental maintenance is a v6.7.x candidate if observation
1019    /// shows the ANALYZE walk cost dominates.
1020    cold_row_count: u64,
1021    /// v6.7.0 — set when the cached `cold_row_count` may be wrong
1022    /// because rows moved into / out of the cold tier since the last
1023    /// ANALYZE. The virtual-table surface reports the cached value
1024    /// regardless (operators run ANALYZE to refresh).
1025    cold_row_count_stale: bool,
1026}
1027
1028impl Table {
1029    pub fn new(schema: TableSchema) -> Self {
1030        Self {
1031            schema,
1032            rows: PersistentVec::new(),
1033            indices: Vec::new(),
1034            hot_bytes: 0,
1035            cold_row_count: 0,
1036            cold_row_count_stale: false,
1037        }
1038    }
1039
1040    /// Total encoded byte size of every row currently in the hot tier
1041    /// (`self.rows`). See struct docs for the maintenance contract.
1042    /// Returns 0 for an empty table.
1043    #[must_use]
1044    pub const fn hot_bytes(&self) -> u64 {
1045        self.hot_bytes
1046    }
1047
1048    /// v6.7.0 — cached count of cold-tier rows. See struct field
1049    /// docs for the staleness contract.
1050    #[must_use]
1051    pub const fn cold_row_count(&self) -> u64 {
1052        self.cold_row_count
1053    }
1054
1055    /// v6.7.0 — overwrite the cached count. Called by the engine's
1056    /// `analyze_one_table` after walking the indices.
1057    pub fn set_cold_row_count(&mut self, n: u64) {
1058        self.cold_row_count = n;
1059        self.cold_row_count_stale = false;
1060    }
1061
1062    /// v6.7.0 — mark the cached count as potentially out of date.
1063    /// Called by freezer / promote / DELETE paths so a subsequent
1064    /// `spg_statistic` read knows the number may not reflect the
1065    /// current state.
1066    pub fn mark_cold_row_count_stale(&mut self) {
1067        self.cold_row_count_stale = true;
1068    }
1069
1070    /// v6.7.0 — report whether the cached count is known to be out
1071    /// of date. Exposed for completeness; the virtual table surface
1072    /// returns the cached value regardless.
1073    #[must_use]
1074    pub const fn cold_row_count_stale(&self) -> bool {
1075        self.cold_row_count_stale
1076    }
1077
1078    /// v6.7.0 — walk every BTree index and count `RowLocator::Cold`
1079    /// entries; return the MAX across indices. The freeze path
1080    /// (`freeze_oldest_to_cold`) writes cold locators to ONE
1081    /// designated index — that index ends up with the full per-row
1082    /// count. MAX-across-indices yields the precise count when a
1083    /// PK-style index exists; for multi-index tables without a
1084    /// covering index it's a lower bound (rare in practice).
1085    /// Caller responsibility: only invoke under `engine.write()`
1086    /// or after taking ownership; the walk is O(N) over every
1087    /// (key, locator) pair.
1088    #[must_use]
1089    pub fn count_cold_locators(&self) -> u64 {
1090        let mut best: u64 = 0;
1091        for idx in &self.indices {
1092            if let IndexKind::BTree(map) = &idx.kind {
1093                let n: u64 = map
1094                    .iter()
1095                    .map(|(_, locs)| locs.iter().filter(|l| l.is_cold()).count() as u64)
1096                    .sum();
1097                if n > best {
1098                    best = n;
1099                }
1100            }
1101        }
1102        best
1103    }
1104
1105    pub const fn schema(&self) -> &TableSchema {
1106        &self.schema
1107    }
1108
1109    /// v6.7.2 — mutable schema accessor for ALTER TABLE paths.
1110    /// Used by `Engine::exec_alter_table` to flip per-table
1111    /// settings like `hot_tier_bytes`.
1112    pub const fn schema_mut(&mut self) -> &mut TableSchema {
1113        &mut self.schema
1114    }
1115
1116    /// v4.39: returns the persistent row vector by reference. Callers that
1117    /// used to take `&[Row]` should switch to `.iter()` (via
1118    /// `IntoIterator for &PersistentVec`) or `.get(i)` for indexing.
1119    pub const fn rows(&self) -> &PersistentVec<Row> {
1120        &self.rows
1121    }
1122
1123    pub const fn row_count(&self) -> usize {
1124        self.rows.len()
1125    }
1126
1127    /// v6.8.0 — exposed for the engine layer to patch
1128    /// `Index::included_columns` post-creation. Could fold into
1129    /// `add_index` once the engine's IF-NOT-EXISTS guard moves up,
1130    /// but the patch shape is the minimal change for v6.8.0.
1131    pub fn indices_mut(&mut self) -> &mut [Index] {
1132        &mut self.indices
1133    }
1134
1135    pub fn indices(&self) -> &[Index] {
1136        &self.indices
1137    }
1138
1139    /// Compute the next `AUTO_INCREMENT` value for the column at
1140    /// `col_pos`. Defined as `max(existing) + 1`, falling back to `1`
1141    /// when the column currently holds no integer values. NULL / non-
1142    /// integer cells are skipped. Returns `None` when the column isn't
1143    /// an integer type.
1144    pub fn next_auto_value(&self, col_pos: usize) -> Option<i64> {
1145        let ty = self.schema.columns.get(col_pos)?.ty;
1146        if !matches!(ty, DataType::SmallInt | DataType::Int | DataType::BigInt) {
1147            return None;
1148        }
1149        let mut max: Option<i64> = None;
1150        for row in &self.rows {
1151            match row.values.get(col_pos) {
1152                Some(Value::SmallInt(n)) => {
1153                    let v = i64::from(*n);
1154                    max = Some(max.map_or(v, |m| m.max(v)));
1155                }
1156                Some(Value::Int(n)) => {
1157                    let v = i64::from(*n);
1158                    max = Some(max.map_or(v, |m| m.max(v)));
1159                }
1160                Some(Value::BigInt(n)) => {
1161                    max = Some(max.map_or(*n, |m| m.max(*n)));
1162                }
1163                _ => {}
1164            }
1165        }
1166        Some(max.map_or(1, |m| m + 1))
1167    }
1168
1169    /// Return the first index defined over `column_position`, if any.
1170    /// (`v0.8` supports at most one index per column logically; the search
1171    /// just picks the first match.)
1172    pub fn index_on(&self, column_position: usize) -> Option<&Index> {
1173        // v6.7.1 — prefer BTree (has the key→locator map needed
1174        // for `lookup_eq`) over BRIN (metadata-only). When only a
1175        // BRIN exists on the column, return None so the executor
1176        // falls back to the hot-tier row scan instead of trying
1177        // to use BRIN for an equality lookup (which would always
1178        // return an empty slice and look like "no rows matched").
1179        self.indices
1180            .iter()
1181            .find(|i| i.column_position == column_position && matches!(i.kind, IndexKind::BTree(_)))
1182            .or_else(|| {
1183                self.indices.iter().find(|i| {
1184                    i.column_position == column_position && matches!(i.kind, IndexKind::Nsw(_))
1185                })
1186            })
1187    }
1188
1189    /// Insert one row after validating it matches the schema (length + type).
1190    /// Returns `StorageError` on mismatch — the table is left unchanged.
1191    /// Updates every defined index with the new row's key.
1192    pub fn insert(&mut self, row: Row) -> Result<(), StorageError> {
1193        if row.len() != self.schema.columns.len() {
1194            return Err(StorageError::ArityMismatch {
1195                expected: self.schema.columns.len(),
1196                actual: row.len(),
1197            });
1198        }
1199        for (i, (val, col)) in row.values.iter().zip(&self.schema.columns).enumerate() {
1200            if val.is_null() {
1201                if !col.nullable {
1202                    return Err(StorageError::NullInNotNull {
1203                        column: col.name.clone(),
1204                    });
1205                }
1206                continue;
1207            }
1208            let actual = val.data_type().expect("non-null");
1209            // Vector columns require both that the value's variant be Vector
1210            // *and* its dimension match. `actual == col.ty` already encodes
1211            // both because DataType::Vector carries the dim.
1212            //
1213            // VARCHAR(n) / CHAR(n) are storage-equivalent to TEXT — the
1214            // length / padding contract is enforced upstream by
1215            // `coerce_value`. Accept a `Text` value into either.
1216            //
1217            // NUMERIC's `Value::Numeric` carries its actual scale but the
1218            // column declares the *expected* scale (a scale-rescaled
1219            // Value::Numeric is produced upstream by `coerce_value`); the
1220            // structural check here only verifies "value is Numeric and
1221            // its scale equals the column scale".
1222            let compatible = actual == col.ty
1223                || matches!(
1224                    (actual, col.ty),
1225                    (
1226                        DataType::Text,
1227                        DataType::Varchar(_) | DataType::Char(_) | DataType::Json | DataType::Jsonb
1228                    ) | (DataType::Json | DataType::Jsonb, DataType::Text)
1229                        | (DataType::Json, DataType::Jsonb)
1230                        | (DataType::Jsonb, DataType::Json)
1231                        | (DataType::Timestamp, DataType::Timestamptz)
1232                        | (DataType::Timestamptz, DataType::Timestamp)
1233                )
1234                || matches!(
1235                    (actual, col.ty),
1236                    (
1237                        DataType::Numeric { scale: a, .. },
1238                        DataType::Numeric { scale: b, .. },
1239                    ) if a == b
1240                );
1241            if !compatible {
1242                return Err(StorageError::TypeMismatch {
1243                    column: col.name.clone(),
1244                    expected: col.ty,
1245                    actual,
1246                    position: i,
1247                });
1248            }
1249        }
1250        let new_row_idx = self.rows.len();
1251        // Pre-validate before mutating: ensure indices receive an IndexKey.
1252        // For NSW we defer the graph update to *after* the row is pushed
1253        // so the kNN search can see it in `self.rows`.
1254        for idx in &mut self.indices {
1255            match &mut idx.kind {
1256                IndexKind::BTree(map) => {
1257                    if let Some(key) = IndexKey::from_value(&row.values[idx.column_position]) {
1258                        // v4.40: PersistentBTreeMap has no in-place entry-or-default.
1259                        // Clone-then-insert keeps the same semantics — for typical
1260                        // unique-key schemas the Vec is 1-element so the clone is
1261                        // O(1). For dup-heavy columns it's O(M) per insert, traded
1262                        // for the structural-sharing win at clone time.
1263                        let mut entries = map.get(&key).cloned().unwrap_or_default();
1264                        entries.push(RowLocator::Hot(new_row_idx));
1265                        map.insert_mut(key, entries);
1266                    }
1267                }
1268                IndexKind::Gin(map) => {
1269                    // v7.12.3 — extend posting list per lexeme word.
1270                    // NULL or non-TsVector cell → no-op (cell carries
1271                    // no lexemes to index).
1272                    if let Value::TsVector(lexemes) = &row.values[idx.column_position] {
1273                        for lex in lexemes {
1274                            let mut entries = map.get(&lex.word).cloned().unwrap_or_default();
1275                            entries.push(RowLocator::Hot(new_row_idx));
1276                            map.insert_mut(lex.word.clone(), entries);
1277                        }
1278                    }
1279                }
1280                // NSW handled below after the row push (so the new row
1281                // is visible to the kNN-graph connect step). BRIN
1282                // carries no per-row state.
1283                IndexKind::Nsw(_) | IndexKind::Brin { .. } => {}
1284            }
1285        }
1286        // v5.2.1: maintain incremental hot-tier byte counter. Computed
1287        // before the move so we don't need to borrow `row` after push.
1288        self.hot_bytes = self
1289            .hot_bytes
1290            .saturating_add(row_body_encoded_len(&row, &self.schema) as u64);
1291        // v4.39.1: push_mut keeps streaming inserts at Vec::push speed when
1292        // the table is uniquely owned (the spg-embedded path); inside a TX
1293        // wrap where a Catalog snapshot exists, push_mut path-copies the
1294        // tail just like push() and the snapshot stays valid.
1295        self.rows.push_mut(row);
1296        // NSW updates after the push so the new row is visible to the
1297        // greedy search used during connect.
1298        let new_row_idx = self.rows.len() - 1;
1299        let nsw_targets: Vec<usize> = self
1300            .indices
1301            .iter()
1302            .enumerate()
1303            .filter_map(|(i, idx)| {
1304                if matches!(idx.kind, IndexKind::Nsw(_)) {
1305                    Some(i)
1306                } else {
1307                    None
1308                }
1309            })
1310            .collect();
1311        for idx_pos in nsw_targets {
1312            nsw_insert_at(self, idx_pos, new_row_idx);
1313        }
1314        Ok(())
1315    }
1316
1317    /// Build a new B-tree index over the named column. Rebuilds from
1318    /// existing rows. Errors if `column_name` doesn't exist or the index
1319    /// name is taken.
1320    pub fn add_index(&mut self, name: String, column_name: &str) -> Result<(), StorageError> {
1321        if self.indices.iter().any(|i| i.name == name) {
1322            return Err(StorageError::DuplicateIndex { name });
1323        }
1324        let column_position = self.schema.column_position(column_name).ok_or_else(|| {
1325            StorageError::ColumnNotFound {
1326                column: column_name.into(),
1327            }
1328        })?;
1329        let mut idx = Index::new_btree(name, column_position);
1330        if let IndexKind::BTree(map) = &mut idx.kind {
1331            for (i, row) in self.rows.iter().enumerate() {
1332                if let Some(key) = IndexKey::from_value(&row.values[column_position]) {
1333                    let mut entries = map.get(&key).cloned().unwrap_or_default();
1334                    entries.push(RowLocator::Hot(i));
1335                    map.insert_mut(key, entries);
1336                }
1337            }
1338        }
1339        self.indices.push(idx);
1340        Ok(())
1341    }
1342
1343    /// Build a new NSW (HNSW-flavoured) index over the named column.
1344    /// Required for `ORDER BY col <-> literal LIMIT k` to plan as a
1345    /// graph traversal instead of a full scan. Column must be a Vector
1346    /// type. `m` is the maximum number of neighbours per node.
1347    pub fn add_nsw_index(
1348        &mut self,
1349        name: String,
1350        column_name: &str,
1351        m: usize,
1352    ) -> Result<(), StorageError> {
1353        self.add_nsw_index_inner(name, column_name, m, None)
1354    }
1355
1356    /// v6.0.4 — synchronous rebuild of the named NSW index. If
1357    /// `new_encoding` is `Some(target)` and differs from the column's
1358    /// current encoding, every stored cell at the indexed column is
1359    /// re-coded into the target encoding before the new graph
1360    /// builds. Returns `IndexNotFound` if no index by that name exists
1361    /// and `Unsupported` for non-NSW indexes (`BTree` REBUILD is a no-op
1362    /// the engine layer rejects, not a storage-level concept).
1363    ///
1364    /// Holds the caller's `&mut self` for the duration — no
1365    /// concurrency / staging / WAL-replay machinery in v6.0.4. The
1366    /// "live" optimisation lands as v6.0.4.1.
1367    pub fn rebuild_nsw_index(
1368        &mut self,
1369        name: &str,
1370        new_encoding: Option<VecEncoding>,
1371    ) -> Result<(), StorageError> {
1372        let idx_pos = self
1373            .indices
1374            .iter()
1375            .position(|i| i.name == name)
1376            .ok_or_else(|| StorageError::IndexNotFound {
1377                name: String::from(name),
1378            })?;
1379        let col_pos = self.indices[idx_pos].column_position;
1380        let m = match &self.indices[idx_pos].kind {
1381            IndexKind::Nsw(g) => g.m,
1382            IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
1383                return Err(StorageError::Unsupported(format!(
1384                    "ALTER INDEX REBUILD on non-NSW index {name:?} — only NSW indexes can rebuild"
1385                )));
1386            }
1387        };
1388        let col_name = self.schema.columns[col_pos].name.clone();
1389        // 1. Optional re-encoding pass. Done first so the cells
1390        //    match the schema before the graph rebuild walks them.
1391        if let Some(target) = new_encoding {
1392            let current = match self.schema.columns[col_pos].ty {
1393                DataType::Vector { encoding, .. } => encoding,
1394                ref other => {
1395                    return Err(StorageError::Unsupported(format!(
1396                        "ALTER INDEX REBUILD WITH (encoding=…) on non-vector column type {other:?}"
1397                    )));
1398                }
1399            };
1400            if target != current {
1401                let DataType::Vector { dim, .. } = self.schema.columns[col_pos].ty else {
1402                    unreachable!("checked above")
1403                };
1404                let n = self.rows.len();
1405                for i in 0..n {
1406                    let row = self
1407                        .rows
1408                        .get_mut(i)
1409                        .expect("row index in bounds (we iterated up to len())");
1410                    let cell = core::mem::replace(&mut row.values[col_pos], Value::Null);
1411                    let recoded = recode_vector_cell(cell, target)?;
1412                    row.values[col_pos] = recoded;
1413                }
1414                self.schema.columns[col_pos].ty = DataType::Vector {
1415                    dim,
1416                    encoding: target,
1417                };
1418            }
1419        }
1420        // 2. Drop the existing index slot + rebuild from row payload.
1421        self.indices.remove(idx_pos);
1422        self.add_nsw_index_inner(String::from(name), &col_name, m, None)?;
1423        Ok(())
1424    }
1425
1426    /// Restore an NSW index from a pre-built graph (used on
1427    /// deserialize). Skips the bulk-build pass since the topology is
1428    /// already known. Returns `DuplicateIndex` or `ColumnNotFound` on
1429    /// schema mismatch as usual.
1430    pub fn restore_nsw_index(
1431        &mut self,
1432        name: String,
1433        column_name: &str,
1434        graph: NswGraph,
1435    ) -> Result<(), StorageError> {
1436        self.add_nsw_index_inner(name, column_name, graph.m, Some(graph))
1437    }
1438
1439    /// Restore a `BTree` index from a pre-built `(IndexKey, Vec<RowLocator>)`
1440    /// map. Used by [`Catalog::deserialize`] when reading a v9 (or later)
1441    /// catalog snapshot — the map travels on disk so cold-tier locators
1442    /// survive a round-trip, instead of being rebuilt from `self.rows`
1443    /// (which would lose every Cold entry). Same error contract as
1444    /// [`Table::add_index`].
1445    pub fn restore_btree_index(
1446        &mut self,
1447        name: String,
1448        column_name: &str,
1449        map: PersistentBTreeMap<IndexKey, Vec<RowLocator>>,
1450    ) -> Result<(), StorageError> {
1451        if self.indices.iter().any(|i| i.name == name) {
1452            return Err(StorageError::DuplicateIndex { name });
1453        }
1454        let column_position = self.schema.column_position(column_name).ok_or_else(|| {
1455            StorageError::ColumnNotFound {
1456                column: column_name.into(),
1457            }
1458        })?;
1459        self.indices.push(Index {
1460            name,
1461            column_position,
1462            kind: IndexKind::BTree(map),
1463            included_columns: Vec::new(),
1464            partial_predicate: None,
1465            expression: None,
1466            is_unique: false,
1467            extra_column_positions: Vec::new(),
1468        });
1469        Ok(())
1470    }
1471
1472    /// v6.7.1 — public restore counterpart for BRIN indices. Used
1473    /// by `Catalog::deserialize` when a v10 snapshot carries a
1474    /// BRIN index entry. BRIN carries no in-memory data — only the
1475    /// `column_type` snapshot is restored.
1476    pub fn restore_brin_index(
1477        &mut self,
1478        name: String,
1479        column_name: &str,
1480        column_type: DataType,
1481    ) -> Result<(), StorageError> {
1482        if self.indices.iter().any(|i| i.name == name) {
1483            return Err(StorageError::DuplicateIndex { name });
1484        }
1485        let column_position = self.schema.column_position(column_name).ok_or_else(|| {
1486            StorageError::ColumnNotFound {
1487                column: column_name.into(),
1488            }
1489        })?;
1490        self.indices
1491            .push(Index::new_brin(name, column_position, column_type));
1492        Ok(())
1493    }
1494
1495    /// v6.7.1 — public CREATE INDEX counterpart for BRIN. Creates
1496    /// the index entry with a snapshot of the indexed column's
1497    /// current `DataType`.
1498    pub fn add_brin_index(&mut self, name: String, column_name: &str) -> Result<(), StorageError> {
1499        if self.indices.iter().any(|i| i.name == name) {
1500            return Err(StorageError::DuplicateIndex { name });
1501        }
1502        let column_position = self.schema.column_position(column_name).ok_or_else(|| {
1503            StorageError::ColumnNotFound {
1504                column: column_name.into(),
1505            }
1506        })?;
1507        let column_type = self.schema.columns[column_position].ty;
1508        self.indices
1509            .push(Index::new_brin(name, column_position, column_type));
1510        Ok(())
1511    }
1512
1513    /// v7.12.3 — Build a new GIN inverted index over a `tsvector`
1514    /// column. Populates posting lists from existing rows. Errors
1515    /// if the column doesn't exist, isn't `TsVector`, or the index
1516    /// name is taken.
1517    pub fn add_gin_index(&mut self, name: String, column_name: &str) -> Result<(), StorageError> {
1518        if self.indices.iter().any(|i| i.name == name) {
1519            return Err(StorageError::DuplicateIndex { name });
1520        }
1521        let column_position = self.schema.column_position(column_name).ok_or_else(|| {
1522            StorageError::ColumnNotFound {
1523                column: column_name.into(),
1524            }
1525        })?;
1526        if self.schema.columns[column_position].ty != DataType::TsVector {
1527            return Err(StorageError::Corrupt(format!(
1528                "GIN index {name:?} requires a tsvector column; \
1529                 {column_name:?} is {:?}",
1530                self.schema.columns[column_position].ty
1531            )));
1532        }
1533        let mut idx = Index::new_gin(name, column_position);
1534        if let IndexKind::Gin(map) = &mut idx.kind {
1535            for (i, row) in self.rows.iter().enumerate() {
1536                if let Value::TsVector(lexemes) = &row.values[column_position] {
1537                    for lex in lexemes {
1538                        let mut entries = map.get(&lex.word).cloned().unwrap_or_default();
1539                        entries.push(RowLocator::Hot(i));
1540                        map.insert_mut(lex.word.clone(), entries);
1541                    }
1542                }
1543            }
1544        }
1545        self.indices.push(idx);
1546        Ok(())
1547    }
1548
1549    /// v7.12.3 — Restore a GIN index from a deserialised snapshot.
1550    /// Mirrors [`Self::restore_btree_index`] but takes the GIN's
1551    /// `word → Vec<RowLocator>` posting-list map (already populated
1552    /// from the catalog stream) instead of an `IndexKey` map.
1553    pub fn restore_gin_index(
1554        &mut self,
1555        name: String,
1556        column_name: &str,
1557        map: PersistentBTreeMap<String, Vec<RowLocator>>,
1558    ) -> Result<(), StorageError> {
1559        if self.indices.iter().any(|i| i.name == name) {
1560            return Err(StorageError::DuplicateIndex { name });
1561        }
1562        let column_position = self.schema.column_position(column_name).ok_or_else(|| {
1563            StorageError::ColumnNotFound {
1564                column: column_name.into(),
1565            }
1566        })?;
1567        let mut idx = Index::new_gin(name, column_position);
1568        idx.kind = IndexKind::Gin(map);
1569        self.indices.push(idx);
1570        Ok(())
1571    }
1572
1573    /// v5.1: register cold-tier locators on a `BTree` index. Used
1574    /// after [`Catalog::load_segment_bytes`] to wire every cold-
1575    /// tier row's PK back to its segment so
1576    /// [`Catalog::lookup_by_pk`] can resolve it. Each call
1577    /// appends to the index — keys that already have hot or cold
1578    /// locators keep them. Returns the number of locators
1579    /// registered.
1580    ///
1581    /// Pre-v5.2 (freezer) this is the only path that adds Cold
1582    /// variants to a PB; post-freezer the background freezer
1583    /// thread produces these as a batch under the engine write
1584    /// lock and this API becomes its in-memory primitive.
1585    ///
1586    /// Errors if `index_name` doesn't exist or names an NSW graph
1587    /// (NSW indices don't carry per-key row locators — they're
1588    /// vector-search structures).
1589    pub fn register_cold_locators<I>(
1590        &mut self,
1591        index_name: &str,
1592        locators: I,
1593    ) -> Result<usize, StorageError>
1594    where
1595        I: IntoIterator<Item = (IndexKey, RowLocator)>,
1596    {
1597        let idx = self
1598            .indices
1599            .iter_mut()
1600            .find(|i| i.name == index_name)
1601            .ok_or_else(|| StorageError::Corrupt(format!("index {index_name:?} not found")))?;
1602        let map = match &mut idx.kind {
1603            IndexKind::BTree(map) => map,
1604            IndexKind::Nsw(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
1605                return Err(StorageError::Corrupt(format!(
1606                    "index {index_name:?} is not BTree; cold locators apply only to BTree indices"
1607                )));
1608            }
1609        };
1610        let mut count = 0usize;
1611        for (key, locator) in locators {
1612            let mut entries = map.get(&key).cloned().unwrap_or_default();
1613            entries.push(locator);
1614            map.insert_mut(key, entries);
1615            count += 1;
1616        }
1617        Ok(count)
1618    }
1619
1620    /// v7.12.3 — GIN-side parallel to [`Self::register_cold_locators`].
1621    /// Re-attaches `word → cold RowLocator` posting-list entries after
1622    /// the from-rows rebuild loop. Errors when the index doesn't
1623    /// exist or isn't a GIN.
1624    pub fn register_gin_cold_locators<I>(
1625        &mut self,
1626        index_name: &str,
1627        locators: I,
1628    ) -> Result<usize, StorageError>
1629    where
1630        I: IntoIterator<Item = (String, RowLocator)>,
1631    {
1632        let idx = self
1633            .indices
1634            .iter_mut()
1635            .find(|i| i.name == index_name)
1636            .ok_or_else(|| StorageError::Corrupt(format!("index {index_name:?} not found")))?;
1637        let map = match &mut idx.kind {
1638            IndexKind::Gin(map) => map,
1639            IndexKind::BTree(_) | IndexKind::Nsw(_) | IndexKind::Brin { .. } => {
1640                return Err(StorageError::Corrupt(format!(
1641                    "register_gin_cold_locators: index {index_name:?} is not GIN"
1642                )));
1643            }
1644        };
1645        let mut count = 0usize;
1646        for (word, locator) in locators {
1647            let mut entries = map.get(&word).cloned().unwrap_or_default();
1648            entries.push(locator);
1649            map.insert_mut(word, entries);
1650            count += 1;
1651        }
1652        Ok(count)
1653    }
1654
1655    /// v5.2.3: remove every `Cold` locator currently registered on
1656    /// `index_name` under the given `key`. `Hot` locators for the
1657    /// same key are left in place — useful when a row has just been
1658    /// promoted hot-side and the caller wants the old Cold pointer
1659    /// retired without losing the new hot entry.
1660    ///
1661    /// Returns the number of cold locators removed (0 when the key
1662    /// has only hot entries or the key isn't present at all).
1663    /// Errors when the index doesn't exist or isn't a `BTree`.
1664    pub fn remove_cold_locators_for_key(
1665        &mut self,
1666        index_name: &str,
1667        key: &IndexKey,
1668    ) -> Result<usize, StorageError> {
1669        let idx = self
1670            .indices
1671            .iter_mut()
1672            .find(|i| i.name == index_name)
1673            .ok_or_else(|| {
1674                StorageError::Corrupt(format!(
1675                    "remove_cold_locators_for_key: index {index_name:?} not found"
1676                ))
1677            })?;
1678        let map = match &mut idx.kind {
1679            IndexKind::BTree(map) => map,
1680            IndexKind::Nsw(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
1681                return Err(StorageError::Corrupt(format!(
1682                    "remove_cold_locators_for_key: index {index_name:?} is not BTree; \
1683                     cold locators apply only to BTree indices"
1684                )));
1685            }
1686        };
1687        let Some(entries) = map.get(key) else {
1688            return Ok(0);
1689        };
1690        let mut kept: Vec<RowLocator> =
1691            entries.iter().copied().filter(RowLocator::is_hot).collect();
1692        let removed = entries.len() - kept.len();
1693        if removed == 0 {
1694            return Ok(0);
1695        }
1696        kept.shrink_to_fit();
1697        // PersistentBTreeMap has no remove API in v5.2; when every
1698        // locator for `key` was Cold, the key keeps an empty Vec
1699        // entry. `Index::lookup_eq` already treats `Some(&[])` and
1700        // `None` as the same empty slice (via `Vec::as_slice`), so
1701        // callers can't distinguish the two. The space cost is one
1702        // empty Vec per shadowed-then-promoted key — bounded and
1703        // recoverable when the future compaction job lands.
1704        map.insert_mut(key.clone(), kept);
1705        Ok(removed)
1706    }
1707
1708    /// v7.13.0 — append a new column to the schema and back-fill
1709    /// every existing row with `fill_value`. Used by the engine's
1710    /// `ALTER TABLE t ADD COLUMN …` handler (mailrs round-5 G1).
1711    /// Indices on existing columns keep working — column positions
1712    /// don't shift since the new column lands at the end — so no
1713    /// index rebuild is needed.
1714    pub fn add_column(&mut self, col: ColumnSchema, fill_value: Value) {
1715        self.schema.columns.push(col);
1716        let mut new_rows: PersistentVec<Row> = PersistentVec::new();
1717        for row in self.rows.iter() {
1718            let mut values = row.values.clone();
1719            values.push(fill_value.clone());
1720            new_rows.push_mut(Row::new(values));
1721        }
1722        self.rows = new_rows;
1723    }
1724
1725    /// v7.13.3 — drop the column at `col_pos`. Removes the entry
1726    /// from the schema, the value from every row, any index that
1727    /// references the column (pure drop, not shift), and shifts
1728    /// every remaining index/UC/FK column position that pointed
1729    /// past `col_pos` down by one. Used by `ALTER TABLE t DROP
1730    /// COLUMN <c>` (mailrs round-7 S8). FK dependents on this
1731    /// column must already have been removed by the caller (CASCADE
1732    /// path); the helper assumes only same-column index removal is
1733    /// needed.
1734    pub fn drop_column(&mut self, col_pos: usize) {
1735        debug_assert!(col_pos < self.schema.columns.len());
1736        // Strip the column from the schema.
1737        self.schema.columns.remove(col_pos);
1738        // Rewrite every row to omit the cell at col_pos.
1739        let mut new_rows: PersistentVec<Row> = PersistentVec::new();
1740        for row in self.rows.iter() {
1741            let mut values = row.values.clone();
1742            if col_pos < values.len() {
1743                values.remove(col_pos);
1744            }
1745            new_rows.push_mut(Row::new(values));
1746        }
1747        self.rows = new_rows;
1748        // Drop indices on the column outright; shift the rest.
1749        self.indices.retain(|idx| idx.column_position != col_pos);
1750        for idx in &mut self.indices {
1751            if idx.column_position > col_pos {
1752                idx.column_position -= 1;
1753            }
1754            // Same shift for any included-columns reference.
1755            for inc in &mut idx.included_columns {
1756                if *inc as usize > col_pos {
1757                    *inc -= 1;
1758                }
1759            }
1760        }
1761        // Shift uniqueness-constraint column positions (and drop
1762        // entries that lose all columns, though that shouldn't
1763        // happen in practice — caller has already CASCADE-removed
1764        // FKs and there's no general CASCADE for UCs).
1765        let mut surviving_ucs: Vec<UniquenessConstraint> = Vec::new();
1766        for mut uc in core::mem::take(&mut self.schema.uniqueness_constraints) {
1767            uc.columns.retain(|&c| c != col_pos);
1768            if uc.columns.is_empty() {
1769                continue;
1770            }
1771            for c in &mut uc.columns {
1772                if *c > col_pos {
1773                    *c -= 1;
1774                }
1775            }
1776            surviving_ucs.push(uc);
1777        }
1778        self.schema.uniqueness_constraints = surviving_ucs;
1779        // Shift FK local_columns (parent-pointing column positions
1780        // are off-table and untouched).
1781        for fk in &mut self.schema.foreign_keys {
1782            for c in &mut fk.local_columns {
1783                if *c > col_pos {
1784                    *c -= 1;
1785                }
1786            }
1787        }
1788        // Rebuild remaining indices' payload — the column-position
1789        // shift means existing IndexKey entries are still keyed by
1790        // the same column data but the position numbers changed;
1791        // existing key→locator maps stay valid because they're
1792        // keyed by Value not position. The rebuild is conservative
1793        // — same pattern delete_rows uses post-mutation.
1794        self.rebuild_indices();
1795    }
1796
1797    /// v4.4: delete the rows at the given positions in one pass.
1798    /// `positions` must be unique; ordering doesn't matter. Indices
1799    /// are rebuilt from scratch (cheaper than tracking incremental
1800    /// shifts across both B-tree and NSW). Returns the number of
1801    /// rows removed.
1802    pub fn delete_rows(&mut self, positions: &[usize]) -> usize {
1803        if positions.is_empty() {
1804            return 0;
1805        }
1806        // Mark positions; v4.39: PV has no in-place retain, so we rebuild
1807        // a fresh PV by pushing the survivors. Still O(n log₃₂ n); the
1808        // structural-sharing win shows up at `Catalog::clone()`, not here.
1809        let mut to_remove = alloc::vec![false; self.rows.len()];
1810        let mut removed = 0;
1811        for &p in positions {
1812            if p < to_remove.len() && !to_remove[p] {
1813                to_remove[p] = true;
1814                removed += 1;
1815            }
1816        }
1817        let mut new_rows: PersistentVec<Row> = PersistentVec::new();
1818        let mut removed_bytes: u64 = 0;
1819        for (i, row) in self.rows.iter().enumerate() {
1820            if to_remove[i] {
1821                removed_bytes =
1822                    removed_bytes.saturating_add(row_body_encoded_len(row, &self.schema) as u64);
1823            } else {
1824                new_rows.push_mut(row.clone());
1825            }
1826        }
1827        self.rows = new_rows;
1828        self.hot_bytes = self.hot_bytes.saturating_sub(removed_bytes);
1829        self.rebuild_indices();
1830        removed
1831    }
1832
1833    /// v4.4: replace the row at `position` with `new_values` (must
1834    /// match the schema arity + types). Indices are rebuilt for
1835    /// correctness — the affected column might be indexed and its
1836    /// key may have shifted, and a NSW node's vector may have
1837    /// changed, both of which need fresh state.
1838    pub fn update_row(
1839        &mut self,
1840        position: usize,
1841        new_values: Vec<Value>,
1842    ) -> Result<(), StorageError> {
1843        if position >= self.rows.len() {
1844            return Err(StorageError::Corrupt(alloc::format!(
1845                "update_row: position {position} out of bounds (rows={})",
1846                self.rows.len()
1847            )));
1848        }
1849        if new_values.len() != self.schema.columns.len() {
1850            return Err(StorageError::ArityMismatch {
1851                expected: self.schema.columns.len(),
1852                actual: new_values.len(),
1853            });
1854        }
1855        // Reuse the per-cell type-compat validation that `insert`
1856        // applies. The body below mirrors that check intentionally —
1857        // factoring it would be more code than the duplication.
1858        for (i, (val, col)) in new_values.iter().zip(&self.schema.columns).enumerate() {
1859            if val.is_null() {
1860                if !col.nullable {
1861                    return Err(StorageError::NullInNotNull {
1862                        column: col.name.clone(),
1863                    });
1864                }
1865                continue;
1866            }
1867            let actual = val.data_type().expect("non-null");
1868            let compatible = actual == col.ty
1869                || matches!(
1870                    (actual, col.ty),
1871                    (
1872                        DataType::Text,
1873                        DataType::Varchar(_) | DataType::Char(_) | DataType::Json | DataType::Jsonb
1874                    ) | (DataType::Json | DataType::Jsonb, DataType::Text)
1875                        | (DataType::Json, DataType::Jsonb)
1876                        | (DataType::Jsonb, DataType::Json)
1877                        | (DataType::Timestamp, DataType::Timestamptz)
1878                        | (DataType::Timestamptz, DataType::Timestamp)
1879                )
1880                || matches!(
1881                    (actual, col.ty),
1882                    (
1883                        DataType::Numeric { scale: a, .. },
1884                        DataType::Numeric { scale: b, .. },
1885                    ) if a == b
1886                );
1887            if !compatible {
1888                return Err(StorageError::TypeMismatch {
1889                    column: col.name.clone(),
1890                    expected: col.ty,
1891                    actual,
1892                    position: i,
1893                });
1894            }
1895        }
1896        let old_row = self
1897            .rows
1898            .get(position)
1899            .expect("position bounds-checked above");
1900        let old_bytes = row_body_encoded_len(old_row, &self.schema) as u64;
1901        let new_row = Row::new(new_values);
1902        let new_bytes = row_body_encoded_len(&new_row, &self.schema) as u64;
1903        self.rows = self
1904            .rows
1905            .set(position, new_row)
1906            .expect("position bounds-checked above");
1907        self.hot_bytes = self
1908            .hot_bytes
1909            .saturating_sub(old_bytes)
1910            .saturating_add(new_bytes);
1911        self.rebuild_indices();
1912        Ok(())
1913    }
1914
1915    /// v4.4 helper used by `delete_rows` / `update_row`: discard all
1916    /// index payloads and rebuild from `self.rows`. Cheap enough
1917    /// for typical SPG scale (catalogs in the docker-compose
1918    /// deployment shape are small); the alternative — incremental
1919    /// shift bookkeeping across B-tree + NSW — would be far more
1920    /// invasive than the savings justify.
1921    fn rebuild_indices(&mut self) {
1922        // v5.2.3: capture every `Cold` locator on every BTree index
1923        // before the rebuild, so the from-rows re-emission below
1924        // (which only produces `Hot` locators) doesn't drop cold-
1925        // tier entries on keys unrelated to the row that changed.
1926        // Pre-v5.2.3 this was a `freeze_oldest_to_cold` worry only
1927        // and the freezer did its own capture-then-reregister; v5.2.3
1928        // promotes that pattern into the base helper because UPDATE
1929        // / DELETE now run rebuild_indices on tables with cold rows.
1930        let preserved_cold: Vec<(String, Vec<(IndexKey, RowLocator)>)> = self
1931            .indices
1932            .iter()
1933            .filter_map(|idx| match &idx.kind {
1934                IndexKind::BTree(map) => {
1935                    let cold: Vec<(IndexKey, RowLocator)> = map
1936                        .iter()
1937                        .flat_map(|(k, locs)| {
1938                            locs.iter()
1939                                .filter(|l| l.is_cold())
1940                                .copied()
1941                                .map(move |l| (k.clone(), l))
1942                        })
1943                        .collect();
1944                    if cold.is_empty() {
1945                        None
1946                    } else {
1947                        Some((idx.name.clone(), cold))
1948                    }
1949                }
1950                // BRIN / NSW carry no key→locator map. GIN handles
1951                // its own cold preservation below in `preserved_gin_cold`.
1952                IndexKind::Nsw(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => None,
1953            })
1954            .collect();
1955
1956        // v7.12.3 — same cold-preservation pattern for GIN's
1957        // `word → Vec<RowLocator>` posting lists. Parallel to the
1958        // BTree pass above (different key type so a separate vec is
1959        // cleaner than a generic merge).
1960        let preserved_gin_cold: Vec<(String, Vec<(String, RowLocator)>)> = self
1961            .indices
1962            .iter()
1963            .filter_map(|idx| match &idx.kind {
1964                IndexKind::Gin(map) => {
1965                    let cold: Vec<(String, RowLocator)> = map
1966                        .iter()
1967                        .flat_map(|(w, locs)| {
1968                            locs.iter()
1969                                .filter(|l| l.is_cold())
1970                                .copied()
1971                                .map(move |l| (w.clone(), l))
1972                        })
1973                        .collect();
1974                    if cold.is_empty() {
1975                        None
1976                    } else {
1977                        Some((idx.name.clone(), cold))
1978                    }
1979                }
1980                IndexKind::BTree(_) | IndexKind::Nsw(_) | IndexKind::Brin { .. } => None,
1981            })
1982            .collect();
1983
1984        // v6.7.1 — descriptor needs to capture index kind so the
1985        // rebuild loop can resurrect BTree / NSW / BRIN / GIN exactly
1986        // as they were. (NSW carries m; BRIN carries the column type
1987        // snapshot; BTree / GIN need no extra payload.)
1988        #[derive(Clone)]
1989        enum RebuildKind {
1990            BTree,
1991            Nsw(usize),
1992            Brin(DataType),
1993            Gin,
1994        }
1995        let descriptors: Vec<(String, usize, RebuildKind)> = self
1996            .indices
1997            .iter()
1998            .map(|idx| {
1999                let kind = match &idx.kind {
2000                    IndexKind::Nsw(g) => RebuildKind::Nsw(g.m),
2001                    IndexKind::Brin { column_type } => RebuildKind::Brin(*column_type),
2002                    IndexKind::BTree(_) => RebuildKind::BTree,
2003                    IndexKind::Gin(_) => RebuildKind::Gin,
2004                };
2005                (idx.name.clone(), idx.column_position, kind)
2006            })
2007            .collect();
2008        self.indices.clear();
2009        for (name, column_position, rebuild_kind) in descriptors {
2010            match rebuild_kind {
2011                RebuildKind::Nsw(m) => {
2012                    let idx = Index::new_nsw(name, column_position, m);
2013                    self.indices.push(idx);
2014                    let idx_pos = self.indices.len() - 1;
2015                    let row_indices: Vec<usize> = (0..self.rows.len()).collect();
2016                    for row_idx in row_indices {
2017                        nsw_insert_at(self, idx_pos, row_idx);
2018                    }
2019                }
2020                RebuildKind::Brin(column_type) => {
2021                    // BRIN has no in-memory rebuild — the summaries
2022                    // live in cold segments which freeze emits.
2023                    self.indices
2024                        .push(Index::new_brin(name, column_position, column_type));
2025                }
2026                RebuildKind::BTree => {
2027                    let mut idx = Index::new_btree(name, column_position);
2028                    if let IndexKind::BTree(map) = &mut idx.kind {
2029                        for (i, row) in self.rows.iter().enumerate() {
2030                            if let Some(key) = IndexKey::from_value(&row.values[column_position]) {
2031                                let mut entries = map.get(&key).cloned().unwrap_or_default();
2032                                entries.push(RowLocator::Hot(i));
2033                                map.insert_mut(key, entries);
2034                            }
2035                        }
2036                    }
2037                    self.indices.push(idx);
2038                }
2039                RebuildKind::Gin => {
2040                    let mut idx = Index::new_gin(name, column_position);
2041                    if let IndexKind::Gin(map) = &mut idx.kind {
2042                        for (i, row) in self.rows.iter().enumerate() {
2043                            if let Value::TsVector(lexemes) = &row.values[column_position] {
2044                                for lex in lexemes {
2045                                    let mut entries =
2046                                        map.get(&lex.word).cloned().unwrap_or_default();
2047                                    entries.push(RowLocator::Hot(i));
2048                                    map.insert_mut(lex.word.clone(), entries);
2049                                }
2050                            }
2051                        }
2052                    }
2053                    self.indices.push(idx);
2054                }
2055            }
2056        }
2057
2058        // Re-attach preserved cold locators after the from-rows
2059        // rebuild. `register_cold_locators` handles the per-key
2060        // entries-vec append; no key collisions arise because the
2061        // rebuild loop above produced only Hot locators.
2062        for (idx_name, locators) in preserved_cold {
2063            // Errors here would only fire if the index disappeared
2064            // between snapshot and rebuild, which can't happen
2065            // because the rebuild restores the same descriptor set.
2066            let _ = self.register_cold_locators(&idx_name, locators);
2067        }
2068        // v7.12.3 — same for GIN posting-list cold locators.
2069        for (idx_name, locators) in preserved_gin_cold {
2070            let _ = self.register_gin_cold_locators(&idx_name, locators);
2071        }
2072    }
2073
2074    fn add_nsw_index_inner(
2075        &mut self,
2076        name: String,
2077        column_name: &str,
2078        m: usize,
2079        restore: Option<NswGraph>,
2080    ) -> Result<(), StorageError> {
2081        if self.indices.iter().any(|i| i.name == name) {
2082            return Err(StorageError::DuplicateIndex { name });
2083        }
2084        let column_position = self.schema.column_position(column_name).ok_or_else(|| {
2085            StorageError::ColumnNotFound {
2086                column: column_name.into(),
2087            }
2088        })?;
2089        if !matches!(
2090            self.schema.columns[column_position].ty,
2091            DataType::Vector { .. }
2092        ) {
2093            return Err(StorageError::TypeMismatch {
2094                column: column_name.into(),
2095                expected: DataType::Vector {
2096                    dim: 0,
2097                    encoding: VecEncoding::F32,
2098                },
2099                actual: self.schema.columns[column_position].ty,
2100                position: column_position,
2101            });
2102        }
2103        if let Some(graph) = restore {
2104            self.indices.push(Index {
2105                name,
2106                column_position,
2107                kind: IndexKind::Nsw(graph),
2108                included_columns: Vec::new(),
2109                partial_predicate: None,
2110                expression: None,
2111                is_unique: false,
2112                extra_column_positions: Vec::new(),
2113            });
2114            return Ok(());
2115        }
2116        let idx = Index::new_nsw(name, column_position, m);
2117        self.indices.push(idx);
2118        let idx_pos = self.indices.len() - 1;
2119        // Bulk-build by walking the existing rows in order — each insert
2120        // sees the partial graph and links into it.
2121        let row_indices: Vec<usize> = (0..self.rows.len()).collect();
2122        for row_idx in row_indices {
2123            nsw_insert_at(self, idx_pos, row_idx);
2124        }
2125        Ok(())
2126    }
2127}
2128
2129/// v6.0.4 — re-encode a single cell to the target `VecEncoding`.
2130/// Used by `Table::rebuild_nsw_index` when ALTER INDEX REBUILD
2131/// includes the optional `WITH (encoding = …)` clause. Round-trip
2132/// goes through f32: `current → Vec<f32> → target`, leaving NULL
2133/// cells untouched. Returns `Unsupported` on a non-vector cell —
2134/// the caller should have rejected the schema before reaching this.
2135fn recode_vector_cell(cell: Value, target: VecEncoding) -> Result<Value, StorageError> {
2136    if matches!(cell, Value::Null) {
2137        return Ok(cell);
2138    }
2139    // Step 1 — extract the f32 representation of the source cell.
2140    let as_f32: Vec<f32> = match &cell {
2141        Value::Vector(v) => v.clone(),
2142        Value::Sq8Vector(q) => quantize::dequantize(q),
2143        Value::HalfVector(h) => h.to_f32_vec(),
2144        other => {
2145            return Err(StorageError::Unsupported(format!(
2146                "ALTER INDEX REBUILD: cannot recode non-vector cell {:?}",
2147                other.data_type()
2148            )));
2149        }
2150    };
2151    // Step 2 — encode into the target shape. `F32` is the identity
2152    // path (saves one alloc round-trip when the source is already
2153    // F32 — but `Value::Vector(as_f32)` is the right answer
2154    // regardless).
2155    Ok(match target {
2156        VecEncoding::F32 => Value::Vector(as_f32),
2157        VecEncoding::Sq8 => Value::Sq8Vector(quantize::quantize(&as_f32)),
2158        VecEncoding::F16 => Value::HalfVector(halfvec::HalfVector::from_f32_slice(&as_f32)),
2159    })
2160}
2161
2162/// Insert one row into the HNSW graph held by index slot `idx_pos`.
2163/// No-op when the row's value at the indexed column isn't a vector.
2164/// v6.0.1: handles `Value::Sq8Vector` by dequantising into an f32
2165/// "query" surface — the existing greedy + beam-search machinery
2166/// then uses `cell_to_query_metric_distance` to route every
2167/// distance call through the cell's actual encoding.
2168fn nsw_insert_at(table: &mut Table, idx_pos: usize, new_row_idx: usize) {
2169    let col_pos = table.indices[idx_pos].column_position;
2170    let cell_dim: Option<usize> = match &table.rows[new_row_idx].values[col_pos] {
2171        Value::Vector(v) => Some(v.len()),
2172        Value::Sq8Vector(q) => Some(q.bytes.len()),
2173        Value::HalfVector(h) => Some(h.dim()),
2174        _ => None,
2175    };
2176    let Some(dim) = cell_dim else {
2177        // Even non-vector rows occupy a level slot so per-node Vec
2178        // lengths stay aligned with `table.rows.len()`.
2179        ensure_node_slot(table, idx_pos, new_row_idx, 0);
2180        return;
2181    };
2182    if dim == 0 {
2183        ensure_node_slot(table, idx_pos, new_row_idx, 0);
2184        return;
2185    }
2186    let level = nsw_assign_level(new_row_idx);
2187    ensure_node_slot(table, idx_pos, new_row_idx, level);
2188    let (entry, entry_level, m) = match &table.indices[idx_pos].kind {
2189        IndexKind::Nsw(g) => (g.entry, g.entry_level, g.m),
2190        IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
2191            unreachable!("nsw_insert_at on a non-NSW index")
2192        }
2193    };
2194    // First node ever — declare it the entry (it gets its own level).
2195    if entry.is_none() {
2196        if let IndexKind::Nsw(g) = &mut table.indices[idx_pos].kind {
2197            g.entry = Some(new_row_idx);
2198            g.entry_level = level;
2199            *g.levels
2200                .get_mut(new_row_idx)
2201                .expect("levels slot padded by ensure_node_slot") = level;
2202        }
2203        return;
2204    }
2205    // Set the node's recorded level.
2206    if let IndexKind::Nsw(g) = &mut table.indices[idx_pos].kind {
2207        *g.levels
2208            .get_mut(new_row_idx)
2209            .expect("levels slot padded by ensure_node_slot") = level;
2210    }
2211    let query = match &table.rows[new_row_idx].values[col_pos] {
2212        Value::Vector(v) => v.clone(),
2213        // v6.0.1: dequantise the inserted SQ8 cell into an f32 query
2214        // surface so the existing greedy / beam machinery can route
2215        // distances through `cell_to_query_metric_distance`. The
2216        // small dequantisation error is what the recall@10 ≥ 0.95
2217        // envelope already accounts for (V6_DESIGN deliberation #3).
2218        Value::Sq8Vector(q) => quantize::dequantize(q),
2219        // v6.0.3: halfvec dequant is bit-exact at the storage layer,
2220        // so the inserted query is a faithful representation.
2221        Value::HalfVector(h) => h.to_f32_vec(),
2222        _ => return,
2223    };
2224    // Phase 1: greedy descend from `entry` down to `level + 1`, keeping
2225    // exactly one current best so the next layer starts from it.
2226    let mut current = entry.expect("entry was Some above");
2227    let mut current_d = vec_l2_sq(table, col_pos, current, &query);
2228    if entry_level > level {
2229        for layer in (level + 1..=entry_level).rev() {
2230            (current, current_d) =
2231                greedy_layer_walk(table, idx_pos, layer, current, current_d, &query);
2232        }
2233    }
2234    // Phase 2: from `min(level, entry_level)` down to 0, beam-search
2235    // `ef_construction` candidates, run the HNSW §4 heuristic neighbour
2236    // selection over them, and connect bidirectionally.
2237    let top = level.min(entry_level);
2238    let ef = (m * 2).max(8);
2239    for layer in (0..=top).rev() {
2240        let cap = if layer == 0 { m * 2 } else { m };
2241        let mut candidates = layer_beam_search(
2242            table,
2243            idx_pos,
2244            layer,
2245            current,
2246            current_d,
2247            &query,
2248            ef,
2249            NswMetric::L2,
2250        );
2251        candidates.retain(|&(_, n)| n != new_row_idx);
2252        // Take the closest as the entry for the next layer down — done
2253        // before heuristic narrowing because the heuristic can reorder.
2254        if let Some(&(d, n)) = candidates.first() {
2255            current = n;
2256            current_d = d;
2257        }
2258        let peers = select_neighbours_heuristic(&candidates, cap, table, col_pos);
2259        connect_at_layer(table, idx_pos, layer, new_row_idx, &peers);
2260    }
2261    // Phase 3: if the new node climbed above the current entry, take
2262    // over as entry so future inserts/searches start from the new top.
2263    if level > entry_level
2264        && let IndexKind::Nsw(g) = &mut table.indices[idx_pos].kind
2265    {
2266        g.entry = Some(new_row_idx);
2267        g.entry_level = level;
2268    }
2269}
2270
2271/// Make sure `layers[*][new_row_idx]` and `levels[new_row_idx]` exist,
2272/// padding with empty/zero entries as needed. Also grows `layers` to
2273/// accommodate the node's top `level`.
2274fn ensure_node_slot(table: &mut Table, idx_pos: usize, new_row_idx: usize, level: u8) {
2275    let IndexKind::Nsw(g) = &mut table.indices[idx_pos].kind else {
2276        unreachable!("ensure_node_slot on a BTree index");
2277    };
2278    while g.layers.len() <= level as usize {
2279        g.layers.push(PersistentVec::new());
2280    }
2281    while g.levels.len() <= new_row_idx {
2282        g.levels.push_mut(0);
2283    }
2284    for layer_vec in &mut g.layers {
2285        while layer_vec.len() <= new_row_idx {
2286            layer_vec.push_mut(Vec::new());
2287        }
2288    }
2289}
2290
2291/// Single-step greedy walk on one layer: from `current` (with cached
2292/// distance `current_d`), inspect that node's neighbours at `layer` and
2293/// hop to the closest if it beats `current_d`. Repeat until no move
2294/// improves the distance. Cheap variant of beam-search used for the
2295/// "descend" phase that only needs one survivor per layer.
2296fn greedy_layer_walk(
2297    table: &Table,
2298    idx_pos: usize,
2299    layer: u8,
2300    mut current: usize,
2301    mut current_d: f32,
2302    query: &[f32],
2303) -> (usize, f32) {
2304    let g = match &table.indices[idx_pos].kind {
2305        IndexKind::Nsw(g) => g,
2306        IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
2307            return (current, current_d);
2308        }
2309    };
2310    let col_pos = table.indices[idx_pos].column_position;
2311    loop {
2312        let neighbours: &[u32] = g
2313            .layers
2314            .get(layer as usize)
2315            .and_then(|layer_v| layer_v.get(current))
2316            .map_or(&[][..], Vec::as_slice);
2317        let mut best = current;
2318        let mut best_d = current_d;
2319        for &n in neighbours {
2320            let n = n as usize;
2321            let d = vec_l2_sq(table, col_pos, n, query);
2322            if d < best_d {
2323                best = n;
2324                best_d = d;
2325            }
2326        }
2327        if best == current {
2328            return (current, current_d);
2329        }
2330        current = best;
2331        current_d = best_d;
2332    }
2333}
2334
2335/// Beam search on one layer starting from `entry_node` with cached
2336/// `entry_d`. Returns the top `ef` candidates in ascending-distance
2337/// order. Caller picks the closest as the next layer's entry and / or
2338/// trims to M for connection.
2339///
2340/// v3.0.1: uses two `BinaryHeap`s (min-heap for the open frontier,
2341/// max-heap for the working top-`ef` results) and a `Vec<bool>` visited
2342/// bitmap, replacing the v2.x `Vec` + `partition_point` + `BTreeSet`
2343/// implementation. Same algorithm shape (HNSW search algorithm 2 from
2344/// the paper); the data-structure swap cuts per-visit cost from
2345/// `O(ef + log row_count)` to amortised `O(log ef)`.
2346#[allow(clippy::too_many_arguments)] // Beam search threads layer, entry, query, ef, metric — each is intrinsic. Bundling them into a config struct hides the call sites.
2347fn layer_beam_search(
2348    table: &Table,
2349    idx_pos: usize,
2350    layer: u8,
2351    entry_node: usize,
2352    entry_d: f32,
2353    query: &[f32],
2354    ef: usize,
2355    metric: NswMetric,
2356) -> Vec<(f32, usize)> {
2357    let g = match &table.indices[idx_pos].kind {
2358        IndexKind::Nsw(g) => g,
2359        IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => return Vec::new(),
2360    };
2361    let col_pos = table.indices[idx_pos].column_position;
2362    let d0 = if matches!(metric, NswMetric::L2) {
2363        entry_d
2364    } else {
2365        cell_to_query_metric_distance(table, col_pos, entry_node, query, metric)
2366    };
2367    let row_count = table.rows.len();
2368    let mut visited: Vec<bool> = alloc::vec![false; row_count];
2369    if entry_node < row_count {
2370        visited[entry_node] = true;
2371    }
2372    // candidates: min-heap by distance (Closest wrapper) — frontier
2373    // results:    max-heap by distance (Furthest wrapper) — top-ef working set
2374    let mut candidates: alloc::collections::BinaryHeap<NodeClosest> =
2375        alloc::collections::BinaryHeap::with_capacity(ef);
2376    let mut results: alloc::collections::BinaryHeap<NodeFurthest> =
2377        alloc::collections::BinaryHeap::with_capacity(ef);
2378    candidates.push(NodeClosest {
2379        dist: d0,
2380        node: entry_node,
2381    });
2382    results.push(NodeFurthest {
2383        dist: d0,
2384        node: entry_node,
2385    });
2386    while let Some(cur) = candidates.pop() {
2387        let worst = results.peek().map_or(f32::INFINITY, |c| c.dist);
2388        if cur.dist > worst && results.len() >= ef {
2389            break;
2390        }
2391        let neighbours: &[u32] = g
2392            .layers
2393            .get(layer as usize)
2394            .and_then(|layer_v| layer_v.get(cur.node))
2395            .map_or(&[][..], Vec::as_slice);
2396        for &n in neighbours {
2397            let n = n as usize;
2398            if n >= row_count || visited[n] {
2399                continue;
2400            }
2401            visited[n] = true;
2402            // v6.0.1: cell-aware distance — F32 cells take the
2403            // existing scalar metric, SQ8 cells route through
2404            // the asymmetric ADC variant for the same metric.
2405            let dn = cell_to_query_metric_distance(table, col_pos, n, query, metric);
2406            if !dn.is_finite() {
2407                continue;
2408            }
2409            let worst = results.peek().map_or(f32::INFINITY, |c| c.dist);
2410            if results.len() < ef || dn < worst {
2411                results.push(NodeFurthest { dist: dn, node: n });
2412                if results.len() > ef {
2413                    results.pop();
2414                }
2415                candidates.push(NodeClosest { dist: dn, node: n });
2416            }
2417        }
2418    }
2419    // Drain results (max-heap order) and re-sort ascending so callers
2420    // can take `closest = result[0]` without flipping.
2421    let mut out: Vec<(f32, usize)> = results.into_iter().map(|c| (c.dist, c.node)).collect();
2422    out.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(core::cmp::Ordering::Equal));
2423    out
2424}
2425
2426/// Min-heap wrapper: smaller `dist` → higher priority in a `BinaryHeap`
2427/// (which is a max-heap), so we flip the comparison. NaN sorts last
2428/// (lowest priority) to keep the heap total-ordered.
2429#[derive(Debug, Clone, Copy)]
2430struct NodeClosest {
2431    dist: f32,
2432    node: usize,
2433}
2434impl PartialEq for NodeClosest {
2435    fn eq(&self, other: &Self) -> bool {
2436        self.dist == other.dist && self.node == other.node
2437    }
2438}
2439impl Eq for NodeClosest {}
2440impl PartialOrd for NodeClosest {
2441    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
2442        Some(self.cmp(other))
2443    }
2444}
2445impl Ord for NodeClosest {
2446    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
2447        // Reversed: smaller dist = greater priority.
2448        other
2449            .dist
2450            .partial_cmp(&self.dist)
2451            .unwrap_or(core::cmp::Ordering::Equal)
2452    }
2453}
2454
2455/// Max-heap wrapper: larger `dist` sits at the top so the worst result
2456/// can be evicted in O(log n) when a better candidate arrives.
2457#[derive(Debug, Clone, Copy)]
2458struct NodeFurthest {
2459    dist: f32,
2460    node: usize,
2461}
2462impl PartialEq for NodeFurthest {
2463    fn eq(&self, other: &Self) -> bool {
2464        self.dist == other.dist && self.node == other.node
2465    }
2466}
2467impl Eq for NodeFurthest {}
2468impl PartialOrd for NodeFurthest {
2469    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
2470        Some(self.cmp(other))
2471    }
2472}
2473impl Ord for NodeFurthest {
2474    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
2475        self.dist
2476            .partial_cmp(&other.dist)
2477            .unwrap_or(core::cmp::Ordering::Equal)
2478    }
2479}
2480
2481/// HNSW paper §4 algorithm 4: pick `m` neighbours from `candidates` so
2482/// that each chosen point isn't already covered by a closer chosen
2483/// point. Improves graph diversity → fewer hops needed at search time.
2484///
2485/// `candidates` arrives sorted ascending by distance-to-query. We walk
2486/// it in order, keeping a candidate only when no already-chosen point
2487/// is closer to it than the query is. Result is a vector of row
2488/// indices (length ≤ `m`).
2489fn select_neighbours_heuristic(
2490    candidates: &[(f32, usize)],
2491    m: usize,
2492    table: &Table,
2493    col_pos: usize,
2494) -> Vec<usize> {
2495    let mut chosen: Vec<usize> = Vec::with_capacity(m);
2496    for &(d_q, e) in candidates {
2497        if chosen.len() >= m {
2498            break;
2499        }
2500        // v6.0.1: works on either `Value::Vector` (F32) or
2501        // `Value::Sq8Vector` (Sq8) cells — `cell_l2_sq` dispatches
2502        // on encoding. A non-vector cell yields `f32::INFINITY`
2503        // which the `< d_q` test will never accept.
2504        if !matches!(
2505            table.rows.get(e).and_then(|r| r.values.get(col_pos)),
2506            Some(Value::Vector(_) | Value::Sq8Vector(_) | Value::HalfVector(_))
2507        ) {
2508            continue;
2509        }
2510        let mut covered = false;
2511        for &r in &chosen {
2512            // dist(e, r) measured in the same metric the topology was
2513            // built with (L2). If a chosen `r` is closer to `e` than
2514            // the query is, `r` already "covers" `e` for navigation.
2515            if cell_l2_sq(table, col_pos, e, r) < d_q {
2516                covered = true;
2517                break;
2518            }
2519        }
2520        if !covered {
2521            chosen.push(e);
2522        }
2523    }
2524    chosen
2525}
2526
2527/// Bidirectionally connect `new_row_idx` to each of `peers` at `layer`,
2528/// trimming each endpoint's adjacency to that layer's degree cap by
2529/// keeping only the closest neighbours.
2530fn connect_at_layer(
2531    table: &mut Table,
2532    idx_pos: usize,
2533    layer: u8,
2534    new_row_idx: usize,
2535    peers: &[usize],
2536) {
2537    let col_pos = table.indices[idx_pos].column_position;
2538    let cap = match &table.indices[idx_pos].kind {
2539        IndexKind::Nsw(g) => g.cap_for_layer(layer),
2540        IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => return,
2541    };
2542    // v6.1.x: NSW adjacency stores neighbour row indices as u32 (4 B
2543    // each) rather than usize (8 B on 64-bit). Boundary casts here
2544    // assert the row count fits in u32 — the catalog already enforces
2545    // ≤ 4G rows per table, so the conversion can't lose data.
2546    let new_row_u32 = u32::try_from(new_row_idx).expect("row index fits in u32");
2547    if let IndexKind::Nsw(g) = &mut table.indices[idx_pos].kind {
2548        let layer_v = &mut g.layers[layer as usize];
2549        if let Some(slot) = layer_v.get_mut(new_row_idx) {
2550            *slot = peers
2551                .iter()
2552                .map(|&p| u32::try_from(p).expect("row index fits in u32"))
2553                .collect();
2554        }
2555    }
2556    for &peer in peers {
2557        // Skip peers whose indexed cell isn't a vector — same fence
2558        // as the F32 path; SQ8 cells flow through `cell_l2_sq`
2559        // below without dequantising.
2560        if !matches!(
2561            &table.rows[peer].values[col_pos],
2562            Value::Vector(_) | Value::Sq8Vector(_) | Value::HalfVector(_)
2563        ) {
2564            continue;
2565        }
2566        // 1. add the new node to peer's adjacency
2567        if let IndexKind::Nsw(g) = &mut table.indices[idx_pos].kind {
2568            let layer_v = &mut g.layers[layer as usize];
2569            if let Some(slot) = layer_v.get_mut(peer)
2570                && !slot.contains(&new_row_u32)
2571            {
2572                slot.push(new_row_u32);
2573            }
2574        }
2575        // 2. if peer is over budget, rebuild its adjacency with the
2576        //    HNSW §4 heuristic — same diversity criterion as the
2577        //    insert path so connectivity stays consistent.
2578        let needs_trim = match &table.indices[idx_pos].kind {
2579            IndexKind::Nsw(g) => g.layers[layer as usize][peer].len() > cap,
2580            IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => false,
2581        };
2582        if needs_trim {
2583            let current_peers: Vec<usize> = match &table.indices[idx_pos].kind {
2584                IndexKind::Nsw(g) => g.layers[layer as usize][peer]
2585                    .iter()
2586                    .map(|&n| n as usize)
2587                    .collect(),
2588                IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => continue,
2589            };
2590            // Sort by distance from `peer`'s cell ascending so the
2591            // heuristic receives candidates closest-first. `cell_l2_sq`
2592            // dispatches on encoding so SQ8 columns trim using
2593            // symmetric ADC.
2594            let mut tagged: Vec<(f32, usize)> = current_peers
2595                .iter()
2596                .map(|&p| (cell_l2_sq(table, col_pos, peer, p), p))
2597                .collect();
2598            tagged.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(core::cmp::Ordering::Equal));
2599            let kept = select_neighbours_heuristic(&tagged, cap, table, col_pos);
2600            if let IndexKind::Nsw(g) = &mut table.indices[idx_pos].kind
2601                && let Some(slot) = g.layers[layer as usize].get_mut(peer)
2602            {
2603                *slot = kept
2604                    .into_iter()
2605                    .map(|p| u32::try_from(p).expect("row index fits in u32"))
2606                    .collect();
2607            }
2608        }
2609    }
2610}
2611
2612/// Squared L2 distance from `query` (raw f32) to the cell at
2613/// `(row, col_pos)`. Dispatches on cell encoding: `Value::Vector`
2614/// (F32) uses `l2_distance_sq`; `Value::Sq8Vector` uses
2615/// `sq8_l2_distance_sq_asymmetric` (the v6.0.1 quantised path).
2616/// Returns `f32::INFINITY` for any non-vector cell so callers can
2617/// compare uniformly.
2618fn vec_l2_sq(table: &Table, col_pos: usize, row: usize, query: &[f32]) -> f32 {
2619    match table.rows.get(row).and_then(|r| r.values.get(col_pos)) {
2620        Some(Value::Vector(v)) if v.len() == query.len() => l2_distance_sq(v, query),
2621        Some(Value::Sq8Vector(q)) if q.bytes.len() == query.len() => {
2622            quantize::sq8_l2_distance_sq_asymmetric(q, query)
2623        }
2624        // v6.0.6: halfvec → fused NEON SIMD kernel; no Vec<f32>
2625        // allocation. v6.0.3 used `to_f32_vec()` + f32 NEON which
2626        // was correct but allocated per call (5× slower than F32).
2627        Some(Value::HalfVector(h)) if h.dim() == query.len() => {
2628            halfvec::half_l2_distance_sq_asymmetric(h, query)
2629        }
2630        _ => f32::INFINITY,
2631    }
2632}
2633
2634/// Squared L2 distance between two stored cells (no f32 query in
2635/// sight). Used during HNSW graph build — both endpoints are
2636/// rows already in the table, so symmetric ADC applies for SQ8
2637/// columns. Mixed-encoding cells within one column are a
2638/// schema-level impossibility (INSERT-time coercion enforces
2639/// uniform encoding), so the catch-all is an abort.
2640fn cell_l2_sq(table: &Table, col_pos: usize, row_a: usize, row_b: usize) -> f32 {
2641    let Some(cell_a) = table.rows.get(row_a).and_then(|r| r.values.get(col_pos)) else {
2642        return f32::INFINITY;
2643    };
2644    let Some(cell_b) = table.rows.get(row_b).and_then(|r| r.values.get(col_pos)) else {
2645        return f32::INFINITY;
2646    };
2647    match (cell_a, cell_b) {
2648        (Value::Vector(a), Value::Vector(b)) if a.len() == b.len() => l2_distance_sq(a, b),
2649        (Value::Sq8Vector(a), Value::Sq8Vector(b)) if a.bytes.len() == b.bytes.len() => {
2650            quantize::sq8_l2_distance_sq(a, b)
2651        }
2652        // v6.0.6: halfvec symmetric NEON — fused SIMD kernel that
2653        // loads both cells' raw u16 bits, expands to f32 lanes
2654        // inline, FMA-accumulates the squared diff. No Vec<f32>
2655        // allocation per call.
2656        (Value::HalfVector(a), Value::HalfVector(b)) if a.dim() == b.dim() => {
2657            halfvec::half_l2_distance_sq(a, b)
2658        }
2659        _ => f32::INFINITY,
2660    }
2661}
2662
2663/// kNN-search-time distance: stored cell → f32 query under the
2664/// caller's metric. Dispatches on cell encoding so SQ8 columns
2665/// take the ADC path with the right asymmetric variant. NaN /
2666/// dim-mismatch / non-vector → `f32::INFINITY`.
2667fn cell_to_query_metric_distance(
2668    table: &Table,
2669    col_pos: usize,
2670    row: usize,
2671    query: &[f32],
2672    metric: NswMetric,
2673) -> f32 {
2674    match table.rows.get(row).and_then(|r| r.values.get(col_pos)) {
2675        Some(Value::Vector(v)) if v.len() == query.len() => metric_distance(metric, v, query),
2676        Some(Value::Sq8Vector(q)) if q.bytes.len() == query.len() => match metric {
2677            NswMetric::L2 => quantize::sq8_l2_distance_sq_asymmetric(q, query),
2678            NswMetric::InnerProduct => quantize::sq8_inner_product_asymmetric(q, query),
2679            NswMetric::Cosine => quantize::sq8_cosine_distance_asymmetric(q, query),
2680        },
2681        // v6.0.6: halfvec dispatches by metric to fused NEON
2682        // kernels — no Vec<f32> allocation per call.
2683        Some(Value::HalfVector(h)) if h.dim() == query.len() => match metric {
2684            NswMetric::L2 => halfvec::half_l2_distance_sq_asymmetric(h, query),
2685            NswMetric::InnerProduct => halfvec::half_inner_product_asymmetric(h, query),
2686            NswMetric::Cosine => halfvec::half_cosine_distance_asymmetric(h, query),
2687        },
2688        _ => f32::INFINITY,
2689    }
2690}
2691
2692/// Distance metric used at NSW search time. The graph topology is
2693/// always built with `L2`; querying with `InnerProduct` / `Cosine`
2694/// reuses the same edges but ranks candidates by the chosen metric.
2695/// For the corpus-sized graphs this loses negligible recall vs
2696/// building separate per-metric graphs.
2697#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2698pub enum NswMetric {
2699    /// Squared Euclidean — ranks "smaller = closer" (the sqrt is
2700    /// monotonic so we skip it for ordering).
2701    L2,
2702    /// Negated dot product, matching pgvector `<#>` convention so
2703    /// "smaller = more similar" holds across all three metrics.
2704    InnerProduct,
2705    /// Cosine distance `1 - cos(a, b)`. Zero-norm operand yields
2706    /// `f32::INFINITY` so it sorts last.
2707    Cosine,
2708}
2709
2710/// Multi-layer HNSW kNN search: greedy-descend from the entry to layer 0,
2711/// then beam-search there with the requested `ef` to return the top `k`
2712/// results under the caller-chosen metric. Topology was built with L2 —
2713/// upper-layer descent uses L2 as a coarse heuristic; final beam search
2714/// runs in the requested metric so rankings are correct for `<#>` / `<=>`.
2715fn nsw_search(
2716    table: &Table,
2717    idx_pos: usize,
2718    query: &[f32],
2719    k: usize,
2720    ef: usize,
2721    metric: NswMetric,
2722) -> Vec<(f32, usize)> {
2723    let (entry, entry_level) = match &table.indices[idx_pos].kind {
2724        IndexKind::Nsw(g) => (g.entry, g.entry_level),
2725        IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => return Vec::new(),
2726    };
2727    let Some(entry) = entry else {
2728        return Vec::new();
2729    };
2730    let col_pos = table.indices[idx_pos].column_position;
2731    // v6.0.1 step 5: SQ8 columns over-fetch by `SQ8_RERANK_OVER_FETCH`
2732    // so the rerank pass below sees enough candidates to recover
2733    // recall after the ADC re-ordering. F32 + F16 columns skip the
2734    // over-fetch — F32 distances are exact, F16 dequant is
2735    // bit-exact at the storage layer so the beam search already
2736    // ranks under the column's full precision.
2737    let sq8 = matches!(
2738        table.schema.columns.get(col_pos).map(|c| c.ty),
2739        Some(DataType::Vector {
2740            encoding: VecEncoding::Sq8,
2741            ..
2742        })
2743    );
2744    let ef = if sq8 {
2745        ef.max(k).max(k * SQ8_RERANK_OVER_FETCH)
2746    } else {
2747        ef.max(k)
2748    };
2749    // Descend by L2 (the topology metric) so layers prune consistently.
2750    let entry_d = vec_l2_sq(table, col_pos, entry, query);
2751    let mut current = entry;
2752    let mut current_d = entry_d;
2753    for layer in (1..=entry_level).rev() {
2754        (current, current_d) = greedy_layer_walk(table, idx_pos, layer, current, current_d, query);
2755    }
2756    // Final beam search on layer 0 under the caller's metric.
2757    let mut results = layer_beam_search(table, idx_pos, 0, current, current_d, query, ef, metric);
2758    if sq8 {
2759        results = sq8_rerank(table, col_pos, &results, query, metric);
2760    }
2761    results.truncate(k);
2762    results
2763}
2764
2765/// v6.0.1 step 5: re-score ADC top-`K*3` candidates with the
2766/// dequantised cell vs the f32 query, then re-sort. Recovers the
2767/// recall the SQ8 ADC sacrifices for 4× compression — the design's
2768/// "f32 rerank step is on by default" path (deliberation #3).
2769/// `metric` is the same metric the beam search used; the rerank
2770/// arithmetic re-derives the exact distance under that metric.
2771fn sq8_rerank(
2772    table: &Table,
2773    col_pos: usize,
2774    candidates: &[(f32, usize)],
2775    query: &[f32],
2776    metric: NswMetric,
2777) -> Vec<(f32, usize)> {
2778    let mut out: Vec<(f32, usize)> = candidates
2779        .iter()
2780        .filter_map(|&(adc_d, row)| {
2781            let cell = table.rows.get(row).and_then(|r| r.values.get(col_pos))?;
2782            let Value::Sq8Vector(q) = cell else {
2783                // F32 cells shouldn't reach this path (sq8 fence
2784                // above), but stay defensive: pass through with
2785                // the ADC distance unchanged.
2786                return Some((adc_d, row));
2787            };
2788            let deq = quantize::dequantize(q);
2789            if deq.len() != query.len() {
2790                return None;
2791            }
2792            Some((metric_distance(metric, &deq, query), row))
2793        })
2794        .collect();
2795    out.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(core::cmp::Ordering::Equal));
2796    out
2797}
2798
2799/// Multiplier applied to `k` so the SQ8 rerank pass sees a wider
2800/// candidate set. 3× is the design-stage value; v6.0.5 sweep work
2801/// can re-tune once full corpus profiling is in.
2802const SQ8_RERANK_OVER_FETCH: usize = 3;
2803
2804fn metric_distance(metric: NswMetric, a: &[f32], b: &[f32]) -> f32 {
2805    match metric {
2806        NswMetric::L2 => l2_distance_sq(a, b),
2807        NswMetric::InnerProduct => -inner_product_f32(a, b),
2808        NswMetric::Cosine => {
2809            let (dot, na, nb) = cosine_dot_norms_f32(a, b);
2810            if na == 0.0 || nb == 0.0 {
2811                return f32::INFINITY;
2812            }
2813            // `f32::sqrt` lives in std, so hand-roll Newton-Raphson on
2814            // f64 — same trick the L2 binary op already uses.
2815            let denom = sqrt_newton_f32(na) * sqrt_newton_f32(nb);
2816            1.0 - dot / denom
2817        }
2818    }
2819}
2820
2821/// v6.0.2: dispatch wrapper for the f32 dot product (used by `<#>` +
2822/// the cosine numerator). NEON path when `len % 4 == 0 && len >= 4`,
2823/// scalar fallback otherwise. Returns the positive dot — callers
2824/// negate for the pgvector `<#>` "smaller = closer" convention.
2825///
2826/// Public so perf gates + downstream benches can microbenchmark the
2827/// dispatch directly; not part of the STABILITY contract — internal
2828/// SIMD layout can evolve in any release.
2829#[doc(hidden)]
2830#[inline]
2831pub fn inner_product_f32(a: &[f32], b: &[f32]) -> f32 {
2832    #[cfg(target_arch = "aarch64")]
2833    {
2834        if a.len() == b.len() && a.len() >= 4 && a.len().is_multiple_of(4) {
2835            // SAFETY: NEON is a baseline aarch64 feature; preconditions
2836            // (matching lengths, ≥ 1 full lane group) are checked above.
2837            return unsafe { inner_product_neon(a, b) };
2838        }
2839    }
2840    inner_product_scalar(a, b)
2841}
2842
2843fn inner_product_scalar(a: &[f32], b: &[f32]) -> f32 {
2844    let mut dot: f32 = 0.0;
2845    for (x, y) in a.iter().zip(b.iter()) {
2846        dot += x * y;
2847    }
2848    dot
2849}
2850
2851#[cfg(target_arch = "aarch64")]
2852#[target_feature(enable = "neon")]
2853#[allow(clippy::many_single_char_names)] // NEON intrinsics work in single-letter regs by convention
2854unsafe fn inner_product_neon(a: &[f32], b: &[f32]) -> f32 {
2855    use core::arch::aarch64::{
2856        float32x4_t, vaddq_f32, vaddvq_f32, vdupq_n_f32, vfmaq_f32, vld1q_f32,
2857    };
2858    unsafe {
2859        // Two parallel accumulators (same trick as L2 NEON) so the
2860        // FMA dependency chain doesn't serialise.
2861        let zero: float32x4_t = vdupq_n_f32(0.0);
2862        let mut acc0 = zero;
2863        let mut acc1 = zero;
2864        let n = a.len();
2865        let mut i = 0usize;
2866        while i + 8 <= n {
2867            let av0 = vld1q_f32(a.as_ptr().add(i));
2868            let bv0 = vld1q_f32(b.as_ptr().add(i));
2869            acc0 = vfmaq_f32(acc0, av0, bv0);
2870            let av1 = vld1q_f32(a.as_ptr().add(i + 4));
2871            let bv1 = vld1q_f32(b.as_ptr().add(i + 4));
2872            acc1 = vfmaq_f32(acc1, av1, bv1);
2873            i += 8;
2874        }
2875        while i + 4 <= n {
2876            let av = vld1q_f32(a.as_ptr().add(i));
2877            let bv = vld1q_f32(b.as_ptr().add(i));
2878            acc0 = vfmaq_f32(acc0, av, bv);
2879            i += 4;
2880        }
2881        vaddvq_f32(vaddq_f32(acc0, acc1))
2882    }
2883}
2884
2885/// v6.0.2: dispatch wrapper for the three accumulators (`dot`, `||a||²`,
2886/// `||b||²`) cosine needs. Same NEON pre-condition as the L2 / IP
2887/// paths; same scalar fallback shape.
2888///
2889/// Public for benchmarking only (see `inner_product_f32`); not in the
2890/// STABILITY contract.
2891#[doc(hidden)]
2892#[inline]
2893pub fn cosine_dot_norms_f32(a: &[f32], b: &[f32]) -> (f32, f32, f32) {
2894    #[cfg(target_arch = "aarch64")]
2895    {
2896        if a.len() == b.len() && a.len() >= 4 && a.len().is_multiple_of(4) {
2897            // SAFETY: see `inner_product_neon`.
2898            return unsafe { cosine_dot_norms_neon(a, b) };
2899        }
2900    }
2901    cosine_dot_norms_scalar(a, b)
2902}
2903
2904fn cosine_dot_norms_scalar(a: &[f32], b: &[f32]) -> (f32, f32, f32) {
2905    let mut dot: f32 = 0.0;
2906    let mut na: f32 = 0.0;
2907    let mut nb: f32 = 0.0;
2908    for (x, y) in a.iter().zip(b.iter()) {
2909        dot += x * y;
2910        na += x * x;
2911        nb += y * y;
2912    }
2913    (dot, na, nb)
2914}
2915
2916#[cfg(target_arch = "aarch64")]
2917#[target_feature(enable = "neon")]
2918#[allow(clippy::many_single_char_names, clippy::similar_names)]
2919unsafe fn cosine_dot_norms_neon(a: &[f32], b: &[f32]) -> (f32, f32, f32) {
2920    use core::arch::aarch64::{float32x4_t, vaddvq_f32, vdupq_n_f32, vfmaq_f32, vld1q_f32};
2921    unsafe {
2922        let zero: float32x4_t = vdupq_n_f32(0.0);
2923        let mut acc_dot = zero;
2924        let mut acc_na = zero;
2925        let mut acc_nb = zero;
2926        let n = a.len();
2927        let mut i = 0usize;
2928        while i + 4 <= n {
2929            let av = vld1q_f32(a.as_ptr().add(i));
2930            let bv = vld1q_f32(b.as_ptr().add(i));
2931            acc_dot = vfmaq_f32(acc_dot, av, bv);
2932            acc_na = vfmaq_f32(acc_na, av, av);
2933            acc_nb = vfmaq_f32(acc_nb, bv, bv);
2934            i += 4;
2935        }
2936        (vaddvq_f32(acc_dot), vaddvq_f32(acc_na), vaddvq_f32(acc_nb))
2937    }
2938}
2939
2940fn sqrt_newton_f32(x: f32) -> f32 {
2941    if x <= 0.0 {
2942        return 0.0;
2943    }
2944    let mut g = x;
2945    for _ in 0..10 {
2946        g = 0.5 * (g + x / g);
2947    }
2948    g
2949}
2950
2951/// Squared Euclidean distance — used for ordering inside NSW (the sqrt
2952/// preserves the order). Caller takes sqrt before reporting back to SQL.
2953///
2954/// v3.3.2: aarch64 NEON path for `len % 4 == 0` (which covers every
2955/// HNSW-indexed VECTOR(N) where N is a multiple of 4 — i.e. all
2956/// production-shaped embeddings: 64, 128, 256, 384, 512, 768, 1024,
2957/// 1536, ...). Other shapes fall back to the scalar loop.
2958#[inline]
2959fn l2_distance_sq(a: &[f32], b: &[f32]) -> f32 {
2960    #[cfg(target_arch = "aarch64")]
2961    {
2962        if a.len() == b.len() && a.len() >= 4 && a.len().is_multiple_of(4) {
2963            // SAFETY: NEON is a baseline aarch64 feature (ARMv8);
2964            // the precondition is checked above (matching lengths,
2965            // multiple of 4, at least one 128-bit lane group).
2966            return unsafe { l2_distance_sq_neon(a, b) };
2967        }
2968    }
2969    l2_distance_sq_scalar(a, b)
2970}
2971
2972fn l2_distance_sq_scalar(a: &[f32], b: &[f32]) -> f32 {
2973    let mut sum: f32 = 0.0;
2974    for (x, y) in a.iter().zip(b.iter()) {
2975        let d = *x - *y;
2976        sum += d * d;
2977    }
2978    sum
2979}
2980
2981#[cfg(target_arch = "aarch64")]
2982#[target_feature(enable = "neon")]
2983#[allow(clippy::many_single_char_names)] // NEON intrinsics work in single-letter regs by convention
2984unsafe fn l2_distance_sq_neon(a: &[f32], b: &[f32]) -> f32 {
2985    use core::arch::aarch64::{
2986        float32x4_t, vaddq_f32, vaddvq_f32, vdupq_n_f32, vfmaq_f32, vld1q_f32, vsubq_f32,
2987    };
2988    unsafe {
2989        // Two independent accumulator registers so the FMA dependency
2990        // chain doesn't serialise (each FMA depends on prior FMA).
2991        // Pre-conditions checked by caller: `a.len() == b.len()`,
2992        // `a.len() % 4 == 0`, `a.len() >= 4`.
2993        let zero: float32x4_t = vdupq_n_f32(0.0);
2994        let mut acc0 = zero;
2995        let mut acc1 = zero;
2996        let n = a.len();
2997        let mut i = 0usize;
2998        // Process 8 floats per iter when available (two parallel
2999        // accumulators). Tail of 4 falls into the second loop.
3000        while i + 8 <= n {
3001            let d0 = vsubq_f32(vld1q_f32(a.as_ptr().add(i)), vld1q_f32(b.as_ptr().add(i)));
3002            acc0 = vfmaq_f32(acc0, d0, d0);
3003            let d1 = vsubq_f32(
3004                vld1q_f32(a.as_ptr().add(i + 4)),
3005                vld1q_f32(b.as_ptr().add(i + 4)),
3006            );
3007            acc1 = vfmaq_f32(acc1, d1, d1);
3008            i += 8;
3009        }
3010        while i + 4 <= n {
3011            let d = vsubq_f32(vld1q_f32(a.as_ptr().add(i)), vld1q_f32(b.as_ptr().add(i)));
3012            acc0 = vfmaq_f32(acc0, d, d);
3013            i += 4;
3014        }
3015        vaddvq_f32(vaddq_f32(acc0, acc1))
3016    }
3017}
3018
3019/// Public wrapper: run an NSW kNN search and return the top-k row
3020/// indices ordered by ascending distance under the given metric.
3021pub fn nsw_query(
3022    table: &Table,
3023    idx_name: &str,
3024    query: &[f32],
3025    k: usize,
3026    metric: NswMetric,
3027) -> Vec<usize> {
3028    let Some(idx_pos) = table.indices.iter().position(|i| i.name == idx_name) else {
3029        return Vec::new();
3030    };
3031    let ef = (k * 2).max(NSW_DEFAULT_M);
3032    let mut hits = nsw_search(table, idx_pos, query, k, ef, metric);
3033    hits.truncate(k);
3034    hits.into_iter().map(|(_, idx)| idx).collect()
3035}
3036
3037/// Find any NSW index on a column. Used by the planner to decide
3038/// whether an `ORDER BY col <-> literal LIMIT k` query can skip the
3039/// brute-force scan.
3040pub fn nsw_index_on(table: &Table, column_position: usize) -> Option<&Index> {
3041    table
3042        .indices
3043        .iter()
3044        .find(|i| i.column_position == column_position && matches!(i.kind, IndexKind::Nsw(_)))
3045}
3046
3047/// Catalog: insertion-ordered `Vec<Table>` for stable iter / serialize,
3048/// plus a `BTreeMap<String, usize>` sidecar index so `get` / `get_mut`
3049/// run in O(log n) instead of the old linear scan with per-element
3050/// string compares.
3051///
3052/// A pure `BTreeMap<String, Table>` was tried in an interim version
3053/// of v3.1.2 and regressed the single-table catalog benches by ~10%
3054/// (the per-element `BTreeMap` overhead outweighs the lookup win
3055/// when n is small). The sidecar shape preserves the insertion-order
3056/// iteration the on-disk encoding relies on and keeps `last_mut`
3057/// (used by the deserialize hot path) cheap.
3058#[derive(Debug, Clone, Default)]
3059pub struct Catalog {
3060    tables: Vec<Table>,
3061    /// `name → tables[index]`. Kept in lock-step with `tables`.
3062    /// `create_table` is the only write path.
3063    by_name: BTreeMap<String, usize>,
3064    /// v5.1: in-memory cold-tier segments. Side-loaded via
3065    /// [`Catalog::load_segment_bytes`] — they live outside the
3066    /// catalog snapshot (caller persists them as separate files
3067    /// and re-loads on boot, until v5.3's `CatalogManifest` makes
3068    /// that wiring automatic). `RowLocator::Cold { segment_id, .. }`
3069    /// indexes this `Vec`. Cleared on `Catalog::new` / fresh
3070    /// `deserialize`.
3071    ///
3072    /// `Arc` wrap keeps `Catalog::clone` at O(N segments) bumps
3073    /// (rather than O(total segment bytes) memcpy) so the v4.42
3074    /// group-commit pre-image rollback invariant — clone is
3075    /// effectively free — survives the cold-tier addition.
3076    ///
3077    /// v6.7.3 — slots became `Option<…>` so cold-segment compaction
3078    /// can tombstone merged sources without breaking the
3079    /// `segment_id = index_into_vec` contract that on-disk
3080    /// `RowLocator::Cold { segment_id }` already serialized.
3081    /// `None` slot = the segment was retired by compaction; the
3082    /// physical file may still be on disk (next CHECKPOINT writes
3083    /// a manifest that no longer lists it, and the file becomes
3084    /// an orphan eligible for offline cleanup).
3085    cold_segments: Vec<Option<Arc<OwnedSegment>>>,
3086    /// v7.12.4 — user-defined functions (PL/pgSQL + SQL).
3087    /// Keyed by function name (PG overloading is out of scope).
3088    /// Bodies are stored as the raw source text the parser saw
3089    /// between `$$ ... $$`; the engine re-parses on each
3090    /// invocation. This keeps `spg-storage` free of `spg-sql`
3091    /// dependency — same pattern as partial-index predicates.
3092    functions: BTreeMap<String, FunctionDef>,
3093    /// v7.12.4 — triggers in insertion order. Multiple triggers
3094    /// per table / event fire in this order (matching PG's
3095    /// alphabetical-by-default with insertion-stable tie-break
3096    /// behaviour — we just keep insertion order for now).
3097    triggers: Vec<TriggerDef>,
3098}
3099
3100/// v7.12.4 — catalogued user-defined function. `body` is the raw
3101/// source text between `$$ ... $$`; the engine re-parses it on
3102/// invocation. This keeps the storage codec stable when the
3103/// PL/pgSQL surface grows (no breaking-change risk on the disk
3104/// format).
3105#[derive(Debug, Clone, PartialEq, Eq)]
3106pub struct FunctionDef {
3107    pub name: String,
3108    /// Display form of the argument list, e.g.
3109    /// `"(name TEXT, ts TIMESTAMP)"`. Empty `"()"` for the trigger
3110    /// function shape. Parser-side canonicalised before storage.
3111    pub args_repr: String,
3112    /// Display form of the return type, e.g. `"TRIGGER"` /
3113    /// `"INT"` / `"SETOF text"`. The engine special-cases
3114    /// `"TRIGGER"` (case-insensitive) to gate trigger-only
3115    /// semantics (NEW/OLD).
3116    pub returns: String,
3117    /// `LANGUAGE` clause, lowercased. `"plpgsql"` / `"sql"`.
3118    pub language: String,
3119    /// Source body of the function. PL/pgSQL: includes the
3120    /// surrounding `BEGIN ... END;`. SQL: includes the
3121    /// statement(s). The engine re-parses on invocation; bad
3122    /// bodies surface as a parse error at CALL time, not CREATE.
3123    pub body: String,
3124}
3125
3126/// v7.12.4 — catalogued trigger. References its function by
3127/// name; the function must exist at TRIGGER creation time
3128/// (forward references are deferred to v7.12.5+).
3129#[derive(Debug, Clone, PartialEq, Eq)]
3130pub struct TriggerDef {
3131    pub name: String,
3132    /// Watched table. Trigger is dropped when the table drops.
3133    pub table: String,
3134    /// `"BEFORE"` / `"AFTER"` / `"INSTEAD OF"`. Stored as the
3135    /// uppercased keyword so deserialised catalogs round-trip
3136    /// without canonicalisation surprises.
3137    pub timing: String,
3138    /// Each entry is one of `"INSERT"` / `"UPDATE"` / `"DELETE"`
3139    /// / `"TRUNCATE"`. `INSERT OR UPDATE` parses to two entries.
3140    pub events: Vec<String>,
3141    /// `"ROW"` / `"STATEMENT"`. v7.12.4 ships `"ROW"` only;
3142    /// `"STATEMENT"` parses and persists but the executor
3143    /// refuses it at trigger fire time.
3144    pub for_each: String,
3145    /// Name of the PL/pgSQL function to invoke.
3146    pub function: String,
3147    /// v7.13.0 — `UPDATE OF col, col, …` column-list filter
3148    /// (mailrs round-5 G7). Non-empty means the trigger fires
3149    /// only when at least one of these columns appears in the
3150    /// UPDATE's SET list. Empty = no column filter. Stored in
3151    /// catalog FILE_VERSION 23+; older catalogs deserialise with
3152    /// an empty vec.
3153    pub update_columns: Vec<String>,
3154}
3155
3156impl Catalog {
3157    pub const fn new() -> Self {
3158        Self {
3159            tables: Vec::new(),
3160            by_name: BTreeMap::new(),
3161            cold_segments: Vec::new(),
3162            functions: BTreeMap::new(),
3163            triggers: Vec::new(),
3164        }
3165    }
3166
3167    /// v7.12.4 — read-only view of catalogued user-defined
3168    /// functions. Engine callers go through here to look up the
3169    /// function body before re-parsing it for invocation.
3170    pub const fn functions(&self) -> &BTreeMap<String, FunctionDef> {
3171        &self.functions
3172    }
3173
3174    /// v7.12.4 — register a new user-defined function. With
3175    /// `or_replace = false`, errors if the name is taken. The
3176    /// engine validates the body before passing it here.
3177    pub fn create_function(
3178        &mut self,
3179        def: FunctionDef,
3180        or_replace: bool,
3181    ) -> Result<(), StorageError> {
3182        if !or_replace && self.functions.contains_key(&def.name) {
3183            return Err(StorageError::Corrupt(format!(
3184                "function {:?} already exists (drop or use CREATE OR REPLACE)",
3185                def.name
3186            )));
3187        }
3188        self.functions.insert(def.name.clone(), def);
3189        Ok(())
3190    }
3191
3192    /// v7.12.4 — remove a user-defined function by name. Returns
3193    /// `true` if a function was removed, `false` if none matched.
3194    /// Caller decides whether to surface `if_exists` semantics.
3195    pub fn drop_function(&mut self, name: &str) -> bool {
3196        self.functions.remove(name).is_some()
3197    }
3198
3199    /// v7.12.4 — read-only slice of all catalogued triggers.
3200    /// Engine row-write paths filter this by (table, event,
3201    /// timing) and fire matches in slice order.
3202    pub fn triggers(&self) -> &[TriggerDef] {
3203        &self.triggers
3204    }
3205
3206    /// v7.12.4 — register a new trigger. With `or_replace = false`,
3207    /// errors when a trigger with the same name already exists on
3208    /// the same table (PG scoping rule — trigger names are
3209    /// per-table, not global). Trigger function must already
3210    /// exist in the catalog at registration time.
3211    pub fn create_trigger(
3212        &mut self,
3213        def: TriggerDef,
3214        or_replace: bool,
3215    ) -> Result<(), StorageError> {
3216        if !self.by_name.contains_key(&def.table) {
3217            return Err(StorageError::TableNotFound {
3218                name: def.table.clone(),
3219            });
3220        }
3221        if !self.functions.contains_key(&def.function) {
3222            return Err(StorageError::Corrupt(format!(
3223                "trigger {:?} references unknown function {:?}",
3224                def.name, def.function
3225            )));
3226        }
3227        let dup = self
3228            .triggers
3229            .iter()
3230            .position(|t| t.name == def.name && t.table == def.table);
3231        match (dup, or_replace) {
3232            (Some(_), false) => Err(StorageError::Corrupt(format!(
3233                "trigger {:?} already exists on table {:?}",
3234                def.name, def.table
3235            ))),
3236            (Some(i), true) => {
3237                self.triggers[i] = def;
3238                Ok(())
3239            }
3240            (None, _) => {
3241                self.triggers.push(def);
3242                Ok(())
3243            }
3244        }
3245    }
3246
3247    /// v7.12.4 — remove a trigger by `(name, table)`. Returns
3248    /// `true` if one was removed.
3249    pub fn drop_trigger(&mut self, name: &str, table: &str) -> bool {
3250        let before = self.triggers.len();
3251        self.triggers
3252            .retain(|t| !(t.name == name && t.table == table));
3253        before != self.triggers.len()
3254    }
3255
3256    pub fn create_table(&mut self, schema: TableSchema) -> Result<(), StorageError> {
3257        if self.by_name.contains_key(&schema.name) {
3258            return Err(StorageError::DuplicateTable {
3259                name: schema.name.clone(),
3260            });
3261        }
3262        let idx = self.tables.len();
3263        let name = schema.name.clone();
3264        self.tables.push(Table::new(schema));
3265        self.by_name.insert(name, idx);
3266        Ok(())
3267    }
3268
3269    pub fn get(&self, name: &str) -> Option<&Table> {
3270        let idx = *self.by_name.get(name)?;
3271        self.tables.get(idx)
3272    }
3273
3274    pub fn get_mut(&mut self, name: &str) -> Option<&mut Table> {
3275        let idx = *self.by_name.get(name)?;
3276        self.tables.get_mut(idx)
3277    }
3278
3279    pub fn table_count(&self) -> usize {
3280        self.tables.len()
3281    }
3282
3283    /// Borrow-free copy of every table's name in catalog order
3284    /// (= insertion order, matching the on-disk encoding).
3285    pub fn table_names(&self) -> Vec<String> {
3286        self.tables.iter().map(|t| t.schema.name.clone()).collect()
3287    }
3288
3289    /// v5.1: register a cold-tier segment that already lives in
3290    /// memory (caller did the file read). Returns the
3291    /// `segment_id` that `RowLocator::Cold { segment_id, .. }`
3292    /// will reference — currently this is just the index into
3293    /// `cold_segments`, but treat it as an opaque token.
3294    ///
3295    /// Storage is `no_std`, so file I/O is the caller's
3296    /// responsibility — `spg-server` reads the file and forwards
3297    /// the bytes here. The bytes stay resident in the catalog
3298    /// for the life of the `Catalog`, parsed only once.
3299    pub fn load_segment_bytes(&mut self, bytes: Vec<u8>) -> Result<u32, StorageError> {
3300        let id = u32::try_from(self.cold_segments.len()).map_err(|_| {
3301            StorageError::Corrupt("cold segment count would exceed u32::MAX".into())
3302        })?;
3303        let seg = OwnedSegment::from_bytes(bytes)
3304            .map_err(|e| StorageError::Corrupt(format!("cold segment parse failed: {e}")))?;
3305        self.cold_segments.push(Some(Arc::new(seg)));
3306        Ok(id)
3307    }
3308
3309    /// v6.7.3 — register a cold-tier segment at a specific id. Used
3310    /// by the spg-server manifest-boot path so segments whose
3311    /// neighbouring ids were retired by compaction still get back
3312    /// the same `segment_id` they had pre-restart (the
3313    /// `RowLocator::Cold { segment_id }` baked into the BTree-index
3314    /// snapshot persists across restart and must continue to
3315    /// resolve).
3316    ///
3317    /// Pads the Vec with `None` slots up to `target_id` if needed.
3318    /// Errors when the target slot is already occupied (would
3319    /// stomp another segment), the parse fails, or `target_id`
3320    /// exceeds `u32::MAX`.
3321    pub fn load_segment_bytes_at(
3322        &mut self,
3323        target_id: u32,
3324        bytes: Vec<u8>,
3325    ) -> Result<(), StorageError> {
3326        let seg = OwnedSegment::from_bytes(bytes)
3327            .map_err(|e| StorageError::Corrupt(format!("cold segment parse failed: {e}")))?;
3328        let idx = target_id as usize;
3329        while self.cold_segments.len() <= idx {
3330            self.cold_segments.push(None);
3331        }
3332        if self.cold_segments[idx].is_some() {
3333            return Err(StorageError::Corrupt(format!(
3334                "load_segment_bytes_at: segment_id {target_id} already occupied"
3335            )));
3336        }
3337        self.cold_segments[idx] = Some(Arc::new(seg));
3338        Ok(())
3339    }
3340
3341    /// v6.7.3 — retire a cold-tier segment slot (compaction-driven).
3342    /// The physical file is the caller's concern (typically kept
3343    /// on disk until the next CHECKPOINT writes a manifest that
3344    /// no longer lists it); this just flips the in-memory slot
3345    /// to `None` so later cold lookups for `segment_id` resolve
3346    /// as "unknown" instead of returning a stale row.
3347    ///
3348    /// No-op when the slot is already `None`. Errors only when
3349    /// `segment_id` is out of bounds.
3350    pub fn tombstone_segment(&mut self, segment_id: u32) -> Result<(), StorageError> {
3351        let idx = segment_id as usize;
3352        if idx >= self.cold_segments.len() {
3353            return Err(StorageError::Corrupt(format!(
3354                "tombstone_segment: segment_id {segment_id} out of bounds (len={})",
3355                self.cold_segments.len()
3356            )));
3357        }
3358        self.cold_segments[idx] = None;
3359        Ok(())
3360    }
3361
3362    /// Number of *active* (non-tombstoned) cold segments.
3363    #[must_use]
3364    pub fn cold_segment_count(&self) -> usize {
3365        self.cold_segments.iter().filter(|s| s.is_some()).count()
3366    }
3367
3368    /// Slot count including tombstones (= the next id the
3369    /// no-arg `load_segment_bytes` would allocate).
3370    #[must_use]
3371    pub fn cold_segment_slot_count(&self) -> usize {
3372        self.cold_segments.len()
3373    }
3374
3375    /// v6.2.7 — list every *active* cold-tier segment id known to
3376    /// this catalog (skips compaction tombstones since v6.7.3).
3377    /// Used by EXPLAIN ANALYZE to annotate scan nodes with the
3378    /// segments they could have walked.
3379    #[must_use]
3380    pub fn cold_segment_ids_global(&self) -> Vec<u32> {
3381        self.cold_segments
3382            .iter()
3383            .enumerate()
3384            .filter_map(|(i, s)| s.as_ref().map(|_| i as u32))
3385            .collect()
3386    }
3387
3388    /// v5.2.1: sum of `Table::hot_bytes` across every table. The v5.2
3389    /// freezer compares this against `SPG_HOT_TIER_BYTES` (parsed at
3390    /// server startup; default 4 GiB) and wakes when the budget is
3391    /// crossed. Pre-freezer (v5.2.1) this is measurement-only — the
3392    /// counter exposes whether the budget is being approached without
3393    /// triggering any demotion.
3394    #[must_use]
3395    pub fn hot_tier_bytes(&self) -> u64 {
3396        self.tables
3397            .iter()
3398            .map(Table::hot_bytes)
3399            .fold(0u64, u64::saturating_add)
3400    }
3401
3402    /// v5.2.2: freeze the **first** `max_rows` rows of `table_name`'s
3403    /// hot tier into a brand-new cold-tier segment. The named `BTree`
3404    /// index supplies the per-row PK (its column must be an integer
3405    /// type — v5.2.2 only supports `IndexKey::Int` PKs, matching the
3406    /// `index_key_as_u64` constraint used by the cold-tier lookup
3407    /// path). On success returns a [`FreezeReport`] with the
3408    /// freshly-allocated segment id, the count of rows that moved,
3409    /// the encoded segment bytes (so the caller can persist them to
3410    /// disk for later reload via `SPG_PRELOAD_COLD_SEGMENT`), and the
3411    /// hot-tier byte delta that was reclaimed.
3412    ///
3413    /// **Semantics**:
3414    /// 1. The first `max_rows` rows (by hot-tier position — same as
3415    ///    insertion order under v4.39 `PersistentVec`) are read.
3416    /// 2. Rows are sorted ascending by PK and serialised into a new
3417    ///    segment via [`encode_segment`].
3418    /// 3. The hot rows are dropped via [`Table::delete_rows`]; the
3419    ///    `rebuild_indices` it triggers regenerates `Hot` locators
3420    ///    for every remaining row (their positions shift down by
3421    ///    `max_rows`). Existing `Cold` locators in this index — from
3422    ///    a previous freeze — are also rebuilt **but with empty
3423    ///    payload** since rebuild reads only `self.rows`; this
3424    ///    routine re-registers them at the end of the call so the
3425    ///    user-visible state preserves all prior cold locators.
3426    /// 4. The new segment is loaded into `self.cold_segments` via
3427    ///    [`Catalog::load_segment_bytes`] (allocating a fresh
3428    ///    `segment_id`). New `Cold` locators are registered on the
3429    ///    named index — one per frozen row.
3430    ///
3431    /// **v5.2.2 limits** (relaxed in later sub-versions):
3432    /// - INSERT-only flow: subsequent UPDATE/DELETE on a frozen row
3433    ///   returns a stale-locator error (no promote-on-write until
3434    ///   v5.2.3).
3435    /// - Single-table scope: callers iterate tables themselves.
3436    /// - All-or-nothing: returns `Err` and leaves catalog unchanged
3437    ///   if any step fails before the atomic swap point.
3438    ///
3439    /// Errors:
3440    /// - [`StorageError::Corrupt`] for missing table/index, non-`BTree`
3441    ///   index, non-integer PK column, `max_rows == 0`, or
3442    ///   `max_rows > row_count`.
3443    /// - The encoder's [`SegmentError`] surfaces as `Corrupt` (the
3444    ///   only realistic source is "a single row is larger than the
3445    ///   page size"; SPG schemas don't hit it in practice).
3446    pub fn freeze_oldest_to_cold(
3447        &mut self,
3448        table_name: &str,
3449        index_name: &str,
3450        max_rows: usize,
3451    ) -> Result<FreezeReport, StorageError> {
3452        // --- validation phase: never mutates ---------------------
3453        if max_rows == 0 {
3454            return Err(StorageError::Corrupt(
3455                "freeze_oldest_to_cold: max_rows must be > 0".into(),
3456            ));
3457        }
3458        let table = self.get(table_name).ok_or_else(|| {
3459            StorageError::Corrupt(format!(
3460                "freeze_oldest_to_cold: table {table_name:?} not found"
3461            ))
3462        })?;
3463        if max_rows > table.rows.len() {
3464            return Err(StorageError::Corrupt(format!(
3465                "freeze_oldest_to_cold: max_rows {max_rows} > row_count {}",
3466                table.rows.len()
3467            )));
3468        }
3469        let idx = table
3470            .indices
3471            .iter()
3472            .find(|i| i.name == index_name)
3473            .ok_or_else(|| {
3474                StorageError::Corrupt(format!(
3475                    "freeze_oldest_to_cold: index {index_name:?} not found on {table_name:?}"
3476                ))
3477            })?;
3478        if !matches!(idx.kind, IndexKind::BTree(_)) {
3479            return Err(StorageError::Corrupt(format!(
3480                "freeze_oldest_to_cold: index {index_name:?} is NSW; only BTree indices may freeze"
3481            )));
3482        }
3483        let column_position = idx.column_position;
3484
3485        // --- segment build phase: reads only --------------------
3486        let schema = table.schema.clone();
3487        let mut to_freeze: Vec<(u64, Vec<u8>, IndexKey)> = Vec::with_capacity(max_rows);
3488        for row_idx in 0..max_rows {
3489            let row = table.rows.get(row_idx).expect("bounds-checked above");
3490            let key = IndexKey::from_value(&row.values[column_position]).ok_or_else(|| {
3491                StorageError::Corrupt(format!(
3492                    "freeze_oldest_to_cold: row {row_idx} has NULL / non-key value in index column"
3493                ))
3494            })?;
3495            let pk_u64 = index_key_as_u64(&key).ok_or_else(|| {
3496                StorageError::Corrupt(format!(
3497                    "freeze_oldest_to_cold: index {index_name:?} column type is non-integer; \
3498                     v5.2.2 cold tier requires IndexKey::Int (Text PK lands in v5.5+)"
3499                ))
3500            })?;
3501            to_freeze.push((pk_u64, encode_row_body_dense(row, &schema), key));
3502        }
3503        // encode_segment requires ascending u64 keys. Sort by PK
3504        // before encoding; the caller's row-position order is not
3505        // necessarily PK order (e.g. workloads that insert random
3506        // PKs).
3507        to_freeze.sort_by_key(|(k, _, _)| *k);
3508        // Reject duplicate PKs — encode_segment also rejects them
3509        // (`SegmentError::UnsortedKey`), but the resulting error
3510        // message there is misleading. Surface a clearer one.
3511        for w in to_freeze.windows(2) {
3512            if w[0].0 == w[1].0 {
3513                return Err(StorageError::Corrupt(format!(
3514                    "freeze_oldest_to_cold: duplicate PK {} in freeze batch",
3515                    w[0].0
3516                )));
3517            }
3518        }
3519        // Snapshot the (key, locator) pairs that will be registered
3520        // post-swap. Cloning the IndexKey out before the move makes
3521        // the registration loop borrow-free.
3522        let post_swap_keys: Vec<IndexKey> = to_freeze.iter().map(|(_, _, k)| k.clone()).collect();
3523        // Segment encode is now infallible w.r.t. ordering. Map the
3524        // `SegmentError` into a `StorageError::Corrupt` so the
3525        // public surface stays one error type.
3526        let seg_rows: Vec<(u64, Vec<u8>)> = to_freeze
3527            .into_iter()
3528            .map(|(k, body, _)| (k, body))
3529            .collect();
3530        let frozen_rows = seg_rows.len();
3531        let (seg_bytes, _meta) = encode_segment(seg_rows.into_iter(), 0.01, SEGMENT_PAGE_BYTES)
3532            .map_err(|e| StorageError::Corrupt(format!("freeze_oldest_to_cold: encode: {e}")))?;
3533
3534        // --- atomic swap phase: mutations only past this point ---
3535        // v5.2.3 made `Table::rebuild_indices` preserve every Cold
3536        // locator across the per-table rebuild, so `delete_rows`
3537        // below no longer wipes prior-freeze cold entries. The pre-
3538        // v5.2.3 capture-then-re-register that used to live here
3539        // was removed in v5.3.1 — keeping it would double-count
3540        // every prior-frozen key's Cold locator on each subsequent
3541        // freeze.
3542        let bytes_before = self.get(table_name).expect("just validated").hot_bytes();
3543        let positions: Vec<usize> = (0..max_rows).collect();
3544        let t_mut = self
3545            .get_mut(table_name)
3546            .expect("just validated; still present");
3547        let removed = t_mut.delete_rows(&positions);
3548        debug_assert_eq!(removed, max_rows, "delete_rows count matches request");
3549        let bytes_after = t_mut.hot_bytes();
3550        let bytes_freed = bytes_before.saturating_sub(bytes_after);
3551
3552        let segment_id = self
3553            .load_segment_bytes(seg_bytes.clone())
3554            .map_err(|e| StorageError::Corrupt(format!("freeze_oldest_to_cold: load: {e}")))?;
3555        let new_cold = post_swap_keys.into_iter().map(|k| {
3556            (
3557                k,
3558                RowLocator::Cold {
3559                    segment_id,
3560                    page_offset: 0,
3561                },
3562            )
3563        });
3564        let t_mut = self.get_mut(table_name).expect("still present");
3565        t_mut.register_cold_locators(index_name, new_cold)?;
3566
3567        Ok(FreezeReport {
3568            segment_id,
3569            frozen_rows,
3570            bytes_freed,
3571            segment_bytes: seg_bytes,
3572        })
3573    }
3574
3575    /// v5.1: borrow the cold segment at `segment_id`. Used by the
3576    /// spg-server preload path to enumerate (key, locator) pairs
3577    /// after loading a segment, so it can call
3578    /// [`Table::register_cold_locators`] without re-parsing the
3579    /// bytes.
3580    #[must_use]
3581    pub fn cold_segment(&self, segment_id: u32) -> Option<&OwnedSegment> {
3582        self.cold_segments
3583            .get(segment_id as usize)
3584            .and_then(|s| s.as_deref())
3585    }
3586
3587    /// v5.1: resolve a single `RowLocator::Cold` to its underlying
3588    /// `Row`. Decoupled from [`Catalog::lookup_by_pk`] so callers
3589    /// iterating a multi-locator slice (e.g. the engine's index
3590    /// seek path) can dispatch per locator instead of getting back
3591    /// only the first row for a key. Returns `None` when the
3592    /// segment isn't registered, the key isn't `u64`-coercible, or
3593    /// the segment doesn't actually carry the key (bloom or page-
3594    /// index reject).
3595    pub fn resolve_cold_locator(
3596        &self,
3597        table_name: &str,
3598        segment_id: u32,
3599        key: &IndexKey,
3600    ) -> Option<Row> {
3601        let t = self.get(table_name)?;
3602        let u64_key = index_key_as_u64(key)?;
3603        let seg = self.cold_segments.get(segment_id as usize)?.as_ref()?;
3604        let payload = seg.lookup(u64_key)?;
3605        let (row, _) = decode_row_body_dense(&payload, &t.schema).ok()?;
3606        Some(row)
3607    }
3608
3609    /// v5.1: indexed PK lookup that dispatches per locator,
3610    /// returning the first matching row from either the hot tier
3611    /// (`Table::rows`) or a registered cold segment.
3612    ///
3613    /// The cold path requires the index column to be coercible to
3614    /// a `u64` (the segment's PK type) and the segment payload to
3615    /// be a [`encode_row_body_dense`]-encoded row body for the
3616    /// same schema. v5.1 ships this for BIGINT / INT / SMALLINT
3617    /// PKs; other types fall through to hot-only behavior.
3618    ///
3619    /// Returns `None` if (a) the table or index doesn't exist,
3620    /// (b) the key isn't in the index at all, or (c) the key was
3621    /// resolved to a stale locator (Hot index out of range, Cold
3622    /// segment id unknown, segment lookup miss). Does not surface
3623    /// segment-decode errors — those would indicate corrupted
3624    /// cold-tier files and should be caught at
3625    /// [`Catalog::load_segment_bytes`] time.
3626    pub fn lookup_by_pk(&self, table: &str, index_name: &str, key: &IndexKey) -> Option<Row> {
3627        let t = self.get(table)?;
3628        let idx = t.indices.iter().find(|i| i.name == index_name)?;
3629        let locators = idx.lookup_eq(key);
3630        let cold_u64_key = index_key_as_u64(key);
3631        for loc in locators {
3632            match *loc {
3633                RowLocator::Hot(i) => {
3634                    if let Some(row) = t.rows.get(i) {
3635                        return Some(row.clone());
3636                    }
3637                }
3638                RowLocator::Cold {
3639                    segment_id,
3640                    page_offset: _,
3641                } => {
3642                    let Some(u64_key) = cold_u64_key else {
3643                        // Key type not coercible to u64 — cold tier
3644                        // only handles BIGINT/INT/SMALLINT in v5.1.
3645                        continue;
3646                    };
3647                    let Some(seg) = self
3648                        .cold_segments
3649                        .get(segment_id as usize)
3650                        .and_then(|s| s.as_deref())
3651                    else {
3652                        // v6.7.3 — `None` slot = compaction
3653                        // retired this segment; the live locator
3654                        // on a freshly-compacted index points to
3655                        // the merged segment_id, so a Cold hit
3656                        // here against a tombstone means the BTree
3657                        // entry hasn't been swapped yet (mid-
3658                        // compaction reader race) or the caller is
3659                        // looking up a stale snapshot. Skip — the
3660                        // next locator in the list, if any, is
3661                        // typically the merged segment.
3662                        continue;
3663                    };
3664                    let Some(payload) = seg.lookup(u64_key) else {
3665                        continue;
3666                    };
3667                    let (row, _) = decode_row_body_dense(&payload, &t.schema).ok()?;
3668                    return Some(row);
3669                }
3670            }
3671        }
3672        None
3673    }
3674
3675    /// v5.2.3: promote a frozen row back to the hot tier so an
3676    /// UPDATE / DELETE can mutate it. Reads the cold-tier row body
3677    /// (decoded from its registered segment), pushes it into
3678    /// `table.rows` via [`Table::insert`] (which also adds a fresh
3679    /// `Hot(new_idx)` locator on `index_name`), then retires the
3680    /// shadowed `Cold` locator via
3681    /// [`Table::remove_cold_locators_for_key`]. The cold-tier row
3682    /// in the segment file becomes garbage — recoverable when a
3683    /// future cold-segment compaction job lands.
3684    ///
3685    /// Returns:
3686    /// - `Ok(Some(new_hot_idx))` when the key resolved through a
3687    ///   cold locator and the promote completed. `new_hot_idx` is
3688    ///   the position the row now occupies in `table.rows`.
3689    /// - `Ok(None)` when the key has no Cold locator on the index
3690    ///   (already hot, or wasn't present at all). Callers treat this
3691    ///   as "nothing to do here, fall back to the hot-only path".
3692    ///
3693    /// Errors when the table / index doesn't exist, the index isn't
3694    /// `BTree`, the cold segment is missing / can't decode the row,
3695    /// or the inferred row body fails `Table::insert` validation.
3696    pub fn promote_cold_row(
3697        &mut self,
3698        table_name: &str,
3699        index_name: &str,
3700        key: &IndexKey,
3701    ) -> Result<Option<usize>, StorageError> {
3702        let cold_loc = self.find_cold_locator(table_name, index_name, key)?;
3703        let Some((segment_id, _page_offset)) = cold_loc else {
3704            return Ok(None);
3705        };
3706        let u64_key = index_key_as_u64(key).ok_or_else(|| {
3707            StorageError::Corrupt(
3708                "promote_cold_row: key type not coercible to u64 (cold tier requires integer PK)"
3709                    .into(),
3710            )
3711        })?;
3712        // Read the row body from the segment. Borrow the segment +
3713        // schema short-term so we can then take `&mut self` for the
3714        // hot-side insert.
3715        let schema = self
3716            .get(table_name)
3717            .ok_or_else(|| {
3718                StorageError::Corrupt(format!("promote_cold_row: table {table_name:?} not found"))
3719            })?
3720            .schema
3721            .clone();
3722        let seg = self
3723            .cold_segments
3724            .get(segment_id as usize)
3725            .and_then(|s| s.as_ref())
3726            .ok_or_else(|| {
3727                StorageError::Corrupt(format!(
3728                    "promote_cold_row: segment {segment_id} not registered on catalog"
3729                ))
3730            })?;
3731        let payload = seg.lookup(u64_key).ok_or_else(|| {
3732            StorageError::Corrupt(format!(
3733                "promote_cold_row: key {u64_key} resolves to segment {segment_id} \
3734                 but the segment's bloom/page lookup didn't return a row"
3735            ))
3736        })?;
3737        let (row, _consumed) = decode_row_body_dense(&payload, &schema)?;
3738        // Insert the promoted row into the hot tier. `Table::insert`
3739        // appends to `self.rows`, adds a `Hot(new_idx)` locator to
3740        // every BTree index covering the row's keyed columns, and
3741        // increments `hot_bytes`.
3742        let t = self
3743            .get_mut(table_name)
3744            .expect("table existed at lookup time");
3745        t.insert(row)?;
3746        let new_hot_idx =
3747            t.rows.len().checked_sub(1).ok_or_else(|| {
3748                StorageError::Corrupt("promote_cold_row: empty after insert".into())
3749            })?;
3750        // The hot insert added Hot(new_idx) alongside the still-
3751        // present Cold locator. Drop the Cold entry so future
3752        // lookups return only the fresh hot row.
3753        t.remove_cold_locators_for_key(index_name, key)?;
3754        Ok(Some(new_hot_idx))
3755    }
3756
3757    /// v5.2.3: shadow a frozen row's index entry. Used by DELETE
3758    /// when the row to remove lives in a cold-tier segment — the
3759    /// row body stays in the segment file (becoming garbage) but
3760    /// every `Cold` locator for `key` on `index_name` is removed
3761    /// so PK lookups stop returning it.
3762    ///
3763    /// Returns the number of cold locators retired (0 when the key
3764    /// has no cold entries — the DELETE fell on a hot row or a
3765    /// key that was already absent). Errors when the table /
3766    /// index doesn't exist or the index isn't `BTree`.
3767    ///
3768    /// Cold-segment compaction (which merges shadowed-heavy
3769    /// segments and reclaims their disk footprint) lands in a
3770    /// later v5.x sub-version; until then, repeated UPDATE/DELETE
3771    /// of cold rows can amplify cold-segment disk usage by up to
3772    /// 1-2× — still well under typical LSM-tree shadowing because
3773    /// SPG segments are bulk-baked, not write-merged.
3774    pub fn shadow_cold_row(
3775        &mut self,
3776        table_name: &str,
3777        index_name: &str,
3778        key: &IndexKey,
3779    ) -> Result<usize, StorageError> {
3780        let t = self.get_mut(table_name).ok_or_else(|| {
3781            StorageError::Corrupt(format!("shadow_cold_row: table {table_name:?} not found"))
3782        })?;
3783        t.remove_cold_locators_for_key(index_name, key)
3784    }
3785
3786    /// v6.7.4 — read-only slice preparation for the parallel
3787    /// freezer. Walks rows in `row_range`, builds the
3788    /// `(pk_u64, encoded_body, IndexKey)` triples that the
3789    /// coordinator's k-way merge consumes, sorts the slice by
3790    /// `pk_u64`, and returns a [`FreezeSlice`].
3791    ///
3792    /// Caller invariants:
3793    /// - `row_range.end <= table.rows.len()` (caller's job to
3794    ///   compute the partition).
3795    /// - All slices passed to `commit_freeze_slices` must cover a
3796    ///   contiguous half-open range `[0, total_max_rows)` with no
3797    ///   gaps and no overlaps. The coordinator validates this
3798    ///   invariant before committing.
3799    ///
3800    /// `&self`-only — multiple workers can run this concurrently
3801    /// against the same `Catalog` reference under the engine's
3802    /// write lock (workers don't mutate; the coordinator does).
3803    pub fn prepare_freeze_slice(
3804        &self,
3805        table_name: &str,
3806        index_name: &str,
3807        row_range: core::ops::Range<usize>,
3808    ) -> Result<FreezeSlice, StorageError> {
3809        let table = self.get(table_name).ok_or_else(|| {
3810            StorageError::Corrupt(format!(
3811                "prepare_freeze_slice: table {table_name:?} not found"
3812            ))
3813        })?;
3814        let idx = table
3815            .indices
3816            .iter()
3817            .find(|i| i.name == index_name)
3818            .ok_or_else(|| {
3819                StorageError::Corrupt(format!(
3820                    "prepare_freeze_slice: index {index_name:?} not found on {table_name:?}"
3821                ))
3822            })?;
3823        if !matches!(idx.kind, IndexKind::BTree(_)) {
3824            return Err(StorageError::Corrupt(format!(
3825                "prepare_freeze_slice: index {index_name:?} is NSW; only BTree indices may freeze"
3826            )));
3827        }
3828        if row_range.end > table.rows.len() {
3829            return Err(StorageError::Corrupt(format!(
3830                "prepare_freeze_slice: row_range end {} > row_count {}",
3831                row_range.end,
3832                table.rows.len()
3833            )));
3834        }
3835        let column_position = idx.column_position;
3836        let schema = table.schema.clone();
3837        let mut rows: Vec<(u64, Vec<u8>, IndexKey)> = Vec::with_capacity(row_range.len());
3838        for row_idx in row_range.clone() {
3839            let row = table.rows.get(row_idx).expect("bounds-checked above");
3840            let key = IndexKey::from_value(&row.values[column_position]).ok_or_else(|| {
3841                StorageError::Corrupt(format!(
3842                    "prepare_freeze_slice: row {row_idx} has NULL / non-key value in index column"
3843                ))
3844            })?;
3845            let pk_u64 = index_key_as_u64(&key).ok_or_else(|| {
3846                StorageError::Corrupt(format!(
3847                    "prepare_freeze_slice: index {index_name:?} column type is non-integer; \
3848                     v5.2.2 cold tier requires IndexKey::Int (Text PK lands in v5.5+)"
3849                ))
3850            })?;
3851            rows.push((pk_u64, encode_row_body_dense(row, &schema), key));
3852        }
3853        rows.sort_by_key(|(k, _, _)| *k);
3854        Ok(FreezeSlice { row_range, rows })
3855    }
3856
3857    /// v6.7.4 — coordinator commit step. Merges N
3858    /// [`FreezeSlice`]s into one segment via the standard
3859    /// [`encode_segment`] path, atomically swaps the catalog
3860    /// state (delete the union row range + register Cold
3861    /// locators + load the segment).
3862    ///
3863    /// Validates that the slices cover a contiguous, gap-free,
3864    /// overlap-free half-open range starting at index 0 (the
3865    /// freezer always freezes "oldest first" — same semantics as
3866    /// the single-threaded [`Catalog::freeze_oldest_to_cold`]).
3867    ///
3868    /// Empty `slices` → no-op success (returns a zero-row report
3869    /// without mutating). Total row count = `Σ slice.rows.len()`.
3870    pub fn commit_freeze_slices(
3871        &mut self,
3872        table_name: &str,
3873        index_name: &str,
3874        slices: Vec<FreezeSlice>,
3875    ) -> Result<FreezeReport, StorageError> {
3876        // --- validation phase: never mutates ---------------------
3877        let table = self.get(table_name).ok_or_else(|| {
3878            StorageError::Corrupt(format!(
3879                "commit_freeze_slices: table {table_name:?} not found"
3880            ))
3881        })?;
3882        let idx = table
3883            .indices
3884            .iter()
3885            .find(|i| i.name == index_name)
3886            .ok_or_else(|| {
3887                StorageError::Corrupt(format!(
3888                    "commit_freeze_slices: index {index_name:?} not found on {table_name:?}"
3889                ))
3890            })?;
3891        if !matches!(idx.kind, IndexKind::BTree(_)) {
3892            return Err(StorageError::Corrupt(format!(
3893                "commit_freeze_slices: index {index_name:?} is NSW; only BTree indices may freeze"
3894            )));
3895        }
3896        // Validate slice coverage: contiguous from 0, no gaps, no
3897        // overlaps. Allow the caller to pass slices in any order —
3898        // sort by row_range.start first.
3899        let mut ordered = slices;
3900        ordered.sort_by_key(|s| s.row_range.start);
3901        // Drop fully-empty slices that fell out of an uneven
3902        // partition; they carry no data but contribute to the
3903        // contiguity check, so keep them in line.
3904        let mut expected_start = 0usize;
3905        for s in &ordered {
3906            if s.row_range.start != expected_start {
3907                return Err(StorageError::Corrupt(format!(
3908                    "commit_freeze_slices: gap/overlap at row {}; expected start {}",
3909                    s.row_range.start, expected_start
3910                )));
3911            }
3912            expected_start = s.row_range.end;
3913        }
3914        let max_rows = expected_start;
3915        if max_rows > table.rows.len() {
3916            return Err(StorageError::Corrupt(format!(
3917                "commit_freeze_slices: total row range {} exceeds row_count {}",
3918                max_rows,
3919                table.rows.len()
3920            )));
3921        }
3922        if max_rows == 0 {
3923            return Ok(FreezeReport {
3924                segment_id: u32::MAX,
3925                frozen_rows: 0,
3926                bytes_freed: 0,
3927                segment_bytes: Vec::new(),
3928            });
3929        }
3930
3931        // --- segment build phase: reads only --------------------
3932        // K-way merge of already-sorted slices. Each slice's rows
3933        // are ascending by pk_u64; we keep a per-slice cursor and
3934        // pull the next-smallest head until every cursor drains.
3935        let total_rows: usize = ordered.iter().map(|s| s.rows.len()).sum();
3936        if total_rows != max_rows {
3937            return Err(StorageError::Corrupt(format!(
3938                "commit_freeze_slices: total slice rows {total_rows} ≠ row_range coverage {max_rows}"
3939            )));
3940        }
3941        let mut cursors: Vec<usize> = alloc::vec![0; ordered.len()];
3942        let mut merged: Vec<(u64, Vec<u8>, IndexKey)> = Vec::with_capacity(total_rows);
3943        loop {
3944            // Pick the slice whose head row has the smallest key
3945            // and isn't yet exhausted.
3946            let mut pick: Option<usize> = None;
3947            for (i, c) in cursors.iter().enumerate() {
3948                let slice = &ordered[i];
3949                if *c >= slice.rows.len() {
3950                    continue;
3951                }
3952                match pick {
3953                    None => pick = Some(i),
3954                    Some(j) => {
3955                        if slice.rows[*c].0 < ordered[j].rows[cursors[j]].0 {
3956                            pick = Some(i);
3957                        }
3958                    }
3959                }
3960            }
3961            let Some(i) = pick else { break };
3962            let row = ordered[i].rows[cursors[i]].clone();
3963            cursors[i] += 1;
3964            merged.push(row);
3965        }
3966        // Reject duplicate PKs — same error as the single-threaded
3967        // path so callers get a uniform surface.
3968        for w in merged.windows(2) {
3969            if w[0].0 == w[1].0 {
3970                return Err(StorageError::Corrupt(format!(
3971                    "commit_freeze_slices: duplicate PK {} across slices",
3972                    w[0].0
3973                )));
3974            }
3975        }
3976        let post_swap_keys: Vec<IndexKey> = merged.iter().map(|(_, _, k)| k.clone()).collect();
3977        let seg_rows: Vec<(u64, Vec<u8>)> =
3978            merged.into_iter().map(|(k, body, _)| (k, body)).collect();
3979        let frozen_rows = seg_rows.len();
3980        let (seg_bytes, _meta) = encode_segment(seg_rows.into_iter(), 0.01, SEGMENT_PAGE_BYTES)
3981            .map_err(|e| StorageError::Corrupt(format!("commit_freeze_slices: encode: {e}")))?;
3982
3983        // --- atomic swap phase: mutations only past this point ---
3984        let bytes_before = self.get(table_name).expect("just validated").hot_bytes();
3985        let positions: Vec<usize> = (0..max_rows).collect();
3986        let t_mut = self
3987            .get_mut(table_name)
3988            .expect("just validated; still present");
3989        let removed = t_mut.delete_rows(&positions);
3990        debug_assert_eq!(removed, max_rows, "delete_rows count matches request");
3991        let bytes_after = t_mut.hot_bytes();
3992        let bytes_freed = bytes_before.saturating_sub(bytes_after);
3993
3994        let segment_id = self
3995            .load_segment_bytes(seg_bytes.clone())
3996            .map_err(|e| StorageError::Corrupt(format!("commit_freeze_slices: load: {e}")))?;
3997        let new_cold = post_swap_keys.into_iter().map(|k| {
3998            (
3999                k,
4000                RowLocator::Cold {
4001                    segment_id,
4002                    page_offset: 0,
4003                },
4004            )
4005        });
4006        let t_mut = self.get_mut(table_name).expect("still present");
4007        t_mut.register_cold_locators(index_name, new_cold)?;
4008
4009        Ok(FreezeReport {
4010            segment_id,
4011            frozen_rows,
4012            bytes_freed,
4013            segment_bytes: seg_bytes,
4014        })
4015    }
4016
4017    /// v6.7.3 — compact every cold segment on `(table, index)` whose
4018    /// `OwnedSegment::bytes().len()` is below `target_segment_bytes`
4019    /// into a single larger merged segment. Rows present in source
4020    /// segment payloads but no longer referenced by any
4021    /// `RowLocator::Cold` on the index (DELETE'd + frozen rows
4022    /// retired via [`Catalog::shadow_cold_row`]) are GC'd in the
4023    /// merge.
4024    ///
4025    /// **Semantics**:
4026    /// 1. Walk the BTree index to collect every Cold locator that
4027    ///    targets a small (< threshold) segment. Each such
4028    ///    `(key, segment_id)` becomes a row in the merged segment;
4029    ///    payload is looked up from the source segment in-place.
4030    /// 2. Encode the collected rows into one new segment via
4031    ///    [`encode_segment`]; register it via
4032    ///    [`Catalog::load_segment_bytes`] (allocating a fresh
4033    ///    `merged_segment_id` at the end of `cold_segments`).
4034    /// 3. Rewrite the BTree index in one pass: every
4035    ///    `RowLocator::Cold { segment_id ∈ sources }` becomes
4036    ///    `RowLocator::Cold { segment_id = merged_id, page_offset = 0 }`.
4037    ///    Hot locators are untouched.
4038    /// 4. Tombstone every source slot via
4039    ///    [`Catalog::tombstone_segment`]. Source segment payloads
4040    ///    are no longer reachable through the catalog; the on-disk
4041    ///    files are the caller's concern.
4042    ///
4043    /// On fewer than 2 candidate segments the catalog is **not**
4044    /// mutated and a no-op report (`merged_segment_id: None`,
4045    /// `sources: []`) is returned. This is the routine case — a
4046    /// freshly-frozen table has at most 1 small segment, no merge
4047    /// possible.
4048    ///
4049    /// Atomicity: every mutating step runs after the read-only
4050    /// gather phase, so a panic before the merge encode leaves the
4051    /// catalog unchanged. The mutation block itself (load + rewrite +
4052    /// tombstone) takes only `&mut self` — callers serialise the
4053    /// engine write lock outside this function.
4054    ///
4055    /// Errors when the table / index doesn't exist, the index isn't
4056    /// `BTree`, the index column type isn't u64-coercible (cold-tier
4057    /// pre-condition), or a source segment fails its in-place
4058    /// row-body lookup (would indicate prior catalog corruption).
4059    pub fn compact_cold_segments(
4060        &mut self,
4061        table_name: &str,
4062        index_name: &str,
4063        target_segment_bytes: u64,
4064    ) -> Result<CompactReport, StorageError> {
4065        // --- validation phase ----------------------------------
4066        let t = self.get(table_name).ok_or_else(|| {
4067            StorageError::Corrupt(format!(
4068                "compact_cold_segments: table {table_name:?} not found"
4069            ))
4070        })?;
4071        let idx = t
4072            .indices
4073            .iter()
4074            .find(|i| i.name == index_name)
4075            .ok_or_else(|| {
4076                StorageError::Corrupt(format!(
4077                    "compact_cold_segments: index {index_name:?} not found on {table_name:?}"
4078                ))
4079            })?;
4080        let map = match &idx.kind {
4081            IndexKind::BTree(m) => m,
4082            IndexKind::Nsw(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
4083                return Err(StorageError::Corrupt(format!(
4084                    "compact_cold_segments: index {index_name:?} is not BTree; \
4085                     compaction applies only to BTree cold-tier indices"
4086                )));
4087            }
4088        };
4089
4090        // --- gather phase --------------------------------------
4091        // Step A: every segment_id this BTree index Cold-references.
4092        let mut referenced_ids: BTreeSet<u32> = BTreeSet::new();
4093        for (_key, locators) in map.iter() {
4094            for loc in locators {
4095                if let RowLocator::Cold { segment_id, .. } = loc {
4096                    referenced_ids.insert(*segment_id);
4097                }
4098            }
4099        }
4100        // Step B: keep only the small + still-active ones.
4101        let candidate_set: BTreeSet<u32> = referenced_ids
4102            .into_iter()
4103            .filter(|id| {
4104                self.cold_segments
4105                    .get(*id as usize)
4106                    .and_then(|s| s.as_deref())
4107                    .is_some_and(|s| (s.bytes().len() as u64) < target_segment_bytes)
4108            })
4109            .collect();
4110        if candidate_set.len() < 2 {
4111            return Ok(CompactReport {
4112                sources: Vec::new(),
4113                merged_segment_id: None,
4114                merged_segment_bytes: Vec::new(),
4115                merged_rows: 0,
4116                deleted_rows_pruned: 0,
4117                bytes_reclaimed_estimate: 0,
4118            });
4119        }
4120        // Step C: pre-count source rows for the deleted-pruned metric.
4121        let mut source_row_count: usize = 0;
4122        let mut source_byte_total: u64 = 0;
4123        for &id in &candidate_set {
4124            let seg = self.cold_segments[id as usize]
4125                .as_ref()
4126                .expect("candidate selected only when slot is Some");
4127            source_row_count = source_row_count.saturating_add(seg.meta().num_rows as usize);
4128            source_byte_total = source_byte_total.saturating_add(seg.bytes().len() as u64);
4129        }
4130        // Step D: collect (key, body) pairs from every live Cold
4131        // locator pointing at a candidate. dedupe by key — one
4132        // BTree key resolves to at most one cold payload (the
4133        // freezer + promote/shadow flow keeps Cold locators
4134        // unique per key).
4135        let mut collected: BTreeMap<u64, (Vec<u8>, IndexKey)> = BTreeMap::new();
4136        for (key, locators) in map.iter() {
4137            for loc in locators {
4138                let RowLocator::Cold { segment_id, .. } = loc else {
4139                    continue;
4140                };
4141                if !candidate_set.contains(segment_id) {
4142                    continue;
4143                }
4144                let u64_key = index_key_as_u64(key).ok_or_else(|| {
4145                    StorageError::Corrupt(format!(
4146                        "compact_cold_segments: index {index_name:?} has non-integer Cold key; \
4147                         cold tier requires IndexKey::Int (Text PK lands in v5.5+)"
4148                    ))
4149                })?;
4150                let seg = self.cold_segments[*segment_id as usize]
4151                    .as_ref()
4152                    .expect("candidate slot guaranteed Some above");
4153                let payload = seg.lookup(u64_key).ok_or_else(|| {
4154                    StorageError::Corrupt(format!(
4155                        "compact_cold_segments: BTree {index_name:?} points key={u64_key} \
4156                         at segment {segment_id} but the segment lookup missed"
4157                    ))
4158                })?;
4159                collected.insert(u64_key, (payload, key.clone()));
4160                break;
4161            }
4162        }
4163        let merged_rows = collected.len();
4164        let deleted_rows_pruned = source_row_count.saturating_sub(merged_rows);
4165
4166        // Step E: encode the merged segment. `BTreeMap<u64, _>`
4167        // iteration is ascending by key, which is what
4168        // `encode_segment` requires.
4169        let seg_rows: Vec<(u64, Vec<u8>)> = collected
4170            .iter()
4171            .map(|(k, (body, _))| (*k, body.clone()))
4172            .collect();
4173        let (seg_bytes, _meta) = encode_segment(seg_rows.into_iter(), 0.01, SEGMENT_PAGE_BYTES)
4174            .map_err(|e| StorageError::Corrupt(format!("compact_cold_segments: encode: {e}")))?;
4175        let merged_bytes_len = seg_bytes.len() as u64;
4176
4177        // --- atomic mutation phase ------------------------------
4178        let merged_segment_id = self
4179            .load_segment_bytes(seg_bytes.clone())
4180            .map_err(|e| StorageError::Corrupt(format!("compact_cold_segments: load: {e}")))?;
4181
4182        // Rewrite the BTree index: every Cold locator pointing at
4183        // a candidate source becomes a Cold locator pointing at
4184        // the merged segment. Use a flat collect-then-replace
4185        // pattern so we never hold a `&self` borrow across the
4186        // `&mut self` write.
4187        let entries: Vec<(IndexKey, Vec<RowLocator>)> = {
4188            let t = self
4189                .get(table_name)
4190                .expect("table existed at the start of this fn");
4191            let idx = t
4192                .indices
4193                .iter()
4194                .find(|i| i.name == index_name)
4195                .expect("index existed at the start of this fn");
4196            let IndexKind::BTree(map) = &idx.kind else {
4197                unreachable!("validated above");
4198            };
4199            map.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
4200        };
4201        let t_mut = self
4202            .get_mut(table_name)
4203            .expect("table existed at the start of this fn");
4204        let idx_mut = t_mut
4205            .indices
4206            .iter_mut()
4207            .find(|i| i.name == index_name)
4208            .expect("index existed at the start of this fn");
4209        let IndexKind::BTree(map_mut) = &mut idx_mut.kind else {
4210            unreachable!("validated above");
4211        };
4212        for (key, locators) in entries {
4213            let mut new_locs: Vec<RowLocator> = Vec::with_capacity(locators.len());
4214            let mut changed = false;
4215            for loc in &locators {
4216                match *loc {
4217                    RowLocator::Cold {
4218                        segment_id,
4219                        page_offset: _,
4220                    } if candidate_set.contains(&segment_id) => {
4221                        let replacement = RowLocator::Cold {
4222                            segment_id: merged_segment_id,
4223                            page_offset: 0,
4224                        };
4225                        if !new_locs.contains(&replacement) {
4226                            new_locs.push(replacement);
4227                        }
4228                        changed = true;
4229                    }
4230                    other => new_locs.push(other),
4231                }
4232            }
4233            if changed {
4234                map_mut.insert_mut(key, new_locs);
4235            }
4236        }
4237
4238        // Tombstone every source slot. Last step — failures here
4239        // would leave the segment double-referenced in both
4240        // memory + manifest, but `tombstone_segment` only errors
4241        // on out-of-bounds, which we've already validated.
4242        for &id in &candidate_set {
4243            self.tombstone_segment(id)?;
4244        }
4245
4246        let bytes_reclaimed_estimate = source_byte_total.saturating_sub(merged_bytes_len);
4247        Ok(CompactReport {
4248            sources: candidate_set.into_iter().collect(),
4249            merged_segment_id: Some(merged_segment_id),
4250            merged_segment_bytes: seg_bytes,
4251            merged_rows,
4252            deleted_rows_pruned,
4253            bytes_reclaimed_estimate,
4254        })
4255    }
4256
4257    /// Internal helper: scan `(table, index)` for a `Cold` locator
4258    /// keyed by `key`. Returns `Ok(Some((segment_id, page_offset)))`
4259    /// when found, `Ok(None)` when the key has only hot entries
4260    /// or no entries at all, `Err` on the same input-validation
4261    /// errors as the public `promote_cold_row` / `shadow_cold_row`.
4262    fn find_cold_locator(
4263        &self,
4264        table_name: &str,
4265        index_name: &str,
4266        key: &IndexKey,
4267    ) -> Result<Option<(u32, u32)>, StorageError> {
4268        let t = self.get(table_name).ok_or_else(|| {
4269            StorageError::Corrupt(format!("find_cold_locator: table {table_name:?} not found"))
4270        })?;
4271        let idx = t
4272            .indices
4273            .iter()
4274            .find(|i| i.name == index_name)
4275            .ok_or_else(|| {
4276                StorageError::Corrupt(format!(
4277                    "find_cold_locator: index {index_name:?} not found on {table_name:?}"
4278                ))
4279            })?;
4280        if !matches!(idx.kind, IndexKind::BTree(_)) {
4281            return Err(StorageError::Corrupt(format!(
4282                "find_cold_locator: index {index_name:?} is NSW; promote-on-write only applies to BTree indices"
4283            )));
4284        }
4285        for loc in idx.lookup_eq(key) {
4286            if let RowLocator::Cold {
4287                segment_id,
4288                page_offset,
4289            } = *loc
4290            {
4291                return Ok(Some((segment_id, page_offset)));
4292            }
4293        }
4294        Ok(None)
4295    }
4296}
4297
4298/// Coerce an [`IndexKey`] to the `u64` that v5.1 cold-tier
4299/// segments use as their on-disk PK. Returns `None` for keys that
4300/// aren't representable as `u64` — Text PKs need a hash mapping
4301/// the segment writer baked in (deferred to v5.2+), Bool PKs are
4302/// almost never wide enough to be sharded into a cold tier.
4303fn index_key_as_u64(key: &IndexKey) -> Option<u64> {
4304    match key {
4305        // Reinterpret the i64 bit pattern as u64. Cold-tier segments
4306        // are sorted by this u64 view, so the chosen interpretation
4307        // only has to match between insert (bake_segment / freezer)
4308        // and lookup — using cast_unsigned keeps both sides honest
4309        // and silences clippy::cast_sign_loss.
4310        IndexKey::Int(n) => Some(n.cast_unsigned()),
4311        IndexKey::Text(_) | IndexKey::Bool(_) => None,
4312    }
4313}
4314
4315#[derive(Debug, Clone, PartialEq, Eq)]
4316#[non_exhaustive]
4317pub enum StorageError {
4318    DuplicateTable {
4319        name: String,
4320    },
4321    TableNotFound {
4322        name: String,
4323    },
4324    ArityMismatch {
4325        expected: usize,
4326        actual: usize,
4327    },
4328    TypeMismatch {
4329        column: String,
4330        expected: DataType,
4331        actual: DataType,
4332        position: usize,
4333    },
4334    NullInNotNull {
4335        column: String,
4336    },
4337    /// Index with this name already exists on the table.
4338    DuplicateIndex {
4339        name: String,
4340    },
4341    /// Column referenced by an index doesn't exist on the table.
4342    ColumnNotFound {
4343        column: String,
4344    },
4345    /// On-disk format failed to parse — corrupted file, wrong magic, truncated
4346    /// payload, or unknown tag bytes.
4347    Corrupt(String),
4348    /// v6.0.4 — ALTER INDEX targeted an index name that doesn't
4349    /// exist on any table in this catalog.
4350    IndexNotFound {
4351        name: String,
4352    },
4353    /// v6.0.4 — operation requested isn't supported on this index
4354    /// kind / column type (e.g. ALTER INDEX REBUILD on a `BTree`
4355    /// index, or REBUILD WITH (encoding=…) on a non-vector column).
4356    Unsupported(String),
4357}
4358
4359impl fmt::Display for StorageError {
4360    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4361        match self {
4362            Self::DuplicateTable { name } => write!(f, "table already exists: {name}"),
4363            Self::TableNotFound { name } => write!(f, "table not found: {name}"),
4364            Self::ArityMismatch { expected, actual } => write!(
4365                f,
4366                "row arity mismatch: expected {expected} columns, got {actual}"
4367            ),
4368            Self::TypeMismatch {
4369                column,
4370                expected,
4371                actual,
4372                position,
4373            } => write!(
4374                f,
4375                "type mismatch in column {column:?} (position {position}): expected {expected}, got {actual}"
4376            ),
4377            Self::NullInNotNull { column } => {
4378                write!(f, "NULL value in NOT NULL column {column:?}")
4379            }
4380            Self::DuplicateIndex { name } => write!(f, "index already exists: {name}"),
4381            Self::ColumnNotFound { column } => write!(f, "column not found: {column}"),
4382            Self::Corrupt(detail) => write!(f, "corrupt on-disk format: {detail}"),
4383            Self::IndexNotFound { name } => write!(f, "index not found: {name}"),
4384            Self::Unsupported(detail) => write!(f, "unsupported: {detail}"),
4385        }
4386    }
4387}
4388
4389impl ColumnSchema {
4390    pub fn new(name: impl Into<String>, ty: DataType, nullable: bool) -> Self {
4391        Self {
4392            name: name.into(),
4393            ty,
4394            nullable,
4395            default: None,
4396            runtime_default: None,
4397            auto_increment: false,
4398        }
4399    }
4400
4401    /// Builder-style helper to attach a default value to an otherwise
4402    /// plain column schema. Used by the engine when CREATE TABLE
4403    /// specifies `column TYPE DEFAULT <expr>`.
4404    #[must_use]
4405    pub fn with_default(mut self, default: Value) -> Self {
4406        self.default = Some(default);
4407        self
4408    }
4409
4410    /// v7.9.21 — builder for runtime-evaluated defaults
4411    /// (`DEFAULT now()`, `DEFAULT CURRENT_TIMESTAMP`, …).
4412    /// `expr` is the Expr's `Display` form, re-parsed by the
4413    /// engine at each INSERT.
4414    #[must_use]
4415    pub fn with_runtime_default(mut self, expr: impl Into<String>) -> Self {
4416        self.runtime_default = Some(expr.into());
4417        self
4418    }
4419
4420    /// Builder-style helper to mark a column as `AUTO_INCREMENT`.
4421    #[must_use]
4422    pub const fn with_auto_increment(mut self) -> Self {
4423        self.auto_increment = true;
4424        self
4425    }
4426}
4427
4428impl TableSchema {
4429    pub fn new(name: impl Into<String>, columns: Vec<ColumnSchema>) -> Self {
4430        Self {
4431            name: name.into(),
4432            columns,
4433            hot_tier_bytes: None,
4434            foreign_keys: Vec::new(),
4435            uniqueness_constraints: Vec::new(),
4436            checks: Vec::new(),
4437        }
4438    }
4439}
4440
4441// =========================================================================
4442// Persistent binary format for the catalog.
4443//
4444// Layout (little-endian throughout):
4445//
4446//   [magic "SPGDB001" 8 bytes][version u8]
4447//   [table_count u32]
4448//   for each table:
4449//       [name_len u16][name bytes]
4450//       [col_count u16]
4451//       for each col:
4452//           [name_len u16][name bytes]
4453//           [type_tag u8 + optional payload]
4454//               1=Int 2=BigInt 3=Float 4=Text 5=Bool
4455//               6=Vector(u32 dim)
4456//               7=SmallInt
4457//               8=Varchar(u32 max)
4458//               9=Char(u32 size)
4459//               10=Numeric(u8 precision, u8 scale)
4460//               11=Date
4461//               12=Timestamp
4462//           [nullable u8]   0/1
4463//           [default_tag u8] 0=none 1=value (followed by [value_tag u8] + bytes)
4464//       [row_count u32]
4465//       for each row, for each col, one [value_tag u8] + value bytes:
4466//           tag 0 (Null)     → no body
4467//           tag 1 (Int)      → i32 LE
4468//           tag 2 (BigInt)   → i64 LE
4469//           tag 3 (Float)    → f64 LE
4470//           tag 4 (Text)     → u16 LE len + UTF-8 bytes
4471//           tag 5 (Bool)     → u8 0/1
4472//           tag 6 (Vector)   → u32 LE dim + dim×f32 LE
4473//           tag 7 (SmallInt) → i16 LE
4474//           tag 8 (Numeric)  → i128 LE (16 bytes) + u8 scale
4475//           tag 9 (Date)     → i32 LE (days since Unix epoch)
4476//           tag 10 (Timestamp) → i64 LE (microseconds since Unix epoch)
4477//
4478// Bumped to version 3 when NUMERIC was added; to version 4 when
4479// AUTO_INCREMENT (per-column flag) + NSW index `kind` byte landed;
4480// to version 5 when DATE / TIMESTAMP were added; to version 6 when
4481// NSW graph topology started travelling on disk (v2.7); to version 7
4482// when the NSW topology became multi-layer HNSW (v2.13); to version 8
4483// when row encoding switched to schema-driven dense layout (v3.0.2 —
4484// per-row NULL bitmap + per-column fixed-width body, no per-cell type
4485// tag).
4486// =========================================================================
4487
4488const FILE_MAGIC: &[u8; 8] = b"SPGDB001";
4489/// Current catalog snapshot format version emitted by [`Catalog::serialize`].
4490///
4491/// v9 (v5.2) extends v8 by serialising `BTree` index entries directly — every
4492/// `(IndexKey, Vec<RowLocator>)` pair travels on disk with the v5.1
4493/// `RowLocator::write_le` tag-prefixed codec. v8 `BTree` indices stored no
4494/// entries at all (the map was rebuilt from `Table::rows` on load); v9
4495/// preserves on-disk Cold locators so freezer-produced cold-tier index
4496/// entries survive a catalog snapshot round-trip. v8 readers are accepted
4497/// by version dispatch in [`Catalog::deserialize`] — every entry decodes
4498/// as `RowLocator::Hot(_)` via `add_index` rebuild, identical to v5.1
4499/// behaviour.
4500/// v6.7.2 — bumped from 10 to 11 to append per-table
4501/// `hot_tier_bytes: Option<u64>` after the per-table indices
4502/// section. v10 catalogs (v6.7.1) load with `hot_tier_bytes =
4503/// None` for every table (the deserialiser short-circuits when
4504/// version < 11). v11 snapshots written by a pre-v6.7.2 binary
4505/// fail loudly at the version check, matching the v6.1.2 /
4506/// v6.1.4 / v6.2.0 / v6.7.1 envelope-bump upgrade fences.
4507///
4508/// v6.8.0 — bumped from 11 to 12: per-index
4509/// `included_columns: Vec<u16>` appended at the tail of each
4510/// index payload. v11 (= v6.7.2) catalogs load with
4511/// `included_columns = Vec::new()` for every index — same
4512/// "older readers, append-only extension" pattern as the v6.7.2
4513/// hot_tier_bytes byte.
4514/// v7.13.0 — bumped from 22 to 23. mailrs round-5 G3 / G10.
4515/// Per-table appendix gains two new sections:
4516///   * `checks: Vec<String>` — CHECK predicate sources (Display
4517///     form of the AST Expr); re-parsed on INSERT/UPDATE to
4518///     enforce against candidate rows. Same persistence pattern
4519///     as `Index::partial_predicate`.
4520///   * Per `UniquenessConstraint`: trailing `nulls_not_distinct:
4521///     u8` flag for PG 15+ `UNIQUE NULLS NOT DISTINCT (cols)`
4522///     semantics.
4523/// v22 catalogs deserialise with empty `checks` and every UC
4524/// at `nulls_not_distinct = false`.
4525const FILE_VERSION: u8 = 23;
4526/// Oldest format version [`Catalog::deserialize`] still accepts. v8 is the
4527/// v3.0.2 dense-row layout; pre-v8 catalogs require an offline migration.
4528const MIN_SUPPORTED_FILE_VERSION: u8 = 8;
4529
4530// IndexKey wire format (v9):
4531//   tag 0 = Int  → [i64 LE]
4532//   tag 1 = Text → [u16 LE len + UTF-8 bytes] (via write_str / read_str)
4533//   tag 2 = Bool → [u8 0/1]
4534const INDEX_KEY_TAG_INT: u8 = 0;
4535const INDEX_KEY_TAG_TEXT: u8 = 1;
4536const INDEX_KEY_TAG_BOOL: u8 = 2;
4537
4538impl Catalog {
4539    /// Serialize the whole catalog (schema + every row) into a self-contained
4540    /// byte buffer. Format is documented above the impl block.
4541    pub fn serialize(&self) -> Vec<u8> {
4542        let mut out = Vec::with_capacity(64);
4543        out.extend_from_slice(FILE_MAGIC);
4544        out.push(FILE_VERSION);
4545        write_u32(
4546            &mut out,
4547            u32::try_from(self.tables.len()).expect("≤ 4G tables"),
4548        );
4549        for t in &self.tables {
4550            write_str(&mut out, &t.schema.name);
4551            write_u16(
4552                &mut out,
4553                u16::try_from(t.schema.columns.len()).expect("≤ 65k columns/table"),
4554            );
4555            for c in &t.schema.columns {
4556                write_str(&mut out, &c.name);
4557                write_data_type(&mut out, c.ty);
4558                out.push(u8::from(c.nullable));
4559                match &c.default {
4560                    None => out.push(0),
4561                    Some(v) => {
4562                        out.push(1);
4563                        write_value(&mut out, v);
4564                    }
4565                }
4566                out.push(u8::from(c.auto_increment));
4567            }
4568            write_u32(
4569                &mut out,
4570                u32::try_from(t.rows.len()).expect("≤ 4G rows/table"),
4571            );
4572            // v3.0.2 dense row encoding (FILE_VERSION 8): per-row NULL
4573            // bitmap, then tightly-packed bodies. Identical wire format
4574            // as before — extracted into `encode_row_body_dense` so cold-
4575            // tier segments (v5.1+) can share the encoding.
4576            for row in &t.rows {
4577                out.extend_from_slice(&encode_row_body_dense(row, &t.schema));
4578            }
4579            // Index definitions. Per-index payload:
4580            //   [name][col_pos u16][kind u8]
4581            //     kind 0 = B-tree           (no params — rebuilt on load)
4582            //     kind 1 = NSW graph        (u16 M + serialized graph)
4583            // For NSW the graph topology travels on disk so startup
4584            // doesn't re-run the O(n²M) rebuild — see v2.7 notes.
4585            write_u16(
4586                &mut out,
4587                u16::try_from(t.indices.len()).expect("≤ 65k indices/table"),
4588            );
4589            for idx in &t.indices {
4590                write_str(&mut out, &idx.name);
4591                write_u16(
4592                    &mut out,
4593                    u16::try_from(idx.column_position).expect("≤ 65k columns/table"),
4594                );
4595                match &idx.kind {
4596                    IndexKind::BTree(map) => {
4597                        out.push(0);
4598                        // v9: serialise the full PB map. Each entry's
4599                        // RowLocator list travels with the tag-prefixed
4600                        // codec from `row_locator::write_le`, so freezer-
4601                        // produced Cold locators survive a snapshot
4602                        // round-trip. v8 BTree wrote nothing here and
4603                        // rebuilt from rows — v9 readers tolerate v8 by
4604                        // version dispatch in `Catalog::deserialize`.
4605                        write_u32(
4606                            &mut out,
4607                            u32::try_from(map.len()).expect("≤ 4G index entries/index"),
4608                        );
4609                        for (key, locators) in map {
4610                            write_index_key(&mut out, key);
4611                            write_u32(
4612                                &mut out,
4613                                u32::try_from(locators.len()).expect("≤ 4G locators/key"),
4614                            );
4615                            for loc in locators {
4616                                loc.write_le(&mut out);
4617                            }
4618                        }
4619                    }
4620                    IndexKind::Nsw(g) => {
4621                        out.push(1);
4622                        write_u16(&mut out, u16::try_from(g.m).expect("≤ 65k NSW neighbours"));
4623                        write_nsw_graph(&mut out, g);
4624                    }
4625                    IndexKind::Brin { column_type } => {
4626                        // v6.7.1 — tag byte 2 = BRIN. Payload is the
4627                        // column type code (1 byte mapping to the
4628                        // shared DataType numeric encoding); no
4629                        // further data — BRIN summaries live in
4630                        // cold segments, not the catalog.
4631                        out.push(2);
4632                        write_data_type(&mut out, *column_type);
4633                    }
4634                    IndexKind::Gin(map) => {
4635                        // v7.12.3 — tag byte 3 = GIN. Payload mirrors
4636                        // the BTree encoding but with String (lexeme
4637                        // word) keys instead of IndexKey. Tag-prefixed
4638                        // RowLocator codec so freezer-produced Cold
4639                        // locators survive snapshot round-trip.
4640                        // FILE_VERSION 21+; v20 catalogs never wrote a
4641                        // GIN index (the AM degraded to BTree fallback
4642                        // pre-v7.12.3), so no migration shim is needed.
4643                        out.push(3);
4644                        write_u32(
4645                            &mut out,
4646                            u32::try_from(map.len()).expect("≤ 4G GIN posting lists"),
4647                        );
4648                        for (word, locators) in map {
4649                            write_str(&mut out, word);
4650                            write_u32(
4651                                &mut out,
4652                                u32::try_from(locators.len()).expect("≤ 4G locators/posting list"),
4653                            );
4654                            for loc in locators {
4655                                loc.write_le(&mut out);
4656                            }
4657                        }
4658                    }
4659                }
4660                // v6.8.0 — included_columns appendix per index.
4661                // Layout: [u16 num_included][num × u16 column_position].
4662                // v11 readers stop before this u16 (deserialise loop
4663                // gated on version >= 12); v12+ readers always
4664                // consume it. Empty Vec serialises as a bare 0u16.
4665                write_u16(
4666                    &mut out,
4667                    u16::try_from(idx.included_columns.len()).expect("≤ 65k INCLUDE columns/index"),
4668                );
4669                for col_pos in &idx.included_columns {
4670                    write_u16(
4671                        &mut out,
4672                        u16::try_from(*col_pos).expect("≤ 65k columns/table"),
4673                    );
4674                }
4675                // v6.8.1 — partial_predicate appendix per index.
4676                // Layout: [u8 has_pred][u16 LE len][bytes (if has_pred)].
4677                // Same v12 gate as included_columns.
4678                match &idx.partial_predicate {
4679                    None => out.push(0),
4680                    Some(pred) => {
4681                        out.push(1);
4682                        write_str(&mut out, pred);
4683                    }
4684                }
4685                // v6.8.2 — expression appendix. Same shape as
4686                // partial_predicate.
4687                match &idx.expression {
4688                    None => out.push(0),
4689                    Some(expr) => {
4690                        out.push(1);
4691                        write_str(&mut out, expr);
4692                    }
4693                }
4694                // v7.9.29 — is_unique appendix (FILE_VERSION 16+).
4695                // Single byte 0/1. v15-and-below readers stop before
4696                // this byte; v16 readers always consume it. mailrs K1.
4697                out.push(u8::from(idx.is_unique));
4698                // v7.9.29 — extra_column_positions appendix.
4699                // Layout: [u16 count][count × u16 column_position].
4700                write_u16(
4701                    &mut out,
4702                    u16::try_from(idx.extra_column_positions.len())
4703                        .expect("≤ 65k extra cols / index"),
4704                );
4705                for cp in &idx.extra_column_positions {
4706                    write_u16(&mut out, u16::try_from(*cp).expect("≤ 65k columns/table"));
4707                }
4708            }
4709            // v6.7.2 — per-table hot_tier_bytes Option<u64>.
4710            // Layout: [u8 has_value][u64 LE value (if has_value)].
4711            // v10 readers stop before this byte (deserialise loop
4712            // gated on version >= 11); v11+ readers always
4713            // consume it.
4714            match t.schema.hot_tier_bytes {
4715                None => out.push(0),
4716                Some(n) => {
4717                    out.push(1);
4718                    out.extend_from_slice(&n.to_le_bytes());
4719                }
4720            }
4721            // v7.6.1 — FOREIGN KEY appendix (catalog FILE_VERSION 13+).
4722            // Layout: [u16 LE fk_count]
4723            //   per fk:
4724            //     [u8 has_name] [str name (if has_name)]
4725            //     [u16 LE local_arity] [u16 LE local_pos]*arity
4726            //     [str parent_table]
4727            //     [u16 LE parent_arity] [u16 LE parent_pos]*arity
4728            //     [u8 on_delete_tag] [u8 on_update_tag]
4729            // Older catalogs (v12 and below) skip this block entirely;
4730            // their reader stops before this byte.
4731            write_u16(
4732                &mut out,
4733                u16::try_from(t.schema.foreign_keys.len()).expect("≤ 65k FKs/table"),
4734            );
4735            for fk in &t.schema.foreign_keys {
4736                match &fk.name {
4737                    None => out.push(0),
4738                    Some(n) => {
4739                        out.push(1);
4740                        write_str(&mut out, n);
4741                    }
4742                }
4743                write_u16(
4744                    &mut out,
4745                    u16::try_from(fk.local_columns.len()).expect("≤ 65k FK columns"),
4746                );
4747                for &p in &fk.local_columns {
4748                    write_u16(&mut out, u16::try_from(p).expect("≤ 65k columns/table"));
4749                }
4750                write_str(&mut out, &fk.parent_table);
4751                write_u16(
4752                    &mut out,
4753                    u16::try_from(fk.parent_columns.len()).expect("≤ 65k FK parent columns"),
4754                );
4755                for &p in &fk.parent_columns {
4756                    write_u16(&mut out, u16::try_from(p).expect("≤ 65k columns/table"));
4757                }
4758                out.push(fk.on_delete.tag());
4759                out.push(fk.on_update.tag());
4760            }
4761            // v7.9.19 — UniquenessConstraint appendix (catalog
4762            // FILE_VERSION 15+). Layout per table after the FK
4763            // block:
4764            //   [u16 count]
4765            //     per constraint:
4766            //       [u8 is_primary_key]
4767            //       [u16 arity][u16 col_pos]*arity
4768            // Older catalogs (v14 and below) skip this block.
4769            write_u16(
4770                &mut out,
4771                u16::try_from(t.schema.uniqueness_constraints.len())
4772                    .expect("≤ 65k uniqueness constraints/table"),
4773            );
4774            for uc in &t.schema.uniqueness_constraints {
4775                out.push(u8::from(uc.is_primary_key));
4776                write_u16(
4777                    &mut out,
4778                    u16::try_from(uc.columns.len()).expect("≤ 65k cols in uniqueness constraint"),
4779                );
4780                for &p in &uc.columns {
4781                    write_u16(&mut out, u16::try_from(p).expect("≤ 65k columns/table"));
4782                }
4783                // v7.13.0 — `nulls_not_distinct` flag
4784                // (FILE_VERSION 23+). Always written by writers at
4785                // version 23+; deserialise gates on `version >= 23`
4786                // so v22-and-below catalogs round-trip cleanly.
4787                out.push(u8::from(uc.nulls_not_distinct));
4788            }
4789            // v7.9.21 — runtime_default appendix per table.
4790            // Layout: [u16 count] then for each:
4791            //   [u16 col_pos][str expr]
4792            // Only columns whose runtime_default is Some land here;
4793            // catalog stays compact for the common literal-default
4794            // case.
4795            let mut rt_defaults: Vec<(usize, &str)> = Vec::new();
4796            for (i, c) in t.schema.columns.iter().enumerate() {
4797                if let Some(e) = &c.runtime_default {
4798                    rt_defaults.push((i, e.as_str()));
4799                }
4800            }
4801            write_u16(
4802                &mut out,
4803                u16::try_from(rt_defaults.len()).expect("≤ 65k runtime defaults/table"),
4804            );
4805            for (pos, expr) in rt_defaults {
4806                write_u16(&mut out, u16::try_from(pos).expect("≤ 65k columns/table"));
4807                write_str(&mut out, expr);
4808            }
4809            // v7.13.0 — CHECK constraint appendix per table.
4810            // Layout: [u16 count] then `count` Display-form
4811            // expression strings. Re-parsed on every INSERT/UPDATE
4812            // by the engine. FILE_VERSION 23+ only; v22 readers
4813            // never reach this block because the writer also moves
4814            // to v23 in lock-step.
4815            write_u16(
4816                &mut out,
4817                u16::try_from(t.schema.checks.len()).expect("≤ 65k CHECK constraints/table"),
4818            );
4819            for c in &t.schema.checks {
4820                write_str(&mut out, c.as_str());
4821            }
4822        }
4823        // v7.12.4 — catalog-wide appendix: user-defined functions
4824        // then triggers. FILE_VERSION 22+ only. v21 and earlier
4825        // readers stop after the last table; v22 readers always
4826        // consume two `u32` counts (possibly zero).
4827        //
4828        // Function entry layout:
4829        //   [str name] [str args_repr] [str returns]
4830        //   [str language] [str body]
4831        // Trigger entry layout:
4832        //   [str name] [str table] [str timing]
4833        //   [u16 event_count] (event_count × str)
4834        //   [str for_each] [str function]
4835        write_u32(
4836            &mut out,
4837            u32::try_from(self.functions.len()).expect("≤ 4G functions"),
4838        );
4839        for fd in self.functions.values() {
4840            write_str(&mut out, &fd.name);
4841            write_str(&mut out, &fd.args_repr);
4842            write_str(&mut out, &fd.returns);
4843            write_str(&mut out, &fd.language);
4844            write_str_long(&mut out, &fd.body);
4845        }
4846        write_u32(
4847            &mut out,
4848            u32::try_from(self.triggers.len()).expect("≤ 4G triggers"),
4849        );
4850        for td in &self.triggers {
4851            write_str(&mut out, &td.name);
4852            write_str(&mut out, &td.table);
4853            write_str(&mut out, &td.timing);
4854            write_u16(
4855                &mut out,
4856                u16::try_from(td.events.len()).expect("≤ 65k events / trigger"),
4857            );
4858            for ev in &td.events {
4859                write_str(&mut out, ev);
4860            }
4861            write_str(&mut out, &td.for_each);
4862            write_str(&mut out, &td.function);
4863            // v7.13.0 — `UPDATE OF cols` filter
4864            // (FILE_VERSION 23+). v22 readers omit; v23 writers
4865            // always emit (possibly zero).
4866            write_u16(
4867                &mut out,
4868                u16::try_from(td.update_columns.len()).expect("≤ 65k cols / trigger"),
4869            );
4870            for c in &td.update_columns {
4871                write_str(&mut out, c);
4872            }
4873        }
4874        out
4875    }
4876
4877    /// Deserialize a previously-serialized catalog. Rejects bad magic, version
4878    /// mismatch, unknown tags, truncation, and trailing bytes.
4879    pub fn deserialize(buf: &[u8]) -> Result<Self, StorageError> {
4880        let mut cur = Cursor::new(buf);
4881        let magic = cur.take(8)?;
4882        if magic != FILE_MAGIC {
4883            return Err(StorageError::Corrupt(format!(
4884                "bad magic: expected SPGDB001, got {magic:?}"
4885            )));
4886        }
4887        let version = cur.read_u8()?;
4888        if !(MIN_SUPPORTED_FILE_VERSION..=FILE_VERSION).contains(&version) {
4889            return Err(StorageError::Corrupt(format!(
4890                "unsupported file version: {version} (supported: {MIN_SUPPORTED_FILE_VERSION}..={FILE_VERSION})"
4891            )));
4892        }
4893        let table_count = cur.read_u32()? as usize;
4894        let mut cat = Self::new();
4895        for _ in 0..table_count {
4896            deserialize_table(&mut cur, &mut cat, version)?;
4897        }
4898        // v7.12.4 — catalog-wide function + trigger appendix.
4899        // FILE_VERSION 22+ only; v21 and earlier catalogs stop
4900        // after the last table.
4901        if version >= 22 {
4902            let fn_count = cur.read_u32()? as usize;
4903            for _ in 0..fn_count {
4904                let name = cur.read_str()?;
4905                let args_repr = cur.read_str()?;
4906                let returns = cur.read_str()?;
4907                let language = cur.read_str()?;
4908                let body = cur.read_str_long()?;
4909                cat.functions.insert(
4910                    name.clone(),
4911                    FunctionDef {
4912                        name,
4913                        args_repr,
4914                        returns,
4915                        language,
4916                        body,
4917                    },
4918                );
4919            }
4920            let trg_count = cur.read_u32()? as usize;
4921            for _ in 0..trg_count {
4922                let name = cur.read_str()?;
4923                let table = cur.read_str()?;
4924                let timing = cur.read_str()?;
4925                let ev_count = cur.read_u16()? as usize;
4926                let mut events = Vec::with_capacity(ev_count);
4927                for _ in 0..ev_count {
4928                    events.push(cur.read_str()?);
4929                }
4930                let for_each = cur.read_str()?;
4931                let function = cur.read_str()?;
4932                // v7.13.0 — trailing `UPDATE OF cols` filter
4933                // (FILE_VERSION 23+ only; v22 catalogs omit and
4934                // deserialise with an empty vec).
4935                let update_columns = if version >= 23 {
4936                    let n = cur.read_u16()? as usize;
4937                    let mut cols = Vec::with_capacity(n);
4938                    for _ in 0..n {
4939                        cols.push(cur.read_str()?);
4940                    }
4941                    cols
4942                } else {
4943                    Vec::new()
4944                };
4945                cat.triggers.push(TriggerDef {
4946                    name,
4947                    table,
4948                    timing,
4949                    events,
4950                    for_each,
4951                    function,
4952                    update_columns,
4953                });
4954            }
4955        }
4956        if cur.pos < buf.len() {
4957            return Err(StorageError::Corrupt(format!(
4958                "trailing bytes: {} unread",
4959                buf.len() - cur.pos
4960            )));
4961        }
4962        Ok(cat)
4963    }
4964}
4965
4966/// Per-table deserialize body — schema, rows, indices. Pulled out of
4967/// `Catalog::deserialize` to keep the latter under the line-budget lint
4968/// and to give the row hot loop its own scope (so the borrow on `t`
4969/// stays scoped here rather than across the whole catalog loop).
4970fn deserialize_table(
4971    cur: &mut Cursor<'_>,
4972    cat: &mut Catalog,
4973    version: u8,
4974) -> Result<(), StorageError> {
4975    let table_name = cur.read_str()?;
4976    let name = table_name.clone();
4977    let col_count = cur.read_u16()? as usize;
4978    let mut cols = Vec::with_capacity(col_count);
4979    for _ in 0..col_count {
4980        let c_name = cur.read_str()?;
4981        let ty = cur.read_data_type()?;
4982        let nullable = cur.read_u8()? != 0;
4983        let default = match cur.read_u8()? {
4984            0 => None,
4985            1 => Some(cur.read_value()?),
4986            other => {
4987                return Err(StorageError::Corrupt(format!(
4988                    "unknown default tag: {other}"
4989                )));
4990            }
4991        };
4992        let auto_increment = cur.read_u8()? != 0;
4993        // Note: deserialiser sets runtime_default = None for
4994        // older catalogs (≤ v14). v15+ reads it from the
4995        // per-column appendix below.
4996        cols.push(ColumnSchema {
4997            name: c_name,
4998            ty,
4999            nullable,
5000            default,
5001            runtime_default: None,
5002            auto_increment,
5003        });
5004    }
5005    let n_cols = cols.len();
5006    cat.create_table(TableSchema::new(name, cols))?;
5007    // Vec<Table> with insertion-order semantics — the just-pushed
5008    // table is at the end. Sidecar `by_name` is already wired up but
5009    // we skip the map lookup here since we know the position.
5010    let t = cat.tables.last_mut().expect("create_table just pushed");
5011    deserialize_rows(cur, t, n_cols)?;
5012    deserialize_indices(cur, t, version)?;
5013    // v6.7.2 — per-table hot_tier_bytes appendix. v11+ writes
5014    // `[u8 has_value][u64 LE value (if has_value)]`. v10 / v9 / v8
5015    // catalogs skip this entirely (the deserialiser reads no extra
5016    // bytes; the table's hot_tier_bytes stays None from
5017    // TableSchema::new).
5018    if version >= 11 {
5019        let has = cur.read_u8()?;
5020        let hot_tier_bytes = match has {
5021            0 => None,
5022            1 => Some(cur.read_u64()?),
5023            other => {
5024                return Err(StorageError::Corrupt(format!(
5025                    "hot_tier_bytes appendix: unknown has-value byte {other}"
5026                )));
5027            }
5028        };
5029        t.schema_mut().hot_tier_bytes = hot_tier_bytes;
5030    }
5031    // v7.6.1 — FOREIGN KEY appendix (FILE_VERSION 13+). v12 / v11 / …
5032    // catalogs skip this entirely.
5033    if version >= 13 {
5034        let fk_count = cur.read_u16()? as usize;
5035        let mut fks = Vec::with_capacity(fk_count);
5036        for _ in 0..fk_count {
5037            let name = match cur.read_u8()? {
5038                0 => None,
5039                1 => Some(cur.read_str()?),
5040                other => {
5041                    return Err(StorageError::Corrupt(format!(
5042                        "FK appendix: unknown has-name byte {other}"
5043                    )));
5044                }
5045            };
5046            let local_arity = cur.read_u16()? as usize;
5047            let mut local_columns = Vec::with_capacity(local_arity);
5048            for _ in 0..local_arity {
5049                local_columns.push(cur.read_u16()? as usize);
5050            }
5051            let parent_table = cur.read_str()?;
5052            let parent_arity = cur.read_u16()? as usize;
5053            if parent_arity != local_arity {
5054                return Err(StorageError::Corrupt(format!(
5055                    "FK arity mismatch in catalog: local {local_arity} vs parent {parent_arity}"
5056                )));
5057            }
5058            let mut parent_columns = Vec::with_capacity(parent_arity);
5059            for _ in 0..parent_arity {
5060                parent_columns.push(cur.read_u16()? as usize);
5061            }
5062            let on_delete = FkAction::from_tag(cur.read_u8()?).ok_or_else(|| {
5063                StorageError::Corrupt("FK appendix: unknown on_delete tag".into())
5064            })?;
5065            let on_update = FkAction::from_tag(cur.read_u8()?).ok_or_else(|| {
5066                StorageError::Corrupt("FK appendix: unknown on_update tag".into())
5067            })?;
5068            fks.push(ForeignKeyConstraint {
5069                name,
5070                local_columns,
5071                parent_table,
5072                parent_columns,
5073                on_delete,
5074                on_update,
5075            });
5076        }
5077        t.schema_mut().foreign_keys = fks;
5078    }
5079    // v7.9.19 — UniquenessConstraint appendix (FILE_VERSION 15+).
5080    // v14 and below skip this entirely.
5081    if version >= 15 {
5082        let uc_count = cur.read_u16()? as usize;
5083        let mut ucs = Vec::with_capacity(uc_count);
5084        for _ in 0..uc_count {
5085            let is_pk = cur.read_u8()? != 0;
5086            let arity = cur.read_u16()? as usize;
5087            let mut cols = Vec::with_capacity(arity);
5088            for _ in 0..arity {
5089                cols.push(cur.read_u16()? as usize);
5090            }
5091            // v7.13.0 — trailing `nulls_not_distinct` flag
5092            // (FILE_VERSION 23+). v22 and below skip — flag
5093            // defaults to false (= NULLS DISTINCT).
5094            let nulls_not_distinct = if version >= 23 {
5095                cur.read_u8()? != 0
5096            } else {
5097                false
5098            };
5099            ucs.push(UniquenessConstraint {
5100                is_primary_key: is_pk,
5101                columns: cols,
5102                nulls_not_distinct,
5103            });
5104        }
5105        t.schema_mut().uniqueness_constraints = ucs;
5106        // v7.9.21 — runtime_default appendix (FILE_VERSION 15+).
5107        let rt_count = cur.read_u16()? as usize;
5108        for _ in 0..rt_count {
5109            let pos = cur.read_u16()? as usize;
5110            let expr = cur.read_str()?;
5111            if let Some(col) = t.schema_mut().columns.get_mut(pos) {
5112                col.runtime_default = Some(expr);
5113            }
5114        }
5115    }
5116    // v7.13.0 — CHECK constraints appendix (FILE_VERSION 23+).
5117    // v22 and below leave the vec empty.
5118    if version >= 23 {
5119        let check_count = cur.read_u16()? as usize;
5120        let mut checks = Vec::with_capacity(check_count);
5121        for _ in 0..check_count {
5122            checks.push(cur.read_str()?);
5123        }
5124        t.schema_mut().checks = checks;
5125    }
5126    let _ = table_name;
5127    Ok(())
5128}
5129
5130fn deserialize_rows(
5131    cur: &mut Cursor<'_>,
5132    t: &mut Table,
5133    _n_cols: usize,
5134) -> Result<(), StorageError> {
5135    let row_count = cur.read_u32()? as usize;
5136    // v4.39: PV has no `reserve` (the BVT doesn't preallocate a
5137    // contiguous buffer); we just push directly and let the trie
5138    // grow. v5.1: row decode reuses `decode_row_body_dense` so the
5139    // catalog and cold-tier segments share one row codec.
5140    let mut hot_bytes: u64 = 0;
5141    for _ in 0..row_count {
5142        let tail = &cur.buf[cur.pos..];
5143        let (row, consumed) = decode_row_body_dense(tail, &t.schema)?;
5144        cur.pos += consumed;
5145        // v5.2.1: account for hot bytes as we go; the snapshot's row
5146        // block bytes are exactly what `encode_row_body_dense` would
5147        // produce, so `consumed` would do too — but going via the
5148        // helper keeps the counter's definition coupled to the
5149        // encoder rather than the snapshot's row prefix layout.
5150        hot_bytes = hot_bytes.saturating_add(row_body_encoded_len(&row, &t.schema) as u64);
5151        t.rows.push_mut(row);
5152    }
5153    t.hot_bytes = hot_bytes;
5154    Ok(())
5155}
5156
5157fn deserialize_indices(
5158    cur: &mut Cursor<'_>,
5159    t: &mut Table,
5160    version: u8,
5161) -> Result<(), StorageError> {
5162    let index_count = cur.read_u16()? as usize;
5163    for _ in 0..index_count {
5164        let idx_name = cur.read_str()?;
5165        let col_pos = cur.read_u16()? as usize;
5166        let column_name = t
5167            .schema
5168            .columns
5169            .get(col_pos)
5170            .ok_or_else(|| {
5171                StorageError::Corrupt(format!(
5172                    "index {idx_name:?} points at non-existent column position {col_pos}"
5173                ))
5174            })?
5175            .name
5176            .clone();
5177        let kind_tag = cur.read_u8()?;
5178        match kind_tag {
5179            0 => {
5180                if version >= 9 {
5181                    // v9+: BTree entries serialised inline (tag-prefixed
5182                    // locator codec). Restore the map directly so any
5183                    // freezer-produced Cold locators come back exactly
5184                    // as they went out.
5185                    let map = read_btree_map(cur)?;
5186                    t.restore_btree_index(idx_name, &column_name, map)?;
5187                } else {
5188                    // v8: no entries on disk; rebuild from rows. Every
5189                    // entry is materialised as `RowLocator::Hot(i)` —
5190                    // semantically identical to the v5.1 in-memory state
5191                    // since v8 catalogs never produced Cold locators.
5192                    t.add_index(idx_name, &column_name)?;
5193                }
5194            }
5195            1 => {
5196                let m = cur.read_u16()? as usize;
5197                let graph = cur.read_nsw_graph(m)?;
5198                t.restore_nsw_index(idx_name, &column_name, graph)?;
5199            }
5200            2 => {
5201                // v6.7.1 — BRIN tag. Payload is the column type
5202                // tag. No further data — summaries live in cold
5203                // segments.
5204                let column_type = cur.read_data_type()?;
5205                t.restore_brin_index(idx_name, &column_name, column_type)?;
5206            }
5207            3 => {
5208                // v7.12.3 — GIN tag. Payload mirrors the BTree
5209                // encoding but with String (lexeme word) keys.
5210                // Only emitted by FILE_VERSION 21+ writers — v20
5211                // and earlier degraded `USING gin` to BTree.
5212                let map = read_gin_map(cur)?;
5213                t.restore_gin_index(idx_name, &column_name, map)?;
5214            }
5215            other => {
5216                return Err(StorageError::Corrupt(format!(
5217                    "unknown index kind tag: {other}"
5218                )));
5219            }
5220        }
5221        // v6.8.0 — included_columns appendix per index. v11- snapshots
5222        // stop before this u16; v12+ always carries it (possibly 0).
5223        if version >= 12 {
5224            let num_included = cur.read_u16()? as usize;
5225            if num_included > 0 {
5226                let mut included: Vec<usize> = Vec::with_capacity(num_included);
5227                for _ in 0..num_included {
5228                    let cp = cur.read_u16()? as usize;
5229                    if cp >= t.schema.columns.len() {
5230                        return Err(StorageError::Corrupt(format!(
5231                            "INCLUDE column position {cp} out of range \
5232                             ({} schema columns)",
5233                            t.schema.columns.len()
5234                        )));
5235                    }
5236                    included.push(cp);
5237                }
5238                if let Some(last) = t.indices.last_mut() {
5239                    last.included_columns = included;
5240                }
5241            }
5242            // v6.8.1 — partial_predicate appendix.
5243            match cur.read_u8()? {
5244                0 => {}
5245                1 => {
5246                    let pred = cur.read_str()?;
5247                    if let Some(last) = t.indices.last_mut() {
5248                        last.partial_predicate = Some(pred);
5249                    }
5250                }
5251                other => {
5252                    return Err(StorageError::Corrupt(format!(
5253                        "partial_predicate tag: unknown byte {other}"
5254                    )));
5255                }
5256            }
5257            // v6.8.2 — expression appendix.
5258            match cur.read_u8()? {
5259                0 => {}
5260                1 => {
5261                    let expr = cur.read_str()?;
5262                    if let Some(last) = t.indices.last_mut() {
5263                        last.expression = Some(expr);
5264                    }
5265                }
5266                other => {
5267                    return Err(StorageError::Corrupt(format!(
5268                        "expression tag: unknown byte {other}"
5269                    )));
5270                }
5271            }
5272            // v7.9.29 — is_unique appendix (FILE_VERSION 16+).
5273            // v15-and-below catalogs stop before this byte. mailrs K1.
5274            if version >= 16 {
5275                match cur.read_u8()? {
5276                    0 => {}
5277                    1 => {
5278                        if let Some(last) = t.indices.last_mut() {
5279                            last.is_unique = true;
5280                        }
5281                    }
5282                    other => {
5283                        return Err(StorageError::Corrupt(format!(
5284                            "is_unique tag: unknown byte {other}"
5285                        )));
5286                    }
5287                }
5288                // v7.9.29 — extra_column_positions appendix.
5289                let n = cur.read_u16()? as usize;
5290                if n > 0 {
5291                    let mut extras: Vec<usize> = Vec::with_capacity(n);
5292                    for _ in 0..n {
5293                        let cp = cur.read_u16()? as usize;
5294                        if cp >= t.schema.columns.len() {
5295                            return Err(StorageError::Corrupt(format!(
5296                                "extra column position {cp} out of range \
5297                                 ({} schema columns)",
5298                                t.schema.columns.len()
5299                            )));
5300                        }
5301                        extras.push(cp);
5302                    }
5303                    if let Some(last) = t.indices.last_mut() {
5304                        last.extra_column_positions = extras;
5305                    }
5306                }
5307            }
5308        }
5309    }
5310    Ok(())
5311}
5312
5313/// Parse a v9 `BTree` index payload — `[u32 entry_count]` followed by
5314/// `entry_count` `(IndexKey, Vec<RowLocator>)` pairs. The locator list
5315/// uses the v5.1 tag-prefixed wire format (`RowLocator::read_le`).
5316fn read_btree_map(
5317    cur: &mut Cursor<'_>,
5318) -> Result<PersistentBTreeMap<IndexKey, Vec<RowLocator>>, StorageError> {
5319    let entry_count = cur.read_u32()? as usize;
5320    let mut map = PersistentBTreeMap::new();
5321    for _ in 0..entry_count {
5322        let key = cur.read_index_key()?;
5323        let locator_count = cur.read_u32()? as usize;
5324        let mut locators = Vec::with_capacity(locator_count);
5325        for _ in 0..locator_count {
5326            let tail = &cur.buf[cur.pos..];
5327            let (loc, consumed) = RowLocator::read_le(tail).map_err(|e| {
5328                StorageError::Corrupt(format!("row_locator decode at offset {}: {e}", cur.pos))
5329            })?;
5330            cur.pos += consumed;
5331            locators.push(loc);
5332        }
5333        map.insert_mut(key, locators);
5334    }
5335    Ok(map)
5336}
5337
5338/// v7.12.3 — parse a `Gin` index payload. Mirrors [`read_btree_map`]
5339/// but with `String` (lexeme word) keys instead of `IndexKey`.
5340/// FILE_VERSION 21+ only.
5341fn read_gin_map(
5342    cur: &mut Cursor<'_>,
5343) -> Result<PersistentBTreeMap<String, Vec<RowLocator>>, StorageError> {
5344    let entry_count = cur.read_u32()? as usize;
5345    let mut map = PersistentBTreeMap::new();
5346    for _ in 0..entry_count {
5347        let word = cur.read_str()?;
5348        let locator_count = cur.read_u32()? as usize;
5349        let mut locators = Vec::with_capacity(locator_count);
5350        for _ in 0..locator_count {
5351            let tail = &cur.buf[cur.pos..];
5352            let (loc, consumed) = RowLocator::read_le(tail).map_err(|e| {
5353                StorageError::Corrupt(format!("row_locator decode at offset {}: {e}", cur.pos))
5354            })?;
5355            cur.pos += consumed;
5356            locators.push(loc);
5357        }
5358        map.insert_mut(word, locators);
5359    }
5360    Ok(map)
5361}
5362
5363// --- low-level binary helpers ---------------------------------------------
5364
5365/// Write a `DataType` as a tag byte + optional payload (Vector carries its
5366/// `u32` dimension). Inverse: [`read_data_type`].
5367/// Serialize an HNSW graph after the `[kind=1][u16 M]` header (v7).
5368/// Layout:
5369/// - `[u16 m_max_0]`
5370/// - `[entry u32]` — `u32::MAX` means `None`, else the entry node index
5371/// - `[u8 entry_level]`
5372/// - `[node_count u32]`
5373/// - for each node: `[u8 level]`  (top layer for this node)
5374/// - `[layer_count u8]`
5375/// - for each layer `0..layer_count`:
5376///     - `[u32 layer_node_count]` (== `node_count`; per-layer slot)
5377///     - for each node: `[u16 neighbor_count] [u32 neighbor]*`
5378fn write_nsw_graph(out: &mut Vec<u8>, g: &NswGraph) {
5379    let entry = g.entry.map_or(u32::MAX, |e| {
5380        u32::try_from(e).expect("NSW entry fits in u32")
5381    });
5382    write_u16(
5383        out,
5384        u16::try_from(g.m_max_0).expect("HNSW m_max_0 fits in u16"),
5385    );
5386    out.extend_from_slice(&entry.to_le_bytes());
5387    out.push(g.entry_level);
5388    let node_count = g.levels.len();
5389    write_u32(
5390        out,
5391        u32::try_from(node_count).expect("HNSW node count fits in u32"),
5392    );
5393    for &lvl in &g.levels {
5394        out.push(lvl);
5395    }
5396    let layer_count = u8::try_from(g.layers.len()).expect("HNSW layer count ≤ 255");
5397    out.push(layer_count);
5398    for layer in &g.layers {
5399        write_u32(
5400            out,
5401            u32::try_from(layer.len()).expect("HNSW per-layer node count fits in u32"),
5402        );
5403        for neighbors in layer {
5404            write_u16(
5405                out,
5406                u16::try_from(neighbors.len()).expect("HNSW neighbour list fits in u16"),
5407            );
5408            // v6.1.x: neighbour slot is already u32 in memory; just
5409            // emit the raw bytes. (v6.0 stored usize and converted
5410            // here.)
5411            for &peer in neighbors {
5412                write_u32(out, peer);
5413            }
5414        }
5415    }
5416}
5417
5418fn write_data_type(out: &mut Vec<u8>, t: DataType) {
5419    match t {
5420        DataType::Int => out.push(1),
5421        DataType::BigInt => out.push(2),
5422        DataType::Float => out.push(3),
5423        DataType::Text => out.push(4),
5424        DataType::Bool => out.push(5),
5425        DataType::Vector { dim, encoding } => match encoding {
5426            // Tag 6: pre-v6 F32 vector. Layout unchanged; pre-v6
5427            // binaries continue to deserialise this exactly as
5428            // before.
5429            VecEncoding::F32 => {
5430                out.push(6);
5431                out.extend_from_slice(&dim.to_le_bytes());
5432            }
5433            // v6.0.3: tag 15 for `VECTOR(N) USING HALF`. Same
5434            // forward-compat fence story as SQ8 below.
5435            VecEncoding::F16 => {
5436                out.push(15);
5437                out.extend_from_slice(&dim.to_le_bytes());
5438            }
5439            // v6.0.1: new tag 14 for `VECTOR(N) USING SQ8` column
5440            // type. Pre-v6 readers fall through `read_data_type`'s
5441            // catch-all and surface `Corrupt("unknown data type tag")`
5442            // — the explicit forward-compat fence called out in
5443            // V6_DESIGN deliberation #5.
5444            VecEncoding::Sq8 => {
5445                out.push(14);
5446                out.extend_from_slice(&dim.to_le_bytes());
5447            }
5448        },
5449        DataType::SmallInt => out.push(7),
5450        DataType::Varchar(max) => {
5451            out.push(8);
5452            out.extend_from_slice(&max.to_le_bytes());
5453        }
5454        DataType::Char(size) => {
5455            out.push(9);
5456            out.extend_from_slice(&size.to_le_bytes());
5457        }
5458        DataType::Numeric { precision, scale } => {
5459            out.push(10);
5460            out.push(precision);
5461            out.push(scale);
5462        }
5463        DataType::Date => out.push(11),
5464        DataType::Timestamp => out.push(12),
5465        // v7.9.2 — tag 17 for TIMESTAMPTZ. Body = i64 microseconds
5466        // UTC, identical to tag 12. Only the schema-side type tag
5467        // differs (for wire OID advertisement).
5468        DataType::Timestamptz => out.push(17),
5469        // INTERVAL is runtime-only — CREATE TABLE never produces a
5470        // column with this type, so write_data_type must not be called
5471        // on it. (Disk-format codepoint reserved for a future v3 where
5472        // INTERVAL becomes storable.)
5473        DataType::Interval => {
5474            unreachable!("DataType::Interval has no on-disk encoding in v2.11")
5475        }
5476        DataType::Json => out.push(13),
5477        // v7.9.0: tag 16 for `JSONB`. Same on-disk layout as
5478        // tag 13 — only the wire OID differs.
5479        DataType::Jsonb => out.push(16),
5480        // v7.10.4: tag 18 for `BYTEA`. Body = [u16 len][bytes].
5481        DataType::Bytes => out.push(18),
5482        // v7.10.9: tag 19 for `TEXT[]`. Body = [u16 count][per
5483        // element: u8 null + (if non-null) u16 len + utf-8].
5484        DataType::TextArray => out.push(19),
5485        // v7.11.12: tag 20 for `INT[]`. Body = [u16 count][per
5486        // element: u8 null + (if non-null) i32 LE].
5487        DataType::IntArray => out.push(20),
5488        // v7.11.12: tag 21 for `BIGINT[]`. Body = [u16 count][per
5489        // element: u8 null + (if non-null) i64 LE].
5490        DataType::BigIntArray => out.push(21),
5491        // v7.12.0: tag 22 for `tsvector`. No body — type identity
5492        // alone. Catalog FILE_VERSION 20+.
5493        DataType::TsVector => out.push(22),
5494        // v7.12.0: tag 23 for `tsquery`. No body. Catalog
5495        // FILE_VERSION 20+.
5496        DataType::TsQuery => out.push(23),
5497    }
5498}
5499
5500impl Cursor<'_> {
5501    fn read_data_type(&mut self) -> Result<DataType, StorageError> {
5502        let tag = self.read_u8()?;
5503        match tag {
5504            1 => Ok(DataType::Int),
5505            2 => Ok(DataType::BigInt),
5506            3 => Ok(DataType::Float),
5507            4 => Ok(DataType::Text),
5508            5 => Ok(DataType::Bool),
5509            6 => Ok(DataType::Vector {
5510                dim: self.read_u32()?,
5511                encoding: VecEncoding::F32,
5512            }),
5513            7 => Ok(DataType::SmallInt),
5514            8 => Ok(DataType::Varchar(self.read_u32()?)),
5515            9 => Ok(DataType::Char(self.read_u32()?)),
5516            10 => {
5517                let precision = self.read_u8()?;
5518                let scale = self.read_u8()?;
5519                Ok(DataType::Numeric { precision, scale })
5520            }
5521            11 => Ok(DataType::Date),
5522            12 => Ok(DataType::Timestamp),
5523            13 => Ok(DataType::Json),
5524            14 => Ok(DataType::Vector {
5525                dim: self.read_u32()?,
5526                encoding: VecEncoding::Sq8,
5527            }),
5528            // v6.0.3: tag 15 for `VECTOR(N) USING HALF`. Same
5529            // [u32 dim] type-tag payload as F32 / SQ8; the encoding
5530            // lives in the tag byte itself.
5531            15 => Ok(DataType::Vector {
5532                dim: self.read_u32()?,
5533                encoding: VecEncoding::F16,
5534            }),
5535            // v7.9.0: tag 16 for `JSONB`. Storage shape == Json;
5536            // we only carry the type tag so the wire layer can
5537            // emit PG OID 3802 instead of 114.
5538            16 => Ok(DataType::Jsonb),
5539            // v7.9.2: tag 17 for `TIMESTAMPTZ`. Storage shape ==
5540            // Timestamp (i64 microseconds UTC); only the wire OID
5541            // (1184) differs.
5542            17 => Ok(DataType::Timestamptz),
5543            // v7.10.4: tag 18 for `BYTEA`. Catalog FILE_VERSION 17+.
5544            18 => Ok(DataType::Bytes),
5545            // v7.10.9: tag 19 for `TEXT[]`. Catalog FILE_VERSION 18+.
5546            19 => Ok(DataType::TextArray),
5547            // v7.11.12: tags 20/21 for INT[]/BIGINT[]. FILE_VERSION 19+.
5548            20 => Ok(DataType::IntArray),
5549            21 => Ok(DataType::BigIntArray),
5550            // v7.12.0: tags 22/23 for tsvector / tsquery. Catalog
5551            // FILE_VERSION 20+.
5552            22 => Ok(DataType::TsVector),
5553            23 => Ok(DataType::TsQuery),
5554            other => Err(StorageError::Corrupt(format!(
5555                "unknown data type tag: {other}"
5556            ))),
5557        }
5558    }
5559}
5560
5561/// Fast computation of the byte length [`encode_row_body_dense`]
5562/// would produce, without allocating the output buffer. Mirrors the
5563/// encoder's per-column body sizing so the v5.2.1 `Table::hot_bytes`
5564/// incremental counter doesn't pay an alloc-per-insert tax. Returns
5565/// the exact same `usize` as `encode_row_body_dense(row, schema).len()`.
5566pub fn row_body_encoded_len(row: &Row, schema: &TableSchema) -> usize {
5567    debug_assert_eq!(
5568        row.values.len(),
5569        schema.columns.len(),
5570        "row_body_encoded_len: row arity must match schema"
5571    );
5572    let bitmap_bytes = schema.columns.len().div_ceil(8);
5573    let mut n = bitmap_bytes;
5574    for (col_idx, v) in row.values.iter().enumerate() {
5575        if matches!(v, Value::Null) {
5576            continue;
5577        }
5578        n += value_body_encoded_len(v, schema.columns[col_idx].ty);
5579    }
5580    n
5581}
5582
5583/// Byte length a single cell consumes when written by
5584/// `write_value_body`. Used by [`row_body_encoded_len`]; kept in
5585/// lock-step with the encoder. The `_ty` slot is reserved for future
5586/// type-dependent encodings — every variant currently writes a fixed
5587/// body shape regardless of the declared column type.
5588fn value_body_encoded_len(v: &Value, _ty: DataType) -> usize {
5589    match v {
5590        Value::SmallInt(_) => 2,
5591        // 4-byte body: i32 / Date.
5592        Value::Int(_) | Value::Date(_) => 4,
5593        // 8-byte body: i64 / f64 / Timestamp.
5594        Value::BigInt(_) | Value::Float(_) | Value::Timestamp(_) => 8,
5595        Value::Bool(_) => 1,
5596        // Text/Varchar/Char/Json share the [u16 len][utf-8] layout.
5597        Value::Text(s) | Value::Json(s) => 2 + s.len(),
5598        // [u32 dim][f32 * dim]
5599        Value::Vector(vec) => 4 + 4 * vec.len(),
5600        // v6.0.1: SQ8 cell on-disk shape — [u32 dim][f32 min]
5601        // [f32 max][u8 * dim] = 12 + dim bytes. `hot_bytes`
5602        // tracking on `Table::insert` calls this every row, so
5603        // returning the real size now (even though the actual
5604        // `write_value_body` writer lands in step 6) keeps the
5605        // sizing arithmetic honest for in-memory benches.
5606        Value::Sq8Vector(q) => 4 + 4 + 4 + q.bytes.len(),
5607        // v6.0.3: halfvec on-disk shape — [u32 dim][u16 LE * dim]
5608        // = 4 + 2 * dim bytes.
5609        Value::HalfVector(h) => 4 + h.bytes.len(),
5610        // [i128 scaled][u8 scale]
5611        Value::Numeric { .. } => 16 + 1,
5612        // v7.10.4: BYTEA on-disk shape mirrors Text — [u16 len][bytes].
5613        // The 16-bit length cap is the same TEXT/JSON limit (~65 KB);
5614        // larger blobs need toast-style chunking which is a v7.11
5615        // carve-out (kept aligned with TEXT for now so the catalog
5616        // snapshot stays simple).
5617        Value::Bytes(b) => 2 + b.len(),
5618        // v7.10.9: TEXT[] on-disk shape — [u16 count][per element:
5619        // u8 null flag + (when non-null) u16 len + utf-8 bytes].
5620        Value::TextArray(items) => {
5621            let mut n = 2; // count prefix
5622            for item in items {
5623                n += 1; // null flag
5624                if let Some(s) = item {
5625                    n += 2 + s.len();
5626                }
5627            }
5628            n
5629        }
5630        // v7.11.12: INT[] / BIGINT[] — [u16 count][per element:
5631        // u8 null + (when non-null) fixed-width LE].
5632        Value::IntArray(items) => {
5633            2 + items
5634                .iter()
5635                .map(|x| if x.is_some() { 5 } else { 1 })
5636                .sum::<usize>()
5637        }
5638        Value::BigIntArray(items) => {
5639            2 + items
5640                .iter()
5641                .map(|x| if x.is_some() { 9 } else { 1 })
5642                .sum::<usize>()
5643        }
5644        // v7.12.0: tsvector dense body — [u16 lexeme_count][per
5645        // lex: u16 word_len + utf-8 word + u16 pos_count + (u16
5646        // LE * pos_count) + u8 weight].
5647        Value::TsVector(lexs) => {
5648            let mut n = 2;
5649            for l in lexs {
5650                n += 2 + l.word.len() + 2 + 2 * l.positions.len() + 1;
5651            }
5652            n
5653        }
5654        // v7.12.0: tsquery dense body — prefix-coded tree.
5655        // Sizing must match `write_tsquery_body` walker.
5656        Value::TsQuery(ast) => tsquery_encoded_len(ast),
5657        // NULL is encoded only in the bitmap, never in the body.
5658        Value::Null => 0,
5659        // INTERVAL has no on-disk encoding (see write_value_body).
5660        Value::Interval { .. } => {
5661            unreachable!("Value::Interval has no on-disk encoding")
5662        }
5663    }
5664}
5665
5666/// Encode one row's body in the v3.0.2 dense format (`FILE_VERSION`
5667/// 8): per-row NULL bitmap (1 bit/col, ceil(cols/8) bytes), then
5668/// each non-NULL cell as `write_value_body`. Same wire shape the
5669/// catalog snapshot writes per row inside its rows-block. Exposed
5670/// pub so v5.1+ cold-tier segment writers can produce row payloads
5671/// that the catalog [`decode_row_body_dense`] decodes 1:1.
5672///
5673/// `row.values.len()` must equal `schema.columns.len()` — the row
5674/// is expected to have been validated by `Table::insert` (the
5675/// engine's INSERT path) before reaching this function.
5676pub fn encode_row_body_dense(row: &Row, schema: &TableSchema) -> Vec<u8> {
5677    debug_assert_eq!(
5678        row.values.len(),
5679        schema.columns.len(),
5680        "dense encode: row arity must match schema"
5681    );
5682    let bitmap_bytes = schema.columns.len().div_ceil(8);
5683    // 8 B per fixed-width cell is a reasonable average; the buffer
5684    // grows past this for variable-width Text/Vector cells.
5685    let mut out = Vec::with_capacity(bitmap_bytes + schema.columns.len() * 8);
5686    let bitmap_offset = out.len();
5687    out.resize(bitmap_offset + bitmap_bytes, 0);
5688    for (i, v) in row.values.iter().enumerate() {
5689        if matches!(v, Value::Null) {
5690            out[bitmap_offset + i / 8] |= 1 << (i % 8);
5691        }
5692    }
5693    for (col_idx, v) in row.values.iter().enumerate() {
5694        if matches!(v, Value::Null) {
5695            continue;
5696        }
5697        write_value_body(&mut out, v, schema.columns[col_idx].ty);
5698    }
5699    out
5700}
5701
5702/// Inverse of [`encode_row_body_dense`]. Reads one row's body from
5703/// `bytes` and returns it plus the number of bytes consumed (so a
5704/// caller decoding a back-to-back stream of rows can advance its
5705/// cursor). Returns `StorageError::Corrupt` on truncation, bad
5706/// UTF-8, or unknown cell tags.
5707pub fn decode_row_body_dense(
5708    bytes: &[u8],
5709    schema: &TableSchema,
5710) -> Result<(Row, usize), StorageError> {
5711    let mut cur = Cursor::new(bytes);
5712    let bitmap_bytes = schema.columns.len().div_ceil(8);
5713    let mut bitmap_buf = [0u8; 32];
5714    if bitmap_bytes > bitmap_buf.len() {
5715        return Err(StorageError::Corrupt(format!(
5716            "row NULL bitmap {bitmap_bytes} B exceeds 32 B cap"
5717        )));
5718    }
5719    let slice = cur.take(bitmap_bytes)?;
5720    bitmap_buf[..bitmap_bytes].copy_from_slice(slice);
5721    let mut values = Vec::with_capacity(schema.columns.len());
5722    for (col_idx, col) in schema.columns.iter().enumerate() {
5723        if (bitmap_buf[col_idx / 8] >> (col_idx % 8)) & 1 == 1 {
5724            values.push(Value::Null);
5725        } else {
5726            values.push(cur.read_value_body(col.ty)?);
5727        }
5728    }
5729    Ok((Row { values }, cur.pos))
5730}
5731
5732/// Schema-driven dense value encoding (`FILE_VERSION` 8). Caller already
5733/// knows the column type and has decided this cell is non-NULL, so we
5734/// skip the per-cell type tag the v7 `write_value` was writing. NULL
5735/// is encoded via the per-row bitmap before this function runs, never
5736/// reaches here. Used only inside the row-encoding hot loop; the
5737/// schema-default path still goes through the legacy `write_value` so
5738/// DEFAULT values keep their self-describing tag and remain decodable
5739/// without consulting a column type.
5740fn write_value_body(out: &mut Vec<u8>, v: &Value, ty: DataType) {
5741    match (v, ty) {
5742        (Value::SmallInt(n), DataType::SmallInt) => out.extend_from_slice(&n.to_le_bytes()),
5743        (Value::Int(n), DataType::Int) => out.extend_from_slice(&n.to_le_bytes()),
5744        (Value::BigInt(n), DataType::BigInt) => out.extend_from_slice(&n.to_le_bytes()),
5745        (Value::Float(x), DataType::Float) => out.extend_from_slice(&x.to_le_bytes()),
5746        (Value::Bool(b), DataType::Bool) => out.push(u8::from(*b)),
5747        (Value::Text(s), DataType::Text | DataType::Varchar(_) | DataType::Char(_)) => {
5748            write_str(out, s);
5749        }
5750        (
5751            Value::Vector(v),
5752            DataType::Vector {
5753                encoding: VecEncoding::F32,
5754                ..
5755            },
5756        ) => {
5757            let dim = u32::try_from(v.len()).expect("vector dim fits in u32");
5758            out.extend_from_slice(&dim.to_le_bytes());
5759            for x in v {
5760                out.extend_from_slice(&x.to_le_bytes());
5761            }
5762        }
5763        // v6.0.1: SQ8 dense body — [u32 dim][f32 min][f32 max]
5764        // [u8 * dim]. Self-describes its length so v6 readers
5765        // walking rows of a v6 catalog stay aligned even if the
5766        // declared column dim drifts (defensive, not normally
5767        // possible since CREATE TABLE pins the dim).
5768        (
5769            Value::Sq8Vector(q),
5770            DataType::Vector {
5771                encoding: VecEncoding::Sq8,
5772                ..
5773            },
5774        ) => {
5775            let dim = u32::try_from(q.bytes.len()).expect("vector dim fits in u32");
5776            out.extend_from_slice(&dim.to_le_bytes());
5777            out.extend_from_slice(&q.min.to_le_bytes());
5778            out.extend_from_slice(&q.max.to_le_bytes());
5779            out.extend_from_slice(&q.bytes);
5780        }
5781        // v6.0.3: halfvec dense body — [u32 dim][u16 LE * dim].
5782        // The raw u16 bytes already live in `h.bytes` little-
5783        // endian, so we just splat them.
5784        (
5785            Value::HalfVector(h),
5786            DataType::Vector {
5787                encoding: VecEncoding::F16,
5788                ..
5789            },
5790        ) => {
5791            let dim = u32::try_from(h.dim()).expect("vector dim fits in u32");
5792            out.extend_from_slice(&dim.to_le_bytes());
5793            out.extend_from_slice(&h.bytes);
5794        }
5795        (Value::Numeric { scaled, .. }, DataType::Numeric { scale, .. }) => {
5796            out.extend_from_slice(&scaled.to_le_bytes());
5797            out.push(scale);
5798        }
5799        (Value::Date(d), DataType::Date) => out.extend_from_slice(&d.to_le_bytes()),
5800        (Value::Timestamp(t), DataType::Timestamp | DataType::Timestamptz) => {
5801            out.extend_from_slice(&t.to_le_bytes())
5802        }
5803        // v4.9: JSON stores as length-prefixed text; same shape as
5804        // Text — the type tag lives in the column schema, not the
5805        // per-cell body.
5806        (Value::Json(s), DataType::Json | DataType::Jsonb) => write_str(out, s),
5807        // v7.10.4: BYTEA shares the [u16 len][bytes] shape with
5808        // Text but writes raw bytes (no UTF-8 invariant).
5809        (Value::Bytes(b), DataType::Bytes) => {
5810            let len = u16::try_from(b.len()).expect("BYTEA cell ≤ 64 KiB");
5811            out.extend_from_slice(&len.to_le_bytes());
5812            out.extend_from_slice(b);
5813        }
5814        // v7.10.9: TEXT[] dense body — [u16 count][per element:
5815        // u8 null flag + (when non-null) u16 len + utf-8 bytes].
5816        (Value::TextArray(items), DataType::TextArray) => {
5817            let count = u16::try_from(items.len()).expect("TEXT[] ≤ 65k elements");
5818            out.extend_from_slice(&count.to_le_bytes());
5819            for item in items {
5820                match item {
5821                    None => out.push(1),
5822                    Some(s) => {
5823                        out.push(0);
5824                        let len = u16::try_from(s.len()).expect("TEXT[] element ≤ 64 KiB");
5825                        out.extend_from_slice(&len.to_le_bytes());
5826                        out.extend_from_slice(s.as_bytes());
5827                    }
5828                }
5829            }
5830        }
5831        // v7.11.12: INT[] dense body — [u16 count][per element:
5832        // u8 null + (when non-null) i32 LE].
5833        (Value::IntArray(items), DataType::IntArray) => {
5834            let count = u16::try_from(items.len()).expect("INT[] ≤ 65k elements");
5835            out.extend_from_slice(&count.to_le_bytes());
5836            for item in items {
5837                match item {
5838                    None => out.push(1),
5839                    Some(n) => {
5840                        out.push(0);
5841                        out.extend_from_slice(&n.to_le_bytes());
5842                    }
5843                }
5844            }
5845        }
5846        // v7.11.12: BIGINT[] dense body — [u16 count][per element:
5847        // u8 null + (when non-null) i64 LE].
5848        (Value::BigIntArray(items), DataType::BigIntArray) => {
5849            let count = u16::try_from(items.len()).expect("BIGINT[] ≤ 65k elements");
5850            out.extend_from_slice(&count.to_le_bytes());
5851            for item in items {
5852                match item {
5853                    None => out.push(1),
5854                    Some(n) => {
5855                        out.push(0);
5856                        out.extend_from_slice(&n.to_le_bytes());
5857                    }
5858                }
5859            }
5860        }
5861        // v7.12.0: tsvector dense body — see `value_body_encoded_len`
5862        // for layout. Lexemes are written in their already-sorted order.
5863        (Value::TsVector(lexs), DataType::TsVector) => write_tsvector_body(out, lexs),
5864        // v7.12.0: tsquery dense body — prefix-coded tree.
5865        (Value::TsQuery(ast), DataType::TsQuery) => write_tsquery_body(out, ast),
5866        // Type mismatch shouldn't happen — `Table::insert` validates
5867        // value type against column type before pushing. Treat as a
5868        // bug, not a runtime error.
5869        (other, ty) => unreachable!(
5870            "schema-driven encode received mismatched value/type pair: \
5871             value tag={:?}, column type={:?}",
5872            other.data_type(),
5873            ty
5874        ),
5875    }
5876}
5877
5878fn write_value(out: &mut Vec<u8>, v: &Value) {
5879    match v {
5880        Value::Null => out.push(0),
5881        Value::SmallInt(n) => {
5882            out.push(7);
5883            out.extend_from_slice(&n.to_le_bytes());
5884        }
5885        Value::Int(n) => {
5886            out.push(1);
5887            out.extend_from_slice(&n.to_le_bytes());
5888        }
5889        Value::BigInt(n) => {
5890            out.push(2);
5891            out.extend_from_slice(&n.to_le_bytes());
5892        }
5893        Value::Float(x) => {
5894            out.push(3);
5895            out.extend_from_slice(&x.to_le_bytes());
5896        }
5897        // v4.9: JSON shares the tag-4 (Text) on-disk encoding —
5898        // schema decides which variant comes back on read. The
5899        // bodies are byte-identical so collapsing the match keeps
5900        // clippy::match_same_arms quiet.
5901        Value::Text(s) | Value::Json(s) => {
5902            out.push(4);
5903            write_str(out, s);
5904        }
5905        Value::Bool(b) => {
5906            out.push(5);
5907            out.push(u8::from(*b));
5908        }
5909        Value::Vector(v) => {
5910            out.push(6);
5911            let dim = u32::try_from(v.len()).expect("vector dim fits in u32");
5912            out.extend_from_slice(&dim.to_le_bytes());
5913            for x in v {
5914                out.extend_from_slice(&x.to_le_bytes());
5915            }
5916        }
5917        // v6.0.1: new tag 11 for an SQ8 cell carried with its full
5918        // header. Layout matches the dense row body shape so a
5919        // round-trip through write_value → read_value bit-equals
5920        // the original `Value::Sq8Vector`.
5921        Value::Sq8Vector(q) => {
5922            out.push(11);
5923            let dim = u32::try_from(q.bytes.len()).expect("vector dim fits in u32");
5924            out.extend_from_slice(&dim.to_le_bytes());
5925            out.extend_from_slice(&q.min.to_le_bytes());
5926            out.extend_from_slice(&q.max.to_le_bytes());
5927            out.extend_from_slice(&q.bytes);
5928        }
5929        // v6.0.3: tag 12 for a HalfVector cell.
5930        // Layout: `[u32 dim][u16 LE × dim]` — bit-identical to the
5931        // dense row body so `write_value` / `read_value` bit-equal
5932        // the original `Value::HalfVector`.
5933        Value::HalfVector(h) => {
5934            out.push(12);
5935            let dim = u32::try_from(h.dim()).expect("vector dim fits in u32");
5936            out.extend_from_slice(&dim.to_le_bytes());
5937            out.extend_from_slice(&h.bytes);
5938        }
5939        Value::Numeric { scaled, scale } => {
5940            out.push(8);
5941            out.extend_from_slice(&scaled.to_le_bytes());
5942            out.push(*scale);
5943        }
5944        Value::Date(d) => {
5945            out.push(9);
5946            out.extend_from_slice(&d.to_le_bytes());
5947        }
5948        Value::Timestamp(t) => {
5949            out.push(10);
5950            out.extend_from_slice(&t.to_le_bytes());
5951        }
5952        // Interval is a runtime-only value (no on-disk representation in
5953        // v2.11). CREATE TABLE rejects `DataType::Interval` columns, so a
5954        // Value::Interval here would mean the engine bypassed that gate.
5955        Value::Interval { .. } => {
5956            unreachable!(
5957                "Value::Interval has no on-disk encoding; engine must reject it before write"
5958            )
5959        }
5960        // v7.10.4: BYTEA — [u8 tag=13_b][u16 len][bytes]. Tag
5961        // distinct from Text (4) so the schema-agnostic
5962        // read_value path can disambiguate. (Tag 11 is taken by
5963        // the WAL `auto_commit_sql` shape elsewhere, hence 14.)
5964        Value::Bytes(b) => {
5965            out.push(14);
5966            let len = u16::try_from(b.len()).expect("BYTEA value ≤ 64 KiB");
5967            out.extend_from_slice(&len.to_le_bytes());
5968            out.extend_from_slice(b);
5969        }
5970        // v7.10.9: TEXT[] — [u8 tag=15][u16 count][per elem: u8
5971        // null + (if non-null) u16 len + utf-8 bytes].
5972        Value::TextArray(items) => {
5973            out.push(15);
5974            let count = u16::try_from(items.len()).expect("TEXT[] ≤ 65k elements");
5975            out.extend_from_slice(&count.to_le_bytes());
5976            for item in items {
5977                match item {
5978                    None => out.push(1),
5979                    Some(s) => {
5980                        out.push(0);
5981                        let len = u16::try_from(s.len()).expect("TEXT[] element ≤ 64 KiB");
5982                        out.extend_from_slice(&len.to_le_bytes());
5983                        out.extend_from_slice(s.as_bytes());
5984                    }
5985                }
5986            }
5987        }
5988        // v7.11.12: INT[] — tag 16. [u16 count][per elem: u8 null +
5989        // (if non-null) i32 LE].
5990        Value::IntArray(items) => {
5991            out.push(16);
5992            let count = u16::try_from(items.len()).expect("INT[] ≤ 65k elements");
5993            out.extend_from_slice(&count.to_le_bytes());
5994            for item in items {
5995                match item {
5996                    None => out.push(1),
5997                    Some(n) => {
5998                        out.push(0);
5999                        out.extend_from_slice(&n.to_le_bytes());
6000                    }
6001                }
6002            }
6003        }
6004        // v7.11.12: BIGINT[] — tag 17. [u16 count][per elem: u8 null +
6005        // (if non-null) i64 LE].
6006        Value::BigIntArray(items) => {
6007            out.push(17);
6008            let count = u16::try_from(items.len()).expect("BIGINT[] ≤ 65k elements");
6009            out.extend_from_slice(&count.to_le_bytes());
6010            for item in items {
6011                match item {
6012                    None => out.push(1),
6013                    Some(n) => {
6014                        out.push(0);
6015                        out.extend_from_slice(&n.to_le_bytes());
6016                    }
6017                }
6018            }
6019        }
6020        // v7.12.0: tsvector — tag 18. Body shape matches
6021        // `write_tsvector_body`.
6022        Value::TsVector(lexs) => {
6023            out.push(18);
6024            write_tsvector_body(out, lexs);
6025        }
6026        // v7.12.0: tsquery — tag 19. Body shape matches
6027        // `write_tsquery_body`.
6028        Value::TsQuery(ast) => {
6029            out.push(19);
6030            write_tsquery_body(out, ast);
6031        }
6032    }
6033}
6034
6035/// v7.12.0: shared tsvector body writer (used by both dense and
6036/// schema-agnostic codecs).
6037fn write_tsvector_body(out: &mut Vec<u8>, lexs: &[TsLexeme]) {
6038    let count = u16::try_from(lexs.len()).expect("tsvector ≤ 65k lexemes");
6039    out.extend_from_slice(&count.to_le_bytes());
6040    for l in lexs {
6041        let wlen = u16::try_from(l.word.len()).expect("tsvector word ≤ 64 KiB");
6042        out.extend_from_slice(&wlen.to_le_bytes());
6043        out.extend_from_slice(l.word.as_bytes());
6044        let plen = u16::try_from(l.positions.len()).expect("tsvector pos count ≤ 65k");
6045        out.extend_from_slice(&plen.to_le_bytes());
6046        for p in &l.positions {
6047            out.extend_from_slice(&p.to_le_bytes());
6048        }
6049        out.push(l.weight);
6050    }
6051}
6052
6053/// v7.12.0: shared tsquery body writer. Prefix-coded tree: each
6054/// node starts with `[u8 tag]` then a tag-specific payload. Tags:
6055/// 0=Term, 1=And, 2=Or, 3=Not, 4=Phrase.
6056fn write_tsquery_body(out: &mut Vec<u8>, ast: &TsQueryAst) {
6057    match ast {
6058        TsQueryAst::Term { word, weight_mask } => {
6059            out.push(0);
6060            let len = u16::try_from(word.len()).expect("tsquery term ≤ 64 KiB");
6061            out.extend_from_slice(&len.to_le_bytes());
6062            out.extend_from_slice(word.as_bytes());
6063            out.push(*weight_mask);
6064        }
6065        TsQueryAst::And(a, b) => {
6066            out.push(1);
6067            write_tsquery_body(out, a);
6068            write_tsquery_body(out, b);
6069        }
6070        TsQueryAst::Or(a, b) => {
6071            out.push(2);
6072            write_tsquery_body(out, a);
6073            write_tsquery_body(out, b);
6074        }
6075        TsQueryAst::Not(x) => {
6076            out.push(3);
6077            write_tsquery_body(out, x);
6078        }
6079        TsQueryAst::Phrase {
6080            left,
6081            right,
6082            distance,
6083        } => {
6084            out.push(4);
6085            out.extend_from_slice(&distance.to_le_bytes());
6086            write_tsquery_body(out, left);
6087            write_tsquery_body(out, right);
6088        }
6089    }
6090}
6091
6092/// v7.12.0: byte length that `write_tsquery_body` would emit.
6093fn tsquery_encoded_len(ast: &TsQueryAst) -> usize {
6094    match ast {
6095        TsQueryAst::Term { word, .. } => 1 + 2 + word.len() + 1,
6096        TsQueryAst::And(a, b) | TsQueryAst::Or(a, b) => {
6097            1 + tsquery_encoded_len(a) + tsquery_encoded_len(b)
6098        }
6099        TsQueryAst::Not(x) => 1 + tsquery_encoded_len(x),
6100        TsQueryAst::Phrase { left, right, .. } => {
6101            1 + 2 + tsquery_encoded_len(left) + tsquery_encoded_len(right)
6102        }
6103    }
6104}
6105
6106fn write_u16(out: &mut Vec<u8>, n: u16) {
6107    out.extend_from_slice(&n.to_le_bytes());
6108}
6109fn write_u32(out: &mut Vec<u8>, n: u32) {
6110    out.extend_from_slice(&n.to_le_bytes());
6111}
6112fn write_str(out: &mut Vec<u8>, s: &str) {
6113    let len = u16::try_from(s.len()).expect("identifier / text fits in u16");
6114    write_u16(out, len);
6115    out.extend_from_slice(s.as_bytes());
6116}
6117
6118/// v7.12.4 — long-string variant: `[u32 LE len][bytes]`. For
6119/// payloads that can plausibly exceed 64 KiB (notably PL/pgSQL
6120/// function bodies). Identifiers + short text continue to use
6121/// the u16 [`write_str`] codec.
6122fn write_str_long(out: &mut Vec<u8>, s: &str) {
6123    let len = u32::try_from(s.len()).expect("function body fits in u32");
6124    write_u32(out, len);
6125    out.extend_from_slice(s.as_bytes());
6126}
6127
6128/// Serialise an [`IndexKey`] using the v9 tagged codec. `read_index_key`
6129/// is the inverse. v8 catalogs never wrote index keys (`BTree` entries were
6130/// rebuilt from `Table::rows`), so this codec is v9+ only.
6131fn write_index_key(out: &mut Vec<u8>, key: &IndexKey) {
6132    match key {
6133        IndexKey::Int(n) => {
6134            out.push(INDEX_KEY_TAG_INT);
6135            out.extend_from_slice(&n.to_le_bytes());
6136        }
6137        IndexKey::Text(s) => {
6138            out.push(INDEX_KEY_TAG_TEXT);
6139            write_str(out, s);
6140        }
6141        IndexKey::Bool(b) => {
6142            out.push(INDEX_KEY_TAG_BOOL);
6143            out.push(u8::from(*b));
6144        }
6145    }
6146}
6147
6148struct Cursor<'a> {
6149    buf: &'a [u8],
6150    pos: usize,
6151}
6152
6153impl<'a> Cursor<'a> {
6154    const fn new(buf: &'a [u8]) -> Self {
6155        Self { buf, pos: 0 }
6156    }
6157
6158    fn take(&mut self, n: usize) -> Result<&'a [u8], StorageError> {
6159        let end = self
6160            .pos
6161            .checked_add(n)
6162            .ok_or_else(|| StorageError::Corrupt(format!("length overflow taking {n} bytes")))?;
6163        if end > self.buf.len() {
6164            return Err(StorageError::Corrupt(format!(
6165                "unexpected EOF at offset {} (wanted {n} more bytes)",
6166                self.pos
6167            )));
6168        }
6169        let s = &self.buf[self.pos..end];
6170        self.pos = end;
6171        Ok(s)
6172    }
6173
6174    fn read_u8(&mut self) -> Result<u8, StorageError> {
6175        Ok(self.take(1)?[0])
6176    }
6177    fn read_u16(&mut self) -> Result<u16, StorageError> {
6178        let s = self.take(2)?;
6179        Ok(u16::from_le_bytes([s[0], s[1]]))
6180    }
6181    fn read_u32(&mut self) -> Result<u32, StorageError> {
6182        let s = self.take(4)?;
6183        Ok(u32::from_le_bytes([s[0], s[1], s[2], s[3]]))
6184    }
6185    fn read_i32(&mut self) -> Result<i32, StorageError> {
6186        let s = self.take(4)?;
6187        Ok(i32::from_le_bytes([s[0], s[1], s[2], s[3]]))
6188    }
6189    /// v6.7.2 — u64 LE read for the per-table `hot_tier_bytes`
6190    /// catalog appendix.
6191    fn read_u64(&mut self) -> Result<u64, StorageError> {
6192        let s = self.take(8)?;
6193        Ok(u64::from_le_bytes([
6194            s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
6195        ]))
6196    }
6197    fn read_i64(&mut self) -> Result<i64, StorageError> {
6198        let s = self.take(8)?;
6199        let arr: [u8; 8] = s.try_into().expect("checked");
6200        Ok(i64::from_le_bytes(arr))
6201    }
6202    fn read_f64(&mut self) -> Result<f64, StorageError> {
6203        let s = self.take(8)?;
6204        let arr: [u8; 8] = s.try_into().expect("checked");
6205        Ok(f64::from_le_bytes(arr))
6206    }
6207    fn read_f32(&mut self) -> Result<f32, StorageError> {
6208        let s = self.take(4)?;
6209        Ok(f32::from_le_bytes([s[0], s[1], s[2], s[3]]))
6210    }
6211    fn read_str(&mut self) -> Result<String, StorageError> {
6212        let len = self.read_u16()? as usize;
6213        let bytes = self.take(len)?;
6214        core::str::from_utf8(bytes)
6215            .map(String::from)
6216            .map_err(|_| StorageError::Corrupt("invalid UTF-8 in identifier or text".into()))
6217    }
6218
6219    /// v7.12.4 — long-string variant for payloads written via
6220    /// [`write_str_long`] (u32-length prefix). Used for PL/pgSQL
6221    /// function bodies which can plausibly exceed 64 KiB.
6222    fn read_str_long(&mut self) -> Result<String, StorageError> {
6223        let len = self.read_u32()? as usize;
6224        let bytes = self.take(len)?;
6225        core::str::from_utf8(bytes)
6226            .map(String::from)
6227            .map_err(|_| StorageError::Corrupt("invalid UTF-8 in long-string payload".into()))
6228    }
6229
6230    /// Parse an [`IndexKey`] emitted by `write_index_key` (v9 tagged
6231    /// codec). Returns `StorageError::Corrupt` on unknown tag or
6232    /// truncated payload.
6233    fn read_index_key(&mut self) -> Result<IndexKey, StorageError> {
6234        let tag = self.read_u8()?;
6235        match tag {
6236            INDEX_KEY_TAG_INT => Ok(IndexKey::Int(self.read_i64()?)),
6237            INDEX_KEY_TAG_TEXT => Ok(IndexKey::Text(self.read_str()?)),
6238            INDEX_KEY_TAG_BOOL => Ok(IndexKey::Bool(self.read_u8()? != 0)),
6239            other => Err(StorageError::Corrupt(format!(
6240                "unknown index key tag: {other}"
6241            ))),
6242        }
6243    }
6244    /// Schema-driven dense value decode (`FILE_VERSION` 8). Caller has
6245    /// already cleared the NULL bit from the row bitmap; we read the
6246    /// fixed-width body for the given column type. Used inside the row
6247    /// hot loop; column defaults still go through `read_value` (which
6248    /// reads its own type tag) so DEFAULT round-trips without a schema.
6249    fn read_value_body(&mut self, ty: DataType) -> Result<Value, StorageError> {
6250        match ty {
6251            DataType::SmallInt => {
6252                let s = self.take(2)?;
6253                Ok(Value::SmallInt(i16::from_le_bytes([s[0], s[1]])))
6254            }
6255            DataType::Int => Ok(Value::Int(self.read_i32()?)),
6256            DataType::BigInt => Ok(Value::BigInt(self.read_i64()?)),
6257            DataType::Float => Ok(Value::Float(self.read_f64()?)),
6258            DataType::Bool => Ok(Value::Bool(self.read_u8()? != 0)),
6259            DataType::Text | DataType::Varchar(_) | DataType::Char(_) => {
6260                Ok(Value::Text(self.read_str()?))
6261            }
6262            DataType::Vector {
6263                encoding: VecEncoding::F32,
6264                ..
6265            } => {
6266                let dim = self.read_u32()? as usize;
6267                let mut v = Vec::with_capacity(dim);
6268                for _ in 0..dim {
6269                    let bytes: [u8; 4] = self.take(4)?.try_into().expect("checked");
6270                    v.push(f32::from_le_bytes(bytes));
6271                }
6272                Ok(Value::Vector(v))
6273            }
6274            DataType::Vector {
6275                encoding: VecEncoding::Sq8,
6276                ..
6277            } => {
6278                let dim = self.read_u32()? as usize;
6279                let min = self.read_f32()?;
6280                let max = self.read_f32()?;
6281                let bytes = self.take(dim)?.to_vec();
6282                Ok(Value::Sq8Vector(quantize::Sq8Vector { min, max, bytes }))
6283            }
6284            DataType::Vector {
6285                encoding: VecEncoding::F16,
6286                ..
6287            } => {
6288                let dim = self.read_u32()? as usize;
6289                let bytes = self.take(dim * 2)?.to_vec();
6290                Ok(Value::HalfVector(halfvec::HalfVector { bytes }))
6291            }
6292            DataType::Numeric { .. } => {
6293                let s = self.take(16)?;
6294                let arr: [u8; 16] = s.try_into().expect("checked");
6295                let scaled = i128::from_le_bytes(arr);
6296                let scale = self.read_u8()?;
6297                Ok(Value::Numeric { scaled, scale })
6298            }
6299            DataType::Date => Ok(Value::Date(self.read_i32()?)),
6300            DataType::Timestamp => Ok(Value::Timestamp(self.read_i64()?)),
6301            DataType::Timestamptz => Ok(Value::Timestamp(self.read_i64()?)),
6302            DataType::Jsonb => Ok(Value::Json(self.read_str()?)),
6303            DataType::Interval => {
6304                // Defensive — schema gate (CREATE TABLE rejects Interval
6305                // columns) means this branch can't be hit through normal
6306                // flow; reject corrupt files explicitly rather than
6307                // panic.
6308                Err(StorageError::Corrupt(
6309                    "INTERVAL column found on disk — runtime-only type, v3.0.2 rejects it".into(),
6310                ))
6311            }
6312            DataType::Json => Ok(Value::Json(self.read_str()?)),
6313            // v7.10.4: BYTEA on-disk is [u16 len][bytes]. Same wire
6314            // shape as Text, but read as raw Vec<u8>.
6315            DataType::Bytes => {
6316                let len = self.read_u16()? as usize;
6317                let bytes = self.take(len)?.to_vec();
6318                Ok(Value::Bytes(bytes))
6319            }
6320            // v7.10.9: TEXT[] dense body.
6321            DataType::TextArray => {
6322                let count = self.read_u16()? as usize;
6323                let mut items: Vec<Option<String>> = Vec::with_capacity(count);
6324                for _ in 0..count {
6325                    match self.read_u8()? {
6326                        0 => items.push(Some(self.read_str()?)),
6327                        1 => items.push(None),
6328                        other => {
6329                            return Err(StorageError::Corrupt(format!(
6330                                "TEXT[] null flag: unknown byte {other}"
6331                            )));
6332                        }
6333                    }
6334                }
6335                Ok(Value::TextArray(items))
6336            }
6337            // v7.11.12: INT[] dense body.
6338            DataType::IntArray => {
6339                let count = self.read_u16()? as usize;
6340                let mut items: Vec<Option<i32>> = Vec::with_capacity(count);
6341                for _ in 0..count {
6342                    match self.read_u8()? {
6343                        0 => items.push(Some(self.read_i32()?)),
6344                        1 => items.push(None),
6345                        other => {
6346                            return Err(StorageError::Corrupt(format!(
6347                                "INT[] null flag: unknown byte {other}"
6348                            )));
6349                        }
6350                    }
6351                }
6352                Ok(Value::IntArray(items))
6353            }
6354            // v7.11.12: BIGINT[] dense body.
6355            DataType::BigIntArray => {
6356                let count = self.read_u16()? as usize;
6357                let mut items: Vec<Option<i64>> = Vec::with_capacity(count);
6358                for _ in 0..count {
6359                    match self.read_u8()? {
6360                        0 => items.push(Some(self.read_i64()?)),
6361                        1 => items.push(None),
6362                        other => {
6363                            return Err(StorageError::Corrupt(format!(
6364                                "BIGINT[] null flag: unknown byte {other}"
6365                            )));
6366                        }
6367                    }
6368                }
6369                Ok(Value::BigIntArray(items))
6370            }
6371            // v7.12.0: tsvector dense body — [u16 lex_count]
6372            // [per lex: u16 word_len + utf-8 word + u16 pos_count
6373            // + (u16 LE * pos_count) + u8 weight].
6374            DataType::TsVector => Ok(Value::TsVector(self.read_tsvector_body()?)),
6375            DataType::TsQuery => Ok(Value::TsQuery(self.read_tsquery_body()?)),
6376        }
6377    }
6378
6379    /// v7.12.0 — read a tsvector body emitted by `write_tsvector_body`.
6380    fn read_tsvector_body(&mut self) -> Result<Vec<TsLexeme>, StorageError> {
6381        let count = self.read_u16()? as usize;
6382        let mut out = Vec::with_capacity(count);
6383        for _ in 0..count {
6384            let word = self.read_str()?;
6385            let pos_count = self.read_u16()? as usize;
6386            let mut positions = Vec::with_capacity(pos_count);
6387            for _ in 0..pos_count {
6388                positions.push(self.read_u16()?);
6389            }
6390            let weight = self.read_u8()?;
6391            out.push(TsLexeme {
6392                word,
6393                positions,
6394                weight,
6395            });
6396        }
6397        Ok(out)
6398    }
6399
6400    /// v7.12.0 — read a tsquery body emitted by `write_tsquery_body`.
6401    fn read_tsquery_body(&mut self) -> Result<TsQueryAst, StorageError> {
6402        let tag = self.read_u8()?;
6403        match tag {
6404            0 => {
6405                let word = self.read_str()?;
6406                let weight_mask = self.read_u8()?;
6407                Ok(TsQueryAst::Term { word, weight_mask })
6408            }
6409            1 => {
6410                let a = self.read_tsquery_body()?;
6411                let b = self.read_tsquery_body()?;
6412                Ok(TsQueryAst::And(Box::new(a), Box::new(b)))
6413            }
6414            2 => {
6415                let a = self.read_tsquery_body()?;
6416                let b = self.read_tsquery_body()?;
6417                Ok(TsQueryAst::Or(Box::new(a), Box::new(b)))
6418            }
6419            3 => {
6420                let x = self.read_tsquery_body()?;
6421                Ok(TsQueryAst::Not(Box::new(x)))
6422            }
6423            4 => {
6424                let distance = self.read_u16()?;
6425                let left = self.read_tsquery_body()?;
6426                let right = self.read_tsquery_body()?;
6427                Ok(TsQueryAst::Phrase {
6428                    left: Box::new(left),
6429                    right: Box::new(right),
6430                    distance,
6431                })
6432            }
6433            other => Err(StorageError::Corrupt(format!(
6434                "tsquery: unknown node tag {other}"
6435            ))),
6436        }
6437    }
6438
6439    fn read_value(&mut self) -> Result<Value, StorageError> {
6440        let tag = self.read_u8()?;
6441        match tag {
6442            0 => Ok(Value::Null),
6443            1 => Ok(Value::Int(self.read_i32()?)),
6444            2 => Ok(Value::BigInt(self.read_i64()?)),
6445            3 => Ok(Value::Float(self.read_f64()?)),
6446            4 => Ok(Value::Text(self.read_str()?)),
6447            5 => Ok(Value::Bool(self.read_u8()? != 0)),
6448            6 => {
6449                let dim = self.read_u32()? as usize;
6450                let mut v = Vec::with_capacity(dim);
6451                for _ in 0..dim {
6452                    let bytes: [u8; 4] = self.take(4)?.try_into().expect("checked");
6453                    v.push(f32::from_le_bytes(bytes));
6454                }
6455                Ok(Value::Vector(v))
6456            }
6457            7 => {
6458                let s = self.take(2)?;
6459                Ok(Value::SmallInt(i16::from_le_bytes([s[0], s[1]])))
6460            }
6461            8 => {
6462                let s = self.take(16)?;
6463                let arr: [u8; 16] = s.try_into().expect("checked");
6464                let scaled = i128::from_le_bytes(arr);
6465                let scale = self.read_u8()?;
6466                Ok(Value::Numeric { scaled, scale })
6467            }
6468            9 => Ok(Value::Date(self.read_i32()?)),
6469            10 => Ok(Value::Timestamp(self.read_i64()?)),
6470            // v6.0.1: tag 11 — Sq8Vector. Pre-v6 readers fall
6471            // through to the catch-all and surface
6472            // `Corrupt("unknown value tag")`, matching the
6473            // forward-compat fence on the column-type side.
6474            11 => {
6475                let dim = self.read_u32()? as usize;
6476                let min = self.read_f32()?;
6477                let max = self.read_f32()?;
6478                let bytes = self.take(dim)?.to_vec();
6479                Ok(Value::Sq8Vector(quantize::Sq8Vector { min, max, bytes }))
6480            }
6481            // v6.0.3: tag 12 — HalfVector. Same forward-compat
6482            // fence story as tag 11.
6483            12 => {
6484                let dim = self.read_u32()? as usize;
6485                let bytes = self.take(dim * 2)?.to_vec();
6486                Ok(Value::HalfVector(halfvec::HalfVector { bytes }))
6487            }
6488            // v7.10.4: tag 14 — BYTEA. [u16 len][bytes].
6489            14 => {
6490                let len = self.read_u16()? as usize;
6491                let bytes = self.take(len)?.to_vec();
6492                Ok(Value::Bytes(bytes))
6493            }
6494            // v7.10.9: tag 15 — TEXT[]. [u16 count][per elem: u8
6495            // null + (when non-null) u16 len + utf-8 bytes].
6496            15 => {
6497                let count = self.read_u16()? as usize;
6498                let mut items: Vec<Option<String>> = Vec::with_capacity(count);
6499                for _ in 0..count {
6500                    match self.read_u8()? {
6501                        0 => items.push(Some(self.read_str()?)),
6502                        1 => items.push(None),
6503                        other => {
6504                            return Err(StorageError::Corrupt(format!(
6505                                "TEXT[] null flag in value tag: unknown byte {other}"
6506                            )));
6507                        }
6508                    }
6509                }
6510                Ok(Value::TextArray(items))
6511            }
6512            // v7.11.12: tags 16/17 — INT[] / BIGINT[].
6513            16 => {
6514                let count = self.read_u16()? as usize;
6515                let mut items: Vec<Option<i32>> = Vec::with_capacity(count);
6516                for _ in 0..count {
6517                    match self.read_u8()? {
6518                        0 => items.push(Some(self.read_i32()?)),
6519                        1 => items.push(None),
6520                        other => {
6521                            return Err(StorageError::Corrupt(format!(
6522                                "INT[] null flag in value tag: unknown byte {other}"
6523                            )));
6524                        }
6525                    }
6526                }
6527                Ok(Value::IntArray(items))
6528            }
6529            17 => {
6530                let count = self.read_u16()? as usize;
6531                let mut items: Vec<Option<i64>> = Vec::with_capacity(count);
6532                for _ in 0..count {
6533                    match self.read_u8()? {
6534                        0 => items.push(Some(self.read_i64()?)),
6535                        1 => items.push(None),
6536                        other => {
6537                            return Err(StorageError::Corrupt(format!(
6538                                "BIGINT[] null flag in value tag: unknown byte {other}"
6539                            )));
6540                        }
6541                    }
6542                }
6543                Ok(Value::BigIntArray(items))
6544            }
6545            // v7.12.0: tag 18 — tsvector. Body matches the dense
6546            // form (`read_tsvector_body`).
6547            18 => Ok(Value::TsVector(self.read_tsvector_body()?)),
6548            // v7.12.0: tag 19 — tsquery.
6549            19 => Ok(Value::TsQuery(self.read_tsquery_body()?)),
6550            other => Err(StorageError::Corrupt(format!("unknown value tag: {other}"))),
6551        }
6552    }
6553
6554    /// Read an NSW graph that was emitted via `write_nsw_graph`. `m`
6555    /// is passed in because it was already consumed from the per-
6556    /// index header. Returns the reconstituted `NswGraph`.
6557    fn read_nsw_graph(&mut self, m: usize) -> Result<NswGraph, StorageError> {
6558        let m_max_0 = self.read_u16()? as usize;
6559        let entry_raw = self.read_u32()?;
6560        let entry = if entry_raw == u32::MAX {
6561            None
6562        } else {
6563            Some(entry_raw as usize)
6564        };
6565        let entry_level = self.read_u8()?;
6566        let node_count = self.read_u32()? as usize;
6567        // v5.5.0: levels/per-layer are PV-backed in memory, but the wire
6568        // format is unchanged — decode element-by-element into a PV via
6569        // push_mut (transient in-place, no per-element path-copy here since
6570        // the freshly-built PV is uniquely owned).
6571        let mut levels: PersistentVec<u8> = PersistentVec::new();
6572        for _ in 0..node_count {
6573            levels.push_mut(self.read_u8()?);
6574        }
6575        let layer_count = self.read_u8()? as usize;
6576        let mut layers: Vec<PersistentVec<Vec<u32>>> = Vec::with_capacity(layer_count);
6577        for _ in 0..layer_count {
6578            let n = self.read_u32()? as usize;
6579            let mut per_layer: PersistentVec<Vec<u32>> = PersistentVec::new();
6580            for _ in 0..n {
6581                let cnt = self.read_u16()? as usize;
6582                let mut row: Vec<u32> = Vec::with_capacity(cnt);
6583                for _ in 0..cnt {
6584                    row.push(self.read_u32()?);
6585                }
6586                per_layer.push_mut(row);
6587            }
6588            layers.push(per_layer);
6589        }
6590        Ok(NswGraph {
6591            m,
6592            m_max_0,
6593            entry,
6594            entry_level,
6595            levels,
6596            layers,
6597        })
6598    }
6599}
6600
6601#[cfg(test)]
6602mod tests {
6603    use super::*;
6604    use alloc::string::ToString;
6605    use alloc::vec;
6606
6607    #[cfg(target_arch = "aarch64")]
6608    #[test]
6609    fn neon_l2_matches_scalar() {
6610        // For every dim that's a multiple of 4 (4, 8, 12, 16, 64,
6611        // 128, 256, 384, 512, 768, 1024, 1536), the NEON impl must
6612        // agree with the scalar reference within tight float
6613        // tolerance (FMA rounding differs from separate * + +).
6614        let dims = [4usize, 8, 12, 16, 64, 128, 256, 384, 512, 768, 1024, 1536];
6615        for &d in &dims {
6616            let mut state: u64 = (d as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15);
6617            let mut a = Vec::with_capacity(d);
6618            let mut b = Vec::with_capacity(d);
6619            for _ in 0..d {
6620                state = state
6621                    .wrapping_mul(6_364_136_223_846_793_005)
6622                    .wrapping_add(1);
6623                #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
6624                let x = (((state >> 32) & 0x00FF_FFFF) as f32) / (0x80_0000_u32 as f32) - 1.0;
6625                state = state
6626                    .wrapping_mul(6_364_136_223_846_793_005)
6627                    .wrapping_add(1);
6628                #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
6629                let y = (((state >> 32) & 0x00FF_FFFF) as f32) / (0x80_0000_u32 as f32) - 1.0;
6630                a.push(x);
6631                b.push(y);
6632            }
6633            let scalar = l2_distance_sq_scalar(&a, &b);
6634            let neon = unsafe { l2_distance_sq_neon(&a, &b) };
6635            let tol = (scalar.abs().max(1e-6)) * 1e-4;
6636            assert!(
6637                (scalar - neon).abs() <= tol,
6638                "dim={d}: scalar={scalar} neon={neon} diff={}",
6639                (scalar - neon).abs()
6640            );
6641        }
6642    }
6643
6644    #[cfg(target_arch = "aarch64")]
6645    #[test]
6646    fn neon_inner_product_matches_scalar() {
6647        // v6.0.2 step 1: NEON IP must agree with scalar across every
6648        // production-shaped dim. FMA rounding differs from
6649        // separate * + +, so the tolerance scales with magnitude.
6650        let dims = [4usize, 8, 12, 16, 64, 128, 256, 512, 1024];
6651        for &d in &dims {
6652            let mut state: u64 = (d as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15);
6653            let mut a = Vec::with_capacity(d);
6654            let mut b = Vec::with_capacity(d);
6655            for _ in 0..d {
6656                state = state
6657                    .wrapping_mul(6_364_136_223_846_793_005)
6658                    .wrapping_add(1);
6659                #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
6660                let x = (((state >> 32) & 0x00FF_FFFF) as f32) / (0x80_0000_u32 as f32) - 1.0;
6661                state = state
6662                    .wrapping_mul(6_364_136_223_846_793_005)
6663                    .wrapping_add(1);
6664                #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
6665                let y = (((state >> 32) & 0x00FF_FFFF) as f32) / (0x80_0000_u32 as f32) - 1.0;
6666                a.push(x);
6667                b.push(y);
6668            }
6669            let scalar = inner_product_scalar(&a, &b);
6670            let neon = unsafe { inner_product_neon(&a, &b) };
6671            #[allow(clippy::cast_precision_loss)]
6672            let tol = (scalar.abs().max(1e-6)) * 1e-4 + (d as f32) * 1e-6;
6673            assert!(
6674                (scalar - neon).abs() <= tol,
6675                "IP dim={d}: scalar={scalar} neon={neon} diff={}",
6676                (scalar - neon).abs()
6677            );
6678        }
6679    }
6680
6681    #[cfg(target_arch = "aarch64")]
6682    #[allow(clippy::similar_names)]
6683    #[test]
6684    fn neon_cosine_dot_norms_matches_scalar() {
6685        let dims = [4usize, 8, 12, 16, 64, 128, 256, 512, 1024];
6686        for &d in &dims {
6687            let mut state: u64 = (d as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9);
6688            let mut a = Vec::with_capacity(d);
6689            let mut b = Vec::with_capacity(d);
6690            for _ in 0..d {
6691                state = state
6692                    .wrapping_mul(6_364_136_223_846_793_005)
6693                    .wrapping_add(1);
6694                #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
6695                let x = (((state >> 32) & 0x00FF_FFFF) as f32) / (0x80_0000_u32 as f32) - 1.0;
6696                state = state
6697                    .wrapping_mul(6_364_136_223_846_793_005)
6698                    .wrapping_add(1);
6699                #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
6700                let y = (((state >> 32) & 0x00FF_FFFF) as f32) / (0x80_0000_u32 as f32) - 1.0;
6701                a.push(x);
6702                b.push(y);
6703            }
6704            let (dot_s, na_s, nb_s) = cosine_dot_norms_scalar(&a, &b);
6705            let (dot_n, na_n, nb_n) = unsafe { cosine_dot_norms_neon(&a, &b) };
6706            #[allow(clippy::cast_precision_loss)]
6707            let tol_d = (dot_s.abs().max(1e-6)) * 1e-4 + (d as f32) * 1e-6;
6708            #[allow(clippy::cast_precision_loss)]
6709            let tol_n = (na_s.abs().max(1e-6)) * 1e-4 + (d as f32) * 1e-6;
6710            assert!(
6711                (dot_s - dot_n).abs() <= tol_d,
6712                "cosine dot dim={d}: scalar={dot_s} neon={dot_n}"
6713            );
6714            assert!(
6715                (na_s - na_n).abs() <= tol_n,
6716                "cosine na dim={d}: scalar={na_s} neon={na_n}"
6717            );
6718            assert!(
6719                (nb_s - nb_n).abs() <= tol_n,
6720                "cosine nb dim={d}: scalar={nb_s} neon={nb_n}"
6721            );
6722        }
6723    }
6724
6725    fn make_users_schema() -> TableSchema {
6726        TableSchema::new(
6727            "users",
6728            vec![
6729                ColumnSchema::new("id", DataType::Int, false),
6730                ColumnSchema::new("name", DataType::Text, false),
6731                ColumnSchema::new("score", DataType::Float, true),
6732            ],
6733        )
6734    }
6735
6736    #[test]
6737    fn value_type_tag_matches_variant() {
6738        assert_eq!(Value::Int(1).data_type(), Some(DataType::Int));
6739        assert_eq!(Value::BigInt(1).data_type(), Some(DataType::BigInt));
6740        assert_eq!(Value::Float(1.0).data_type(), Some(DataType::Float));
6741        assert_eq!(Value::Text("x".into()).data_type(), Some(DataType::Text));
6742        assert_eq!(Value::Bool(true).data_type(), Some(DataType::Bool));
6743        assert_eq!(Value::Null.data_type(), None);
6744        assert!(Value::Null.is_null());
6745        assert!(!Value::Int(0).is_null());
6746    }
6747
6748    #[test]
6749    fn sq8_value_reports_sq8_data_type() {
6750        // v6.0.1: a `Value::Sq8Vector` cell surfaces its dim
6751        // (= bytes.len()) and encoding through `data_type()` so
6752        // INSERT-time column type-checks (step 3) can route on
6753        // both shape and encoding.
6754        let q = crate::quantize::quantize(&[0.0, 0.25, 0.5, 0.75, 1.0]);
6755        let v = Value::Sq8Vector(q);
6756        assert_eq!(
6757            v.data_type(),
6758            Some(DataType::Vector {
6759                dim: 5,
6760                encoding: VecEncoding::Sq8,
6761            }),
6762        );
6763    }
6764
6765    #[test]
6766    fn datatype_display_matches_pg_keyword() {
6767        assert_eq!(DataType::Int.to_string(), "INT");
6768        assert_eq!(DataType::BigInt.to_string(), "BIGINT");
6769        assert_eq!(DataType::Float.to_string(), "FLOAT");
6770        assert_eq!(DataType::Text.to_string(), "TEXT");
6771        assert_eq!(DataType::Bool.to_string(), "BOOL");
6772    }
6773
6774    #[test]
6775    fn row_len_and_emptiness() {
6776        let r = Row::new(vec![Value::Int(1), Value::Null]);
6777        assert_eq!(r.len(), 2);
6778        assert!(!r.is_empty());
6779        assert!(Row::new(Vec::new()).is_empty());
6780    }
6781
6782    #[test]
6783    fn table_schema_column_position() {
6784        let s = make_users_schema();
6785        assert_eq!(s.column_position("id"), Some(0));
6786        assert_eq!(s.column_position("score"), Some(2));
6787        assert_eq!(s.column_position("missing"), None);
6788    }
6789
6790    #[test]
6791    fn catalog_create_table_then_lookup() {
6792        let mut cat = Catalog::new();
6793        cat.create_table(make_users_schema()).unwrap();
6794        assert_eq!(cat.table_count(), 1);
6795        assert!(cat.get("users").is_some());
6796        assert!(cat.get("nope").is_none());
6797    }
6798
6799    #[test]
6800    fn catalog_duplicate_table_is_rejected() {
6801        let mut cat = Catalog::new();
6802        cat.create_table(make_users_schema()).unwrap();
6803        let err = cat.create_table(make_users_schema()).unwrap_err();
6804        assert!(matches!(err, StorageError::DuplicateTable { ref name } if name == "users"));
6805    }
6806
6807    #[test]
6808    fn table_insert_happy_path_appends_row() {
6809        let mut cat = Catalog::new();
6810        cat.create_table(make_users_schema()).unwrap();
6811        let t = cat.get_mut("users").unwrap();
6812        t.insert(Row::new(vec![
6813            Value::Int(1),
6814            Value::Text("alice".into()),
6815            Value::Float(99.5),
6816        ]))
6817        .unwrap();
6818        assert_eq!(t.row_count(), 1);
6819        assert_eq!(t.rows()[0].values[1], Value::Text("alice".into()));
6820    }
6821
6822    #[test]
6823    fn table_insert_arity_mismatch() {
6824        let mut cat = Catalog::new();
6825        cat.create_table(make_users_schema()).unwrap();
6826        let t = cat.get_mut("users").unwrap();
6827        let err = t.insert(Row::new(vec![Value::Int(1)])).unwrap_err();
6828        assert!(matches!(
6829            err,
6830            StorageError::ArityMismatch {
6831                expected: 3,
6832                actual: 1
6833            }
6834        ));
6835        assert_eq!(t.row_count(), 0);
6836    }
6837
6838    #[test]
6839    fn table_insert_type_mismatch_reports_column() {
6840        let mut cat = Catalog::new();
6841        cat.create_table(make_users_schema()).unwrap();
6842        let t = cat.get_mut("users").unwrap();
6843        let err = t
6844            .insert(Row::new(vec![
6845                Value::Int(1),
6846                Value::Int(42), // name expects Text
6847                Value::Float(0.0),
6848            ]))
6849            .unwrap_err();
6850        match err {
6851            StorageError::TypeMismatch {
6852                ref column,
6853                expected,
6854                actual,
6855                position,
6856            } => {
6857                assert_eq!(column, "name");
6858                assert_eq!(expected, DataType::Text);
6859                assert_eq!(actual, DataType::Int);
6860                assert_eq!(position, 1);
6861            }
6862            other => panic!("unexpected: {other:?}"),
6863        }
6864        assert_eq!(t.row_count(), 0);
6865    }
6866
6867    #[test]
6868    fn table_insert_null_into_not_null_rejected() {
6869        let mut cat = Catalog::new();
6870        cat.create_table(make_users_schema()).unwrap();
6871        let t = cat.get_mut("users").unwrap();
6872        let err = t
6873            .insert(Row::new(vec![
6874                Value::Int(1),
6875                Value::Null, // name is NOT NULL
6876                Value::Float(1.0),
6877            ]))
6878            .unwrap_err();
6879        assert!(matches!(err, StorageError::NullInNotNull { ref column } if column == "name"));
6880    }
6881
6882    #[test]
6883    fn table_insert_null_into_nullable_ok() {
6884        let mut cat = Catalog::new();
6885        cat.create_table(make_users_schema()).unwrap();
6886        let t = cat.get_mut("users").unwrap();
6887        t.insert(Row::new(vec![
6888            Value::Int(1),
6889            Value::Text("bob".into()),
6890            Value::Null,
6891        ]))
6892        .unwrap();
6893        assert_eq!(t.row_count(), 1);
6894    }
6895
6896    #[test]
6897    fn catalog_get_mut_independent_per_table() {
6898        let mut cat = Catalog::new();
6899        cat.create_table(TableSchema::new(
6900            "a",
6901            vec![ColumnSchema::new("v", DataType::Int, false)],
6902        ))
6903        .unwrap();
6904        cat.create_table(TableSchema::new(
6905            "b",
6906            vec![ColumnSchema::new("v", DataType::Int, false)],
6907        ))
6908        .unwrap();
6909        cat.get_mut("a")
6910            .unwrap()
6911            .insert(Row::new(vec![Value::Int(1)]))
6912            .unwrap();
6913        assert_eq!(cat.get("a").unwrap().row_count(), 1);
6914        assert_eq!(cat.get("b").unwrap().row_count(), 0);
6915    }
6916
6917    // --- v0.6 persistence round-trips --------------------------------------
6918
6919    fn assert_round_trip(cat: &Catalog) {
6920        let bytes = cat.serialize();
6921        let restored = Catalog::deserialize(&bytes).expect("deserialize");
6922        // Compare semantic state: same tables in same order, same schema +
6923        // rows in each.
6924        assert_eq!(restored.table_count(), cat.table_count());
6925        for (a, b) in cat.tables.iter().zip(restored.tables.iter()) {
6926            assert_eq!(a.schema, b.schema);
6927            assert_eq!(a.rows, b.rows);
6928        }
6929    }
6930
6931    #[test]
6932    fn serialize_empty_catalog_round_trips() {
6933        assert_round_trip(&Catalog::new());
6934    }
6935
6936    #[test]
6937    fn serialize_single_empty_table_round_trips() {
6938        let mut cat = Catalog::new();
6939        cat.create_table(make_users_schema()).unwrap();
6940        assert_round_trip(&cat);
6941    }
6942
6943    #[test]
6944    fn nsw_clone_is_o1() {
6945        // v5.5.0: NswGraph::clone must be O(1) structural sharing, not the
6946        // pre-v5.5 O(N) element copy — it rides on Catalog::clone for every
6947        // group-commit write on a vector table. Build a non-trivial multi-
6948        // layer graph, clone it, and prove the clone shares the very same PV
6949        // storage (root+tail Arc) for `levels` and every `layers[l]`. Sharing
6950        // ⇒ no per-node element copy ⇒ clone cost independent of N (node
6951        // count); only the outer layer Vec (len ≤ 8) is copied, O(1) in
6952        // practice.
6953        let mut cat = Catalog::new();
6954        cat.create_table(TableSchema::new(
6955            "docs",
6956            alloc::vec![
6957                ColumnSchema::new("id", DataType::Int, false),
6958                ColumnSchema::new(
6959                    "v",
6960                    DataType::Vector {
6961                        dim: 3,
6962                        encoding: VecEncoding::F32
6963                    },
6964                    true
6965                ),
6966            ],
6967        ))
6968        .unwrap();
6969        let t = cat.get_mut("docs").unwrap();
6970        for i in 0..1500_i32 {
6971            #[allow(clippy::cast_precision_loss)] // 0..1500 — no precision lost
6972            let base = (i as f32) * 0.01;
6973            t.insert(Row::new(alloc::vec![
6974                Value::Int(i),
6975                Value::Vector(alloc::vec![base, base + 0.05, base + 0.1]),
6976            ]))
6977            .unwrap();
6978        }
6979        t.add_nsw_index("docs_nsw".into(), "v", NSW_DEFAULT_M)
6980            .unwrap();
6981        let g = match &cat.get("docs").unwrap().indices()[0].kind {
6982            IndexKind::Nsw(g) => g,
6983            IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
6984                panic!("expected NSW")
6985            }
6986        };
6987        // Non-trivial graph: one level slot per row, and the geometric level
6988        // distribution puts some nodes above layer 0.
6989        assert_eq!(g.levels.len(), 1500, "one level slot per inserted row");
6990        assert!(
6991            g.layers.len() >= 2,
6992            "1500 nodes should populate at least two HNSW layers, got {}",
6993            g.layers.len()
6994        );
6995
6996        let cloned = g.clone();
6997
6998        assert!(
6999            g.levels.shares_storage_with(&cloned.levels),
7000            "levels PV not shared after clone — clone copied elements (O(N))"
7001        );
7002        assert_eq!(g.layers.len(), cloned.layers.len());
7003        for (l, (orig, cl)) in g.layers.iter().zip(cloned.layers.iter()).enumerate() {
7004            assert!(
7005                orig.shares_storage_with(cl),
7006                "layer {l} PV not shared after clone — clone copied elements (O(N))"
7007            );
7008        }
7009    }
7010
7011    #[test]
7012    fn sq8_catalog_serialise_roundtrip_preserves_cells_and_index() {
7013        // v6.0.1 step 6 verify: a catalog with an `VECTOR(N)
7014        // USING SQ8` column + NSW index survives a full
7015        // serialise → deserialise cycle. Cells re-decode bit-
7016        // identically (per-vector affine triple), the NSW
7017        // topology stays intact, and kNN search still routes
7018        // through the SQ8 ADC dispatcher after the catalog hop.
7019        let mut cat = Catalog::new();
7020        cat.create_table(TableSchema::new(
7021            "vecs",
7022            alloc::vec![
7023                ColumnSchema::new("id", DataType::Int, false),
7024                ColumnSchema::new(
7025                    "v",
7026                    DataType::Vector {
7027                        dim: 8,
7028                        encoding: VecEncoding::Sq8,
7029                    },
7030                    false,
7031                ),
7032            ],
7033        ))
7034        .unwrap();
7035        let t = cat.get_mut("vecs").unwrap();
7036        for i in 0..32_i32 {
7037            #[allow(clippy::cast_precision_loss)]
7038            let base = (i as f32) * 0.03;
7039            let v: Vec<f32> = (0..8_i32)
7040                .map(|j| {
7041                    #[allow(clippy::cast_precision_loss)]
7042                    let off = (j as f32) * 0.01;
7043                    base + off
7044                })
7045                .collect();
7046            t.insert(Row::new(alloc::vec![
7047                Value::Int(i),
7048                Value::Sq8Vector(quantize::quantize(&v)),
7049            ]))
7050            .unwrap();
7051        }
7052        t.add_nsw_index("v_idx".into(), "v", NSW_DEFAULT_M).unwrap();
7053        // Capture a pre-serialise reference cell + nsw hits to
7054        // compare against the restored catalog.
7055        let query = alloc::vec![0.15_f32, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22];
7056        let (before_cell, before_ty, before_hits) = {
7057            let t_ref = cat.get("vecs").unwrap();
7058            (
7059                t_ref.rows()[5].values[1].clone(),
7060                t_ref.schema().columns[1].ty,
7061                nsw_query(t_ref, "v_idx", &query, 5, NswMetric::L2),
7062            )
7063        };
7064
7065        let bytes = cat.serialize();
7066        let restored = Catalog::deserialize(&bytes).expect("deserialize ok");
7067        let rt = restored.get("vecs").unwrap();
7068        assert_eq!(rt.schema().columns[1].ty, before_ty);
7069        assert_eq!(rt.rows()[5].values[1], before_cell);
7070        let after_hits = nsw_query(rt, "v_idx", &query, 5, NswMetric::L2);
7071        assert_eq!(before_hits, after_hits);
7072    }
7073
7074    #[test]
7075    fn half_catalog_serialise_roundtrip_preserves_cells_and_index() {
7076        // v6.0.3 step 4 verify: a catalog with a `VECTOR(N) USING
7077        // HALF` column + NSW index survives a full serialise →
7078        // deserialise cycle. Cells re-decode bit-identically (raw
7079        // u16 LE bytes), the NSW topology stays intact, and kNN
7080        // search still returns the same hit IDs against the
7081        // restored catalog.
7082        use crate::halfvec;
7083        let mut cat = Catalog::new();
7084        cat.create_table(TableSchema::new(
7085            "vecs",
7086            alloc::vec![
7087                ColumnSchema::new("id", DataType::Int, false),
7088                ColumnSchema::new(
7089                    "v",
7090                    DataType::Vector {
7091                        dim: 8,
7092                        encoding: VecEncoding::F16,
7093                    },
7094                    false,
7095                ),
7096            ],
7097        ))
7098        .unwrap();
7099        let t = cat.get_mut("vecs").unwrap();
7100        for i in 0..32_i32 {
7101            #[allow(clippy::cast_precision_loss)]
7102            let base = (i as f32) * 0.03;
7103            let v: Vec<f32> = (0..8_i32)
7104                .map(|j| {
7105                    #[allow(clippy::cast_precision_loss)]
7106                    let off = (j as f32) * 0.01;
7107                    base + off
7108                })
7109                .collect();
7110            t.insert(Row::new(alloc::vec![
7111                Value::Int(i),
7112                Value::HalfVector(halfvec::HalfVector::from_f32_slice(&v)),
7113            ]))
7114            .unwrap();
7115        }
7116        t.add_nsw_index("v_idx".into(), "v", NSW_DEFAULT_M).unwrap();
7117        let query = alloc::vec![0.15_f32, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22];
7118        let (before_cell, before_ty, before_hits) = {
7119            let t_ref = cat.get("vecs").unwrap();
7120            (
7121                t_ref.rows()[5].values[1].clone(),
7122                t_ref.schema().columns[1].ty,
7123                nsw_query(t_ref, "v_idx", &query, 5, NswMetric::L2),
7124            )
7125        };
7126        let bytes = cat.serialize();
7127        let restored = Catalog::deserialize(&bytes).expect("deserialize ok");
7128        let rt = restored.get("vecs").unwrap();
7129        assert_eq!(rt.schema().columns[1].ty, before_ty);
7130        assert_eq!(rt.rows()[5].values[1], before_cell);
7131        let after_hits = nsw_query(rt, "v_idx", &query, 5, NswMetric::L2);
7132        assert_eq!(before_hits, after_hits);
7133    }
7134
7135    #[test]
7136    #[allow(clippy::similar_names)]
7137    fn hnsw_half_recall_at_10_matches_f32_groundtruth() {
7138        // v6.0.3 step 3 verify: HALF column NSW retrieves ≥ 95%
7139        // top-10 overlap vs brute-force F32 ground truth.
7140        // Half-precision dequantises bit-exactly at the storage
7141        // layer (no rerank pass), so the recall floor is tighter
7142        // than the SQ8 case — only the rounding noise from f32 →
7143        // f16 quantisation contributes.
7144        use crate::halfvec;
7145        fn next(state: &mut u64) -> f32 {
7146            *state = state
7147                .wrapping_add(0x9E37_79B9_7F4A_7C15)
7148                .wrapping_mul(0xBF58_476D_1CE4_E5B9);
7149            #[allow(clippy::cast_precision_loss)]
7150            let u = ((*state >> 32) as u32 as f32) / (u32::MAX as f32);
7151            2.0 * u - 1.0
7152        }
7153        let dim: u32 = 32;
7154        let n: usize = 512;
7155        let dim_us = dim as usize;
7156        let mut seed: u64 = 0xF16_F16_F16_F16_u64;
7157        let corpus: Vec<Vec<f32>> = (0..n)
7158            .map(|_| (0..dim_us).map(|_| next(&mut seed)).collect())
7159            .collect();
7160        let queries: Vec<Vec<f32>> = (0..32)
7161            .map(|_| (0..dim_us).map(|_| next(&mut seed)).collect())
7162            .collect();
7163        let exact_top10: Vec<Vec<usize>> = queries
7164            .iter()
7165            .map(|q| {
7166                let mut scored: Vec<(f32, usize)> = corpus
7167                    .iter()
7168                    .enumerate()
7169                    .map(|(i, v)| (l2_distance_sq(v, q), i))
7170                    .collect();
7171                scored.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(core::cmp::Ordering::Equal));
7172                scored.into_iter().take(10).map(|(_, i)| i).collect()
7173            })
7174            .collect();
7175        let mut cat = Catalog::new();
7176        cat.create_table(TableSchema::new(
7177            "vecs",
7178            alloc::vec![
7179                ColumnSchema::new("id", DataType::Int, false),
7180                ColumnSchema::new(
7181                    "v",
7182                    DataType::Vector {
7183                        dim,
7184                        encoding: VecEncoding::F16,
7185                    },
7186                    false,
7187                ),
7188            ],
7189        ))
7190        .unwrap();
7191        let t = cat.get_mut("vecs").unwrap();
7192        for (i, v) in corpus.iter().enumerate() {
7193            t.insert(Row::new(alloc::vec![
7194                Value::Int(i32::try_from(i).unwrap()),
7195                Value::HalfVector(halfvec::HalfVector::from_f32_slice(v)),
7196            ]))
7197            .unwrap();
7198        }
7199        t.add_nsw_index("v_idx".into(), "v", NSW_DEFAULT_M).unwrap();
7200        let table = cat.get("vecs").unwrap();
7201        let mut total_overlap = 0_usize;
7202        for (q, exact) in queries.iter().zip(exact_top10.iter()) {
7203            let hits = nsw_query(table, "v_idx", q, 10, NswMetric::L2);
7204            for h in &hits {
7205                if exact.contains(h) {
7206                    total_overlap += 1;
7207                }
7208            }
7209        }
7210        #[allow(clippy::cast_precision_loss)]
7211        let recall = total_overlap as f32 / (10.0 * queries.len() as f32);
7212        assert!(
7213            recall >= 0.95,
7214            "HALF HNSW recall@10 = {recall:.3}, below floor 0.95 — \
7215             check halfvec dispatch in `cell_to_query_metric_distance`"
7216        );
7217    }
7218
7219    #[test]
7220    fn hnsw_sq8_recall_at_10_above_0_95_vs_f32_groundtruth() {
7221        // v6.0.1 step 5 verify: build TWO catalogs over the same
7222        // corpus — one F32, one SQ8 — and confirm SQ8 NSW + f32
7223        // rerank retrieves ≥ 95% top-10 overlap vs brute-force F32
7224        // ground truth. The rerank pass (sq8_rerank) re-scores ADC
7225        // candidates with dequantised cells, recovering recall the
7226        // raw ADC sacrifices for 4× compression.
7227        use crate::quantize;
7228        // Deterministic Gaussian-ish corpus via splitmix64. Vectors
7229        // get normalised so SQ8's per-vector `(min, max)` lives in
7230        // a sensible range; matches the v6.0.0 fuzz harness.
7231        fn next(state: &mut u64) -> f32 {
7232            *state = state
7233                .wrapping_add(0x9E37_79B9_7F4A_7C15)
7234                .wrapping_mul(0xBF58_476D_1CE4_E5B9);
7235            #[allow(clippy::cast_precision_loss)]
7236            let u = ((*state >> 32) as u32 as f32) / (u32::MAX as f32);
7237            2.0 * u - 1.0
7238        }
7239        let dim: u32 = 32;
7240        let n: usize = 512;
7241        let dim_us = dim as usize;
7242        let mut seed: u64 = 0xCAFE_BABE_DEAD_BEEFu64;
7243        let corpus: Vec<Vec<f32>> = (0..n)
7244            .map(|_| (0..dim_us).map(|_| next(&mut seed)).collect())
7245            .collect();
7246        let queries: Vec<Vec<f32>> = (0..32)
7247            .map(|_| (0..dim_us).map(|_| next(&mut seed)).collect())
7248            .collect();
7249        // F32 ground truth — pure exact arithmetic, brute force.
7250        let exact_top10: Vec<Vec<usize>> = queries
7251            .iter()
7252            .map(|q| {
7253                let mut scored: Vec<(f32, usize)> = corpus
7254                    .iter()
7255                    .enumerate()
7256                    .map(|(i, v)| (l2_distance_sq(v, q), i))
7257                    .collect();
7258                scored.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(core::cmp::Ordering::Equal));
7259                scored.into_iter().take(10).map(|(_, i)| i).collect()
7260            })
7261            .collect();
7262        // SQ8 catalog — INSERTs land as `Value::Sq8Vector` cells;
7263        // HNSW build uses the ADC path verified in step 4.
7264        let mut cat = Catalog::new();
7265        cat.create_table(TableSchema::new(
7266            "vecs",
7267            alloc::vec![
7268                ColumnSchema::new("id", DataType::Int, false),
7269                ColumnSchema::new(
7270                    "v",
7271                    DataType::Vector {
7272                        dim,
7273                        encoding: VecEncoding::Sq8,
7274                    },
7275                    false,
7276                ),
7277            ],
7278        ))
7279        .unwrap();
7280        let t = cat.get_mut("vecs").unwrap();
7281        for (i, v) in corpus.iter().enumerate() {
7282            t.insert(Row::new(alloc::vec![
7283                Value::Int(i32::try_from(i).unwrap()),
7284                Value::Sq8Vector(quantize::quantize(v)),
7285            ]))
7286            .unwrap();
7287        }
7288        t.add_nsw_index("v_idx".into(), "v", NSW_DEFAULT_M).unwrap();
7289        let table = cat.get("vecs").unwrap();
7290        let mut total_overlap = 0_usize;
7291        for (q, exact) in queries.iter().zip(exact_top10.iter()) {
7292            let hits = nsw_query(table, "v_idx", q, 10, NswMetric::L2);
7293            for h in &hits {
7294                if exact.contains(h) {
7295                    total_overlap += 1;
7296                }
7297            }
7298        }
7299        #[allow(clippy::cast_precision_loss)]
7300        let recall = total_overlap as f32 / (10.0 * queries.len() as f32);
7301        assert!(
7302            recall >= 0.95,
7303            "SQ8 HNSW recall@10 = {recall:.3}, below floor 0.95 — \
7304             check `sq8_rerank` is wired in `nsw_search` for SQ8 columns"
7305        );
7306    }
7307
7308    #[test]
7309    fn nsw_index_topology_persists_through_round_trip() {
7310        // Build an NSW index, capture its (entry, neighbors) tuple, do
7311        // a full serialize → deserialize, and verify the restored
7312        // graph is byte-for-byte identical. The point of v2.7 is that
7313        // startup skips the rebuild, so the topology has to survive
7314        // the disk hop.
7315        let mut cat = Catalog::new();
7316        cat.create_table(TableSchema::new(
7317            "docs",
7318            alloc::vec![
7319                ColumnSchema::new("id", DataType::Int, false),
7320                ColumnSchema::new(
7321                    "v",
7322                    DataType::Vector {
7323                        dim: 3,
7324                        encoding: VecEncoding::F32
7325                    },
7326                    true
7327                ),
7328            ],
7329        ))
7330        .unwrap();
7331        let t = cat.get_mut("docs").unwrap();
7332        for i in 0..6_i32 {
7333            #[allow(clippy::cast_precision_loss)] // 0..6 — no precision lost
7334            let base = (i as f32) * 0.1;
7335            let row = Row::new(alloc::vec![
7336                Value::Int(i),
7337                Value::Vector(alloc::vec![base, base + 0.05, base + 0.1]),
7338            ]);
7339            t.insert(row).unwrap();
7340        }
7341        t.add_nsw_index("docs_nsw".into(), "v", NSW_DEFAULT_M)
7342            .unwrap();
7343        let original = match &cat.get("docs").unwrap().indices()[0].kind {
7344            IndexKind::Nsw(g) => g.clone(),
7345            IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
7346                panic!("expected NSW")
7347            }
7348        };
7349        let bytes = cat.serialize();
7350        let restored = Catalog::deserialize(&bytes).expect("deserialize");
7351        let restored_graph = match &restored.get("docs").unwrap().indices()[0].kind {
7352            IndexKind::Nsw(g) => g.clone(),
7353            IndexKind::BTree(_) | IndexKind::Brin { .. } | IndexKind::Gin(_) => {
7354                panic!("expected NSW")
7355            }
7356        };
7357        assert_eq!(restored_graph.m, original.m);
7358        assert_eq!(restored_graph.m_max_0, original.m_max_0);
7359        assert_eq!(restored_graph.entry, original.entry);
7360        assert_eq!(restored_graph.entry_level, original.entry_level);
7361        assert_eq!(restored_graph.levels, original.levels);
7362        assert_eq!(restored_graph.layers, original.layers);
7363    }
7364
7365    #[test]
7366    fn hnsw_level_assignment_is_deterministic() {
7367        // Same row index always produces the same level — the topology
7368        // must be reproducible (matters for serialize round-trip).
7369        for i in 0..32usize {
7370            assert_eq!(nsw_assign_level(i), nsw_assign_level(i));
7371        }
7372    }
7373
7374    #[test]
7375    fn hnsw_layer_0_dominates_population() {
7376        // Sanity: out of N inserts, the vast majority should land on
7377        // layer 0. The 4-bit-clear promotion rule gives roughly 1/16
7378        // promotion to layer ≥ 1, so under 50 nodes we expect ~3 on
7379        // layer ≥ 1 and the rest on layer 0.
7380        let on_zero = (0..200usize).filter(|&i| nsw_assign_level(i) == 0).count();
7381        assert!(on_zero > 150, "level-0 nodes too few: {on_zero}");
7382    }
7383
7384    #[test]
7385    fn hnsw_search_matches_brute_force_for_l2_top1() {
7386        // Build a small dataset, query it, and confirm the top result
7387        // matches the brute-force nearest by L2. Topology variability
7388        // shouldn't break recall at k=1 for well-separated vectors.
7389        let mut cat = Catalog::new();
7390        cat.create_table(TableSchema::new(
7391            "vecs",
7392            alloc::vec![
7393                ColumnSchema::new("id", DataType::Int, false),
7394                ColumnSchema::new(
7395                    "v",
7396                    DataType::Vector {
7397                        dim: 3,
7398                        encoding: VecEncoding::F32
7399                    },
7400                    true
7401                ),
7402            ],
7403        ))
7404        .unwrap();
7405        let t = cat.get_mut("vecs").unwrap();
7406        let dataset: alloc::vec::Vec<(i32, [f32; 3])> = alloc::vec![
7407            (1, [0.0, 0.0, 0.0]),
7408            (2, [1.0, 0.0, 0.0]),
7409            (3, [0.0, 1.0, 0.0]),
7410            (4, [0.0, 0.0, 1.0]),
7411            (5, [1.0, 1.0, 0.0]),
7412            (6, [1.0, 0.0, 1.0]),
7413            (7, [0.0, 1.0, 1.0]),
7414            (8, [1.0, 1.0, 1.0]),
7415            (9, [0.5, 0.5, 0.5]),
7416            (10, [0.2, 0.8, 0.5]),
7417        ];
7418        for &(id, v) in &dataset {
7419            t.insert(Row::new(alloc::vec![
7420                Value::Int(id),
7421                Value::Vector(alloc::vec![v[0], v[1], v[2]]),
7422            ]))
7423            .unwrap();
7424        }
7425        t.add_nsw_index("v_idx".into(), "v", NSW_DEFAULT_M).unwrap();
7426        let idx_pos = cat
7427            .get("vecs")
7428            .unwrap()
7429            .indices()
7430            .iter()
7431            .position(|i| i.name == "v_idx")
7432            .unwrap();
7433        for query in [[0.4, 0.4, 0.4], [0.9, 0.1, 0.0], [0.0, 0.9, 0.9]] {
7434            let table = cat.get("vecs").unwrap();
7435            let hnsw_top = nsw_search(table, idx_pos, &query, 1, 16, NswMetric::L2);
7436            let mut brute: alloc::vec::Vec<(f32, usize)> = (0..table.rows.len())
7437                .map(|i| {
7438                    let Value::Vector(v) = &table.rows[i].values[1] else {
7439                        return (f32::INFINITY, i);
7440                    };
7441                    (l2_distance_sq(v, &query), i)
7442                })
7443                .collect();
7444            brute.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(core::cmp::Ordering::Equal));
7445            assert!(!hnsw_top.is_empty(), "HNSW returned no results");
7446            assert_eq!(
7447                hnsw_top[0].1, brute[0].1,
7448                "HNSW top-1 != brute-force top-1 for {query:?}"
7449            );
7450        }
7451    }
7452
7453    #[test]
7454    fn serialize_table_with_rows_round_trips() {
7455        let mut cat = Catalog::new();
7456        cat.create_table(make_users_schema()).unwrap();
7457        let t = cat.get_mut("users").unwrap();
7458        t.insert(Row::new(vec![
7459            Value::Int(1),
7460            Value::Text("alice".into()),
7461            Value::Float(95.5),
7462        ]))
7463        .unwrap();
7464        t.insert(Row::new(vec![
7465            Value::Int(2),
7466            Value::Text("bob".into()),
7467            Value::Null,
7468        ]))
7469        .unwrap();
7470        assert_round_trip(&cat);
7471    }
7472
7473    #[test]
7474    fn serialize_multiple_tables_round_trips() {
7475        let mut cat = Catalog::new();
7476        cat.create_table(make_users_schema()).unwrap();
7477        cat.create_table(TableSchema::new(
7478            "flags",
7479            vec![
7480                ColumnSchema::new("id", DataType::BigInt, false),
7481                ColumnSchema::new("active", DataType::Bool, false),
7482            ],
7483        ))
7484        .unwrap();
7485        cat.get_mut("flags")
7486            .unwrap()
7487            .insert(Row::new(vec![Value::BigInt(7), Value::Bool(true)]))
7488            .unwrap();
7489        assert_round_trip(&cat);
7490    }
7491
7492    #[test]
7493    fn deserialize_rejects_bad_magic() {
7494        let mut buf = b"BADMAGIC".to_vec();
7495        buf.push(FILE_VERSION);
7496        buf.extend_from_slice(&0u32.to_le_bytes());
7497        let err = Catalog::deserialize(&buf).unwrap_err();
7498        assert!(matches!(err, StorageError::Corrupt(_)));
7499    }
7500
7501    #[test]
7502    fn deserialize_rejects_unsupported_version() {
7503        let mut buf = FILE_MAGIC.to_vec();
7504        buf.push(99); // future version
7505        buf.extend_from_slice(&0u32.to_le_bytes());
7506        let err = Catalog::deserialize(&buf).unwrap_err();
7507        assert!(matches!(err, StorageError::Corrupt(ref s) if s.contains("version")));
7508    }
7509
7510    #[test]
7511    fn deserialize_rejects_truncated_file() {
7512        let mut cat = Catalog::new();
7513        cat.create_table(make_users_schema()).unwrap();
7514        let bytes = cat.serialize();
7515        // Drop the last byte to simulate truncation.
7516        let truncated = &bytes[..bytes.len() - 1];
7517        assert!(matches!(
7518            Catalog::deserialize(truncated),
7519            Err(StorageError::Corrupt(_))
7520        ));
7521    }
7522
7523    #[test]
7524    fn deserialize_rejects_trailing_garbage() {
7525        let cat = Catalog::new();
7526        let mut bytes = cat.serialize();
7527        bytes.push(0xFF);
7528        assert!(matches!(
7529            Catalog::deserialize(&bytes),
7530            Err(StorageError::Corrupt(ref s)) if s.contains("trailing")
7531        ));
7532    }
7533
7534    // --- v0.8 indices ------------------------------------------------------
7535
7536    fn populated_users() -> Catalog {
7537        let mut cat = Catalog::new();
7538        cat.create_table(make_users_schema()).unwrap();
7539        let t = cat.get_mut("users").unwrap();
7540        for (id, name, score) in [
7541            (1, "alice", Some(90.0)),
7542            (2, "bob", None),
7543            (3, "alice", Some(70.0)), // duplicate name → maps to two row idxs
7544        ] {
7545            t.insert(Row::new(vec![
7546                Value::Int(id),
7547                Value::Text(name.into()),
7548                score.map_or(Value::Null, Value::Float),
7549            ]))
7550            .unwrap();
7551        }
7552        cat
7553    }
7554
7555    #[test]
7556    fn add_index_builds_from_existing_rows() {
7557        let mut cat = populated_users();
7558        cat.get_mut("users")
7559            .unwrap()
7560            .add_index("by_id".into(), "id")
7561            .unwrap();
7562        let t = cat.get("users").unwrap();
7563        let idx = t.index_on(0).expect("index_on(0)");
7564        assert_eq!(idx.lookup_eq(&IndexKey::Int(2)), &[RowLocator::Hot(1)]);
7565        assert_eq!(idx.lookup_eq(&IndexKey::Int(99)), &[] as &[RowLocator]);
7566    }
7567
7568    #[test]
7569    fn add_index_dup_name_rejected() {
7570        let mut cat = populated_users();
7571        let t = cat.get_mut("users").unwrap();
7572        t.add_index("ix".into(), "id").unwrap();
7573        let err = t.add_index("ix".into(), "name").unwrap_err();
7574        assert!(matches!(err, StorageError::DuplicateIndex { ref name } if name == "ix"));
7575    }
7576
7577    #[test]
7578    fn add_index_unknown_column_rejected() {
7579        let mut cat = populated_users();
7580        let err = cat
7581            .get_mut("users")
7582            .unwrap()
7583            .add_index("ix".into(), "ghost")
7584            .unwrap_err();
7585        assert!(matches!(err, StorageError::ColumnNotFound { ref column } if column == "ghost"));
7586    }
7587
7588    #[test]
7589    fn insert_after_create_index_updates_it() {
7590        let mut cat = populated_users();
7591        let t = cat.get_mut("users").unwrap();
7592        t.add_index("by_name".into(), "name").unwrap();
7593        t.insert(Row::new(vec![
7594            Value::Int(4),
7595            Value::Text("dave".into()),
7596            Value::Null,
7597        ]))
7598        .unwrap();
7599        let idx = t.index_on(1).unwrap();
7600        assert_eq!(
7601            idx.lookup_eq(&IndexKey::Text("dave".into())),
7602            &[RowLocator::Hot(3)]
7603        );
7604        // Pre-existing duplicates remain mapped to the two original row idxs.
7605        assert_eq!(
7606            idx.lookup_eq(&IndexKey::Text("alice".into())),
7607            &[RowLocator::Hot(0), RowLocator::Hot(2)]
7608        );
7609    }
7610
7611    #[test]
7612    fn null_or_float_values_are_not_indexed() {
7613        let mut cat = populated_users();
7614        let t = cat.get_mut("users").unwrap();
7615        t.add_index("by_score".into(), "score").unwrap();
7616        let idx = t.index_on(2).unwrap();
7617        // bob's score is NULL → no entry for bob.
7618        // Score is Float → the spec says we don't index NaN-prone columns,
7619        // so even the present scores are absent. Lookups via IndexKey::Int(90)
7620        // mis-match the column type and trivially find nothing.
7621        assert_eq!(idx.lookup_eq(&IndexKey::Int(90)), &[] as &[RowLocator]);
7622    }
7623
7624    // --- v0.11 vector type -------------------------------------------------
7625
7626    #[test]
7627    fn vector_value_data_type_carries_dim() {
7628        let v = Value::Vector(vec![1.0, 2.0, 3.0]);
7629        assert_eq!(
7630            v.data_type(),
7631            Some(DataType::Vector {
7632                dim: 3,
7633                encoding: VecEncoding::F32
7634            })
7635        );
7636    }
7637
7638    #[test]
7639    fn vector_column_insert_matching_dim_ok() {
7640        let mut cat = Catalog::new();
7641        cat.create_table(TableSchema::new(
7642            "emb",
7643            vec![ColumnSchema::new(
7644                "v",
7645                DataType::Vector {
7646                    dim: 3,
7647                    encoding: VecEncoding::F32,
7648                },
7649                false,
7650            )],
7651        ))
7652        .unwrap();
7653        cat.get_mut("emb")
7654            .unwrap()
7655            .insert(Row::new(vec![Value::Vector(vec![1.0, 2.0, 3.0])]))
7656            .unwrap();
7657    }
7658
7659    #[test]
7660    fn vector_column_insert_dim_mismatch_rejected() {
7661        let mut cat = Catalog::new();
7662        cat.create_table(TableSchema::new(
7663            "emb",
7664            vec![ColumnSchema::new(
7665                "v",
7666                DataType::Vector {
7667                    dim: 3,
7668                    encoding: VecEncoding::F32,
7669                },
7670                false,
7671            )],
7672        ))
7673        .unwrap();
7674        let err = cat
7675            .get_mut("emb")
7676            .unwrap()
7677            .insert(Row::new(vec![Value::Vector(vec![1.0, 2.0])]))
7678            .unwrap_err();
7679        assert!(matches!(err, StorageError::TypeMismatch { .. }));
7680    }
7681
7682    #[test]
7683    fn vector_value_survives_catalog_round_trip() {
7684        let mut cat = Catalog::new();
7685        cat.create_table(TableSchema::new(
7686            "emb",
7687            vec![
7688                ColumnSchema::new("id", DataType::Int, false),
7689                ColumnSchema::new(
7690                    "v",
7691                    DataType::Vector {
7692                        dim: 4,
7693                        encoding: VecEncoding::F32,
7694                    },
7695                    false,
7696                ),
7697            ],
7698        ))
7699        .unwrap();
7700        cat.get_mut("emb")
7701            .unwrap()
7702            .insert(Row::new(vec![
7703                Value::Int(1),
7704                Value::Vector(vec![0.5, -1.25, 3.0, 7.0]),
7705            ]))
7706            .unwrap();
7707        let restored = Catalog::deserialize(&cat.serialize()).expect("round-trip");
7708        let table = restored.get("emb").unwrap();
7709        assert_eq!(
7710            table.schema().columns[1].ty,
7711            DataType::Vector {
7712                dim: 4,
7713                encoding: VecEncoding::F32
7714            }
7715        );
7716        assert_eq!(
7717            table.rows()[0].values[1],
7718            Value::Vector(vec![0.5, -1.25, 3.0, 7.0])
7719        );
7720    }
7721
7722    #[test]
7723    fn index_survives_serialize_deserialize_round_trip() {
7724        let mut cat = populated_users();
7725        cat.get_mut("users")
7726            .unwrap()
7727            .add_index("by_name".into(), "name")
7728            .unwrap();
7729        let restored = Catalog::deserialize(&cat.serialize()).unwrap();
7730        let idx = restored
7731            .get("users")
7732            .unwrap()
7733            .index_on(1)
7734            .expect("index_on(1) after restore");
7735        assert_eq!(idx.name, "by_name");
7736        // Data was rebuilt from rows, not deserialized directly.
7737        assert_eq!(
7738            idx.lookup_eq(&IndexKey::Text("alice".into())),
7739            &[RowLocator::Hot(0), RowLocator::Hot(2)]
7740        );
7741    }
7742
7743    // --- v5.1 cold-tier integration tests ----------------------
7744
7745    /// Schema with a BIGINT PK column matching what the v5.1 cold-
7746    /// tier path supports (`IndexKey::Int` → `u64` cast).
7747    fn bigint_pk_users_schema() -> TableSchema {
7748        TableSchema::new(
7749            "users",
7750            vec![
7751                ColumnSchema::new("id", DataType::BigInt, false),
7752                ColumnSchema::new("name", DataType::Text, false),
7753            ],
7754        )
7755    }
7756
7757    fn make_user_row(id: i64, name: &str) -> Row {
7758        Row::new(vec![Value::BigInt(id), Value::Text(name.into())])
7759    }
7760
7761    #[test]
7762    fn lookup_by_pk_finds_row_via_hot_index() {
7763        let mut cat = Catalog::new();
7764        cat.create_table(bigint_pk_users_schema()).unwrap();
7765        let t = cat.get_mut("users").unwrap();
7766        for (id, name) in [(1i64, "alice"), (2, "bob"), (3, "carol")] {
7767            t.insert(make_user_row(id, name)).unwrap();
7768        }
7769        t.add_index("by_id".into(), "id").unwrap();
7770        // All locators are Hot; cold_segments is empty.
7771        let got = cat
7772            .lookup_by_pk("users", "by_id", &IndexKey::Int(2))
7773            .unwrap();
7774        assert_eq!(got, make_user_row(2, "bob"));
7775        assert_eq!(cat.cold_segment_count(), 0);
7776    }
7777
7778    #[test]
7779    fn lookup_by_pk_returns_none_when_key_missing() {
7780        let mut cat = Catalog::new();
7781        cat.create_table(bigint_pk_users_schema()).unwrap();
7782        let t = cat.get_mut("users").unwrap();
7783        t.insert(make_user_row(1, "alice")).unwrap();
7784        t.add_index("by_id".into(), "id").unwrap();
7785        assert!(
7786            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(999))
7787                .is_none()
7788        );
7789        // Also: unknown table / unknown index name.
7790        assert!(
7791            cat.lookup_by_pk("other_table", "by_id", &IndexKey::Int(1))
7792                .is_none()
7793        );
7794        assert!(
7795            cat.lookup_by_pk("users", "no_such_index", &IndexKey::Int(1))
7796                .is_none()
7797        );
7798    }
7799
7800    #[test]
7801    fn lookup_by_pk_resolves_cold_locator_via_loaded_segment() {
7802        // Build a cold-tier segment whose payloads are dense-encoded
7803        // BIGINT rows. Wire each PK into the BTree index as a Cold
7804        // locator. The hot tier carries no rows for those PKs.
7805        let mut cat = Catalog::new();
7806        cat.create_table(bigint_pk_users_schema()).unwrap();
7807        let t = cat.get_mut("users").unwrap();
7808        t.add_index("by_id".into(), "id").unwrap();
7809        let schema = t.schema.clone();
7810
7811        let cold_rows: Vec<(i64, &str)> =
7812            vec![(100, "ivy"), (200, "joe"), (300, "kim"), (400, "lin")];
7813        let seg_rows: Vec<(u64, Vec<u8>)> = cold_rows
7814            .iter()
7815            .map(|(id, name)| {
7816                let row = make_user_row(*id, name);
7817                ((*id).cast_unsigned(), encode_row_body_dense(&row, &schema))
7818            })
7819            .collect();
7820        let (seg_bytes, _meta) =
7821            encode_segment(seg_rows.into_iter(), 0.01, SEGMENT_PAGE_BYTES).unwrap();
7822        let seg_id = cat.load_segment_bytes(seg_bytes).unwrap();
7823        assert_eq!(seg_id, 0);
7824        assert_eq!(cat.cold_segment_count(), 1);
7825
7826        let pairs: Vec<(IndexKey, RowLocator)> = cold_rows
7827            .iter()
7828            .map(|(id, _)| {
7829                (
7830                    IndexKey::Int(*id),
7831                    RowLocator::Cold {
7832                        segment_id: seg_id,
7833                        page_offset: 0,
7834                    },
7835                )
7836            })
7837            .collect();
7838        let registered = cat
7839            .get_mut("users")
7840            .unwrap()
7841            .register_cold_locators("by_id", pairs)
7842            .unwrap();
7843        assert_eq!(registered, 4);
7844
7845        for (id, name) in &cold_rows {
7846            let got = cat
7847                .lookup_by_pk("users", "by_id", &IndexKey::Int(*id))
7848                .unwrap_or_else(|| panic!("cold key {id} not found"));
7849            assert_eq!(got, make_user_row(*id, name));
7850        }
7851        // Cold key that isn't in the segment must return None.
7852        assert!(
7853            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(999))
7854                .is_none()
7855        );
7856    }
7857
7858    #[test]
7859    fn lookup_by_pk_mixes_hot_and_cold_tiers() {
7860        // Half the rows live in the hot tier (Table::rows + add_index
7861        // produces Hot locators); half live in a cold segment and have
7862        // Cold locators wired manually. Each lookup hits the right tier.
7863        let mut cat = Catalog::new();
7864        cat.create_table(bigint_pk_users_schema()).unwrap();
7865        let t = cat.get_mut("users").unwrap();
7866        for (id, name) in [(1i64, "alice"), (2, "bob")] {
7867            t.insert(make_user_row(id, name)).unwrap();
7868        }
7869        t.add_index("by_id".into(), "id").unwrap();
7870        let schema = t.schema.clone();
7871
7872        let cold_rows: Vec<(i64, &str)> = vec![(100, "ivy"), (200, "joe")];
7873        let seg_rows: Vec<(u64, Vec<u8>)> = cold_rows
7874            .iter()
7875            .map(|(id, name)| {
7876                let row = make_user_row(*id, name);
7877                ((*id).cast_unsigned(), encode_row_body_dense(&row, &schema))
7878            })
7879            .collect();
7880        let (seg_bytes, _) =
7881            encode_segment(seg_rows.into_iter(), 0.01, SEGMENT_PAGE_BYTES).unwrap();
7882        let seg_id = cat.load_segment_bytes(seg_bytes).unwrap();
7883        let pairs: Vec<(IndexKey, RowLocator)> = cold_rows
7884            .iter()
7885            .map(|(id, _)| {
7886                (
7887                    IndexKey::Int(*id),
7888                    RowLocator::Cold {
7889                        segment_id: seg_id,
7890                        page_offset: 0,
7891                    },
7892                )
7893            })
7894            .collect();
7895        cat.get_mut("users")
7896            .unwrap()
7897            .register_cold_locators("by_id", pairs)
7898            .unwrap();
7899
7900        // Hot tier hits.
7901        assert_eq!(
7902            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(1))
7903                .unwrap(),
7904            make_user_row(1, "alice")
7905        );
7906        assert_eq!(
7907            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(2))
7908                .unwrap(),
7909            make_user_row(2, "bob")
7910        );
7911        // Cold tier hits.
7912        assert_eq!(
7913            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(100))
7914                .unwrap(),
7915            make_user_row(100, "ivy")
7916        );
7917        assert_eq!(
7918            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(200))
7919                .unwrap(),
7920            make_user_row(200, "joe")
7921        );
7922        // Miss in both tiers.
7923        assert!(
7924            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(50))
7925                .is_none()
7926        );
7927    }
7928
7929    #[test]
7930    fn register_cold_locators_rejects_nsw_index() {
7931        let mut cat = Catalog::new();
7932        cat.create_table(TableSchema::new(
7933            "vecs",
7934            vec![
7935                ColumnSchema::new("id", DataType::Int, false),
7936                ColumnSchema::new(
7937                    "v",
7938                    DataType::Vector {
7939                        dim: 4,
7940                        encoding: VecEncoding::F32,
7941                    },
7942                    false,
7943                ),
7944            ],
7945        ))
7946        .unwrap();
7947        let t = cat.get_mut("vecs").unwrap();
7948        t.insert(Row::new(vec![
7949            Value::Int(1),
7950            Value::Vector(vec![1.0, 0.0, 0.0, 0.0]),
7951        ]))
7952        .unwrap();
7953        t.add_nsw_index("by_v".into(), "v", NSW_DEFAULT_M).unwrap();
7954        let err = t
7955            .register_cold_locators(
7956                "by_v",
7957                vec![(
7958                    IndexKey::Int(1),
7959                    RowLocator::Cold {
7960                        segment_id: 0,
7961                        page_offset: 0,
7962                    },
7963                )],
7964            )
7965            .unwrap_err();
7966        // v6.7.1: message switched from "is NSW" to "is not BTree"
7967        // when the Brin variant was added.
7968        assert!(matches!(err, StorageError::Corrupt(ref s) if s.contains("not BTree")));
7969    }
7970
7971    #[test]
7972    fn load_segment_bytes_rejects_garbage() {
7973        let mut cat = Catalog::new();
7974        let err = cat.load_segment_bytes(vec![0u8; 10]).unwrap_err();
7975        assert!(matches!(err, StorageError::Corrupt(ref s) if s.contains("segment")));
7976        // Loader doesn't mutate state on error.
7977        assert_eq!(cat.cold_segment_count(), 0);
7978    }
7979
7980    #[test]
7981    fn load_segment_bytes_returns_sequential_ids() {
7982        let mut cat = Catalog::new();
7983        cat.create_table(bigint_pk_users_schema()).unwrap();
7984        let schema = cat.get("users").unwrap().schema.clone();
7985        for batch in 0u32..3 {
7986            let rows: Vec<(u64, Vec<u8>)> = (0u64..4)
7987                .map(|i| {
7988                    let id = u64::from(batch) * 100 + i;
7989                    let row = make_user_row(id.cast_signed(), "x");
7990                    (id, encode_row_body_dense(&row, &schema))
7991                })
7992                .collect();
7993            let (bytes, _) = encode_segment(rows.into_iter(), 0.01, SEGMENT_PAGE_BYTES).unwrap();
7994            assert_eq!(cat.load_segment_bytes(bytes).unwrap(), batch);
7995        }
7996        assert_eq!(cat.cold_segment_count(), 3);
7997    }
7998
7999    // --- v5.2 catalog format v9 ----------------------------------
8000
8001    /// Hand-craft a v8 catalog byte stream and confirm the v9 reader
8002    /// accepts it and surfaces every `BTree` entry as a Hot locator.
8003    /// Guards the backward-compat read path: existing v3.0.2 / v4.x
8004    /// snapshots on disk must keep loading after the v5.2 bump.
8005    #[test]
8006    fn v8_catalog_decodes_as_all_hot_under_v9_reader() {
8007        // Build a populated catalog in memory, snapshot it with the
8008        // v9 serializer, then patch the version byte back to 8 and
8009        // strip the v9 BTree payload bytes so the layout matches what
8010        // a real v8 snapshot would have produced on disk. The v9
8011        // reader's version dispatch path then rebuilds the index
8012        // from rows (every locator becomes Hot).
8013        let mut cat = populated_users();
8014        cat.get_mut("users")
8015            .unwrap()
8016            .add_index("by_name".into(), "name")
8017            .unwrap();
8018
8019        // To produce a faithful v8 byte stream we re-encode the same
8020        // catalog with the v8 layout: identical bytes up to (and
8021        // including) the per-index kind tag, but no inline BTree
8022        // entries.
8023        let v8_bytes = encode_as_v8(&cat);
8024        assert_eq!(v8_bytes[FILE_MAGIC.len()], 8, "version byte must be 8");
8025
8026        let restored = Catalog::deserialize(&v8_bytes).expect("v9 reader accepts v8 stream");
8027        let idx = restored
8028            .get("users")
8029            .unwrap()
8030            .index_on(1)
8031            .expect("index_on(1) after restore");
8032        // v8 path always materialises Hot locators (no cold tier
8033        // existed pre-v5.2).
8034        assert_eq!(
8035            idx.lookup_eq(&IndexKey::Text("alice".into())),
8036            &[RowLocator::Hot(0), RowLocator::Hot(2)]
8037        );
8038        // No accidental Cold leak.
8039        for entry in idx.lookup_eq(&IndexKey::Text("alice".into())) {
8040            assert!(entry.is_hot(), "v8 → v9 read must yield Hot only");
8041        }
8042    }
8043
8044    /// Encode `cat` using the v8 layout (no inline `BTree` entries,
8045    /// version byte = 8). Pure test helper — duplicates just enough
8046    /// of `Catalog::serialize` to produce a faithful v8 stream that
8047    /// real v3.0.2 / v4.x deployments wrote.
8048    fn encode_as_v8(cat: &Catalog) -> Vec<u8> {
8049        let mut out = Vec::with_capacity(64);
8050        out.extend_from_slice(FILE_MAGIC);
8051        out.push(8u8);
8052        write_u32(&mut out, u32::try_from(cat.tables.len()).unwrap());
8053        for t in &cat.tables {
8054            write_str(&mut out, &t.schema.name);
8055            write_u16(&mut out, u16::try_from(t.schema.columns.len()).unwrap());
8056            for c in &t.schema.columns {
8057                write_str(&mut out, &c.name);
8058                write_data_type(&mut out, c.ty);
8059                out.push(u8::from(c.nullable));
8060                match &c.default {
8061                    None => out.push(0),
8062                    Some(v) => {
8063                        out.push(1);
8064                        write_value(&mut out, v);
8065                    }
8066                }
8067                out.push(u8::from(c.auto_increment));
8068            }
8069            write_u32(&mut out, u32::try_from(t.rows.len()).unwrap());
8070            for row in &t.rows {
8071                out.extend_from_slice(&encode_row_body_dense(row, &t.schema));
8072            }
8073            write_u16(&mut out, u16::try_from(t.indices.len()).unwrap());
8074            for idx in &t.indices {
8075                write_str(&mut out, &idx.name);
8076                write_u16(&mut out, u16::try_from(idx.column_position).unwrap());
8077                match &idx.kind {
8078                    // v8 BTree wrote only the kind tag; entries
8079                    // rebuild from rows on read.
8080                    IndexKind::BTree(_) => out.push(0),
8081                    IndexKind::Nsw(g) => {
8082                        out.push(1);
8083                        write_u16(&mut out, u16::try_from(g.m).unwrap());
8084                        write_nsw_graph(&mut out, g);
8085                    }
8086                    // v8 had no BRIN / GIN; this test-only writer
8087                    // can't serialise either into the legacy format.
8088                    IndexKind::Brin { .. } => panic!(
8089                        "v8 catalog writer cannot serialise BRIN — \
8090                         tests with BRIN indices must use the current writer"
8091                    ),
8092                    IndexKind::Gin(_) => panic!(
8093                        "v8 catalog writer cannot serialise GIN — \
8094                         tests with GIN indices must use the current writer"
8095                    ),
8096                }
8097            }
8098        }
8099        out
8100    }
8101
8102    /// Build a catalog that carries both hot and cold locators on a
8103    /// `BTree` index, snapshot it through `serialize`, then deserialise
8104    /// and confirm every Cold locator round-trips byte-identical and
8105    /// `lookup_by_pk` resolves through the rebuilt cold-segment
8106    /// registry.
8107    #[test]
8108    fn v9_catalog_round_trip_preserves_cold_locators() {
8109        let mut cat = Catalog::new();
8110        cat.create_table(bigint_pk_users_schema()).unwrap();
8111        let t = cat.get_mut("users").unwrap();
8112        // Hot rows: 1, 2
8113        for (id, name) in [(1i64, "alice"), (2, "bob")] {
8114            t.insert(make_user_row(id, name)).unwrap();
8115        }
8116        t.add_index("by_id".into(), "id").unwrap();
8117        let schema = t.schema.clone();
8118
8119        // Cold rows: 100, 200, 300 — sit in a single segment.
8120        let cold_rows: Vec<(i64, &str)> = vec![(100, "ivy"), (200, "joe"), (300, "kim")];
8121        let seg_rows: Vec<(u64, Vec<u8>)> = cold_rows
8122            .iter()
8123            .map(|(id, name)| {
8124                let row = make_user_row(*id, name);
8125                ((*id).cast_unsigned(), encode_row_body_dense(&row, &schema))
8126            })
8127            .collect();
8128        let (seg_bytes, _) =
8129            encode_segment(seg_rows.into_iter(), 0.01, SEGMENT_PAGE_BYTES).unwrap();
8130        let seg_id = cat.load_segment_bytes(seg_bytes.clone()).unwrap();
8131        let pairs: Vec<(IndexKey, RowLocator)> = cold_rows
8132            .iter()
8133            .map(|(id, _)| {
8134                (
8135                    IndexKey::Int(*id),
8136                    RowLocator::Cold {
8137                        segment_id: seg_id,
8138                        page_offset: 0,
8139                    },
8140                )
8141            })
8142            .collect();
8143        cat.get_mut("users")
8144            .unwrap()
8145            .register_cold_locators("by_id", pairs)
8146            .unwrap();
8147
8148        // Snapshot + restore via the v9 codec.
8149        let bytes = cat.serialize();
8150        assert_eq!(bytes[FILE_MAGIC.len()], FILE_VERSION);
8151        let mut restored = Catalog::deserialize(&bytes).expect("v9 round-trip parses");
8152
8153        // Catalog::serialize does not yet emit cold segment file
8154        // bytes (v5.3 manifest is the future home for that). For
8155        // this v9 test the caller side-loads the segment again so
8156        // lookup_by_pk can resolve the Cold locator. The point of
8157        // this assertion is that the locator metadata survived the
8158        // catalog round-trip.
8159        let restored_seg_id = restored.load_segment_bytes(seg_bytes).unwrap();
8160        assert_eq!(restored_seg_id, seg_id);
8161
8162        let idx = restored.get("users").unwrap().index_on(0).unwrap();
8163        // Hot locators round-trip.
8164        assert_eq!(idx.lookup_eq(&IndexKey::Int(1)), &[RowLocator::Hot(0)]);
8165        assert_eq!(idx.lookup_eq(&IndexKey::Int(2)), &[RowLocator::Hot(1)]);
8166        // Cold locators round-trip byte-identical.
8167        for (id, _) in &cold_rows {
8168            assert_eq!(
8169                idx.lookup_eq(&IndexKey::Int(*id)),
8170                &[RowLocator::Cold {
8171                    segment_id: seg_id,
8172                    page_offset: 0,
8173                }]
8174            );
8175        }
8176        // End-to-end: lookup_by_pk resolves both tiers.
8177        assert_eq!(
8178            restored
8179                .lookup_by_pk("users", "by_id", &IndexKey::Int(2))
8180                .unwrap(),
8181            make_user_row(2, "bob")
8182        );
8183        for (id, name) in &cold_rows {
8184            assert_eq!(
8185                restored
8186                    .lookup_by_pk("users", "by_id", &IndexKey::Int(*id))
8187                    .unwrap(),
8188                make_user_row(*id, name)
8189            );
8190        }
8191    }
8192
8193    // --- v5.2.1 hot tier byte tracking ---------------------------
8194
8195    /// `row_body_encoded_len` is the perf-critical fast path; pin it
8196    /// against `encode_row_body_dense(...).len()` for every
8197    /// representative cell type so an encoder change can't silently
8198    /// desync the counter.
8199    #[test]
8200    fn row_body_encoded_len_matches_actual_encode_for_all_types() {
8201        let schema = TableSchema::new(
8202            "wide",
8203            vec![
8204                ColumnSchema::new("a", DataType::SmallInt, true),
8205                ColumnSchema::new("b", DataType::Int, false),
8206                ColumnSchema::new("c", DataType::BigInt, false),
8207                ColumnSchema::new("d", DataType::Float, false),
8208                ColumnSchema::new("e", DataType::Bool, false),
8209                ColumnSchema::new("f", DataType::Text, false),
8210                ColumnSchema::new(
8211                    "g",
8212                    DataType::Vector {
8213                        dim: 3,
8214                        encoding: VecEncoding::F32,
8215                    },
8216                    false,
8217                ),
8218                ColumnSchema::new(
8219                    "h",
8220                    DataType::Numeric {
8221                        precision: 18,
8222                        scale: 2,
8223                    },
8224                    false,
8225                ),
8226                ColumnSchema::new("i", DataType::Date, false),
8227                ColumnSchema::new("j", DataType::Timestamp, false),
8228            ],
8229        );
8230        let cases: &[Row] = &[
8231            Row::new(vec![
8232                Value::SmallInt(7),
8233                Value::Int(42),
8234                Value::BigInt(1_000_000),
8235                Value::Float(1.5),
8236                Value::Bool(true),
8237                Value::Text("hello".into()),
8238                Value::Vector(vec![1.0, 2.0, 3.0]),
8239                Value::Numeric {
8240                    scaled: 12345,
8241                    scale: 2,
8242                },
8243                Value::Date(20_000),
8244                Value::Timestamp(1_700_000_000_000_000),
8245            ]),
8246            // NULL in the bitmap, varied text length.
8247            Row::new(vec![
8248                Value::Null,
8249                Value::Int(0),
8250                Value::BigInt(0),
8251                Value::Float(0.0),
8252                Value::Bool(false),
8253                Value::Text(String::new()),
8254                Value::Vector(vec![]),
8255                Value::Numeric {
8256                    scaled: 0,
8257                    scale: 2,
8258                },
8259                Value::Date(0),
8260                Value::Timestamp(0),
8261            ]),
8262            Row::new(vec![
8263                Value::SmallInt(-1),
8264                Value::Int(-1),
8265                Value::BigInt(-1),
8266                Value::Float(-0.5),
8267                Value::Bool(true),
8268                Value::Text("a much longer payload here".into()),
8269                Value::Vector(vec![0.1, 0.2, 0.3]),
8270                Value::Numeric {
8271                    scaled: -999_999_999,
8272                    scale: 2,
8273                },
8274                Value::Date(-1),
8275                Value::Timestamp(-1),
8276            ]),
8277        ];
8278        for row in cases {
8279            let actual = encode_row_body_dense(row, &schema).len();
8280            let fast = row_body_encoded_len(row, &schema);
8281            assert_eq!(actual, fast, "row {row:?}");
8282        }
8283    }
8284
8285    #[test]
8286    fn hot_bytes_grows_on_insert_and_matches_encoded_sum() {
8287        let mut cat = Catalog::new();
8288        cat.create_table(bigint_pk_users_schema()).unwrap();
8289        let t = cat.get_mut("users").unwrap();
8290        assert_eq!(t.hot_bytes(), 0);
8291        let mut expected: u64 = 0;
8292        for (id, name) in [(1i64, "alice"), (2, "bob"), (3, "carol")] {
8293            let row = make_user_row(id, name);
8294            expected += encode_row_body_dense(&row, &t.schema).len() as u64;
8295            t.insert(row).unwrap();
8296        }
8297        assert_eq!(t.hot_bytes(), expected);
8298        assert_eq!(cat.hot_tier_bytes(), expected);
8299    }
8300
8301    #[test]
8302    fn hot_bytes_shrinks_on_delete() {
8303        let mut cat = Catalog::new();
8304        cat.create_table(bigint_pk_users_schema()).unwrap();
8305        let t = cat.get_mut("users").unwrap();
8306        for (id, name) in [(1i64, "alice"), (2, "bob"), (3, "carol")] {
8307            t.insert(make_user_row(id, name)).unwrap();
8308        }
8309        let before = t.hot_bytes();
8310        // Delete row at position 1 (bob).
8311        let bob_row = make_user_row(2, "bob");
8312        let bob_bytes = encode_row_body_dense(&bob_row, &t.schema).len() as u64;
8313        let removed = t.delete_rows(&[1]);
8314        assert_eq!(removed, 1);
8315        assert_eq!(t.hot_bytes(), before - bob_bytes);
8316    }
8317
8318    #[test]
8319    fn hot_bytes_diffs_on_update_for_variable_width_columns() {
8320        let mut cat = Catalog::new();
8321        cat.create_table(bigint_pk_users_schema()).unwrap();
8322        let t = cat.get_mut("users").unwrap();
8323        t.insert(make_user_row(1, "alice")).unwrap();
8324        let after_insert = t.hot_bytes();
8325        // Update with a longer text payload — bytes must grow exactly
8326        // by the text-length delta.
8327        let new_row = make_user_row(1, "alice-the-longer-name");
8328        let old_len = encode_row_body_dense(&make_user_row(1, "alice"), &t.schema).len() as u64;
8329        let new_len = encode_row_body_dense(&new_row, &t.schema).len() as u64;
8330        t.update_row(0, new_row.values).unwrap();
8331        assert_eq!(t.hot_bytes(), after_insert - old_len + new_len);
8332        assert!(t.hot_bytes() > after_insert, "longer text grew the counter");
8333    }
8334
8335    #[test]
8336    fn hot_bytes_round_trips_through_serialize_deserialize() {
8337        let mut cat = Catalog::new();
8338        cat.create_table(bigint_pk_users_schema()).unwrap();
8339        let t = cat.get_mut("users").unwrap();
8340        for i in 0..10 {
8341            t.insert(make_user_row(i, &alloc::format!("name-{i}")))
8342                .unwrap();
8343        }
8344        let pre = cat.hot_tier_bytes();
8345        let restored = Catalog::deserialize(&cat.serialize()).unwrap();
8346        assert_eq!(restored.hot_tier_bytes(), pre);
8347        assert_eq!(restored.get("users").unwrap().hot_bytes(), pre);
8348    }
8349
8350    // --- v5.2.2 freezer atomic swap -------------------------------
8351
8352    /// Happy path: freeze the first half of a populated hot tier,
8353    /// confirm row counts shift, `hot_bytes` shrinks, and every frozen
8354    /// PK still resolves via `lookup_by_pk` (now through the cold
8355    /// segment registered by the freeze).
8356    #[test]
8357    fn freeze_oldest_to_cold_moves_rows_and_keeps_lookups_working() {
8358        let mut cat = Catalog::new();
8359        cat.create_table(bigint_pk_users_schema()).unwrap();
8360        let t = cat.get_mut("users").unwrap();
8361        for id in 0..10i64 {
8362            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8363                .unwrap();
8364        }
8365        t.add_index("by_id".into(), "id").unwrap();
8366        let total_bytes_before = t.hot_bytes();
8367
8368        let report = cat
8369            .freeze_oldest_to_cold("users", "by_id", 6)
8370            .expect("freeze succeeds");
8371        assert_eq!(report.frozen_rows, 6);
8372        assert_eq!(report.segment_id, 0);
8373        assert!(report.bytes_freed > 0);
8374        assert!(!report.segment_bytes.is_empty());
8375
8376        let t = cat.get("users").unwrap();
8377        assert_eq!(t.row_count(), 4, "4 hot rows remain (10 - 6 frozen)");
8378        assert_eq!(cat.cold_segment_count(), 1);
8379        // Hot bytes shrank by exactly the freed amount.
8380        assert_eq!(
8381            t.hot_bytes(),
8382            total_bytes_before - report.bytes_freed,
8383            "hot_bytes accounting matches FreezeReport"
8384        );
8385
8386        // Every original PK still resolves — frozen ones via the
8387        // cold segment, kept ones via the (renumbered) hot tier.
8388        for id in 0..10i64 {
8389            let got = cat
8390                .lookup_by_pk("users", "by_id", &IndexKey::Int(id))
8391                .unwrap_or_else(|| panic!("PK {id} disappeared after freeze"));
8392            assert_eq!(got, make_user_row(id, &alloc::format!("u-{id}")));
8393        }
8394    }
8395
8396    /// Two successive freezes on the same index must preserve the
8397    /// first batch's cold locators when the second freeze runs.
8398    /// Catches the `rebuild_indices` wipe-Cold-on-delete bug that
8399    /// `collect_cold_locators` / re-register guards against.
8400    #[test]
8401    fn freeze_twice_preserves_prior_cold_locators() {
8402        let mut cat = Catalog::new();
8403        cat.create_table(bigint_pk_users_schema()).unwrap();
8404        let t = cat.get_mut("users").unwrap();
8405        for id in 0..12i64 {
8406            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8407                .unwrap();
8408        }
8409        t.add_index("by_id".into(), "id").unwrap();
8410
8411        cat.freeze_oldest_to_cold("users", "by_id", 4)
8412            .expect("first freeze ok");
8413        cat.freeze_oldest_to_cold("users", "by_id", 4)
8414            .expect("second freeze ok");
8415
8416        assert_eq!(cat.get("users").unwrap().row_count(), 4);
8417        assert_eq!(cat.cold_segment_count(), 2);
8418        // All 12 PKs still resolve — first 4 via segment 0,
8419        // next 4 via segment 1, last 4 still hot.
8420        for id in 0..12i64 {
8421            let got = cat
8422                .lookup_by_pk("users", "by_id", &IndexKey::Int(id))
8423                .unwrap_or_else(|| panic!("PK {id} not resolvable after two freezes"));
8424            assert_eq!(got, make_user_row(id, &alloc::format!("u-{id}")));
8425        }
8426    }
8427
8428    /// Validation guard tests. Each must return `Err` and **not
8429    /// mutate the catalog** — the API is all-or-nothing.
8430    #[test]
8431    fn freeze_oldest_to_cold_rejects_invalid_input() {
8432        let mut cat = Catalog::new();
8433        cat.create_table(bigint_pk_users_schema()).unwrap();
8434        let t = cat.get_mut("users").unwrap();
8435        for id in 0..3i64 {
8436            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8437                .unwrap();
8438        }
8439        t.add_index("by_id".into(), "id").unwrap();
8440
8441        // max_rows == 0
8442        assert!(matches!(
8443            cat.freeze_oldest_to_cold("users", "by_id", 0),
8444            Err(StorageError::Corrupt(_))
8445        ));
8446        // table missing
8447        assert!(matches!(
8448            cat.freeze_oldest_to_cold("missing", "by_id", 1),
8449            Err(StorageError::Corrupt(_))
8450        ));
8451        // index missing
8452        assert!(matches!(
8453            cat.freeze_oldest_to_cold("users", "no_such_index", 1),
8454            Err(StorageError::Corrupt(_))
8455        ));
8456        // max_rows > row_count
8457        assert!(matches!(
8458            cat.freeze_oldest_to_cold("users", "by_id", 999),
8459            Err(StorageError::Corrupt(_))
8460        ));
8461        // Catalog still untouched.
8462        assert_eq!(cat.get("users").unwrap().row_count(), 3);
8463        assert_eq!(cat.cold_segment_count(), 0);
8464    }
8465
8466    /// Freeze with a non-integer PK column must surface a clear
8467    /// error (Text PKs land in v5.5+).
8468    #[test]
8469    fn freeze_oldest_to_cold_rejects_non_integer_pk() {
8470        let mut cat = Catalog::new();
8471        cat.create_table(TableSchema::new(
8472            "by_name",
8473            vec![
8474                ColumnSchema::new("name", DataType::Text, false),
8475                ColumnSchema::new("payload", DataType::BigInt, false),
8476            ],
8477        ))
8478        .unwrap();
8479        let t = cat.get_mut("by_name").unwrap();
8480        t.insert(Row::new(vec![Value::Text("a".into()), Value::BigInt(1)]))
8481            .unwrap();
8482        t.add_index("by_n".into(), "name").unwrap();
8483        let err = cat
8484            .freeze_oldest_to_cold("by_name", "by_n", 1)
8485            .expect_err("non-integer PK rejected");
8486        match err {
8487            StorageError::Corrupt(s) => assert!(
8488                s.contains("non-integer"),
8489                "error message names the constraint: {s}"
8490            ),
8491            other => panic!("expected Corrupt, got {other:?}"),
8492        }
8493        // Catalog untouched.
8494        assert_eq!(cat.get("by_name").unwrap().row_count(), 1);
8495        assert_eq!(cat.cold_segment_count(), 0);
8496    }
8497
8498    /// Hot-tier rows after the freeze must keep their secondary-
8499    /// index lookups working — `delete_rows` shifts positions, and
8500    /// `rebuild_indices` must regenerate Hot locators at the new
8501    /// indices.
8502    #[test]
8503    fn freeze_keeps_remaining_hot_rows_addressable_via_secondary_index() {
8504        let mut cat = Catalog::new();
8505        cat.create_table(bigint_pk_users_schema()).unwrap();
8506        let t = cat.get_mut("users").unwrap();
8507        for id in 0..6i64 {
8508            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8509                .unwrap();
8510        }
8511        t.add_index("by_id".into(), "id").unwrap();
8512        t.add_index("by_name".into(), "name").unwrap();
8513
8514        cat.freeze_oldest_to_cold("users", "by_id", 3).unwrap();
8515
8516        // Remaining hot rows: id 3, 4, 5. They moved to positions
8517        // 0, 1, 2 inside `self.rows`; the `by_name` index must now
8518        // resolve them via fresh Hot locators.
8519        let idx = cat.get("users").unwrap().index_on(1).unwrap();
8520        let got = idx.lookup_eq(&IndexKey::Text("u-4".into()));
8521        assert_eq!(got.len(), 1);
8522        assert!(got[0].is_hot(), "kept-hot rows still surface as Hot");
8523        match got[0] {
8524            RowLocator::Hot(i) => {
8525                // The 4th-inserted row was at position 4; after
8526                // dropping positions 0..3 it sits at position 1.
8527                assert_eq!(i, 1);
8528            }
8529            RowLocator::Cold { .. } => unreachable!(),
8530        }
8531    }
8532
8533    // --- v5.2.3 promote-on-write primitives ----------------------
8534
8535    /// Build a populated catalog with the first N rows frozen, then
8536    /// run `promote_cold_row` and verify the row crossed tiers
8537    /// correctly: the cold locator is retired, a fresh Hot locator
8538    /// appears, `lookup_by_pk` returns the row from the hot tier, and
8539    /// `hot_bytes` grew by the row's encoded byte length.
8540    #[test]
8541    fn promote_cold_row_pulls_frozen_row_back_to_hot_tier() {
8542        let mut cat = Catalog::new();
8543        cat.create_table(bigint_pk_users_schema()).unwrap();
8544        let t = cat.get_mut("users").unwrap();
8545        for id in 0..6i64 {
8546            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8547                .unwrap();
8548        }
8549        t.add_index("by_id".into(), "id").unwrap();
8550        // Freeze first 4 rows (ids 0..3). After: hot rows = 4, 5 at
8551        // positions 0, 1; cold locators for keys 0..3.
8552        cat.freeze_oldest_to_cold("users", "by_id", 4).unwrap();
8553        let hot_bytes_before = cat.get("users").unwrap().hot_bytes();
8554
8555        // Promote PK=2 — it lives in segment 0 as a cold row.
8556        let new_idx = cat
8557            .promote_cold_row("users", "by_id", &IndexKey::Int(2))
8558            .expect("promote ok")
8559            .expect("PK 2 was cold");
8560        assert_eq!(
8561            new_idx, 2,
8562            "promoted row appended after the 2 surviving hot rows"
8563        );
8564
8565        let t = cat.get("users").unwrap();
8566        assert_eq!(t.row_count(), 3, "hot tier grew from 2 to 3");
8567        // Hot-bytes climbed by exactly one row's encoded length.
8568        let row = make_user_row(2, "u-2");
8569        let row_len = encode_row_body_dense(&row, &t.schema).len() as u64;
8570        assert_eq!(t.hot_bytes(), hot_bytes_before + row_len);
8571
8572        // The index now reports a Hot locator (the freshly inserted
8573        // row) — no Cold locator left for PK 2.
8574        let entries = t.index_on(0).unwrap().lookup_eq(&IndexKey::Int(2));
8575        assert_eq!(entries.len(), 1, "exactly one locator per key");
8576        assert!(entries[0].is_hot(), "promote retired the Cold locator");
8577        // End-to-end: lookup_by_pk still returns the row body.
8578        assert_eq!(
8579            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(2))
8580                .unwrap(),
8581            row
8582        );
8583        // Other cold rows untouched — still resolvable through the
8584        // segment.
8585        assert_eq!(
8586            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(0))
8587                .unwrap(),
8588            make_user_row(0, "u-0")
8589        );
8590    }
8591
8592    /// `promote_cold_row` on a key that's already hot (or absent)
8593    /// returns `Ok(None)` — not an error. The caller falls back to
8594    /// the hot-only update/delete path.
8595    #[test]
8596    fn promote_cold_row_returns_none_when_key_is_not_cold() {
8597        let mut cat = Catalog::new();
8598        cat.create_table(bigint_pk_users_schema()).unwrap();
8599        let t = cat.get_mut("users").unwrap();
8600        t.insert(make_user_row(7, "alice")).unwrap();
8601        t.add_index("by_id".into(), "id").unwrap();
8602
8603        // Hot-only key.
8604        assert!(
8605            cat.promote_cold_row("users", "by_id", &IndexKey::Int(7))
8606                .unwrap()
8607                .is_none()
8608        );
8609        // Absent key.
8610        assert!(
8611            cat.promote_cold_row("users", "by_id", &IndexKey::Int(99))
8612                .unwrap()
8613                .is_none()
8614        );
8615        // Catalog untouched on both no-op paths.
8616        assert_eq!(cat.get("users").unwrap().row_count(), 1);
8617        assert_eq!(cat.cold_segment_count(), 0);
8618    }
8619
8620    /// `shadow_cold_row` removes every Cold locator for a key on a
8621    /// `BTree` index. After the shadow, `lookup_by_pk` for that key
8622    /// returns None (the row data still sits in the segment file,
8623    /// but it's now garbage; compaction will reclaim it later).
8624    #[test]
8625    fn shadow_cold_row_removes_cold_locators_and_drops_lookup() {
8626        let mut cat = Catalog::new();
8627        cat.create_table(bigint_pk_users_schema()).unwrap();
8628        let t = cat.get_mut("users").unwrap();
8629        for id in 0..5i64 {
8630            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8631                .unwrap();
8632        }
8633        t.add_index("by_id".into(), "id").unwrap();
8634        cat.freeze_oldest_to_cold("users", "by_id", 3).unwrap();
8635
8636        // Shadow PK=1 — pre-shadow lookup hits the cold tier.
8637        assert!(
8638            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(1))
8639                .is_some(),
8640            "frozen PK resolves before shadow"
8641        );
8642        let removed = cat
8643            .shadow_cold_row("users", "by_id", &IndexKey::Int(1))
8644            .unwrap();
8645        assert_eq!(removed, 1, "exactly one cold locator retired");
8646
8647        // Post-shadow: lookup misses, even though the row still
8648        // exists in segment 0.
8649        assert!(
8650            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(1))
8651                .is_none(),
8652            "shadowed key no longer resolves"
8653        );
8654        // Other cold keys still resolve.
8655        assert_eq!(
8656            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(0))
8657                .unwrap(),
8658            make_user_row(0, "u-0")
8659        );
8660        assert_eq!(
8661            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(2))
8662                .unwrap(),
8663            make_user_row(2, "u-2")
8664        );
8665    }
8666
8667    /// `shadow_cold_row` returns 0 (not Err) for keys with only Hot
8668    /// entries or no entries — the engine's DELETE path uses this
8669    /// signal to decide whether the cold-tier shadow path consumed
8670    /// the work.
8671    #[test]
8672    fn shadow_cold_row_returns_zero_when_key_is_not_cold() {
8673        let mut cat = Catalog::new();
8674        cat.create_table(bigint_pk_users_schema()).unwrap();
8675        let t = cat.get_mut("users").unwrap();
8676        t.insert(make_user_row(1, "alice")).unwrap();
8677        t.add_index("by_id".into(), "id").unwrap();
8678        assert_eq!(
8679            cat.shadow_cold_row("users", "by_id", &IndexKey::Int(1))
8680                .unwrap(),
8681            0,
8682            "hot-only key drops no cold locators"
8683        );
8684        assert_eq!(
8685            cat.shadow_cold_row("users", "by_id", &IndexKey::Int(999))
8686                .unwrap(),
8687            0,
8688            "absent key drops no cold locators"
8689        );
8690        assert_eq!(cat.get("users").unwrap().row_count(), 1);
8691    }
8692
8693    /// Validation guards on both promote / shadow primitives.
8694    #[test]
8695    fn promote_and_shadow_reject_invalid_inputs() {
8696        let mut cat = Catalog::new();
8697        cat.create_table(bigint_pk_users_schema()).unwrap();
8698        let t = cat.get_mut("users").unwrap();
8699        t.insert(make_user_row(1, "alice")).unwrap();
8700        t.add_index("by_id".into(), "id").unwrap();
8701
8702        // Missing table.
8703        assert!(matches!(
8704            cat.promote_cold_row("missing", "by_id", &IndexKey::Int(1)),
8705            Err(StorageError::Corrupt(_))
8706        ));
8707        assert!(matches!(
8708            cat.shadow_cold_row("missing", "by_id", &IndexKey::Int(1)),
8709            Err(StorageError::Corrupt(_))
8710        ));
8711        // Missing index.
8712        assert!(matches!(
8713            cat.promote_cold_row("users", "no_such_index", &IndexKey::Int(1)),
8714            Err(StorageError::Corrupt(_))
8715        ));
8716        assert!(matches!(
8717            cat.shadow_cold_row("users", "no_such_index", &IndexKey::Int(1)),
8718            Err(StorageError::Corrupt(_))
8719        ));
8720    }
8721
8722    // --- v6.7.4 parallel-freezer slice/commit API -----------------
8723
8724    /// One slice covering the entire freeze produces the same
8725    /// catalog state as the single-threaded `freeze_oldest_to_cold`
8726    /// — segment id, frozen row count, hot byte delta, and every
8727    /// post-freeze PK lookup match exactly.
8728    #[test]
8729    fn commit_freeze_slices_single_slice_matches_freeze_oldest() {
8730        let mut a = Catalog::new();
8731        let mut b = Catalog::new();
8732        for cat in [&mut a, &mut b] {
8733            cat.create_table(bigint_pk_users_schema()).unwrap();
8734            let t = cat.get_mut("users").unwrap();
8735            for id in 0..10i64 {
8736                t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8737                    .unwrap();
8738            }
8739            t.add_index("by_id".into(), "id").unwrap();
8740        }
8741        let single = a.freeze_oldest_to_cold("users", "by_id", 6).unwrap();
8742        let slice = b
8743            .prepare_freeze_slice("users", "by_id", 0..6)
8744            .expect("prepare");
8745        let parallel = b
8746            .commit_freeze_slices("users", "by_id", alloc::vec![slice])
8747            .expect("commit");
8748        assert_eq!(single.segment_id, parallel.segment_id);
8749        assert_eq!(single.frozen_rows, parallel.frozen_rows);
8750        assert_eq!(single.bytes_freed, parallel.bytes_freed);
8751        assert_eq!(single.segment_bytes, parallel.segment_bytes);
8752        // Same post-freeze lookup behaviour on both catalogs.
8753        for id in 0..10i64 {
8754            assert_eq!(
8755                a.lookup_by_pk("users", "by_id", &IndexKey::Int(id)),
8756                b.lookup_by_pk("users", "by_id", &IndexKey::Int(id)),
8757                "PK {id} differs after single vs slice freeze"
8758            );
8759        }
8760    }
8761
8762    /// Two slices covering disjoint halves of the freeze produce
8763    /// the same merged segment as one slice covering the full
8764    /// range. The k-way merge preserves PK ordering even when
8765    /// slice halves alternate.
8766    #[test]
8767    fn commit_freeze_slices_two_slices_match_single_slice() {
8768        let mut a = Catalog::new();
8769        let mut b = Catalog::new();
8770        for cat in [&mut a, &mut b] {
8771            cat.create_table(bigint_pk_users_schema()).unwrap();
8772            let t = cat.get_mut("users").unwrap();
8773            // Random-ish PKs so the per-slice sort actually has
8774            // work to do (and slice halves carry interleaved keys).
8775            for id in [3, 7, 1, 9, 5, 0, 8, 4, 2, 6].iter().copied() {
8776                t.insert(make_user_row(id as i64, &alloc::format!("u-{id}")))
8777                    .unwrap();
8778            }
8779            t.add_index("by_id".into(), "id").unwrap();
8780        }
8781        let single = a
8782            .prepare_freeze_slice("users", "by_id", 0..8)
8783            .expect("prepare");
8784        let one = a
8785            .commit_freeze_slices("users", "by_id", alloc::vec![single])
8786            .expect("commit one");
8787        let s1 = b
8788            .prepare_freeze_slice("users", "by_id", 0..4)
8789            .expect("prepare s1");
8790        let s2 = b
8791            .prepare_freeze_slice("users", "by_id", 4..8)
8792            .expect("prepare s2");
8793        let two = b
8794            .commit_freeze_slices("users", "by_id", alloc::vec![s1, s2])
8795            .expect("commit two");
8796        assert_eq!(one.segment_bytes, two.segment_bytes);
8797        assert_eq!(one.frozen_rows, two.frozen_rows);
8798        // Every PK that survived freeze (hot or cold) resolves on
8799        // both catalogs.
8800        for id in 0..10i64 {
8801            assert_eq!(
8802                a.lookup_by_pk("users", "by_id", &IndexKey::Int(id)),
8803                b.lookup_by_pk("users", "by_id", &IndexKey::Int(id)),
8804                "PK {id} differs after one-slice vs two-slice freeze"
8805            );
8806        }
8807    }
8808
8809    /// Gap between slices → error before any mutation lands.
8810    #[test]
8811    fn commit_freeze_slices_rejects_gap() {
8812        let mut cat = Catalog::new();
8813        cat.create_table(bigint_pk_users_schema()).unwrap();
8814        let t = cat.get_mut("users").unwrap();
8815        for id in 0..6i64 {
8816            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8817                .unwrap();
8818        }
8819        t.add_index("by_id".into(), "id").unwrap();
8820        let s1 = cat.prepare_freeze_slice("users", "by_id", 0..2).unwrap();
8821        let s2 = cat.prepare_freeze_slice("users", "by_id", 3..5).unwrap();
8822        assert!(matches!(
8823            cat.commit_freeze_slices("users", "by_id", alloc::vec![s1, s2]),
8824            Err(StorageError::Corrupt(_))
8825        ));
8826        // Catalog untouched.
8827        assert_eq!(cat.cold_segment_count(), 0);
8828        assert_eq!(cat.get("users").unwrap().row_count(), 6);
8829    }
8830
8831    /// Empty slice list → no-op success, catalog untouched.
8832    #[test]
8833    fn commit_freeze_slices_empty_is_noop() {
8834        let mut cat = Catalog::new();
8835        cat.create_table(bigint_pk_users_schema()).unwrap();
8836        let t = cat.get_mut("users").unwrap();
8837        for id in 0..3i64 {
8838            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8839                .unwrap();
8840        }
8841        t.add_index("by_id".into(), "id").unwrap();
8842        let report = cat
8843            .commit_freeze_slices("users", "by_id", Vec::new())
8844            .unwrap();
8845        assert_eq!(report.frozen_rows, 0);
8846        assert_eq!(cat.cold_segment_count(), 0);
8847        assert_eq!(cat.get("users").unwrap().row_count(), 3);
8848    }
8849
8850    // --- v6.7.3 cold-segment compaction ---------------------------
8851
8852    /// Two small cold segments merge into a single larger one. The
8853    /// merged segment carries every cold-resident row; the source
8854    /// slots are tombstoned; every PK still resolves through the
8855    /// new merged segment via `lookup_by_pk`.
8856    #[test]
8857    fn compact_merges_small_segments_storage_unit() {
8858        let mut cat = Catalog::new();
8859        cat.create_table(bigint_pk_users_schema()).unwrap();
8860        let t = cat.get_mut("users").unwrap();
8861        for id in 0..8i64 {
8862            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8863                .unwrap();
8864        }
8865        t.add_index("by_id".into(), "id").unwrap();
8866        // Two freezes of 3 rows each → two small cold segments.
8867        cat.freeze_oldest_to_cold("users", "by_id", 3).unwrap();
8868        cat.freeze_oldest_to_cold("users", "by_id", 3).unwrap();
8869        assert_eq!(cat.cold_segment_count(), 2);
8870        assert_eq!(cat.cold_segment_slot_count(), 2);
8871
8872        // Pick a threshold larger than either segment's size so
8873        // both qualify.
8874        let max_seg_bytes = cat
8875            .cold_segment_ids_global()
8876            .iter()
8877            .map(|id| cat.cold_segment(*id).unwrap().bytes().len() as u64)
8878            .max()
8879            .unwrap();
8880        let target = max_seg_bytes + 1;
8881
8882        let report = cat
8883            .compact_cold_segments("users", "by_id", target)
8884            .expect("compact succeeds");
8885        assert_eq!(report.sources.len(), 2);
8886        let merged_id = report.merged_segment_id.expect("merge happened");
8887        assert_eq!(report.merged_rows, 6);
8888        assert_eq!(report.deleted_rows_pruned, 0);
8889        assert!(!report.merged_segment_bytes.is_empty());
8890
8891        // Active count drops back to 1; slot count grew to 3
8892        // (2 sources tombstoned + 1 merged appended).
8893        assert_eq!(cat.cold_segment_count(), 1);
8894        assert_eq!(cat.cold_segment_slot_count(), 3);
8895        assert_eq!(cat.cold_segment_ids_global(), alloc::vec![merged_id]);
8896
8897        // Every PK that was frozen still resolves (via the merged
8898        // segment); the 2 hot rows still resolve too.
8899        for id in 0..8i64 {
8900            let got = cat
8901                .lookup_by_pk("users", "by_id", &IndexKey::Int(id))
8902                .unwrap_or_else(|| panic!("PK {id} lost after compaction"));
8903            assert_eq!(got, make_user_row(id, &alloc::format!("u-{id}")));
8904        }
8905    }
8906
8907    /// DELETE'd-but-frozen rows are dropped during the merge. Set
8908    /// up two small segments, then shadow one row in each; the
8909    /// merged segment must NOT carry the shadowed rows.
8910    #[test]
8911    fn compact_drops_shadowed_cold_rows() {
8912        let mut cat = Catalog::new();
8913        cat.create_table(bigint_pk_users_schema()).unwrap();
8914        let t = cat.get_mut("users").unwrap();
8915        for id in 0..6i64 {
8916            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8917                .unwrap();
8918        }
8919        t.add_index("by_id".into(), "id").unwrap();
8920        cat.freeze_oldest_to_cold("users", "by_id", 3).unwrap();
8921        cat.freeze_oldest_to_cold("users", "by_id", 3).unwrap();
8922        // Shadow PK 1 (in seg 0) + PK 4 (in seg 1).
8923        assert_eq!(
8924            cat.shadow_cold_row("users", "by_id", &IndexKey::Int(1))
8925                .unwrap(),
8926            1
8927        );
8928        assert_eq!(
8929            cat.shadow_cold_row("users", "by_id", &IndexKey::Int(4))
8930                .unwrap(),
8931            1
8932        );
8933
8934        let max_seg_bytes = cat
8935            .cold_segment_ids_global()
8936            .iter()
8937            .map(|id| cat.cold_segment(*id).unwrap().bytes().len() as u64)
8938            .max()
8939            .unwrap();
8940        let report = cat
8941            .compact_cold_segments("users", "by_id", max_seg_bytes + 1)
8942            .expect("compact succeeds");
8943        assert_eq!(report.sources.len(), 2);
8944        assert_eq!(report.merged_rows, 4, "6 frozen − 2 shadowed = 4 live");
8945        assert_eq!(report.deleted_rows_pruned, 2);
8946
8947        // PK 1 and 4 stay invisible after compact.
8948        for shadowed in [1i64, 4i64] {
8949            assert!(
8950                cat.lookup_by_pk("users", "by_id", &IndexKey::Int(shadowed))
8951                    .is_none(),
8952                "shadowed PK {shadowed} must remain invisible after compact"
8953            );
8954        }
8955        // The other 4 frozen rows resolve.
8956        for live in [0i64, 2, 3, 5] {
8957            cat.lookup_by_pk("users", "by_id", &IndexKey::Int(live))
8958                .unwrap_or_else(|| panic!("live PK {live} lost after compact"));
8959        }
8960    }
8961
8962    /// No-op cases: 0 or 1 candidate segment under the threshold
8963    /// leaves the catalog untouched.
8964    #[test]
8965    fn compact_is_noop_below_two_candidates() {
8966        let mut cat = Catalog::new();
8967        cat.create_table(bigint_pk_users_schema()).unwrap();
8968        let t = cat.get_mut("users").unwrap();
8969        for id in 0..6i64 {
8970            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
8971                .unwrap();
8972        }
8973        t.add_index("by_id".into(), "id").unwrap();
8974        // 0 cold segments.
8975        let report = cat
8976            .compact_cold_segments("users", "by_id", 1 << 30)
8977            .expect("noop ok");
8978        assert!(report.merged_segment_id.is_none());
8979        assert!(report.sources.is_empty());
8980
8981        // 1 cold segment — still a no-op (need ≥2 to merge).
8982        cat.freeze_oldest_to_cold("users", "by_id", 4).unwrap();
8983        let report = cat
8984            .compact_cold_segments("users", "by_id", 1 << 30)
8985            .expect("noop ok");
8986        assert!(report.merged_segment_id.is_none());
8987        assert_eq!(cat.cold_segment_count(), 1);
8988
8989        // Threshold too small to cover the single segment → still
8990        // no-op.
8991        let report = cat
8992            .compact_cold_segments("users", "by_id", 1)
8993            .expect("noop ok");
8994        assert!(report.merged_segment_id.is_none());
8995        assert_eq!(cat.cold_segment_count(), 1);
8996    }
8997
8998    /// Manifest-style atomicity: a Catalog snapshot taken AFTER
8999    /// `compact_cold_segments` returns must round-trip with the
9000    /// post-compact BTree state, while the cold-tier registry is
9001    /// re-derived from the source-of-truth manifest (=
9002    /// `load_segment_bytes_at` with the merged id + the still-on-
9003    /// disk merged bytes). This mirrors the boot path: catalog
9004    /// snapshot + cold-segment files = full state.
9005    #[test]
9006    fn compact_swap_survives_catalog_roundtrip_via_load_at() {
9007        let mut cat = Catalog::new();
9008        cat.create_table(bigint_pk_users_schema()).unwrap();
9009        let t = cat.get_mut("users").unwrap();
9010        for id in 0..6i64 {
9011            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
9012                .unwrap();
9013        }
9014        t.add_index("by_id".into(), "id").unwrap();
9015        cat.freeze_oldest_to_cold("users", "by_id", 3).unwrap();
9016        cat.freeze_oldest_to_cold("users", "by_id", 3).unwrap();
9017        let max_seg_bytes = cat
9018            .cold_segment_ids_global()
9019            .iter()
9020            .map(|id| cat.cold_segment(*id).unwrap().bytes().len() as u64)
9021            .max()
9022            .unwrap();
9023        let report = cat
9024            .compact_cold_segments("users", "by_id", max_seg_bytes + 1)
9025            .expect("compact ok");
9026        let merged_id = report.merged_segment_id.unwrap();
9027
9028        // Serialise the catalog (BTree index points at merged_id
9029        // now) and the merged segment bytes; pretend to crash; on
9030        // restart, re-hydrate the catalog and reload only the
9031        // merged segment at its baked-in id.
9032        let cat_bytes = cat.serialize();
9033        let merged_bytes = report.merged_segment_bytes.clone();
9034
9035        let mut restored = Catalog::deserialize(&cat_bytes).expect("deserialize ok");
9036        restored
9037            .load_segment_bytes_at(merged_id, merged_bytes)
9038            .expect("reload merged ok");
9039
9040        // All 6 PKs still resolve through the restored merged segment.
9041        for id in 0..6i64 {
9042            let got = restored
9043                .lookup_by_pk("users", "by_id", &IndexKey::Int(id))
9044                .unwrap_or_else(|| panic!("PK {id} lost across roundtrip"));
9045            assert_eq!(got, make_user_row(id, &alloc::format!("u-{id}")));
9046        }
9047        // No source slot ever rehydrates — confirmed by
9048        // `cold_segment_count` matching only the merged segment.
9049        assert_eq!(restored.cold_segment_count(), 1);
9050    }
9051
9052    /// `load_segment_bytes_at` refuses to stomp an occupied slot
9053    /// and pads with `None` when the target id is past the end.
9054    #[test]
9055    fn load_segment_bytes_at_pads_and_rejects_collision() {
9056        let mut cat = Catalog::new();
9057        cat.create_table(bigint_pk_users_schema()).unwrap();
9058        let t = cat.get_mut("users").unwrap();
9059        for id in 0..4i64 {
9060            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
9061                .unwrap();
9062        }
9063        t.add_index("by_id".into(), "id").unwrap();
9064        let report = cat.freeze_oldest_to_cold("users", "by_id", 2).unwrap();
9065        let bytes_seg0 = report.segment_bytes.clone();
9066
9067        // Pad to id=5 (slots 1..5 are None, slot 5 holds the
9068        // segment loaded back). The slot count jumps, the active
9069        // count is now 2 (seg 0 + seg 5).
9070        cat.load_segment_bytes_at(5, bytes_seg0.clone())
9071            .expect("pad + load ok");
9072        assert_eq!(cat.cold_segment_slot_count(), 6);
9073        assert_eq!(cat.cold_segment_count(), 2);
9074
9075        // Re-loading at the same id collides.
9076        assert!(matches!(
9077            cat.load_segment_bytes_at(5, bytes_seg0.clone()),
9078            Err(StorageError::Corrupt(_))
9079        ));
9080        // Re-loading at id 0 (already occupied) also collides.
9081        assert!(matches!(
9082            cat.load_segment_bytes_at(0, bytes_seg0),
9083            Err(StorageError::Corrupt(_))
9084        ));
9085    }
9086
9087    /// Round trip: freeze → promote → re-freeze. The same PK can
9088    /// migrate hot ↔ cold multiple times. After two cycles only the
9089    /// final Hot locator should be live.
9090    #[test]
9091    fn promote_then_refreeze_does_not_leave_orphan_locators() {
9092        let mut cat = Catalog::new();
9093        cat.create_table(bigint_pk_users_schema()).unwrap();
9094        let t = cat.get_mut("users").unwrap();
9095        for id in 0..4i64 {
9096            t.insert(make_user_row(id, &alloc::format!("u-{id}")))
9097                .unwrap();
9098        }
9099        t.add_index("by_id".into(), "id").unwrap();
9100
9101        // Cycle 1: freeze first 2 rows, then promote PK 0.
9102        cat.freeze_oldest_to_cold("users", "by_id", 2).unwrap();
9103        let promoted = cat
9104            .promote_cold_row("users", "by_id", &IndexKey::Int(0))
9105            .unwrap();
9106        assert!(promoted.is_some());
9107        let entries_after_promote = cat
9108            .get("users")
9109            .unwrap()
9110            .index_on(0)
9111            .unwrap()
9112            .lookup_eq(&IndexKey::Int(0))
9113            .to_vec();
9114        assert_eq!(entries_after_promote.len(), 1);
9115        assert!(entries_after_promote[0].is_hot());
9116
9117        // Cycle 2: freeze the front rows again. PK 0 is now at
9118        // position 2 (after the survivors); it could still go cold
9119        // again on a future freeze depending on policy, but the
9120        // current "first N positions" policy leaves it alone here.
9121        // What matters: prior cold locators for PKs 0..1 are gone,
9122        // PKs 2..3 still resolve through their original segments.
9123        for id in [2i64, 3] {
9124            assert_eq!(
9125                cat.lookup_by_pk("users", "by_id", &IndexKey::Int(id))
9126                    .unwrap(),
9127                make_user_row(id, &alloc::format!("u-{id}"))
9128            );
9129        }
9130    }
9131}