Calling macros across projects in dbt - Time & Space Complexity
When using dbt, calling macros from other projects can affect how long your code takes to run.
We want to know how the time to run grows when calling macros across projects.
Analyze the time complexity of the following dbt code snippet.
{{
project_macro('argument')
}}
{% macro project_macro(arg) %}
{{ other_project.some_macro(arg) }}
{% endmacro %}
This code calls a macro from another project inside a macro in the current project.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling a macro from another project once per invocation.
- How many times: Once each time the macro is called in the model or another macro.
Each time you call the macro, dbt looks up and runs the other project's macro.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 macro calls, 10 cross-project lookups |
| 100 | 100 macro calls, 100 cross-project lookups |
| 1000 | 1000 macro calls, 1000 cross-project lookups |
Pattern observation: The time grows directly with how many times you call the macro.
Time Complexity: O(n)
This means the time to run grows linearly with the number of macro calls across projects.
[X] Wrong: "Calling a macro from another project is instant and has no cost."
[OK] Correct: Each cross-project macro call requires dbt to find and run that macro, which takes time proportional to how often it is called.
Understanding how calling macros across projects affects performance shows you can think about how code structure impacts run time, a useful skill in real projects.
"What if the macro called from the other project itself calls multiple macros inside it? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of macros
Macros are reusable pieces of code that help avoid repetition.Step 2: Recognize the benefit of calling macros across projects
Calling macros across projects allows sharing code, which keeps projects clean and organized.Final Answer:
To reuse code and keep projects organized -> Option CQuick Check:
Code reuse = To reuse code and keep projects organized [OK]
- Thinking macros increase project size
- Believing macros slow down the project
- Ignoring the need for dependencies
calculate_total from a project named sales_project?Solution
Step 1: Recall the correct macro call syntax
Macros from other projects are called using{{ project_name.macro_name() }}.Step 2: Match the syntax to the options
{{ sales_project.calculate_total() }} uses the correct dot notation and double curly braces.Final Answer:
{{ sales_project.calculate_total() }} -> Option AQuick Check:
Use dot notation inside {{ }} = {{ sales_project.calculate_total() }} [OK]
- Using {% %} instead of {{ }} for macro calls
- Missing the project name before the macro
- Using incorrect symbols like ->
packages.yml snippet and macro call, what will happen?packages:
- package: sales_project
version: 1.0.0
-- In a model SQL file:
SELECT {{ sales_project.calculate_discount(100) }} AS discount_amountSolution
Step 1: Check the packages.yml for dependencies
The sales_project is listed as a dependency, so it is available.Step 2: Verify the macro call syntax
The macro is called correctly using {{ sales_project.calculate_discount(100) }}.Final Answer:
The macro will run and return the discount value -> Option AQuick Check:
Dependency + correct syntax = macro runs [OK]
- Forgetting to add dependency in packages.yml
- Using wrong macro call syntax
- Assuming macro calls run without dependencies
Solution
Step 1: Identify common errors in cross-project macro calls
Missing the other project in packages.yml means dbt cannot find the macro.Step 2: Compare other options
Misspelling or syntax errors cause different errors, but missing dependency is most common.Final Answer:
The other project is not listed in packages.yml -> Option DQuick Check:
Missing dependency = macro call error [OK]
- Ignoring packages.yml dependencies
- Using wrong macro call syntax
- Forgetting parentheses in macro calls
format_date from a project utils_project inside your project. You added utils_project in packages.yml. Which is the correct way to pass a date string '2024-01-01' to the macro and use its result in a model?Solution
Step 1: Confirm dependency is added
utils_project is added in packages.yml, so macro can be called.Step 2: Check correct string passing in macro call
Strings in Jinja macros should be double-quoted inside {{ }} to avoid syntax issues.Step 3: Validate SQL syntax
SELECT {{ utils_project.format_date("2024-01-01") }} AS formatted_date correctly uses double quotes inside the macro call and proper SQL aliasing.Final Answer:
SELECT {{ utils_project.format_date("2024-01-01") }} AS formatted_date -> Option BQuick Check:
Double quotes for strings in macro calls = SELECT {{ utils_project.format_date("2024-01-01") }} AS formatted_date [OK]
- Using unquoted strings causing errors
- Incorrect SQL alias syntax
- Missing quotes or wrong quote types
Ready to prove it?
Pass the test and this pattern moves unknown
