Sub topic 6
How datasets reference each other and form a graph
This chapter assumes you can write basic SQL and PySpark, and that you know Unity Catalog catalogs and schemas. It also assumes you have seen streaming tables and materialized views. Here you learn how a pipeline works out the order in which to run your datasets.
The pipeline builds a graph from your queries
You do not tell a pipeline which dataset runs first. Each dataset has a query, and that query reads other datasets. The pipeline reads those references and builds a directed acyclic graph (DAG). In this graph, each node is a dataset and each edge points from a source to the dataset that reads it. "Acyclic" means no dataset can depend on itself, directly or through other datasets.
From this graph, the pipeline decides the run order, runs independent branches in parallel, and shows you a lineage view of the result.
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 FROM orders_silver clause is the whole dependency declaration. The pipeline sees it and runs orders_silver before daily_revenue.
Referencing datasets in SQL
In SQL, you name another dataset in the pipeline directly. An unqualified name such as orders_silver resolves to the pipeline's target catalog and schema. To read a streaming table as a stream, wrap the name in STREAM(...):
CREATE OR REFRESH STREAMING TABLE orders_silver
AS SELECT order_id, customer_id, amount, order_ts
FROM STREAM(orders_bronze);STREAM(orders_bronze) reads only new rows. A plain FROM orders_bronze would read the whole table each time, which is the batch behavior a materialized view expects.
Referencing datasets in Python
In Python, use dlt.read for a full read and dlt.read_stream for an incremental one. Both take the dataset name as a string.
import dlt
@dlt.table
def orders_silver():
return dlt.read_stream("orders_bronze").select(
"order_id", "customer_id", "amount", "order_ts"
)
@dlt.table
def daily_revenue():
return (
dlt.read("orders_silver")
.groupBy("order_ts")
.sum("amount")
)The graph is built from these calls. Functions can be defined in any order and in any file, because the pipeline resolves names first and runs them afterward.
Reading the graph
The diagram below shows the graph for the orders flow from this tutorial.
The pipeline UI draws the same picture after an update. Each node shows its status and row counts, so you can see where a failure or a slowdown occurs. A node can have several upstream sources, as in a join, and several downstream readers. The pipeline runs branches that do not depend on each other at the same time.
Common errors
- Circular reference. If
areadsbandbreadsa, the pipeline cannot order them and fails when it builds the graph. Break the loop by moving shared logic into a third dataset that both can read. - Misspelled or missing dataset. A name that matches no dataset in the pipeline is treated as an external table. If it does not exist in Unity Catalog, the update fails with a table-not-found error.
- Streaming read of a materialized view. A streaming table needs an append-only source. Reading a source that is updated or deleted in place with
STREAM(...)can fail. Read it as a batch instead, or use a streaming table upstream. - Qualified names hiding dependencies. A fully qualified name such as
main.demo.orders_silvercan reference a table written by another pipeline. This is not an edge in your graph, so the pipeline does not order it for you.
Exercise
A materialized view report contains FROM STREAM(orders_silver), and a second query uses FROM orders_silver. Which one performs an incremental read, and what does the pipeline infer from both queries?
Show answer
STREAM(orders_silver) reads the table as a stream of new rows, while the plain FROM orders_silver reads the full table. In both cases the pipeline infers a dependency on orders_silver, so it runs orders_silver first and places an edge from it to each reading dataset.
Exercise
Two datasets, a and b, each read the other. What happens, and how do you fix it?
Show answer
The graph has a cycle, so the pipeline cannot determine an order and the update fails. Move the logic both need into a third dataset c, and have a and b read from c without reading each other.