Skip to content

Commit

Permalink
Merge branch 'main' into allow_extra_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
NiallRees committed Jan 18, 2024
2 parents 9f146fa + 56ecce4 commit f1eb9a5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .changes/2.3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### Features

- Support for query tags from profiles.yml and environment variables ([#21](https://github.com/get-select/dbt-snowflake-query-tags/pull/21))
- Add support for custom query comment keys and values ([#22](https://github.com/get-select/dbt-snowflake-query-tags/pull/22))

### Contributors
- [@maddoc1](https://github.com/maddoc1) (Features)

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ and is generated by [Changie](https://github.com/miniscruff/changie).

### Features

- Support for query tags from profiles.yml and environment variables ([#21](https://github.com/get-select/dbt-snowflake-query-tags/pull/21))
- Add support for custom query comment keys and values ([#22](https://github.com/get-select/dbt-snowflake-query-tags/pull/22))

### Contributors
- [@maddoc1](https://github.com/maddoc1) (Features)


## dbt-snowflake-query-tags 2.3.1 - August 18, 2023
Expand Down
46 changes: 34 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ query-comment:

### Query tags

To extend the information added in the query tags, set the [query_tag](https://docs.getdbt.com/reference/resource-configs/snowflake-configs#query-tags) config value to a mapping type. Examples:
To extend the information added in the query tags, there are a few options:

#### Compatible config
#### Model config

Set the [query_tag](https://docs.getdbt.com/reference/resource-configs/snowflake-configs#query-tags) config value to a mapping type. Example:

Model
```sql
Expand All @@ -125,16 +127,12 @@ Model
select ...
```

Results in a final query tag of
Results in the following query tag. The additional information is added by this package.
```
'{"team": "data", "app": "dbt", "dbt_snowflake_query_tags_version": "2.3.2", "is_incremental": true}'
```
the additional information is added by this package.
#### Incompatible config
Using a non-mapping type in the `query_tag` config will result in a warning, and the config being ignored.
Note that using a non-mapping type in the `query_tag` config will result in a warning, and the config being ignored.
Model
```sql
Expand All @@ -145,17 +143,41 @@ Model
select ...
```

Leads to a warning
Warning:
```
dbt-snowflake-query-tags warning: the query_tag config value of 'data team' is not a mapping type, so is being ignored. If you'd like to add additional query tag information, use a mapping type instead, or remove it to avoid this message.
```

Results in a final query tag of
#### Profiles.yml

Additionally, you can set the `query_tag` value in the `profiles.yml`. This must be a valid json object.

profiles.yml
```yml
default:
outputs:
dev:
query_tag: '{"team": "data"}'
...
target: dev
```
'{"app": "dbt", "dbt_snowflake_query_tags_version": "2.3.2", "is_incremental": false}'
#### Environment variables
Another option is to use the optional project variable `env_vars_to_query_tag_list` to provide a list of environment variables to pull query tag values from.

Example:

dbt_project.yml:
```yml
vars:
env_vars_to_query_tag_list: ['TEAM','JOB_NAME']
```

Note that the query_tag value of 'data team' is not present.
Results in a final query tag of
```
'{"team": "data", "job_name": "daily", "app": "dbt", "dbt_snowflake_query_tags_version": "2.3.2", "is_incremental": true}'
```
## Contributing
Expand Down
24 changes: 23 additions & 1 deletion macros/query_tags.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
{%- endmacro %}

{% macro default__set_query_tag() -%}
{# Get session level query tag #}
{% set original_query_tag = get_current_query_tag() %}
{% set original_query_tag_parsed = {} %}

{% if original_query_tag %}
{% if fromjson(original_query_tag) is mapping %}
{% set original_query_tag_parsed = fromjson(original_query_tag) %}
{% else %}
{% do log("dbt-snowflake-query-tags warning: the session level query tag value of '{}' is not a mapping type, so is being ignored. If you'd like to add additional query tag information, use a mapping type instead, or remove it to avoid this message.".format(original_query_tag), True) %}
{% endif %}
{% endif %}

{# The env_vars_to_query_tag_list should contain an environment variables list to construct query tag dict #}
{% set env_var_query_tags = {} %}
{% if var('env_vars_to_query_tag_list', '') %} {# Get a list of env vars from env_vars_to_query_tag_list variable to add additional query tags #}
{% for k in var('env_vars_to_query_tag_list') %}
{% set v = env_var(k, '') %}
{% do env_var_query_tags.update({k.lower(): v}) if v %}
{% endfor %}
{% endif %}

{# Start with any model-configured dict #}
{% set query_tag = config.get('query_tag', default={}) %}

Expand All @@ -11,6 +32,8 @@
{% set query_tag = {} %} {# If the user has set the query tag config as a non mapping type, start fresh #}
{% endif %}

{% do query_tag.update(original_query_tag_parsed) %}
{% do query_tag.update(env_var_query_tags) %}

{%- do query_tag.update(
app='dbt',
Expand All @@ -32,7 +55,6 @@
{% endif %}

{% set query_tag_json = tojson(query_tag) %}
{% set original_query_tag = get_current_query_tag() %}
{{ log("Setting query_tag to '" ~ query_tag_json ~ "'. Will reset to '" ~ original_query_tag ~ "' after materialization.") }}
{% do run_query("alter session set query_tag = '{}'".format(query_tag_json)) %}
{{ return(original_query_tag)}}
Expand Down

0 comments on commit f1eb9a5

Please sign in to comment.