Sub topic 13
Organizing data with the medallion pattern
This chapter assumes you can write basic SQL and PySpark. It also assumes you know that Delta tables in Unity Catalog are addressed as catalog.schema.table.
What the pattern is
The medallion pattern is a way to organize tables in a lakehouse into layers. Each layer holds data that is cleaner and more useful than the layer before it. The three layers are bronze, silver, and gold.
| Layer | Contents | Typical consumer |
|---|---|---|
| Bronze | Raw data, kept as it arrived | Data engineers |
| Silver | Cleaned, deduplicated, validated data | Engineers, data scientists |
| Gold | Aggregated data shaped for a business question | Analysts, dashboards |
Data moves in one direction. Each step reads from one layer and writes a Delta table in the next.
Why layers help
Keeping bronze means you can rebuild silver and gold if you find a bug in your cleaning logic. You do not need to fetch the source data again. Because every layer is a Delta table, each one also has a transaction log and versioned history. You can inspect or roll back a bad write.
Layers also give you natural permission boundaries. You might let engineers read bronze, while analysts see only gold.
A worked example
Suppose the main.tutorial.orders table from earlier chapters acts as bronze. The silver step removes invalid rows and duplicate order IDs. The gold step totals spend per customer.
CREATE SCHEMA IF NOT EXISTS main.tutorial;
CREATE OR REPLACE TABLE main.tutorial.orders_silver AS
SELECT DISTINCT order_id, customer_id, amount, order_ts
FROM main.tutorial.orders
WHERE order_id IS NOT NULL
AND amount > 0;
CREATE OR REPLACE TABLE main.tutorial.customer_totals_gold AS
SELECT customer_id,
SUM(amount) AS total_amount,
COUNT(*) AS order_count
FROM main.tutorial.orders_silver
GROUP BY customer_id;
SELECT * FROM main.tutorial.customer_totals_gold;SELECT DISTINCT removes only rows that are identical in every column. If two rows share an order_id but differ in another column, both survive. Deduplicating by key usually needs a window function or a MERGE.
Common errors
- Cleaning in bronze. Changing data on the way in destroys your ability to replay it. Add metadata columns if you need them, but keep the original values.
- Gold tables built from bronze. This skips validation, so bad rows reach reports. Build gold from silver.
- Full rewrites at scale.
CREATE OR REPLACE TABLErecomputes everything. It is fine for small tables. For large ones, use incremental logic such asMERGE INTOor a streaming read. - Treating the names as rules. Bronze, silver, and gold are a convention. Some teams add a fourth layer or use only two. Choose what fits your data.
Connecting it to jobs
Each transformation can be a notebook task in a job. Bronze-to-silver runs first, and silver-to-gold depends on it. The job's task dependencies enforce the order.
Exercise
An analyst reports that a dashboard shows duplicate orders. Your gold table is built directly from bronze. Which change fixes the design, and why?
Show answer
Add a silver step that deduplicates and validates the data, then build gold from silver. Gold should read only from validated data, so bad rows cannot reach reports. Bronze stays unchanged so you can replay it.