From fa9b19fc6c1c53ebdfd4f31460bef37a0437405d Mon Sep 17 00:00:00 2001 From: Niall Woodward <niall@niallrees.com> Date: Fri, 18 Aug 2023 10:02:36 +0100 Subject: [PATCH] Add warning when non-mapping query tag is set --- .changes/2.3.1.md | 7 +++ CHANGELOG.md | 8 +++ README.md | 57 ++++++++++++++++++- dbt_project.yml | 2 +- .../models/materialized_incremental.sql | 2 +- .../models/materialized_table.sql | 5 +- macros/query_comment.sql | 2 +- macros/query_tags.sql | 22 ++++--- 8 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 .changes/2.3.1.md diff --git a/.changes/2.3.1.md b/.changes/2.3.1.md new file mode 100644 index 0000000..37e6ad8 --- /dev/null +++ b/.changes/2.3.1.md @@ -0,0 +1,7 @@ +## dbt-snowflake-query-tags 2.3.1 - August 18, 2023 + +### Features + +- Handle non-mapping configs gracefully ([#17](https://github.com/get-select/dbt-snowflake-query-tags/pull/17)) + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 98bc340..9bd0835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html), and is generated by [Changie](https://github.com/miniscruff/changie). +## dbt-snowflake-query-tags 2.3.1 - August 18, 2023 + +### Features + +- Handle non-mapping configs gracefully ([#17](https://github.com/get-select/dbt-snowflake-query-tags/pull/17)) + + + ## dbt-snowflake-query-tags 2.3.0 - June 29, 2023 ### Features diff --git a/README.md b/README.md index 555ac62..82aa4a9 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ An example query comment contains: ```json { - "dbt_snowflake_query_tags_version": "2.3.0", + "dbt_snowflake_query_tags_version": "2.3.1", "app": "dbt", "dbt_version": "1.4.0", "project_name": "my_project", @@ -45,7 +45,7 @@ Query tags are used solely for attaching the `is_incremental` flag, as this isn' ```json { - "dbt_snowflake_query_tags_version": "2.3.0", + "dbt_snowflake_query_tags_version": "2.3.1", "app": "dbt", "is_incremental": true } @@ -96,6 +96,59 @@ query-comment: That's it! All dbt-issued queries will now be tagged. +## Adding additional metadata + +### Query comments + +To extend the information added in the query comments, use [meta](https://docs.getdbt.com/reference/resource-configs/meta) or [tag](https://docs.getdbt.com/reference/resource-configs/tags) configs. These are automatically added to the query comments. + +### 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: + +#### Compatible config + +Model +```sql +{{ config( + query_tag = {'team': 'data'} +) }} + +select ... +``` + +Results in a final query tag of +``` +'{"team": "data", "app": "dbt", "dbt_snowflake_query_tags_version": "2.3.1", "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. + +Model +```sql +{{ config( + query_tag = 'data team' +) }} + +select ... +``` + +Leads to a 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 +``` +'{"app": "dbt", "dbt_snowflake_query_tags_version": "2.3.1", "is_incremental": false}' +``` + +Note that the query_tag value of 'data team' is not present. + ## Contributing ### Adding a CHANGELOG Entry diff --git a/dbt_project.yml b/dbt_project.yml index 367649f..cce6929 100644 --- a/dbt_project.yml +++ b/dbt_project.yml @@ -1,3 +1,3 @@ name: 'dbt_snowflake_query_tags' -version: '2.3.0' +version: '2.3.1' config-version: 2 diff --git a/integration_test_project/models/materialized_incremental.sql b/integration_test_project/models/materialized_incremental.sql index bd6e4fc..9855ef1 100644 --- a/integration_test_project/models/materialized_incremental.sql +++ b/integration_test_project/models/materialized_incremental.sql @@ -1,4 +1,4 @@ -{{ config(materialized='incremental', tags=['a', 'b', 'c']) }} +{{ config(materialized='incremental', tags=['a', 'b', 'c'], query_tag={'test': 'test'}) }} select 1 as a diff --git a/integration_test_project/models/materialized_table.sql b/integration_test_project/models/materialized_table.sql index af11a9f..153a1be 100644 --- a/integration_test_project/models/materialized_table.sql +++ b/integration_test_project/models/materialized_table.sql @@ -1,11 +1,12 @@ {{ config( meta={ - "owner": "@alice", + "owner": "@alice", "model_maturity": "in dev" }, materialized="table", - tags='a' + tags='a', + query_tag='data team' ) }} diff --git a/macros/query_comment.sql b/macros/query_comment.sql index 6466669..cbec5ba 100644 --- a/macros/query_comment.sql +++ b/macros/query_comment.sql @@ -2,7 +2,7 @@ {%- set comment_dict = {} -%} {%- do comment_dict.update( app='dbt', - dbt_snowflake_query_tags_version='2.3.0', + dbt_snowflake_query_tags_version='2.3.1', dbt_version=dbt_version, project_name=project_name, target_name=target.name, diff --git a/macros/query_tags.sql b/macros/query_tags.sql index 63ec09c..b9ac487 100644 --- a/macros/query_tags.sql +++ b/macros/query_tags.sql @@ -4,15 +4,21 @@ {% macro default__set_query_tag() -%} {# Start with any model-configured dict #} - {% set tag_dict = config.get('query_tag', default={}) %} + {% set query_tag = config.get('query_tag', default={}) %} - {%- do tag_dict.update( + {% if query_tag is not mapping %} + {% do log("dbt-snowflake-query-tags warning: the query_tag config 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(query_tag), True) %} + {% set query_tag = {} %} {# If the user has set the query tag config as a non mapping type, start fresh #} + {% endif %} + + + {%- do query_tag.update( app='dbt', - dbt_snowflake_query_tags_version='2.3.0', + dbt_snowflake_query_tags_version='2.3.1', ) -%} {% if thread_id %} - {%- do tag_dict.update( + {%- do query_tag.update( thread_id=thread_id ) -%} {% endif %} @@ -20,15 +26,15 @@ {# We have to bring is_incremental through here because its not available in the comment context #} {% if model.resource_type == 'model' %} - {%- do tag_dict.update( + {%- do query_tag.update( is_incremental=is_incremental() ) -%} {% endif %} - {% set new_query_tag = tojson(tag_dict) %} + {% set query_tag_json = tojson(query_tag) %} {% set original_query_tag = get_current_query_tag() %} - {{ log("Setting query_tag to '" ~ new_query_tag ~ "'. Will reset to '" ~ original_query_tag ~ "' after materialization.") }} - {% do run_query("alter session set query_tag = '{}'".format(new_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)}} {% endmacro %}