Skip to content

An ingested file's tables are typed datasets a writer can approve - #266

Merged
zlareb1 merged 3 commits into
mainfrom
feat/datasets
Nov 5, 2026
Merged

zlareb1 merged 3 commits into
mainfrom
feat/datasets

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Related: #157

Changelog category (leave one):

  • New Feature

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

An ingested CSV or XLSX file's tables become typed datasets. knowledge.dataset_get returns each dataset's schema, what needs an answer, the rows rejected and its first rows, and knowledge.dataset_approve approves a schema and retypes its columns.

Why

The issue asks for spreadsheets to be queried as typed data, not only searched as passages. The extractor already returns each sheet as a table of typed values in the artifact. This PR stores those tables as typed snapshots with a schema a person can check and correct. A structured query over them (knowledge.query) comes next and reads these tables. Parquet comes in its own change.

Design

  • Tables: two tables hold every dataset, so no upload creates a table (migration 083; another open change also takes 083, and whichever merges second renumbers).
    • loom.datasets is the catalog. It has one ReplacingMergeTree row per (source, dataset, document version), read with FINAL and built by dataset_row, which carries every column. A row holds the schema (JSON: each column's name, type, inferred type and slot), the state (inferred, needs_clarification, approved), the clarifications, the rows stored and rejected (the first 20 with why), complete, the snapshot serving it and the identity tuple.
    • loom.dataset_rows holds the rows, in 72 typed slots: text and times in s1..s24, numbers and durations in n1..n24 (Decimal128(9); the number columns past those in f1..f8, Float64), dates and date-times in t1..t8, booleans in b1..b8. All of them are Nullable, so a count or a null test is exact.
    • It is a MergeTree keyed (org, namespace, dataset_id, snapshot_id, row_id), and each row carries the identity tuple.
    • A column past its kind's slots is not stored, and the schema says so once for each type (past_capacity, naming the first such columns).
  • Typing: a column takes the extractor's type. A mixed column is held as text until a person types it.
    • A row with a cell error, or a value its column's type cannot hold, is rejected whole and counted, so every stored value is typed and a sum never skips a #DIV/0! in silence. That includes a number of 10^29 or more after rounding to nine places in an n slot, one a double cannot hold in an f slot, and an instant outside 1900-2299 (what DateTime64 holds; a time zone is turned to UTC).
    • A dataset is complete only when the extraction kept every value of its table: none of its rows, merges, values or cells failed, no shared string was lost, and the artifact lost none of the capabilities a value depends on (formula values, shared strings, number formats).
    • Clarifications are asked for a mixed column, a generated name (an empty header), a header whose every name reads as a number or a date, and the columns no slot holds. The answers about single columns stop at 100, and inference runs off the event loop, so a 16,384-column table of mixed values costs a bounded catalog row and does not stall the worker.
    • A repeated column name (an extractor other than the reference one may send one) is renamed in the dataset as the reference extractor names it, <name> (2), and said (duplicate_name), since a column is addressed by its name; the artifact and its chunks are unaffected.
    • A dataset id is stable across revisions: a sheet by its name, a CSV file's table by its id, and a sheet an extractor split into more than one table by its name and the table's id.
  • Publication: the ingest job writes each dataset after the chunks and before the source's claim.
    • An empty catalog row goes first, so a run that stops midway finds the rows it wrote. The rows go in batches of 5,000 under a new snapshot, typed off the event loop, with the job read again before each, so an erasure that deleted it stops the rest. Then the full catalog row is written with the document version the job's row will take.
    • A dataset is served while the source's live document row has that version. The document row is therefore the one switch for chunks and datasets: a failed or cancelled job serves nothing new, and a text write or a delete stops serving at once, with no second pointer to keep in step.
    • A metadata edit (knowledge.update_metadata) of an ingested source keeps its datasets: under the source's claim it writes each catalog row at the new version (the same snapshot) before the document row, and refuses with 409 when the source moved since it was read, 503 while another write holds the claim (both documented on the feature page; the op's budget now counts the claim and the carry). A carried snapshot's rows keep the version they were written at, so a delete keeps the rows of any snapshot a catalog row past its bound still serves, when the edit's carry landed before the delete read the catalog. A delete takes no claim: an edit whose carry lands between that read and the delete may revive the document over the delete (when its version is the later one), its chunks tombstoned and its datasets serving with their rows deleted: the race a claim-free delete already has with an edit, which a delete of the source settles. Making deletes take the claim would close both, at a claim per source for a bulk delete of up to 500; it is left for a change of its own. Build N-1's metadata edit carries nothing; rule 29 says its datasets stop being served until the file is ingested again.
    • Once the row lands (or when a rerun finishes a landed publish), the source's snapshots of earlier versions are deleted. A run that stops before its row lands deletes its own.
    • A revision whose earlier dataset was approved keeps the approved types when its columns are the same names in the same order. An approval that lands after the publish read the live datasets (it takes the claim first) is caught under the claim and puts the job back to run again at once, without counting the attempt, so the approval is carried, not dropped.
  • Approval: knowledge.dataset_approve (writer, audited, experimental) names the snapshot the caller reviewed, plus the types to change.
    • A retype types the rows again from the stored artifact under a new snapshot, written before the source's claim is taken.
    • Under the claim (the one an ingest's publish takes, now ingest_run.claim_source) it checks that the document and the snapshot are unchanged, stamps the catalog row's version there (past the row it replaces, so an approval written while it typed cannot outrank it), writes it and deletes the replaced snapshot's rows.
    • If the source is deleted, the claim is held elsewhere, or the approval fails before its catalog row is sent, it writes no catalog row and deletes only its own rows; a catalog insert that fails once sent keeps them, since the row may have landed. A stale snapshot, a later revision, or another approval that retyped the dataset since the read is 409 PRECONDITION_FAILED (read it again); a deleted source is found: false. Another approval that kept the snapshot is overwritten by this one.
    • The catalog row is the approver's write: its actor and approved_by name the approver and its source_class is asserted; the rows stay the uploader's (user_id and their own source_class), as a user erasure finds them; a retype takes them from the live document row the ingest wrote (re-checked under the claim), since the catalog row may be an earlier approver's write.
  • Reads: knowledge.dataset_get (viewer, experimental) reads the live document and its version's catalog rows in the artifact's order. With sample_rows (up to 50) it reads the first rows of the first 20 datasets in one statement, each snapshot a range of the key (row_id under the rows asked for plus the rejected rows below them: the catalog row names the first 20 rejected rows in row order, so the bound is exact unless it reaches past the last of them, and only then is every unnamed one counted), and loom.dataset_rows keeps granules of 1,024 rows, so the read takes the granules holding them, not the tables (system.query_log): 3,672 rows read for 3 datasets of 100,000 rows each, against 215,960 for a plain LIMIT n BY read; 38,912 rows and 20 ms for 20 datasets of 1,000 rows. A UNION ALL branch per snapshot read as little but cost about 3 ms of planning a branch. An integer past 2^53, or a fraction a double cannot hold, is returned as text.
  • Deletes: snapshots go by lightweight DELETE FROM with lightweight_deletes_sync = 0. Nothing reads a snapshot once no catalog row serves it, so no request waits on a delete.
    • knowledge.delete and knowledge.bulk_delete delete the datasets of the document versions they retire, up to each source's latest row as read before the delete, superseded or not. A delete takes no claim, so it can land between a first ingest's batches; an ingest running meanwhile writes a later version, which the delete leaves whole, so the dataset it serves holds every row it counts. A delete of a source already deleted still deletes what is left of its retired versions (an approval's snapshot landing as the first delete ran, or one build N-1 left).
    • A text write over an ingested source leaves its old snapshot unserved until the source's next ingest or delete.
    • No new ALTER mutation.
  • Erasure: both tables are deleted in documents.tombstone with the ingestion tables, by namespace and by user (the rows carry the uploader's user_id), and both have census rows.
  • Rolling upgrade: build N-1 does not know the tables. It ingests files without datasets, and its deletes leave datasets unserved (the document row stops serving them), so N's reads stay correct while both run. An erasure N-1 runs during the rollout misses the rows N wrote; docs/architecture/rolling-upgrades.md (rule 29) says to request those erasures again once N-1 stops serving.

Alternatives considered:

  • A table per upload: typed DDL per file, unbounded table count, and DDL on the request path. Rejected for one pooled table with slots.
  • A catalog row per dataset with its own serving pointer: it needs a second switch kept in step with the document row, and a failed publish would have to put the pointer back. Keying the catalog by document version makes the document row the only switch.
  • A sweep worker for unserved snapshots: one more worker and its settings, for what the publish, the approval and the delete already know when they run.

Tests

  • Unit (tests/unit/test_knowledge_datasets.py):
    • the column sets against the schema; the catalog builder carrying every column; slot assignment, overflow to f slots and past capacity; each clarification; overrides; stable ids, a sheet holding two tables; approval carry-over; every failed unit and lost capability that makes a table incomplete;
    • typing of every type (a time zone to UTC, durations, 29-digit decimals); whole-row rejection with a bounded sample; a number that rounds up to 10^29, an f value past a double, instants outside 1900-2299; batches; slot rows; a fraction returned as text;
    • dataset_get and dataset_approve over faked stores: approval without a retype keeps the snapshot; a retype makes a new one from the stored artifact; the approver as the catalog row's actor and source class while a retype's rows keep the uploader's; an unknown column; a stale snapshot; an approval overtaken by another approval or by a new revision; a retype refused or cut off at the claim; an approval stamped before another that switched first; a catalog insert that may have landed; every sample in one read.
  • Unit (tests/unit/test_knowledge_ingest.py): a CSV ingest writes its dataset before the document row with the job's identity; a new revision keeps an approval of the same columns and drops the old snapshot; a metadata edit keeps serving the datasets and their approval, waits for the claim, and is refused when another write landed while it waited; other columns are inferred again; a failed publish deletes its own datasets; a publish that fails between a table's batches deletes the rows it wrote; an erasure between batches stops the rest; a delete between a first ingest's batches leaves its rows whole; a delete of a deleted source clears a snapshot left behind; an approval landing mid-publish runs the job again, uncounted, and the next run carries it.
  • Unit (tests/unit/test_knowledge_shared_blob.py): knowledge.delete and knowledge.bulk_delete delete the datasets of each source's retired version; a bulk delete of sources already deleted still deletes what is left of theirs.
  • Integration (tests/integration/test_knowledge_ingest_live.py) against a real server, ClickHouse and extractor: ingest a CSV, read the stored rows back, refuse a stale approval, retype a column (one rejected row), read the new snapshot, delete, and see both tables' rows go; a bulk delete of two sources at different document versions removes both, and keeps the rows of a running ingest of one of them at a version between the two (a single bound for both would take them); a delete bounded before a metadata edit keeps the rows of the snapshot the edit carried; a two-sheet workbook with a cell error is sampled in one read, each sheet cut to the rows asked for and read past its rejected row. Eleven tests passed against a server running the head; the leakage suites passed (42 passed, 17 skipped).
  • Probes, each a short script against a local ClickHouse: datasets.publish of 100,000 rows × 12 columns took 6.0 s with a 167 MB peak RSS (resource.getrusage); the sample read's rows read are from system.query_log after SYSTEM FLUSH LOGS.
  • Full unit suite: run on the rebased head before the merge.

🤖 Generated with Claude Code

Each table of a CSV or XLSX artifact becomes a typed dataset: a catalog row in
loom.datasets and its rows in loom.dataset_rows (migration 083), a column's
values in the typed slot its schema names. The ingest job writes them before
the document row, which is the switch that serves them; older snapshots go by
lightweight DELETE once it lands. knowledge.dataset_get reads a source's
datasets, their schema, what needs an answer, the rows rejected and a sample;
knowledge.dataset_approve approves a schema and retypes columns from the
stored artifact under a new snapshot, checked under the source's claim. A
metadata edit of an ingested document carries its datasets; deletes remove
the datasets of the versions they retire. Erasure deletes both tables with
the documents.

Refs #157

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 2 commits November 5, 2026 08:14
The public SDK surface snapshot gains client.knowledge.dataset_get and
dataset_approve, and the delete tombstone test fakes the dataset cleanup the
deletes now make, asserting each passes the version it read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 75f329d into main Nov 5, 2026
17 of 18 checks passed
@zlareb1
zlareb1 deleted the feat/datasets branch November 5, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant