diff --git a/.changes/1.5.0.md b/.changes/1.5.0.md new file mode 100644 index 0000000..798c54a --- /dev/null +++ b/.changes/1.5.0.md @@ -0,0 +1,7 @@ +## dbt-snowflake-monitoring 1.5.0 - January 25, 2023 + +### Fixes + +- Fix query_text_no_comments regex ([#72](https://github.com/get-select/dbt-snowflake-monitoring/pull/72)) + + diff --git a/CHANGELOG.md b/CHANGELOG.md index b4349e5..9b6e10a 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-monitoring 1.5.0 - January 25, 2023 + +### Fixes + +- Fix query_text_no_comments regex ([#72](https://github.com/get-select/dbt-snowflake-monitoring/pull/72)) + + + ## dbt-snowflake-monitoring 1.4.3 - January 20, 2023 ### Fixes diff --git a/README.md b/README.md index 94206ac..b249d35 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Add the following to your `packages.yml` file: ```yaml packages: - package: get-select/dbt_snowflake_monitoring - version: 1.4.2 + version: 1.5.0 ``` To attribute costs to individual models via the `dbt_metadata` column in the `query_history_enriched` model, add the following to `dbt_project.yml`: @@ -29,7 +29,7 @@ query-comment: The generate URLs to dbt Cloud jobs and runs in the `dbt_queries` model, add the following variable to `dbt_project.yml`: ```yaml vars: - dbt_cloud_account_id: 12345 # https://cloud.getdbt.com/deploy//projects//jobs + dbt_cloud_account_id: 12345 # https://cloud.getdbt.com/deploy//projects//jobs ``` ### Only want to use the get_query_comment macro? diff --git a/dbt_project.yml b/dbt_project.yml index 28ff717..2772893 100644 --- a/dbt_project.yml +++ b/dbt_project.yml @@ -1,5 +1,5 @@ name: 'dbt_snowflake_monitoring' -version: '1.4.3' +version: '1.5.0' config-version: 2 profile: dbt_snowflake_monitoring diff --git a/macros/create_regex_udf.sql b/macros/create_regex_udf.sql new file mode 100644 index 0000000..411cf89 --- /dev/null +++ b/macros/create_regex_udf.sql @@ -0,0 +1,14 @@ +{% macro create_regexp_replace_udf() %} + +create or replace function {{ target.database }}.{{ target.schema }}.dbt_snowflake_monitoring_regexp_replace(subject text, pattern text, replacement text) +returns string +language javascript +comment = 'Created by dbt-snowflake-monitoring dbt package.' +as +$$ + const p = SUBJECT; + let regex = new RegExp(PATTERN, 'g') + return p.replace(regex, REPLACEMENT); +$$ + +{% endmacro %} diff --git a/models/query_history_enriched.sql b/models/query_history_enriched.sql index f7451f0..b5ac460 100644 --- a/models/query_history_enriched.sql +++ b/models/query_history_enriched.sql @@ -1,14 +1,16 @@ -{{ config(materialized='incremental', unique_key=['query_id', 'start_time']) }} +{{ config( + materialized='incremental', + unique_key=['query_id', 'start_time'], + pre_hook="{{ create_regexp_replace_udf() }}" +) }} with query_history as ( select *, - -- this removes comments enclosed by /* */ - regexp_replace(query_text, '(/\*.*\*/)') as _query_text_no_comments, - -- this removes single line comments starting with -- and either ending with a new line or end of string - regexp_replace(_query_text_no_comments, '(--.*$)|(--.*\n)') as query_text_no_comments, + -- this removes comments enclosed by /* */ and single line comments starting with -- and either ending with a new line or end of string + {{ target.database }}.{{ target.schema }}.dbt_snowflake_monitoring_regexp_replace(query_text, $$(/\*(.|\n)+?\*/)|(--.*$)|(--.*\n)$$, '') as query_text_no_comments, regexp_substr(query_text, '/\\*\\s({"app":\\s"dbt".*})\\s\\*/', 1, 1, 'ie') as _dbt_json_meta, try_parse_json(_dbt_json_meta) as dbt_metadata