Sub topic 4
Materialized views for transformed and aggregated results
This chapter assumes you can write SQL aggregations, know what a Unity Catalog catalog and schema are, and have read the earlier chapters on the declarative model and streaming tables.
What a materialized view is
A materialized view is a dataset defined by a query. The pipeline runs the query and stores the result as a table. On each update, the pipeline brings the stored result in line with the current contents of its sources.
The definition is the whole contract. You don't write MERGE statements or decide what to delete. If an upstream row changes or disappears, the next update corrects the result.
Defining one
This gold-layer view aggregates the silver table into daily revenue:
CREATE OR REFRESH MATERIALIZED VIEW daily_revenue
AS SELECT
date(order_ts) AS order_date,
SUM(amount) AS revenue
FROM orders_silver
GROUP BY date(order_ts);The pipeline reads orders_silver in the FROM clause and infers that daily_revenue depends on it. It updates orders_silver first.
In Python, a function decorated with @dlt.table that returns a batch DataFrame defines a materialized view. The same decorator defines a streaming table when the function returns a streaming DataFrame:
import dlt
from pyspark.sql.functions import col, date_trunc, sum as sum_
@dlt.table(comment="Revenue per day")
def daily_revenue():
return (
dlt.read("orders_silver")
.groupBy(date_trunc("day", col("order_ts")).alias("order_date"))
.agg(sum_("amount").alias("revenue"))
)Note dlt.read here, not dlt.read_stream. A batch read gives a materialized view.
Streaming table or materialized view
| Question | Streaming table | Materialized view |
|---|---|---|
| Query reads the source as | An append-only stream | A full relation |
| Each input row is processed | Once | Every time the result is recomputed |
| Handles updates and deletes in the source | Not by default | Yes, the result reflects them |
| Typical layer | Bronze, sometimes silver | Silver joins, gold aggregates |
Use a materialized view when the result is an aggregate, a join, or anything that must stay correct when upstream data changes. Use a streaming table when you want to process each new row once.
Why aggregates belong here
An aggregate such as SUM(amount) per day can change when a late row arrives. A streaming table would have to maintain that state by hand. A materialized view declares the result, and the pipeline keeps it correct. The pipeline may refresh it incrementally, which a later chapter covers. Either way, the result must match a full recomputation.
Common errors
- Using a stream in a materialized view.
STREAM(...)ordlt.read_streammakes the dataset a streaming table. Use plain table references for a materialized view. - Expecting live results between updates. The view changes only when the pipeline runs an update. A query between updates sees the last stored result.
- Using non-deterministic functions. Functions such as
current_timestamp()in the definition can force a full recompute or make results differ between runs. Prefer values from the data. - Forgetting the target schema. The view is created in the pipeline's target catalog and schema. Query it as
main.demo.daily_revenue, not by the bare name.
Exercises
Exercise
You change daily_revenue so that it reads STREAM(orders_silver) instead of orders_silver. What kind of dataset do you now define, and why can that hurt an aggregate?
Show answer
You define a streaming table. It processes each input row once and does not recompute from the full source. If an upstream row is corrected or deleted, the aggregate does not reflect it.
Exercise
A dashboard queries daily_revenue at 10:00. New orders arrived at 09:55, but the pipeline's next update is at 11:00. Which numbers does the dashboard show?
Show answer
The numbers from the last completed update. A materialized view changes only when the pipeline runs an update, so the 09:55 orders are not included until 11:00.