Databricks DE Β· Topic 5.3
Boolean rules on each row in an SDP pipeline. On violation: warn (keep + metric), drop (remove), or fail (abort) β plus the quarantine pattern. This deep dive walks each with the code.
βΈ rule: amount >= 0 Β· two rows violate it
| Action | Bad row is⦠| Update | Metric? | Use for |
|---|---|---|---|---|
| Warn (default) | kept & written | continues | β yes | track quality without blocking |
| Drop | removed before write | continues | β yes | keep the table clean |
| Fail | β | aborts / rolls back | β no | truly critical invariants |
CREATE OR REFRESH STREAMING TABLE silver_orders ( CONSTRAINT valid_id EXPECT (order_id IS NOT NULL) ON VIOLATION DROP ROW, -- drop CONSTRAINT valid_amt EXPECT (amount >= 0), -- warn (default) CONSTRAINT has_date EXPECT (order_date IS NOT NULL) ON VIOLATION FAIL UPDATE -- fail ) AS SELECT * FROM STREAM bronze_orders;
from pyspark import pipelines as dp @dp.table @dp.expect("valid_amt", "amount >= 0") # warn @dp.expect_or_drop("valid_id", "order_id IS NOT NULL") # drop @dp.expect_or_fail("has_date", "order_date IS NOT NULL")# fail def silver_orders(): return spark.readStream.table("bronze_orders") # group many with a dict: @dp.expect_all / _all_or_drop / _all_or_fail RULES = {"valid_id": "order_id IS NOT NULL", "valid_amt": "amount >= 0"} @dp.table @dp.expect_all_or_drop(RULES) # drop a row failing ANY rule def silver_orders2(): return spark.readStream.table("bronze_orders")
Conditions are SQL booleans β no Python UDFs, external calls, or subqueries referencing other tables. (Legacy @dlt.expect* still works; prefer pyspark.pipelines.)
Drop discards bad rows; fail halts the pipeline. To retain suspect rows, split the stream on the rules: clean β silver, bad β quarantine.
from pyspark.sql.functions import expr RULES = {"valid_id": "order_id IS NOT NULL", "valid_amt": "amount >= 0"} valid = " AND ".join(RULES.values()) @dp.table def silver_orders(): # clean rows only return spark.readStream.table("bronze_orders").filter(expr(valid)) @dp.table def quarantined_orders(): # bad rows kept for review/replay return spark.readStream.table("bronze_orders").filter(expr(f"NOT({valid})"))
order_id, warns on amount < 0 (tracked so analysts notice), and fails the update if a critical reference table is empty.expect_all_or_drop/_or_fail dict triggers the action.@dlt.expect* in new code β prefer from pyspark import pipelines.