Extending a DBT var in dbt_project.yml

675 Views Asked by At

I have a variable defined in my dbt_project.yml file as a set. I want to extend this set by adding a single string element to it or in future potentially another set. Here a template of what my dbt_project.yml currently looks like:

vars:
  var1_tuple: [item1, item2, item3] # existing set
  var2: '{{ (var('var1') + ['install']) | join(", ") }}'

Then in table.sql

WHERE column1 "{{ var('var2') }}"

But when I call this variable var2 from the SQL code, the code breaks as it doesn't correctly parse the var as a SQL set but rather as a string.

How can I correctly parse var2?

2

There are 2 best solutions below

3
On

Jinja treats tuples differently: in order to append something into a tuple, you first need to convert it into an array. On the other hand, you should also set the value you want to append into an array.

So, the following should work:

my_var: ['item1', 'item2', 'item3']
my_other_var: '{{ var("my_var") + ["item4"] }}'

From here, if you run something like: {% do log(var('my_other_var'), info=True) %}

you should be seeing: ['item1', 'item2', 'item3', 'item4']

1
On

The vars context method can only be used in dbt_project.yml for variables passed in via the command line with --vars. See this dbt help doc for details.

The following context methods and variables are available when configuring resources in the dbt_project.yml file. This applies to the models:, seeds:, and snapshots: keys in the dbt_project.yml file.

Available context methods:

  • env_var
  • var (Note: only variables defined with --vars are available)