The ref() Function
Reference Resolution Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β REF() FUNCTION RESOLUTION FLOW β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β TEMPLATE βββββΆβ RESOLVER βββββΆβ OUTPUT β β
β β INPUT β β ENGINE β β SQL β β
β β β β β β β β
β β {{ ref( β β β’ Manifest β β database. β β
β β 'model' βββββΆβ β’ Graph βββββΆβ schema. β β
β β )}} β β β’ Packages β β model_name β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β REFERENCE TYPES β β
β β β β
β β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββ β β
β β β Local Ref β β Package Ref β β Cross-Project Ref β β β
β β β β β β β β β β
β β β {{ ref( β β {{ ref( β β {{ ref( β β β
β β β 'model' β β 'pkg', β β 'project', β β β
β β β )}} β β 'model' β β 'model' β β β
β β β β β )}} β β )}} β β β
β β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Dependency Graph
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DEPENDENCY GRAPH RESOLUTION β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β YOUR PROJECT β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β β
β β ββββββββββββ ββββββββββββ ββββββββββββ β β
β β β stg_ βββββββΆβ int_ βββββββΆβ fct_ β β β
β β β orders β β orders β β orders β β β
β β ββββββββββββ ββββββββββββ ββββββββββββ β β
β β β² β² β² β β
β β β β β β β
β β ββββββ΄ββββββββββββββββββββ΄ββββββββββββββββββββ΄βββββββββββββββββ β β
β β β DEPENDENCIES β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β
β β β β’ dbt_utils (package) β β β
β β β β’ analytics (project) β β β
β β β β’ raw (source) β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Package Resolution
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PACKAGE REFERENCE RESOLUTION β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β PACKAGES.YML β β
β β β β
β β packages: β β
β β - package: dbt-labs/dbt_utils β β
β β version: [">=1.0.0", "<2.0.0"] β β
β β - package: calogica/dbt_expectations β β
β β version: [">=0.10.0"] β β
β β - git: "https://github.com/org/repo.git" β β
β β revision: main β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β DBT_PACKAGES/ β β
β β β β
β β βββ dbt_utils/ β β
β β β βββ macros/ β β
β β β β βββ date_spine.sql β β
β β β β βββ generate_series.sql β β
β β β β βββ pivot.sql β β
β β β βββ dbt_project.yml β β
β β βββ dbt_expectations/ β β
β β βββ macros/ β β
β β β βββ schema_tests.sql β β
β β β βββ row_count.sql β β
β β βββ dbt_project.yml β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Detailed Explanation
The ref() function is the most fundamental and powerful function in dbt. It serves as the primary mechanism for referencing other models, packages, and sources within your dbt project. Understanding ref() is crucial for building maintainable, scalable data pipelines.
How ref() Works
When you write {{ ref('model_name') }} in a dbt model, dbt performs several operations:
- Parsing: During the parse phase, dbt identifies all
ref()calls in your SQL files - Graph Construction: dbt builds a Directed Acyclic Graph (DAG) based on these references
- Resolution: When compiling, dbt resolves
ref()calls to the actual database object references - Execution: The resolved SQL is executed against your data warehouse
Reference Types
Local References
Local references point to models within the same dbt project:
{{ ref('stg_orders') }}
This resolves to database.schema.stg_orders based on your profile configuration.
Package references
Package references point to models in installed packages:
{{ ref('dbt_utils', 'surrogate_key') }}
This references the surrogate_key macro from the dbt_utils package.
Cross-project references
Cross-project references allow you to reference models in other dbt projects:
{{ ref('analytics', 'dim_customers') }}
This references the dim_customers model from the analytics project, enabling data mesh architectures.
Resolution Process
The resolution process follows these steps:
- Check local project: Look for the model in your current project
- Check packages: Look for the model in installed packages
- Check cross-project: If enabled, look for the model in external projects
- Error handling: If not found, raise a compilation error
Benefits of using ref()
- Graph awareness: dbt automatically resolves dependencies
- Environment flexibility: Works across dev, staging, and production
- Package management: Clean references to package models
- Lineage tracking: Complete data lineage from source to mart
- Testing integration: Models can be tested in isolation
- Documentation: Automatic documentation of dependencies
Code Examples
Basic Local Reference
-- models/marts/fct_orders.sql
with orders as (
select * from {{ ref('stg_orders') }}
),
customers as (
select * from {{ ref('dim_customers') }}
),
final as (
select
orders.order_id,
orders.order_date,
customers.customer_name,
orders.amount
from orders
left join customers on orders.customer_id = customers.customer_id
)
select * from final
Package Reference
-- models/marts/dim_dates.sql
{{
config(
materialized='table'
)
}}
with date_spine as (
{{ dbt_utils.date_spine(
start_date="cast('2020-01-01' as date)",
end_date="cast('2025-12-31' as date)",
datepart="day"
)}}
),
final as (
select
date_day as date_date,
{{ dbt_utils.date_part("year", "date_day") }} as date_year,
{{ dbt_utils.date_part("month", "date_day") }} as date_month,
{{ dbt_utils.date_part("day", "date_day") }} as date_day_of_month,
{{ dbt_utils.date_part("dayofweek", "date_day") }} as date_day_of_week
from date_spine
)
select * from final
Cross-Project Reference
-- models/marts/fct_company_metrics.sql
{{
config(
materialized='incremental',
unique_key='company_id'
)
}}
with companies as (
select * from {{ ref('analytics', 'dim_companies') }}
),
revenue as (
select * from {{ ref('finance', 'fct_revenue') }}
),
final as (
select
companies.company_id,
companies.company_name,
sum(revenue.amount) as total_revenue,
current_timestamp() as updated_at
from companies
left join revenue on companies.company_id = revenue.company_id
group by 1, 2
)
select * from final
Advanced Reference Patterns
-- models/staging/stg_orders.sql
{{
config(
materialized='view',
schema='staging'
)
}}
{% set source_relation = source('raw', 'orders') %}
with source as (
select * from {{ source_relation }}
),
renamed as (
select
id as order_id,
customer_id,
status,
created_at,
updated_at
from source
)
select * from renamed
Dynamic Reference with Variables
-- models/marts/fct_orders.sql
{{
config(
materialized='incremental'
)
}}
{% set source_model = var('order_source', 'stg_orders') %}
with orders as (
select * from {{ ref(source_model) }}
),
final as (
select
order_id,
customer_id,
order_date,
amount,
current_timestamp() as dbt_updated_at
from orders
)
select * from final
{% if is_incremental() %}
where dbt_updated_at > (select max(dbt_updated_at) from {{ this }})
{% endif %}
Performance Metrics
| Reference Type | Resolution Time | Overhead | Use Case |
|---|---|---|---|
| Local | ~1ms | Minimal | Same project |
| Package | ~2ms | Low | Reusable code |
| Cross-project | ~5-10ms | Medium | Data mesh |
| Source | ~1ms | Minimal | External data |
| Ephemeral | ~3ms | CTE injection | Reusable logic |
Best Practices
- Use ref() for all model references - Never hardcode table names
- Organize models in layers - staging β intermediate β marts
- Use package references for shared logic across projects
- Implement cross-project refs for data mesh architectures
- Document all references with descriptions in YAML files
- Test referenced models to ensure data quality
- Use source() for external data - Never ref() external tables
- Version control your packages with specific version constraints