The ref() Function

Free Lesson

Advertisement

The ref() Function

Reference Resolution Architecture

Architecture Diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     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

Architecture Diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     DEPENDENCY GRAPH RESOLUTION                             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                             β”‚
β”‚  YOUR PROJECT                                                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚                                                                     β”‚   β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”‚   β”‚
β”‚  β”‚  β”‚ stg_     │─────▢│ int_     │─────▢│ fct_     β”‚                 β”‚   β”‚
β”‚  β”‚  β”‚ orders   β”‚      β”‚ orders   β”‚      β”‚ orders   β”‚                 β”‚   β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β”‚   β”‚
β”‚  β”‚       β–²                   β–²                   β–²                    β”‚   β”‚
β”‚  β”‚       β”‚                   β”‚                   β”‚                    β”‚   β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚   β”‚
β”‚  β”‚  β”‚                    DEPENDENCIES                              β”‚  β”‚   β”‚
β”‚  β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚   β”‚
β”‚  β”‚  β”‚  β€’ dbt_utils   (package)                                   β”‚  β”‚   β”‚
β”‚  β”‚  β”‚  β€’ analytics   (project)                                   β”‚  β”‚   β”‚
β”‚  β”‚  β”‚  β€’ raw         (source)                                    β”‚  β”‚   β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Package Resolution

Architecture Diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     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:

  1. Parsing: During the parse phase, dbt identifies all ref() calls in your SQL files
  2. Graph Construction: dbt builds a Directed Acyclic Graph (DAG) based on these references
  3. Resolution: When compiling, dbt resolves ref() calls to the actual database object references
  4. 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:

  1. Check local project: Look for the model in your current project
  2. Check packages: Look for the model in installed packages
  3. Check cross-project: If enabled, look for the model in external projects
  4. Error handling: If not found, raise a compilation error

Benefits of using ref()

  1. Graph awareness: dbt automatically resolves dependencies
  2. Environment flexibility: Works across dev, staging, and production
  3. Package management: Clean references to package models
  4. Lineage tracking: Complete data lineage from source to mart
  5. Testing integration: Models can be tested in isolation
  6. 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 TypeResolution TimeOverheadUse Case
Local~1msMinimalSame project
Package~2msLowReusable code
Cross-project~5-10msMediumData mesh
Source~1msMinimalExternal data
Ephemeral~3msCTE injectionReusable logic

Best Practices

  1. Use ref() for all model references - Never hardcode table names
  2. Organize models in layers - staging β†’ intermediate β†’ marts
  3. Use package references for shared logic across projects
  4. Implement cross-project refs for data mesh architectures
  5. Document all references with descriptions in YAML files
  6. Test referenced models to ensure data quality
  7. Use source() for external data - Never ref() external tables
  8. Version control your packages with specific version constraints

Advertisement

Need Expert dbt Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement