Sub topic 3
Gold: Tables Shaped for Business Consumers
Assume you know basic SQL, PySpark DataFrames, and Databricks notebooks. The gold layer contains curated tables shaped for a specific business use, such as a dashboard, finance report, or machine-learning feature.
Gold tables for business questions
Gold tables hide pipeline details from consumers. Instead of asking analysts to filter raw orders and define revenue themselves, provide a table with one row per day:
CREATE OR REPLACE TABLE main.gold.daily_revenue AS
SELECT
CAST(order_ts AS DATE) AS order_date,
COUNT(*) AS orders,
SUM(amount) AS revenue
FROM main.silver.orders
WHERE status = 'completed'
GROUP BY CAST(order_ts AS DATE);The table has a stable grain: one row per order_date. Document that grain and the meaning of each metric. Create separate gold tables when different consumers need different grains or definitions.
Building the layers declaratively
A declarative pipeline describes the desired tables and their dependencies. Databricks determines processing order and manages incremental updates, rather than requiring each notebook to coordinate the next step.
import dlt
from pyspark.sql import functions as F
@dlt.table
def orders_bronze():
return spark.readStream.format("cloudFiles") \
.option("cloudFiles.format", "json") \
.load("/Volumes/main/sales/landing/orders/")
@dlt.table
@dlt.expect_or_drop("valid_order_id", "order_id IS NOT NULL")
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
def daily_revenue_gold():
return dlt.read("orders_silver").where("status = 'completed'") \
.groupBy(F.to_date("order_ts").alias("order_date")) \
.agg(F.count("*").alias("orders"), F.sum("amount").alias("revenue"))Scheduling with jobs
Use a Databricks job to run the pipeline on a schedule or after an upstream task. Set retries, failure notifications, and a timeout. Choose a trigger that matches freshness needs. An hourly job costs less than continuous processing when hourly data is sufficient.
Permissions and governance
Grant analysts USE CATALOG, USE SCHEMA, and SELECT on main.gold. Restrict bronze and silver to engineering or approved service principals. Unity Catalog permissions, table comments, lineage, and audit logs make ownership and access reviewable.
Maintenance and trade-offs
Compact small files, remove obsolete files with an appropriate retention policy, and inspect query plans. Add clustering or partitioning only when access patterns justify it.
The medallion pattern adds boundaries, storage, and operational work. Keep fewer layers for a small, stable dataset. Add specialized marts when one gold table cannot serve different grains or security rules.
Exercise
Which layer should a dashboard query, and why?
Show answer
The gold layer, because it contains documented, business-shaped results without requiring the dashboard to repeat transformation logic.