ADR-0008: Incremental key, watermark state, strategies¶
Architecture: Section 9, Incremental processing, write strategies, schema change
Context¶
Incremental logic must be declared, not inferred (lesson L12), and must survive crashes and orchestrator retries. Every write option must be applied by each strategy or rejected (L9).
Options¶
- Watermark kept in the target (
max(column)of the target). No extra store, but needs the target to exist and read access to it. - Watermark in a
StateStorewith compare-and-swap. Works for targets that cannot be queried cheaply, at the cost of a store. - Both, selected per model with
watermark_source: state | target.
Decision¶
A declared key plus a lookback, with watermarks in a StateStore. watermark_source: target reads max(column) of the target instead.
- Per unit: determine
is_incremental, read the watermarkw, filter the input withfilter_since(input, column, w - lookback), write, thencas_watermark(expected=w, new=max_value(filtered input)). A CAS failure is PLM203. - State stores:
sqlite(core, local),json(core, atomic rename),table(spark plugin).boundary: taskrequirestableorjsonon a shared path. - Strategies: append, overwrite, replace_partitions, merge, scd2. Each returns a
WriteResult. - Idempotency: each write receives
token = "<run_id>:<model>". Engines without idempotent writes declareidempotent_writes=False. on_schema_change:fail(default),ignore,append_new_columns,sync_all_columns.- Maintenance (
optimize,vacuum, clustering) runs only frommaintenance:blocks, never by default.
Consequences¶
- The watermark is committed after the write. A crash between the two reprocesses at most the lookback window plus the batch. That is safe for merge, overwrite and replace_partitions, and for append only when the engine has idempotent writes (otherwise PLC505 forbids retries).
- A schema change under the default policy fails the unit (PLM202). Every detected change emits
model.schema_changed.
Amendment (0.9): SCD2¶
The scd2 strategy keeps a type 2 history of each unique_key.
- No new config keys:
unique_key,tracked_columnsand thedeletesblock are reused, so manifests, checksums and plan hashes of projects without scd2 stay byte-identical.tracked_columnsleft unset means every business column except theunique_keycolumns and thedeletes.column; an explicit empty list is PLP416. - System columns have fixed names:
valid_fromandvalid_to(timestamp[us, UTC]) andis_current(boolean). A rename option can come later. - A change is detected by comparing the tracked columns directly, null-safely, with NaN counted as null. There is no row hash: it would depend on the engine and go stale when
tracked_columnschanges. A change in an untracked column is ignored. valid_fromandvalid_tocome from the run's start time (WriteConfig.run_started_at), the same on every retry of a run, so a re-run converges. Timestamps taken from a source column are a non-goal.- Deletes use the existing block with
mode: hardonly (softis PLP416). A flagged row closes the key's current row; a flagged row for an unknown key is ignored. Keys missing from the batch are not closed. A closed key that comes back gets a new current row. - A null key, a key repeated in one batch, a batch column named like a system column, or an existing target without the system columns is PLM206 (
WriteKeyError); the last one asks for--full-refresh. - The first load inserts every non-delete row as current. A full refresh drops the history and reloads as a first load: scd2 keeps its strategy under full refresh. A retry with the same token replays the stored result (ADR-0032); a re-run with an unchanged batch adds no versions.
- Schema policy: scd2 keeps rows. The runner strips the system columns from the target schema before the policy, so the policy acts on business columns only.
- A model contract (ADR-0044) describes the body output, so a system column there is PLP416. PLP416 also covers a
unique_key,partition_by,cluster_by,deletes.columnor tracked column that names a system column, and tracked columns that overlap theunique_key. - Counts reuse the
WriteResultfields:rows_insertedis new current rows,rows_updatedis rows closed by a change,rows_deletedis rows closed by a delete,rows_writtenis their sum. WriteConfiggainstracked_columns(filled by the runner; empty only when the table has no business column besides the key and the delete column) andfull_refresh, appended with defaults, and the engine types gainSCD2_COLUMNS. This is additive and stays within plugin API (0, 5).- Engines without scd2 get PLC501. The memory engine does not declare it; polars and pandas do (they share the filesystem write code, which rewrites the whole table on each scd2 write, like merge).
Status¶
Accepted
Date¶
2026-10-29