letsBeePolite
letsBeePolite

Reputation: 2251

How to merge tuples from macros in DBT

Looking to merge tuples from different macros into one. As in the following code, the intention is to merge the output of

{% macro days15() %}
    (1, 2, 3, 4, 5)                                                                                                                           
{% endmacro %}

{% macro days69() %}
    (6,7,8,9)
{% endmacro %}

{% macro days19() %}
    {{ days15() }} UNION ALL {{ days69() }}
{% endmacro %}

With the expectations that days19 returns a tuple with values union of days15 and days69.

Upvotes: 0

Views: 935

Answers (1)

Matti
Matti

Reputation: 86

I know I am not answering your question directly, but a better and more DRY approach for your "use case" is to create 1 macro with 2 parameters, like this:

{% macro days(from, to) %}
    (
        {%- for i in range(from, to + 1) -%}
        {{ i }}{{ ',' if not loop.last }}
        {%- endfor -%}
    )
{% endmacro %}

you can then call the macro with days(1, 5), days(6, 9) and days(1, 9)

Upvotes: 1

Related Questions