Skip to content

Commit 94d96fa

Browse files
authored
Bugfix/37 fix skip ephemeral models (#38)
* fix: #37 fix skip ephemeral models * updated readme file for store_test_results macro behavior * added description for macro __get_test_model_materialization * fix null table_name
1 parent b4d19a0 commit 94d96fa

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ Since the version 1.4+, all models and metrics will be enabled by default.
303303
#### store_test_results ([source](https://github.com/infinitelambda/dq-tools/blob/main/macros/artifacts/test/store_test_results.sql))
304304

305305
This macro is used to parse `results` variable at the `on-run-end` context to achieve the test result nodes, and save them to the `DQ_ISSUE_LOG` table.
306+
If the model is materialized as `ephemeral`, this macro will insert the null value for aggregated fields related to tested model.
306307

307308
Usage:
308309

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{
2+
config(
3+
materialized='ephemeral'
4+
)
5+
}}
6+
7+
select 1 as col
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 2
2+
3+
models:
4+
- name: test_ephemeral_model
5+
columns:
6+
- name: col
7+
data_tests:
8+
- unique
9+
- not_null
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% macro __get_test_model_materialization(model_name) %}
2+
3+
{% if execute %}
4+
5+
{% for item in graph.nodes.values() | selectattr("name", "equalto", model_name) %}
6+
7+
{% set materialized = item.config.materialized %}
8+
9+
{{ return(materialized) }}
10+
11+
{% endfor %}
12+
13+
{% endif %}
14+
15+
{{ return('') }}
16+
17+
{% endmacro %}

macros/artifacts/test/utilities/general/__select_test_result.sql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{%- set test_type = dq_tools.__get_test_type(result.node) -%}
99
{%- set testing_model = dq_tools.__get_test_model(result.node) -%}
1010
{%- set testing_model_relation = dq_tools.__get_relation(testing_model) -%}
11+
{%- set materialization = dq_tools.__get_test_model_materialization(testing_model.name) -%}
1112
/* {{ testing_model }} */
1213

1314
select '{{ result.node.unique_id }}' as test_unique_id
@@ -18,22 +19,24 @@
1819
,'{{ dq_tools.__get_kpi_categorize(result.node) }}' as test_kpi_category_config
1920
,'{{ dq_tools.__get_dq_issue_type(result.node) }}' as dq_issue_type
2021
,'{{ result.status }}' as test_result
21-
,'{{ testing_model_relation }}' as table_name
22+
,{% if materialization != 'ephemeral' %}
23+
'{{ testing_model_relation }}'
24+
{% else %}''{% endif %} as table_name
2225
,'{{ dq_tools.__get_where_subquery(
2326
testing_model,
2427
result.node.config,
2528
sql_escape=true) }}' as table_query
2629
,'{{ dq_tools.__get_to_relation(result.node) }}' as ref_table
2730
,'{{ dq_tools.__get_column_name(result.node) | escape }}' as column_name
28-
,{% if test_type == 'generic' %}
31+
,{% if test_type == 'generic' and materialization != 'ephemeral' %}
2932
{{ adapter.get_columns_in_relation(testing_model_relation) | length }}
3033
{% else %}null{% endif %} as no_of_table_columns
3134
,'{{ dq_tools.__get_to_column_name(result.node) | escape }}' as ref_column
32-
,{% if test_type == 'generic' %}(
35+
,{% if test_type == 'generic' and materialization != 'ephemeral' %}(
3336
select count(*)
3437
from {{ testing_model_relation }}
3538
){% else %}null{% endif %} as no_of_records
36-
,{% if test_type == 'generic' %}(
39+
,{% if test_type == 'generic' and materialization != 'ephemeral' %}(
3740
select count(*)
3841
from {{ dq_tools.__get_where_subquery(testing_model, result.node.config) }}
3942
){% else %}null{% endif %} as no_of_records_scanned

macros/artifacts/test/utilities/general/_general.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,8 @@ macros:
109109
But if there is a config.where presenting it will become the subquery:
110110
```
111111
(select * from {database}.{schema}.{table} where {where}) as dbt_subquery
112-
```
112+
```
113+
114+
- name: __get_test_model_materialization
115+
description: |
116+
Return the materialization type of the model being tested (eg: table, view, ephemeral etc).

0 commit comments

Comments
 (0)