Sub topic 9
Enforcing data quality between layers
This chapter assumes you can read the bronze, silver, and gold code from earlier chapters. It also assumes you know what a Delta table and a declarative pipeline are. Here you will decide where quality rules belong and what should happen when a row breaks one.
Where to check
Bronze keeps data exactly as it arrived, so it should reject almost nothing. Silver is the first layer that promises clean, typed data, so the main rules belong at the bronze-to-silver boundary. Gold should need few new rules, because it only aggregates data silver has already checked.
A data quality rule is a boolean SQL condition that every row should satisfy, such as amount >= 0. In Databricks pipelines these rules are called expectations.
Choosing warn, drop, or fail
Each rule needs a policy for violations.
| Policy | Decorator | Effect on bad rows | Use when |
|---|---|---|---|
| Warn | `@dlt.expect` | Rows are kept. Violations are counted in pipeline metrics. | The rule is new, or the issue is minor. |
| Drop | `@dlt.expect_or_drop` | Rows are removed from the target table. | A bad row would corrupt downstream results. |
| Fail | `@dlt.expect_or_fail` | The update stops and nothing is committed. | Bad data means a serious upstream fault. |
The _all variants (expect_all, expect_all_or_drop, expect_all_or_fail) take a dictionary of rules, so you can keep the rules in one place.
Dropping without losing data
Dropped rows disappear from silver, and that hides problems. A common fix is a quarantine table that holds the rows that failed. You can then inspect them, repair them, and replay them.
import dlt
from pyspark.sql import functions as F
rules = {
"valid_order_id": "order_id IS NOT NULL",
"positive_amount": "amount >= 0",
}
passes_all = " AND ".join(f"({r})" for r in rules.values())
@dlt.table(comment="Typed orders that pass every rule")
@dlt.expect_all_or_drop(rules)
def orders_silver():
return (
dlt.read_stream("orders_bronze")
.select(
"order_id",
"customer_id",
F.col("order_ts").cast("timestamp").alias("order_ts"),
F.col("amount").cast("decimal(12,2)").alias("amount"),
"status",
)
)
@dlt.table(comment="Bronze rows that failed at least one rule")
def orders_quarantine():
return dlt.read_stream("orders_bronze").where(
f"NOT COALESCE({passes_all}, false)"
)The COALESCE matters. A rule that evaluates to NULL is neither true nor false, so NOT (...) alone would skip those rows and they would vanish from both tables.
Rules outside pipelines
If you use plain notebooks and MERGE, Delta tables can enforce rules themselves with constraints.
ALTER TABLE main.silver.orders
ADD CONSTRAINT valid_amount CHECK (amount >= 0);A write that violates a CHECK constraint fails, which is the "fail" policy. Adding the constraint also fails if existing rows already break it, so clean the table first.
Common errors
- Strict rules on bronze. Dropping or failing at bronze defeats replayability. Keep bronze permissive.
- Failing on everything. One malformed row then blocks the whole update. Use fail only for rules where bad data means the pipeline itself is wrong.
- Never reading the metrics. Warn rules do nothing unless someone checks the violation counts, so review them or alert on them.
- Checking before typing. A rule like
amount >= 0on a string column compares by implicit cast. Apply type-sensitive rules to the typed silver columns where you can.
Exercise
A rule status IN ('completed', 'pending', 'cancelled') is violated by about 0.1% of rows. Your team does not yet know whether the source is sending a new valid status. Which policy should you use first, and why?
Show answer
Use warn (@dlt.expect). The rows stay in silver and the violations are counted in the metrics. That gives you time to learn whether the new value is valid without losing data or stopping the pipeline. If the values turn out to be invalid, switch to drop and send the rows to a quarantine table.