In my previous blog post I explained what dbt models are, how they work and how they can be used to transform raw data into clean and structured datasets.
There may be instances where a project has transformations that requires data from multiple models. But when there are models that depend on other models, how does dbt know which models to run first? This is where sources, references and data lineage comes in.
A key strength of dbt is it's ability to manage dependencies between different models, which allows data teams to build transformation pipelines that are organised, reusable and easier to maintain.
Sources in dbt
In dbt, sources refer to raw data tables that have already been loaded into a data warehouse from external systems, such as customer databases, payment platforms or CRM systems.
Since dbt is only used for the T stage of the ELT process, this raw data is often loaded using separate data ingestion tools.
When referencing raw data tables in dbt, rather than repeatedly writing the full database and schema names of the raw data tables throughout the dbt models, dbt allows us to define these tables as sources in a YAML file. For example, we could create the following sources YAML file in dbt:
sources:
- name: jaffle_shop
database: raw
schema: jaffle_shop
tables:
- name: customers
- name: orders
This tells dbt that we have a source called jaffle_shop that contains two tables - customers and orders. The database and schema configurations also tell dbt where these tables are located in the data warehouse it has been connected to.
Once this YAML source file has been created with all the necessary configurations, it can be referenced using the dbt's source( ) function. For example:
select *
from { { source('jaffle_shop', 'customers') } }
'jaffle_shop' refers to the source name and 'customers' refers to the table name in that source that we want to reference, so when dbt compiles the SQL, it replaces the source('jaffle_shop', 'customers') expression with the appropriate database relation.
For example, in Snowflake the resulting SQL will look like the following:
select *
from raw.jaffle_shop.customers
Main benefits of using the source( ) function:
- Easier management of source tables - For example, if the customers table had been moved to a different schema, and we had hardcoded the database and schema names in multiple models (instead of using the source function), we would need to update every model that references the customers table so that it doesn't break. However by using the source function, we only need to update any changes to the source location in one place - the YAML source file. dbt will then use the updated information wherever the customers source table is referenced
- Automatically establishes dependencies - dependencies are automatically established between the source table and the model that is referencing it, which becomes useful when exploring data lineage.
Connecting models using the ref() function
In dbt, the ref( ) function allows us to reference a dbt model in the SQL query of another dbt model.
For example, imagine we create a model in dbt (called stg_customers) that cleans and standardises the data from the raw customers source table, and we want to create another model that uses this cleaned customers data. Instead of writing the full database and schema of stg_customers, we can use:
select *
from { { ref('stg_customers') } }
When dbt compiles this SQL, it replaces ref('stg_customers') with the appropriate database relation for that model.
Main benefits of using the ref( ) function instead of hard coding model names:
- Easier maintenance - if the schema or database location of a model changes, we don't need to manually update every model that references it, as dbt will automatically update these references
- Dependency management - the ref( ) function tells dbt that one model depends on another. Therefore if model 2 references model 1 and both models are selected for execution, dbt knows that it must build model 1 before building model 2
What is Jinja and why does dbt use it?
Jinja is a templating language used by dbt. The curly brackets that wrap both the source( ) and ref( ) functions above is the Jinja syntax:
{ { source('jaffle_shop', 'customers') } }
{ { ref('stg_customers') } }
Jinja itself isn't SQL, however it allows us to add dynamic functionality to SQL files, or models.
In dbt, Jinja can be used to reference sources and models, configure models, create reusable macros and generate SQL dynamically. During compilation, dbt processes the Jinja expressions and generates SQL that the connected data warehouse can execute. This makes SQL transformations more flexible and easier to maintain.
What is a DAG in dbt?
A DAG (Directed Acyclic Graph) shows a visual representation of the dependencies between resources within a dbt project, i.e. it shows the lineage from the source file to the final model in a project. This is automatically generated from the dependencies defined using functions such as source( ) and ref( ).
Two terms that are often associated with DAG in dbt are upstream and downstream. Upstream models are models that a particular model depends on, while downstream models are models that are dependent on the output of a particular model.
Understanding the lineage of a model is important because any changes made to a source or an upstream model can affect the downstream models that depend on it. It can also be useful when troubleshooting issues within a project.
Additionally, when there are multiple developers working on the same project, having clearly defined models and dependencies makes it easier to understand how different parts of a pipeline fit together, allowing for improved collaboration.
