28 / 35 · The Data System
Data contracts and quality
Use a sales feed to distinguish valid encoding, valid records, a complete interval and a compatible contract change.
The daily sales file parses. Its IDs and amounts are integers. But the first sale appears twice, another line says that two units at 450 cents cost 990 cents, and one expected store is missing. A schema that only names the columns and their types accepts the file.
State the unit of the data#
A data contract records what a producer supplies and what consumers may rely on. For this feed, one row is a line within an order, and an order ID is unique across stores. The pair (order_id, line_no) identifies a row. The contract specifies positive quantities, nonnegative unit prices, an amount equal to quantity times price, and one completion record per expected store for a finished day. Prices use cents. A change to dollars changes the contract even if the type remains an integer. The Open Data Contract Standard distinguishes schema, data quality and service-level sections. ODCS, The basics of a data contract
Checks for a daily sales partition
Work through the violations#
The following checks use just four lines. An amount rule compares every row to its inputs. A key rule compares rows to each other. Use a separate producer completion record to detect a missing store. With only sales rows, a store with no sales can be indistinguishable from a store that has not sent its data.
WITH feed(order_id, line_no, qty, unit_cents, amount_cents) AS (
VALUES (201, 1, 2, 350, 700),
(201, 1, 2, 350, 700),
(202, 1, 1, 600, 600),
(203, 1, 2, 450, 990)
)
SELECT order_id, line_no, 'duplicate key' AS issue
FROM feed
GROUP BY order_id, line_no
HAVING COUNT(*) > 1
UNION ALL
SELECT order_id, line_no, 'amount mismatch'
FROM feed
WHERE amount_cents IS DISTINCT FROM qty * unit_cents
ORDER BY order_id, line_no, issue;
The duplicate check returns 201/1. The amount check returns 203/1 because . The checks say which rules failed. They do not establish whether the source amount or quantity was entered incorrectly. If we drop one duplicate and substitute 900, we get cents, but that is only a candidate repair. The producer still has to establish the correct record, and the completeness gate still applies. The W3C tabular model makes a similar distinction between parsing cells and validating declared keys. W3C, Validating Tables
Check compatibility in both directions#
Suppose a new Avro writer adds terminal_id. An old reader can ignore a field it does not know. A new reader reading an older file needs a default for the added field, or the record cannot be resolved. These are separate directions because the writer and reader schemas have different roles. A default of “unknown” can preserve decoding. If a new report requires the terminal for attribution, that default still fails the report’s semantic requirement. Avro 1.12.0, Schema Resolution
The publication gate can keep the old day’s result visible when a new candidate fails. Record the input version, rule and violating keys so the producer can correct the source or explicitly revise the contract. A successful validation says that the declared checks passed for the checked data. Rules with no check, including outside facts the producer did not provide, remain assumptions.
Next#
A failing key is useful only if we can locate its source and the results that already used it. Catalogs and lineage records what each run actually read and produced.
References#
- Bitol. Open Data Contract Standard. Describes the producer-consumer agreement and its schema, quality and service-level sections.
- W3C. Model for Tabular Data and Metadata on the Web. Recommendation, 2015. Validating Tables describes duplicate primary-key checks on parsed data.
- Apache Avro. Specification 1.12.0, Schema Resolution. Defines reader and writer resolution for added and missing fields.