-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dbt_profile vars not parsing derived variable correctly when used in a yml file #10589
Comments
What if you use a Jinja filter to cast it to a string? i.e. …
vars:
date: "2024-06-30"
year: "{{ modules.datetime.datetime.strptime(var('date'), '%Y-%m-%d').year | string }}"
… Or: …
sources:
- name: xyz
schema: abc
tables:
- name: table_x
identifier: "{{ 'table_identifier_' ~ var('year') | string }}"
… |
Thanks for getting back! Using |
Ah, I see! Here's what's going on: Jinja is not supported within the ![]() There's more information here and here. (The proposal in #2955 mentions being "strict about accepting only literal values", so I don't think it would fit what you're after.) I understand that the way you defined the Since we're not planning on supporting Jinja within the Alternative approachIf you want to keep it as close to your example as possible, you'd need to do this:
vars:
date: "2024-06-30" sources:
- name: xyz
schema: abc
tables:
- name: table_x
identifier: "{{ 'table_identifier_' ~ modules.datetime.datetime.strptime(var('date'), '%Y-%m-%d').year }}" instead of this:
vars:
date: "2024-06-30"
year: "{{ modules.datetime.datetime.strptime(var('date'), '%Y-%m-%d').year }}" sources:
- name: xyz
schema: abc
tables:
- name: table_x
identifier: "{{ 'table_identifier_' ~ var('year') }}" |
Understood, have missed that! Thanks for your help. For anyone else looking I stumbled across a more succinct solution. Yaml will parse a date like this with no quotation marks, so there's no need to use modules.datetime: ...
vars:
date: 2024-06-30
...
...
identifer: "{{ 'table_' ~ var('date').year }}"
... |
Nice find, and thank you for sharing! 🤩 I just verified that you can also include a --vars "{'date': 2022-02-02}" The key is not wrapping the The example values here all worked as well:
|
@dbeatty10 @MichelleArk |
I have a use case that is somewhat related to this. I'm trying to dynamically change the netsuite database that is used in the netsuite package (see step 4). The laziest way would be to do it by doing some sort of jinja in vars:
netsuite_database: "{{ 'staging_raw' if target.name in ('snowflake-staging', 'development') else 'raw' }}" However, the resulting SQL is then: ...
from {{ 'staging_raw' if target.name in ('snowflake-staging', 'development') else 'raw' }}.netsuite_suiteanalytics.location
... It would be really awesome if something like this would be possible. The alternative I'm going for now is don't set the variable in dbt run -s netsuite2 --vars "{'netsuite_database': $NETSUITE_DATABASE}" More background on the things I tried can be found here. |
Is this a new bug in dbt-core?
Current Behavior
Unfortunately I’ve got some sources with different date formats at the end of their table names. I’d like to specify vars in my dbt_project.yml like this:
The year variable works fine when I use it in an sql query, but if I use it in my sources.yml like so:
However when I try and use this with
{{ source(‘xyz’, ‘table_x’) }}
dbt renders, as a table name,abc.table_identifier_{{ modules.datetime…}}
obviously throwing an error.If I hardcode the value for year it works fine.
Expected Behavior
Expecting
{{ source(‘xyz’, ‘table_x’) }}
to resolve toabc.table_identifier_2024
Steps To Reproduce
models/*.yml
, referencing a derived variabledbt show —inline “select * from {{ source(‘xyz’, ‘table_x’) }}”
Unhandled error while executing
Relevant log output
No response
Environment
Which database adapter are you using with dbt?
other (mention it in "Additional Context")
Additional Context
I’m using a custom built adapter for our data infrastructure but I don’t believe this to have an impact. It seems the issue is in how the variables are rendered before any adapter code is run.
Happy to be wrong if this isn’t the case!
The text was updated successfully, but these errors were encountered: