Skip to content

Commit

Permalink
Merge pull request #72 from get-select/fix_query_text_no_comments
Browse files Browse the repository at this point in the history
Fix query_text_no_comments
  • Loading branch information
NiallRees authored Jan 25, 2023
2 parents 6fd213b + c93d54e commit 2c83d2d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changes/1.5.0.md
Original file line number Diff line number Diff line change
@@ -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))


8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand All @@ -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/<this_number>/projects/<not_this_number>/jobs
dbt_cloud_account_id: 12345 # https://cloud.getdbt.com/deploy/<this_number>/projects/<not_this_number>/jobs
```

### Only want to use the get_query_comment macro?
Expand Down
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'dbt_snowflake_monitoring'
version: '1.4.3'
version: '1.5.0'
config-version: 2

profile: dbt_snowflake_monitoring
Expand Down
14 changes: 14 additions & 0 deletions macros/create_regex_udf.sql
Original file line number Diff line number Diff line change
@@ -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 %}
12 changes: 7 additions & 5 deletions models/query_history_enriched.sql
Original file line number Diff line number Diff line change
@@ -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 /* <comment text> */
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 /* <comment text> */ 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
Expand Down

0 comments on commit 2c83d2d

Please sign in to comment.