For years, a production ETL pipeline on Databricks meant a pile of PySpark. SCD Type 2 dimensions, cost-controlled materialized views, idempotent reloads of late data: each one was code you wrote and maintained. That has changed. AUTO CDC handles SCD Type 2 dimensions. REFRESH POLICY controls what a materialized view costs to keep current. REPLACE WHERE makes late-arriving data safe to reload. You can build a full e-commerce warehouse, bronze through platinum, and write every layer of it in Databricks SQL.
Everything below is plain DBSQL: CREATE STREAMING TABLE, AUTO CDC flows, REPLACE WHERE flows, and metric views. Every layer runs directly on a serverless SQL warehouse. You can paste it into the SQL editor and build the whole warehouse, bronze through platinum, with no pipeline and no external orchestrator. When you take it to production you have a choice. You can keep running it in DBSQL, or you can package Bronze through Gold (the streaming tables, the AUTO CDC dimension, and the REPLACE WHERE flow) as a single Lakeflow Spark Declarative Pipeline (SDP), which adds a managed DAG, a shared event log, and clean promotion across dev, staging, and prod through a Databricks Asset Bundle. The Platinum metric views stay in DBSQL either way, because a metric view lives in the SQL warehouse rather than inside a pipeline. We cover deployment at the end.
For comparison, think about how you would have built this before these primitives existed. A traditional warehouse stack would use stored procedures and hand-written MERGE INTO statements; a Spark shop would use PySpark notebooks orchestrated by Workflows. Either way you ended up with a 200-line MERGE for the SCD2 dimension, a script that allocated surrogate keys against a state table, and a separate nightly job that recomputed the gold aggregate from scratch. That pipeline produces correct numbers. It also takes a sprint to build, a couple of weeks to debug, and a senior engineer to keep running.
The version below is about 200 lines of SQL in one file, with the same governance and observability you already have. The difference is that you write the schema and the joins, and Databricks handles the plumbing underneath.
Domain: an e-commerce data warehouse. Three sources:
Targets:
You author everything in plain Databricks SQL, and it runs as written on a serverless SQL warehouse. For production, you can optionally package Bronze through Gold (the streaming tables, the AUTO CDC dimension, and the REPLACE WHERE flow) as one SDP pipeline, which infers the dependencies from the table references and runs them as a single DAG, and promote it across dev, staging, and prod with a Databricks Asset Bundle. The Platinum metric views are created directly in DBSQL on top of the Gold facts in either setup.
Two things shape bronze ingestion. First, the events are streaming, so the bronze tables need to be incremental. Second, line items can arrive up to 24 hours late. Bronze doesn't try to fix that: it lands every event append-only, exactly as it arrives, and leaves late-arrival reconciliation to the Gold layer. The bronze layer keeps a faithful, append-only record of the source and nothing more. Expectations (CONSTRAINT … EXPECT) handle bronze-layer data quality before bad rows poison anything downstream. (CONSTRAINT … EXPECT works on standalone DBSQL streaming tables created in the SQL editor, so you get expectations without deploying an SDP pipeline.)
One placement call worth flagging: we run expectations in bronze to drop bad rows before they land. The more common convention is to leave bronze exactly as it arrives and apply the drops in silver, so you can change a filter and reprocess from an intact bronze without re-ingesting the source. We drop at bronze here because these constraints catch hard parse and schema failures that can never become useful rows, and because the raw JSON still sits in the source volume, so a full refresh can rebuild bronze from scratch if a rule turns out too aggressive. If your expectations are business rules rather than structural checks, prefer the silver-drop pattern.
-- Customer CDC source (Delta with CDF + row tracking)
CREATE OR REPLACE TABLE ${target_catalog}.bronze.customer_changes (
customer_id INT,
email STRING,
segment STRING,
city STRING,
updated_at TIMESTAMP
)
TBLPROPERTIES (
delta.enableChangeDataFeed = true,
delta.enableRowTracking = true
);
-- Order events (streaming, append-only, clustered by order_date, with quality checks)
CREATE STREAMING TABLE IF NOT EXISTS ${target_catalog}.bronze.order_events (
CONSTRAINT valid_order_id EXPECT (order_id IS NOT NULL) ON VIOLATION DROP ROW,
CONSTRAINT valid_customer_id EXPECT (customer_id IS NOT NULL) ON VIOLATION DROP ROW,
CONSTRAINT non_negative_amt EXPECT (total_amount >= 0) ON VIOLATION DROP ROW
)
CLUSTER BY (order_date)
SCHEDULE EVERY 1 HOUR
AS
SELECT
o.order_id,
o.customer_id,
cast(o.order_ts AS DATE) AS order_date,
o.total_amount,
o.order_ts AS event_time
FROM STREAM read_files(
'/Volumes/${target_catalog}/raw/orders/',
format => 'json'
) AS o;
-- Line items (streaming, append-only; late arrivals reconciled downstream in Gold)
CREATE STREAMING TABLE IF NOT EXISTS ${target_catalog}.bronze.line_item_events (
CONSTRAINT valid_line_item EXPECT (line_item_id IS NOT NULL) ON VIOLATION DROP ROW,
CONSTRAINT valid_order_id EXPECT (order_id IS NOT NULL) ON VIOLATION DROP ROW,
CONSTRAINT non_negative_qty EXPECT (quantity > 0) ON VIOLATION DROP ROW
)
CLUSTER BY (order_date)
AS
SELECT
l.line_item_id,
l.order_id,
l.product_id,
l.quantity,
l.line_amount,
cast(l.order_ts AS DATE) AS order_date,
l.event_ts AS event_time
FROM STREAM read_files(
'/Volumes/${target_catalog}/raw/line_items/',
format => 'json'
) AS l;
The whole pipeline uses one clustering approach. Both bronze event tables cluster explicitly by order_date because we know the access pattern: consumers filter by date, and it gives the downstream Gold reconciliation a clean date slice to target. The Silver dimension uses CLUSTER BY AUTO, since its access pattern is workload-dependent and Predictive Optimization can choose the columns. Gold is mixed: fact_orders uses CLUSTER BY AUTO, while fact_line_items clusters by order_date so the REPLACE WHERE flow has a clean slice to rewrite. Under SDP, the pipeline's own schedule drives all of its tables together, so you don't set a cadence per table. When you run a streaming table standalone instead, it carries its own: SCHEDULE EVERY 1 HOUR keeps ingestion incremental on a timer, or TRIGGER ON UPDATE fires when upstream data lands.
Silver holds one table: the customer dimension, maintained by an AUTO CDC flow. AUTO CDC turns the raw customer_changes feed into an SCD Type 2 history table without a hand-written MERGE or any surrogate-key bookkeeping. The fact tables that join against it sit one layer up, in Gold. CLUSTER BY AUTO lets Databricks choose the clustering columns from the actual query workload.
-- SCD Type 2 customer dimension via AUTO CDC
CREATE OR REFRESH STREAMING TABLE ${target_catalog}.silver.dim_customer (
customer_sk BIGINT GENERATED ALWAYS AS IDENTITY,
customer_id INT,
email STRING,
segment STRING,
city STRING,
updated_at TIMESTAMP,
__START_AT TIMESTAMP,
__END_AT TIMESTAMP
)
COMMENT 'SCD Type 2 customer dimension with generated surrogate key'
CLUSTER BY AUTO
FLOW AUTO CDC
FROM STREAM ${target_catalog}.bronze.customer_changes
KEYS (customer_id)
SEQUENCE BY updated_at
STORED AS SCD TYPE 2;
Gotcha: snapshot-join semantics on the dim. When a streaming fact joins the dim as a static table (the fact_orders join in the Gold section below), the dim snapshot is taken at the start of each microbatch. A dim update that lands after a fact row is written won't retroactively change that fact's surrogate. Say an order writes at 10:00 pointing at the customer's silver version; if a correction moves that customer to gold and arrives at 10:05, the order already on disk keeps pointing at silver. When you need the correction applied retroactively, reprocess the affected facts from bronze. That's the SCD2 contract working as designed, not a bug to engineer around.
Gold is where the star schema comes together: the two fact tables, surrogate-resolved against the Silver dim, plus the reconciliation that handles late-arriving line items. (Naming note: these tables keep their silver.* schema names in SQL; Gold is the logical layer here, not the schema.)
fact_orders is a plain streaming fact. order_events arrives on time and append-only, so a standard streaming flow resolves customer_sk against the historical dim with a temporal join:
CREATE OR REFRESH STREAMING TABLE ${target_catalog}.silver.fact_orders
COMMENT 'Order facts with customer_sk resolved against the historical dim'
CLUSTER BY AUTO
AS
SELECT
o.order_id,
c.customer_sk, -- resolved from the dim, not from bronze
c.segment,
o.order_date,
o.total_amount,
o.event_time
FROM STREAM(${target_catalog}.bronze.order_events) o
LEFT JOIN ${target_catalog}.silver.dim_customer c
ON o.customer_id = c.customer_id
AND o.event_time >= c.__START_AT
AND (c.__END_AT IS NULL OR o.event_time < c.__END_AT);
The temporal join picks the correct historical version of the customer for each order. An order placed in April, when the customer was in segment silver, stays silver even if that customer moves to gold later. You write the SCD2 join once and it holds for every fact.
fact_line_items is the table that needs REPLACE WHERE. Line items can land up to 24 hours late, and a pure append flow would strand them: a line item that arrives after its order's date slice was first built would never get reconciled against the order. So instead of appending, we run a REPLACE WHERE flow that rewrites the trailing window of order_date on every refresh, re-evaluating the join over just that slice:
CREATE OR REFRESH STREAMING TABLE ${target_catalog}.silver.fact_line_items
COMMENT 'Line-item facts; late arrivals reconciled by a REPLACE WHERE flow on order_date'
CLUSTER BY (order_date)
-- REPLACE WHERE flow: idempotently rewrite the trailing window that can
-- still receive late line items, re-evaluating the join over just that slice.
FLOW REPLACE WHERE order_date >= date_add(current_date(), -2) BY NAME
SELECT
l.line_item_id,
l.order_id,
o.customer_sk,
o.segment,
l.product_id,
l.quantity,
l.line_amount,
l.order_date,
l.event_time
FROM ${target_catalog}.bronze.line_item_events l
LEFT JOIN ${target_catalog}.silver.fact_orders o
ON l.order_id = o.order_id
WHERE l.order_date >= date_add(current_date(), -2);
fact_line_items inherits customer_sk and segment from fact_orders, so the dim join runs once instead of once per line item, and customer attributes stay consistent between order and line-item rows. A two-day replace window comfortably covers the 24-hour late-arrival bound; widen it if your sources run later.
Gotcha: the REPLACE WHERE flow rewrites only the matching slice, and it does so atomically. Readers see either the old slice or the new one, never a torn state, and rows outside the predicate are left alone. On serverless, incremental refresh reprocesses only the source data that changed inside the window rather than recomputing the whole slice, so a wide safety window stays cheap when little has changed.
Keep the source filter matched to the REPLACE WHERE predicate. The flow already scopes each refresh to the predicate window, so any source rows that fall outside it are simply dropped rather than written. That makes the matching source filter a cost optimization rather than a correctness requirement: it prunes the scan to the same window, which is where the savings come from, and keeps the query's intent obvious to the next reader.
The gold facts are query-ready, but every team that reads them will re-derive the same numbers, “revenue by segment,” “units sold this week,” and drift apart on the definitions. The platinum layer solves that with metric views. A metric view defines each measure once in governed YAML (revenue is SUM(total_amount)) and lets any consumer group it by any dimension at query time. Add a materialization and those aggregates are pre-computed and incrementally refreshed, giving you the query acceleration of a materialized view under one governed definition. Metric views and their materializations are authored and queried entirely in Databricks SQL.
Revenue by segment as a metric view over the gold order fact:
CREATE OR REPLACE VIEW ${target_catalog}.gold.revenue_by_segment
WITH METRICS
LANGUAGE YAML
AS $$
version: 1.1
comment: "Governed revenue metrics by customer segment"
source: ${target_catalog}.silver.fact_orders
dimensions:
- name: Segment
expr: segment
- name: Order Date
expr: order_date
measures:
- name: Total Revenue
expr: SUM(total_amount)
- name: Order Count
expr: COUNT(*)
materialization:
schedule: EVERY 1 HOUR
mode: relaxed
materialized_views:
- name: revenue_by_segment_daily
type: aggregated
dimensions: [Segment, Order Date]
measures: [Total Revenue, Order Count]
$$;
Top products by week over the line-item fact, same shape with a weekly dimension and a distinct-order count:
CREATE OR REPLACE VIEW ${target_catalog}.gold.top_products
WITH METRICS
LANGUAGE YAML
AS $$
version: 1.1
comment: "Governed weekly product metrics"
source: ${target_catalog}.silver.fact_line_items
dimensions:
- name: Product
expr: product_id
- name: Week Starting
expr: date_trunc('week', order_date)
measures:
- name: Product Revenue
expr: SUM(line_amount)
- name: Units Sold
expr: SUM(quantity)
- name: Order Count
expr: COUNT(DISTINCT order_id)
materialization:
schedule: EVERY 1 HOUR
mode: relaxed
materialized_views:
- name: top_products_weekly
type: aggregated
dimensions: [Product, Week Starting]
measures: [Product Revenue, Units Sold, Order Count]
$$;
Consumers query the metric view, not the materialization. Measures are wrapped in MEASURE() and you group by whatever dimensions you need, and the definition of "revenue" is identical no matter who runs it:
SELECT
`Segment`,
`Order Date`,
MEASURE(`Total Revenue`) AS revenue,
MEASURE(`Order Count`) AS orders
FROM ${target_catalog}.gold.revenue_by_segment
WHERE `Order Date` >= current_date() - INTERVAL 30 DAYS
GROUP BY ALL
ORDER BY ALL;
This is where view matching pays off. The query never names revenue_by_segment_daily. When it runs, the optimizer checks the metric view's materializations, sees that the daily aggregate covers the requested dimensions and measures, and rewrites the query to read the pre-computed table instead of scanning the fact. You get materialized-view speed without hard-coding a materialized-view name, and a query that asks for a grouping no materialization covers falls back to the base fact automatically. One object gives you both the standard definition and the acceleration.
Gotcha: a materialization is a config, not a rewrite you own. You declare what to pre-compute in the YAML, and the engine owns the refresh (incremental, on the schedule you set) and the matching. Keep materializations aligned with how the metric view is actually queried: a materialization on dimensions nobody groups by burns refresh compute, and never gets matched. Start from your top dashboard queries and materialize those dimensions and measure combinations. (Metric-view materializations are recently GA and the config surface is still evolving, so check the current YAML reference before you ship.)
The pipeline deploys in two pieces, both authored in plain Databricks SQL. Bronze through Gold (the streaming tables, the AUTO CDC dimension, and the fact_line_items REPLACE WHERE flow) goes into a single SDP pipeline: SDP reads the table references, infers the dependency graph, and runs the layers in order, with no orchestration code to write. Wrap that .sql file in a Declarative Automation Bundles (formerly known as Databricks Asset Bundles), and you get one shared DAG, a shared event log, and clean promotion across dev, staging, and prod.
The Platinum metric views are created separately, directly in DBSQL on top of the Gold facts. A metric view (CREATE VIEW … WITH METRICS) is a SQL-warehouse object, not a pipeline statement, so it lives outside the SDP file and refreshes on its own materialization schedule. The customer_changes CDC feed is likewise an external source table the pipeline reads, not one it manages. So the deploy has two steps: run the SDP pipeline for bronze through gold, then create the two metric views in DBSQL against the gold facts. (None of these statements requires an SDP pipeline. The streaming tables, the AUTO CDC dimension, and the inline REPLACE WHERE reconciliation all run standalone on a serverless SQL warehouse; the SDP pipeline is what adds the managed DAG, the shared event log, and clean promotion across dev, staging, and prod.)
# databricks.yml: bundle definition for the retail warehouse pipeline
bundle:
name: retail_warehouse
variables:
target_catalog:
description: Catalog where the pipeline writes
source_volume_root:
description: Root path for raw event JSON files
resources:
pipelines:
retail_warehouse:
name: retail_warehouse_${bundle.target}
catalog: ${var.target_catalog}
target: gold
libraries:
- file:
path: ./pipeline.sql
configuration:
target_catalog: ${var.target_catalog}
serverless: true
targets:
dev:
variables:
target_catalog: dev_main
source_volume_root: /Volumes/dev_main/raw
staging:
variables:
target_catalog: staging_main
source_volume_root: /Volumes/staging_main/raw
prod:
variables:
target_catalog: prod_main
source_volume_root: /Volumes/prod_main/raw
Deploy with Databricks bundle deploy --target dev (or staging/prod). The same SQL pipeline runs across all three environments with no source code changes; the bundle target injects the catalog parameter at deployment time. The Pipeline UI shows the DAG, the per-table refresh status, and the last-run row counts for whichever target you triggered. The bundle deploys the bronze-through-gold pipeline; create the two metric views in DBSQL against the gold facts as a follow-on step (a post-deploy SQL task, or by hand in the editor), since metric views aren’t pipeline statements.
Once the pipeline runs, check that each materialization is refreshing incrementally rather than fully recomputing. A metric view’s materialization is managed by the view, and the pre-computed table isn’t always surfaced as a plain queryable object under the YAML name. If it is exposed in the target schema (the names you gave in the YAML: revenue_by_segment_daily, top_products_weekly), you can read its planning events from the event log; confirm the exact object names in Catalog Explorer first, and if event_log(TABLE(...)) can’t resolve the name, check the refresh from the metric view’s own materialization state instead:
SELECT 'revenue_by_segment_daily' AS mv, message
FROM event_log(TABLE(${target_catalog}.gold.revenue_by_segment_daily))
WHERE event_type = 'planning_information'
AND origin.flow_name LIKE '%revenue_by_segment_daily%'
ORDER BY timestamp DESC LIMIT 1
UNION ALL
SELECT 'top_products_weekly' AS mv, message
FROM event_log(TABLE(${target_catalog}.gold.top_products_weekly))
WHERE event_type = 'planning_information'
AND origin.flow_name LIKE '%top_products_weekly%'
ORDER BY timestamp DESC LIMIT 1;
Both rows should report GROUP_AGGREGATE, the incremental technique. If either reports FULL_RECOMPUTE, the materialization is silently recomputing from scratch; narrow it to the dimensions and measures your queries actually match, or check the metric-view definition.
For the streaming silver tables, query their event log for flow_progress:
SELECT
origin.flow_name,
details:flow_progress.metrics.num_output_rows AS rows_processed,
timestamp
FROM event_log(TABLE(${target_catalog}.silver.fact_orders))
WHERE event_type = 'flow_progress'
AND origin.flow_name LIKE '%fact_orders%'
ORDER BY timestamp DESC
LIMIT 5;
For the bronze layer's data quality:
SELECT
details:flow_progress.data_quality.expectations
FROM event_log(TABLE(${target_catalog}.bronze.order_events))
WHERE event_type = 'flow_progress'
AND origin.flow_name LIKE '%order_events%'
AND details:flow_progress.data_quality.expectations IS NOT NULL
ORDER BY timestamp DESC
LIMIT 1;
event_log() returns events for the entire pipeline the named table belongs to, not just events about that table. Filtering on origin.flow_name scopes results to the flow you actually care about. Without it, the ORDER BY timestamp DESC LIMIT 1 will return whichever flow happened to log most recently.
The expectations JSON field has pass/fail counts for each CONSTRAINT you declared. Build an alerting query on top. For example, fire an alert when the failure rate on non_negative_amt exceeds 1% over a 5-minute window.
A few things tend to show up once a pipeline like this moves past the proof-of-concept stage.
Source materialization on AUTO CDC. Behind the scenes, AUTO CDC sometimes needs to materialize the source change feed before applying changes. On very large or churn-heavy customer_changes tables, the time spent figuring out whether materialization is needed can end up dominating the silver-dim refresh, even when the actual changeset is small. The fixes are usually mundane: confirm row tracking and CDF are both enabled on bronze, keep source volatility down (avoid frequent updates on bronze unless you actually need them), and shorten retention windows where you don't need long history.
Photon and AUTO CDC SCD2. Type 2 dim performance through AUTO CDC is much better with Photon. On serverless SQL warehouses Photon is on by default, so there is nothing to do. On classic SDP compute, check this first before drawing any conclusions about throughput.
Small-file accumulation on low-volume bronze tables. Predictive Optimization compacts small files in the background, so this bites less than it used to, but very frequent triggers can still outrun compaction and leave slow reads in the gaps. Trigger cadence is the real lever: every 5 to 15 minutes is a reasonable lower bound for streaming ingestion, and hourly is fine for dim CDC streams that update infrequently.
Skew at the surrogate join. If a small number of customer_id values dominate the order stream (the B2B customer with 90% of the orders, say), the dim/fact join will skew, and one task grinds while the others finish. CLUSTER BY AUTO on dim and fact handles most of this by letting Databricks pick clustering columns from real workload. For pathological skew, salt the join key with a small random bucket suffix and aggregate in two stages.
Stream-stream joins without watermarks. This pipeline has no stream-stream join, but if you extend it with one (joining two event streams directly), watermarks matter. Skip the WATERMARK on either side, or the time-bound condition, and state grows without bound. It usually surfaces hours into a long run as memory pressure rather than a clean error, which makes it harder to debug than it should be. Keep a watermark on both sides and a BETWEEN-style time bound in the join condition.
Here is a like-for-like comparison against the 2024-shaped baseline: a PySpark notebook, a 200-line MERGE, a nightly full-refresh job, and manual orchestration.
|
Dimension |
2024 baseline |
This pipeline |
Why |
|
Lines of code |
~800 |
~200 |
Declarative primitives replace procedural SQL/PySpark |
|
Refresh model |
Full nightly recompute on gold |
Incremental on every trigger |
Metric-view materialization + streaming silver |
|
SCD2 dim refresh |
Full MERGE every batch |
Incremental, change-only |
AUTO CDC reads CDF, processes only changes |
|
Surrogate keys |
App-side allocation + lock table |
IDENTITY column |
Engine-managed, no app state |
|
Late-arriving line items |
Custom backfill scripts |
REPLACE WHERE flow (Gold) |
One-line idempotent rewrite |
|
Data quality |
Custom validation step + log scrape |
CONSTRAINT … EXPECT |
First-class in the DDL; metrics in event log |
|
Data layout |
Static partitioning + ZORDER jobs |
CLUSTER BY AUTO |
Self-tuning, skew-resistant, incremental |
|
Orchestration |
Workflows tasks + dependencies wired by hand |
SDP infers DAG |
No orchestration code |
|
Cost-model surprises |
Silent bill spikes when query changes |
Metric-view materializations, incremental |
Governed definitions; view matching auto-accelerates |
|
CI/CD |
Hand-rolled deployment scripts |
Bundle targets |
One YAML file deploys to dev/staging/prod |
|
Observability |
Custom log scraping |
event_log() SQL view |
First-class platform feature |
How much you save depends on your data volume, but the structural change matters more than the dollar figure. Every platinum materialization refresh is incremental. Every silver fact processes only new rows. Dim refresh scales with change volume rather than dim size, and the deployment is reproducible across environments. No single one of these is dramatic on its own; together, they remove a long list of failure modes the 2024 baseline was exposed to.
The SQL above is a working pipeline (bronze through gold) plus its two metric views. The quickest way to evaluate it is to copy it into your own catalog (renaming the tables to match your data), point the bronze sources at your real volumes, and watch the Pipeline UI render the DAG. If you replace an existing PySpark-plus-MERGE pipeline with this pattern, the numbers worth sharing in the comments are line count, end-to-end refresh time, and monthly cost, before and after.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.