The DAG Is the Contract: How dbt Plans a Run Before It Runs Anything
Most dbt users would describe dbt run as "it runs my SQL against the warehouse." That is true, but it leaves out the step that explains dbt's behavior in production. Before a single query reaches the warehouse, dbt reads the whole project and builds the dependency graph that decides what runs and in what order.
Parse, compile, execute
A dbt invocation has three phases. In the parse phase, dbt reads every file in the project without connecting to the warehouse. It finds every ref() and source() call and builds the dependency graph — the DAG — which it writes to target/manifest.json. In the compile phase, dbt renders each model's Jinja into plain SQL for the target environment. In the execute phase, it sends that SQL to the warehouse, one model at a time, in dependency order.
The graph is complete before execution starts, and dbt does not change it during the run. So a ref() to a model that doesn't exist fails during parsing, before any warehouse compute is spent. It is also why dbt run --select stg_orders+ can work out exactly which downstream models to build from the manifest alone. The DAG is not a picture dbt draws for you afterwards. It is the plan the run follows.
-- models/staging/storefront/stg_orders.sql
select
order_id,
customer_id,
cast(order_date as date) as order_date,
channel,
order_status,
order_amount,
cast(updated_at as timestamp) as updated_at
from {{ source('storefront', 'orders') }}
The {{ source(...) }} call is not looked up while the query runs. dbt resolves it during parsing, against the project's source declarations (_storefront__sources.yml in the case study). A typo in the source name therefore stops the run before it starts, instead of failing at 2 a.m. three models downstream.
Where this surprises people
The engineers who get surprised by dbt in production usually think about it one query at a time instead of as a graph. Two examples:
- A
--full-refreshbehaves differently in two environments, because each adapter implements incremental models in its own way. - A Slim CI run rebuilds half the project after a one-line change, because
state:modifiedcompares more than the SQL text: changes to configs and macros count too.
Chapter 2 of dbt for Data Engineers explains the full execution model: the three phases, the manifest, and materializations and where adapters differ. Chapters 1 and 2 are the free sample.