Quality and governance
Fail the daily run when silver does not match the extract, record tables and lineage in a catalog, and mask national IDs and mobile numbers before anyone reads them.
A pipeline that loads bad data on time is worse than a pipeline that is late. The orchestration lab already fails the accounts run when the day's silver slice is empty. This lab replaces that empty check with tests a person can read, then records what the table means and where it came from. Masking sits in the same step: a national ID that reaches an analyst's query has already left the building.
The tables are the Iceberg tables from the lakehouse lab. The job that writes them is the Spark lab. Checks run after the write, in the same quality_accounts task the DAG already waits on.
Prerequisites
- The
data-pipelinenamespace, thelakehousesecret, andsilver.accountsloading for a known date - The
pipeline-stepsimage from the orchestration lab, withgreat_expectationsinstalled in it. Bake the library into the image. A job that runspip installat start will fail on a cluster that cannot reach the internet - OpenMetadata running in the
openmetadatanamespace, serviceopenmetadataon port 8585 - Trino's coordinator,
trino-coordinator, using the samelakecatalog as Spark oc, and theDTof a day you have already extracted, inYYYY-MM-DD
oc get svc openmetadata -n openmetadata
oc get route -n openmetadata
Four controls
| Control | Question it answers | Where it lives |
|---|---|---|
| Checks | Did this load follow the rules? | A step that exits non-zero |
| Catalog | What is this table, and who owns it? | OpenMetadata, crawled from Trino |
| Lineage | Which job wrote it, from which files? | OpenLineage events from Spark and Airflow |
| Masking | Can an analyst see a raw national ID or mobile number? | The silver write, checked like any other column |
DataHub is the same kind of catalog: search, owners, a glossary and a lineage graph. We run OpenMetadata. A platform that already runs DataHub sends the same events to its collector and keeps the checks unchanged.
Checks that fail the run
Two tests, in one script, /opt/jobs/quality_accounts.py. The first is a reconciliation: every id in the day's extract is in silver.accounts after the merge. The second is a Great Expectations suite on those silver rows. DT comes from the scheduler. The script refuses anything that is not a date before it pastes the value into SQL.
import os
import re
import sys
import great_expectations as gx
from pyspark.sql import SparkSession
dt = os.environ["DT"]
if not re.fullmatch(r"\d{4}-\d{2}-\d{2}", dt):
sys.exit(2)
spark = SparkSession.builder.getOrCreate()
extract = spark.read.option("header", True).csv(
f"s3a://lakehouse/bronze/accounts/dt={dt}/"
)
extract = extract.selectExpr("cast(id as bigint) as id", "account_no", "balance")
silver = spark.table("lake.silver.accounts")
missing = extract.join(silver, "id", "left_anti")
if missing.head(1):
missing.show(20, truncate=False)
sys.exit(1)
day = silver.join(extract.select("id"), "id", "inner")
context = gx.get_context(mode="ephemeral")
suite = context.suites.add(gx.ExpectationSuite(name="silver_accounts_day"))
suite.add_expectation(gx.expectations.ExpectTableRowCountToBeBetween(min_value=1))
suite.add_expectation(gx.expectations.ExpectColumnValuesToNotBeNull(column="id"))
suite.add_expectation(gx.expectations.ExpectColumnValuesToBeUnique(column="id"))
suite.add_expectation(gx.expectations.ExpectColumnValuesToNotBeNull(column="account_no"))
suite.add_expectation(gx.expectations.ExpectColumnValuesToNotBeNull(column="balance"))
source = context.data_sources.add_spark(name="lake")
asset = source.add_dataframe_asset(name="silver_accounts_day")
batch = asset.add_batch_definition_whole_dataframe("whole")
vd = gx.ValidationDefinition(name="silver_accounts_day", data=batch, suite=suite)
result = gx.Checkpoint(
name="silver_accounts_day", validation_definitions=[vd]
).run(batch_parameters={"dataframe": day})
if not result.success:
print(result)
sys.exit(1)
balance is only checked for null. An overdraft is a negative number, and a check that demands balance >= 0 would fail a real book. Add a range check when the column's rule is actually a range.
Soda Core is the YAML form of the same idea (row_count > 0, missing_count(account_no) = 0, duplicate_count(id) = 0) and it also exits non-zero. We use Great Expectations here because the suite runs on the dataframe the Spark job already holds.
Run it the way the orchestration lab runs the quality task. The logical date is an argument, so a rerun of 28 October checks 28 October.
oc exec -n airflow deploy/airflow-scheduler -- \
airflow tasks test accounts_daily quality_accounts 2026-10-28
A failed task stops the DAG. Retries are for a node eviction, which the orchestration lab already set. A failed expectation waits for a person. Read the driver log: missing.show prints ids that never landed, and the checkpoint prints which expectation failed.
Catalog
OpenMetadata's Trino connector crawls lake.silver and lake.gold on a schedule. After the next crawl, silver.accounts has a page. Put the owner and one sentence of meaning on that page ("one row per account, after the daily merge"). The crawl does not invent the meaning. Someone who knows the table types it, or the description is applied from Git with the API. Either way it is not a comment buried in a SQL file.
oc get pods -n openmetadata
curl -sS -H "Authorization: Bearer ${OM_TOKEN}" \
"http://openmetadata.openmetadata.svc:8585/api/v1/search/query?q=silver.accounts&index=table_search_index"
OM_TOKEN is a bot JWT from OpenMetadata. Keep it in a secret. The route host from oc get route -n openmetadata is the page people search. Access to the route follows the same roles as the rest of the cluster.
Tag the sensitive columns when you add them in the next section. We use a classification PII and tags NID and Mobile. A tag without a check is a label. The check below is what makes the label true.
Lineage
Spark emits an OpenLineage event when the merge reads the day's CSV and writes silver.accounts. Airflow emits one for the DAG run. OpenMetadata stores both, and the table page shows the file and the task. Bake openlineage-spark into spark-iceberg:3.5. Do not set spark.jars.packages on an air-gapped cluster.
Add this to the sparkConf of accounts-silver. The API key is an environment variable from the openmetadata-bot secret, on the driver, not a value in Git.
spark.extraListeners: io.openlineage.spark.agent.OpenLineageSparkListener
spark.openlineage.transport.type: http
spark.openlineage.transport.url: http://openmetadata.openmetadata.svc:8585
spark.openlineage.transport.endpoint: /api/v1/openlineage/lineage
spark.openlineage.transport.auth.type: api_key
spark.openlineage.namespace: data-pipeline
- name: OPENLINEAGE__TRANSPORT__AUTH__API_KEY
valueFrom:
secretKeyRef:
name: openmetadata-bot
key: token
Point the Airflow scheduler's OpenLineage transport at the same URL and endpoint. The token comes from the same secret. After one successful accounts_daily run, open silver.accounts in the catalog and confirm the upstream file and the DAG are both drawn. A table with no edge means the listener did not load: the driver log says so in the first few lines.
Masking
silver.accounts, as the Spark lab creates it, has no national ID and no mobile number. Those land in a separate bronze prefix, bronze/parties/, and the published table is silver.parties. Analysts are granted that table. They are not granted the bronze prefix. A Bangladesh national ID is 10, 13 or 17 digits. A mobile number here is 11 digits beginning with 01. The silver job keeps the last four digits of the ID and the first three and last three of the mobile, and drops everything else.
CREATE TABLE IF NOT EXISTS lake.silver.parties (
id bigint,
nid string,
mobile string
) USING iceberg;
INSERT OVERWRITE lake.silver.parties
SELECT
cast(id AS bigint) AS id,
CASE
WHEN regexp_replace(nid, '[^0-9]', '') = '' THEN NULL
ELSE concat('****', right(regexp_replace(nid, '[^0-9]', ''), 4))
END AS nid,
CASE
WHEN regexp_replace(mobile, '[^0-9]', '') = '' THEN NULL
ELSE concat(
left(regexp_replace(mobile, '[^0-9]', ''), 3),
'****',
right(regexp_replace(mobile, '[^0-9]', ''), 3)
)
END AS mobile
FROM parties_raw;
parties_raw is that bronze CSV, loaded with a header row and UTF-8, the same way accounts_silver.py reads the accounts extract.
INSERT OVERWRITE replaces the table's contents, so a second run does not append a second copy. When a join needs a stable key and nobody needs to see the digits, write sha2(regexp_replace(nid, '[^0-9]', ''), 256) instead of the mask, and keep the raw value in bronze only.
The mask is a check, not a comment. Run it on silver.parties with the same Great Expectations pattern as the accounts suite:
suite.add_expectation(gx.expectations.ExpectColumnValuesToMatchRegex(
column="nid", regex=r"^\*{4}[0-9]{4}$"))
suite.add_expectation(gx.expectations.ExpectColumnValuesToMatchRegex(
column="mobile", regex=r"^[0-9]{3}\*{4}[0-9]{3}$"))
Run both expectations on the non-null rows. An empty source value is stored as null, and a null is not a broken mask. Then read what Trino will actually return:
oc exec -n data-pipeline deploy/trino-coordinator -- \
trino --execute "SELECT nid, mobile FROM lake.silver.parties LIMIT 5"
A 13-digit value in that result is a failed release. Fix the write, rerun the check, and only then let the DAG go on. Bronze still holds the raw file for the platform team, under a catalog role the analyst group does not have. Time travel on silver.parties shows the masked snapshots. It is not a side door back to the raw ID, because the raw ID was never written there.
What's next
- Pipelines for AI: document, feature and training-data flows. A training folder is cut only after these checks pass.
- Orchestration:
quality_accountsis already the last task inaccounts_daily. Point it at this script. - Data lakehouse: bronze, silver and gold, and the snapshots a bad load rolls back to.
- Apache Spark: the merge this check reads, and the
sparkConfblock the listener is added to.
The pipeline overview is why a check fails the run instead of sending a warning nobody sees at 2 a.m.