Principle:2giosangmitom Sqmeow nvim Result Retention
| Knowledge Sources | |
|---|---|
| Domains | Data_Model, Query_Execution |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
A lossless data retention principle that stores query results in a Polars DataFrame with Object fallback, preserving exact values for editing, paging, and export while sharing buffers across readers.
Description
Result Retention is the principle of storing query results in a data model that preserves every value exactly as the database returned it, without type narrowing or precision loss. Native types (Boolean, Int64, Float64, String, Binary) use Arrow-backed Polars columns for efficient vectorized operations. Types that don't fit native columns (Decimal, Timestamp, Array, Unsupported) use an Object fallback column storing Cell values directly.
This principle addresses the challenge of supporting diverse database types (exact numerics, timestamps with timezones, arrays, JSON, binary, custom types) in a uniform in-memory model. Typed projections for views and exports read from the same shared frame without mutating source cells. Cloning a ResultSet shares Polars buffers through copy-on-write, so snapshot copies are cheap.
Key invariants:
- Every data column has
row_countcells; short rows are padded with NULL, excess cells are trimmed. - Internal column names are positional (
__sqmeow_cell_N), so duplicate database labels never violate Polars' unique-name invariant. - Ingestion buffers (
pending) are moved (not copied) into the frame on first read viaframe(). - A multi-statement run produces multiple calls but consumes one history slot; all calls in a run are evicted together.
Usage
Use this principle when a database tool needs to retain query results for paging, filtering, editing, and export after the query has finished. The lossless model ensures that editing writes back the exact values the database returned, and export produces the exact text.
Theoretical Basis
The principle combines native Polars columns with Object fallback:
- Ingestion: Adapters push row-shaped
Cellvalues into per-columnpendingbuffers. - Finalization:
frame()moves pending cells into native Polars columns where possible, or Object columns for types that don't fit. This is a one-time, lazy operation. - Sharing: Cloning a
ResultSetclones theDataFrame, which shares Arrow buffers through copy-on-write. - Projections: View/export projections read from the shared frame, never writing back. Text projections format only requested rows in their original order.
# Abstract pattern (NOT real implementation):
class ResultSet:
columns: list[Column] # metadata: name, type, key, class
pending: list[list[Cell]] # ingestion buffers
frame: OnceLock[DataFrame] # finalized on first read
def push_row(self, row: list[Cell]):
row.resize(len(self.columns), Cell.Null) # pad/trim
for col, cell in zip(self.pending, row):
col.append(cell)
def frame(self) -> DataFrame:
# Move pending into native columns (Int64, String, ...)
# or Object columns (Decimal, Timestamp, Array, ...)
return self.frame.get_or_init(lambda: finalize(self.pending))