Engineering notes

What a data contract can check, and what it can't

Three scouts bring back reports about bison, wolves and apples. What can we check before the leader makes a decision?

Braidplane engineering · Published · Updated

What does “passed” mean in a data pipeline?

This question is probably as old as data itself. The word “pipeline” goes back to the 19th century, when it meant actual pipes. Passing information around is much older.

Imagine a few scouts coming back from their shenanigans. One saw 10 bison, another a few wolves, the last one brought back a bunch of apples. One of them gathered all this info, transformed it in his head and passed it to the pack leader. The pack just ran a full ETL pipeline.

But can the leader now validate all of this? He wasn't there. He can check if the reports make sense, but he doesn't know how many bison, wolves or apples were out there. For that, he trusts the scouts or goes and checks himself.

Rough stick-figure drawing of three scouts reporting bison, wolves and red fruit to a collector, who passes their report to a leader with a question mark above his head.

The leader could ask the scouts to agree on what a report should contain. Which scout sent it, what did they find, how many? A modern data pipeline can check those rules as the reports arrive. That's our data contract.

Our scouts send one report each from this trip. We give them IDs so we know who reported what. In a file, their reports could look like this:

[
  {"scout_id": "scout-1", "subject": "bison", "count": 10},
  {"scout_id": "scout-2", "subject": "wolves", "count": 2},
  {"scout_id": "scout-3", "subject": "apples", "count": 3}
]

For each report we require scout_id, subject and count. In this example, count must be between 0 and 100.

The limit of 100 is just a choice for this example. It would reject a real sighting of 101 bison too, so we'd need to raise it if larger counts were allowed.

The validator receives these reports and the contract. It doesn't receive a list of the scouts we sent out.

Giving the scouts a contract

This is the contract in Braidplane's format for rows.

{
   "name":"scout-report",
   "representation":"rows",
   "fields":[
      {
         "name":"scout_id",
         "type":"string",
         "required":true
      },
      {
         "name":"subject",
         "type":"string",
         "required":true
      },
      {
         "name":"count",
         "type":"number",
         "required":true,
         "validations":{
            "min":0,
            "max":100
         }
      }
   ]
}

The validator call used here checks required/non-null fields and numeric bounds. It doesn't cover every type or structural rule, or execute ODCS quality declarations. We haven't added rules for allowed subject names or whole-number counts.

Which reports pass?

Results using the pinned Core version
ChangeField checksWhy
Leave out the countFailsThe count is required.
Report 101 bisonFails maxThe count is above our limit of 100.
Report 10 bison when the reference says 12PassesThe contract doesn't compare the count with the reference.
Leave out the wolf scout's reportPassesThe validator doesn't know which scouts should report back.

The first scout's report says 10 bison. Our test has a separate reference observation of 12. The report passes the range check, and comparing it with the reference finds the difference. We supplied that reference ourselves. On a real hunt, two different counts would need investigating.

If scout-2 never reports back, the bison and apple reports still pass. Our test catches this by comparing the IDs received with a separate list of the scouts we expected.

Did the right report reach the leader?

Suppose we change how the reports are collected or combined. Before the leader sends everyone out, I'd check a few things about the quality of the reports:

  • Compare what each scout sent with what the leader received, using scout IDs. If scout-1 reported 10 bison, the leader should also get 10.
  • We sent three scouts and got three reports. Looks fine, unless one scout reported twice and another never reported at all. Compare the scout IDs, not just the number of reports.
  • A wolf report from yesterday may be no use for today's hunt. Check when and where the sighting happened. We'd need to add those fields to the small contract above.
  • Read the final report after the pipeline finishes. Check the apple count there, even if it looked correct halfway through.

Freshness, duplicates and comparisons between datasets are data quality checks. For our scouts, they need more than a count in an allowed range: observation times, expected scout IDs and reports from both ends of the pipeline. Data quality tools can run these checks using that extra information.

Contracts can describe these checks too. ODCS has a section for data quality rules, including SQL and custom checks. A tool still has to support and execute them against the data.

Checking this in Braidplane

In Braidplane, contracts live with the pipeline definition. Run and step records help us follow what happened to the reports. The example in this post tests the validator in memory; it doesn't run a scout-report pipeline.

Our current data quality support is limited to ODCS and field validations. Freshness checks and comparisons between datasets need separate checks or tools.