When should I use a UDF instead of a macro?
Both user-defined functions (UDFs) and macros let you reuse logic across your dbt project, but they work in fundamentally different ways. Here's when to use each:
Use UDFs when:
Use macros when:
Can I use both together?
Yes! You can use a macro to call a UDF or call a macro from within a UDF, combining the benefits of both. So the following example shows how to use a macro to define default values for arguments alongside your logic, for your UDF
{% macro cents_to_dollars(column_name, scale=2) %}
{{ function('cents_to_dollars') }}({{ column_name }}, {{scale}})
{% endmacro %}
Related documentation
Was this page helpful?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
0