ADR-0044: Model contracts¶
Architecture: Section 4, Configuration, section 9, Schema change, and section 10, Data quality
Context¶
A model's output schema is whatever its body returns. on_schema_change (ADR-0026) only compares the batch with the existing target, so a first build, an overwrite or a full refresh accepts any schema, and a downstream consumer learns about a changed type from its own failure. dbt calls the fix a model contract: declared columns with a type and a nullability, enforced at build time, where an enforced contract must match the column set exactly (dbt model contracts). pipeloom already has a logical type vocabulary (pipeloom.engine.types: int64, decimal(18,2), timestamp[us, UTC], ...) that every engine reports through EngineSession.schema, and an evaluate primitive that counts nulls the same way on every engine (ADR-0028).
Options¶
- Column set.
- Exact: every declared column is present, no other column is. The written table is what the contract says.
- Subset: declared columns must be present, others pass. Extra columns reach the table unseen, which is what a contract is meant to stop.
- Types.
- Exact equality of the logical type string.
- Accept a batch type that widens to the declared one and cast it. Needs a cast step before the schema policy and makes the written type depend on the cast.
- Nullability.
- Read the engine's schema nullability. polars and pandas do not track it, and Spark marks most computed columns nullable.
- Count nulls with
EngineSession.evaluateand anot_nullcheck per non-nullable column: one aggregation, the same semantics on every engine, and the expectation suite already compares each engine with the reference. - Where the check runs. Before the expectations, or after them on the rows that will be written.
- Hashes. Keep
columnsout of the node checksum and plan hash, or add it only when it is declared.
Decision¶
- Config. A model may declare
columns, a list ofname,dtypeandnullable(default true). It is a model key, not a default:_defaults.ymlanddefaults.modelscannot set it.dtypemust be a logical type name ofpipeloom.engine.types(LOGICAL_TYPE_PATTERN, also in the JSON Schema); a duplicate column name is rejected. Both are PLS102 at L1. - Exact match. The batch must have exactly the declared column names (case-sensitive, order ignored) and each column's logical type must equal the declared
dtypestring. No widening and no cast: the user casts in the body, so the type is visible where it is produced. Engines report fixed forms (Spark timestamps are alwaystimestamp[us, UTC]), so a project that targets several engines declares the form they share. - Nullability. For every column with
nullable: false, oneevaluatecall counts rows with a null (NaN counts as null, as in expectations). A lazy relation is persisted first, as the quality step does. Engines need thequalitycapability; PLC510 now also covers a non-nullable contract column on an engine without it. Every built-in engine has it. - Order. The contract is checked after the expectations, on the kept rows, and before the schema policy, the quarantine write,
alignand the write. An expectation that drops null rows therefore runs before the contract checks the column. A violation is PLM208, permanent (never retried underretry_on: transient), and nothing is written, the quarantine table included. - Schema-change policy. Unchanged. The contract checks the batch;
on_schema_changestill compares the batch with the target. With a contract the batch schema is fixed, so the policy only acts when the target drifted or the contract itself changed. - Materializations. Tables, incremental models and intermediate models are checked. Views and ephemeral models produce no batch of their own, so
columnson them is PLP414 (L4). Python and SQL models are treated the same. - Manifest and hashes.
nodes[].columnsis an additive field ofpipeloom.manifest/1, present only when a model declarescolumns. The node checksum and the plan hash cover it only then, so a contract change selects the node understate:modifiedand changes the plan hash, while a project without contracts keeps byte-identical manifests, checksums and plan hashes. - Docs site. A model page shows a Columns table when the node has a contract.
- Codes. PLM208 (runtime) and PLP414 (L4) are new in 0.9. PLM209 stays free.
Consequences¶
- A contract stops a changed output before any write, on every engine, at the cost of one schema call and, with non-nullable columns, one aggregation per write.
- Exact types are strict across engines: a polars body that yields
timestamp[ns]fails atimestamp[us]contract until it casts. This is deliberate; a widening mode can be added later as an option. - Lineage, the OpenLineage schema facet and column-level lineage can read
nodes[].columnswithout running anything. - Contracts do not check sources, and they carry no column descriptions yet.
Status¶
Accepted. Amended in 0.9 by ADR-0050.
Date¶
2026-11-06
Amendment 0.9 (ADR-0050)¶
With Python-first authoring (ADR-0050) columns comes from the @model decorator (a list of Column(...) or plain mappings) or from the columns of a properties entry. It stays a model key: the models: tree of pipeloom.yml cannot set it, as _defaults.yml and defaults.models could not. When both set it, the decorator's list replaces the properties list whole; the lists are never merged column by column. ColumnConfig gains an optional description, kept out of the node checksum and the plan hash, so editing a description neither marks a model modified nor makes a deployed job stale. Where descriptions appear in the manifest and on the docs site is settled later in 0.9, outside the plan hash. Every model is a Python function, so the line about Python and SQL models no longer applies. PLP414 does not change.