-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from morsapaes/19-audit_helper
Add compatibility macros for `dbt-audit-helper`
- Loading branch information
Showing
13 changed files
with
320 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "dbt-utils"] | ||
path = dbt-utils | ||
url = https://github.com/dbt-labs/dbt-utils | ||
[submodule "dbt-audit-helper"] | ||
path = dbt-audit-helper | ||
url = https://github.com/dbt-labs/dbt-audit-helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule dbt-audit-helper
added at
d76a87
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
TARGET ?= materialize | ||
|
||
test-materialize: | ||
dbt deps | ||
dbt seed --target $(TARGET) | ||
dbt run --target $(TARGET) | ||
dbt test --target $(TARGET) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name: materialize_dbt_utils_dbt_audit_helper_integration_tests | ||
version: '1.0' | ||
config-version: 2 | ||
|
||
profile: integration_tests | ||
|
||
dispatch: | ||
- macro_namespace: audit_helper | ||
search_order: [materialize_dbt_utils, audit_helper] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
packages: | ||
- local: ../../ | ||
- local: ../../dbt-audit-helper | ||
- local: ../../dbt-audit-helper/integration_tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{% macro materialize__compare_column_values(a_query, b_query, primary_key, column_to_compare) -%} | ||
with a_query as ( | ||
{{ a_query }} | ||
), | ||
|
||
b_query as ( | ||
{{ b_query }} | ||
), | ||
|
||
joined as ( | ||
select | ||
coalesce(a_query.{{ primary_key }}, b_query.{{ primary_key }}) as {{ primary_key }}, | ||
a_query.{{ column_to_compare }} as a_query_value, | ||
b_query.{{ column_to_compare }} as b_query_value, | ||
case | ||
when a_query.{{ column_to_compare }} = b_query.{{ column_to_compare }} then '✅: perfect match' | ||
when a_query.{{ column_to_compare }} is null and b_query.{{ column_to_compare }} is null then '✅: both are null' | ||
when a_query.{{ primary_key }} is null then '🤷: missing from a' | ||
when b_query.{{ primary_key }} is null then '🤷: missing from b' | ||
when a_query.{{ column_to_compare }} is null then '🤷: value is null in a only' | ||
when b_query.{{ column_to_compare }} is null then '🤷: value is null in b only' | ||
when a_query.{{ column_to_compare }} != b_query.{{ column_to_compare }} then '🙅: values do not match' | ||
else 'unknown' -- this should never happen | ||
end as match_status, | ||
case | ||
when a_query.{{ column_to_compare }} = b_query.{{ column_to_compare }} then 0 | ||
when a_query.{{ column_to_compare }} is null and b_query.{{ column_to_compare }} is null then 1 | ||
when a_query.{{ primary_key }} is null then 2 | ||
when b_query.{{ primary_key }} is null then 3 | ||
when a_query.{{ column_to_compare }} is null then 4 | ||
when b_query.{{ column_to_compare }} is null then 5 | ||
when a_query.{{ column_to_compare }} != b_query.{{ column_to_compare }} then 6 | ||
else 7 -- this should never happen | ||
end as match_order | ||
|
||
from a_query | ||
|
||
full outer join b_query on a_query.{{ primary_key }} = b_query.{{ primary_key }} | ||
), | ||
|
||
aggregated as ( | ||
select | ||
match_status, | ||
match_order, | ||
count(*) as count_records | ||
from joined | ||
|
||
group by match_status, match_order | ||
) | ||
|
||
select | ||
match_status, | ||
count_records, | ||
-- TODO(morsapaes): Materialize doesn't support window functions yet, | ||
-- so adding a ugly hack. Once we do, revert to the original: | ||
-- round(100.0 * count_records / sum(count_records) over (), 2) as percent_of_total | ||
round(100.0 * count_records / (select sum(count_records) from aggregated), 2) as percent_of_total | ||
|
||
from aggregated | ||
|
||
order by match_order | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{% macro materialize__compare_queries(a_query, b_query, primary_key=None) %} | ||
|
||
with a as ( | ||
|
||
{{ a_query }} | ||
|
||
), | ||
|
||
b as ( | ||
|
||
{{ b_query }} | ||
|
||
), | ||
|
||
a_intersect_b as ( | ||
|
||
select * from a | ||
{{ dbt_utils.intersect() }} | ||
select * from b | ||
|
||
), | ||
|
||
a_except_b as ( | ||
|
||
select * from a | ||
{{ dbt_utils.except() }} | ||
select * from b | ||
|
||
), | ||
|
||
b_except_a as ( | ||
|
||
select * from b | ||
{{ dbt_utils.except() }} | ||
select * from a | ||
|
||
), | ||
|
||
all_records as ( | ||
|
||
select | ||
*, | ||
true as in_a, | ||
true as in_b | ||
from a_intersect_b | ||
|
||
union all | ||
|
||
select | ||
*, | ||
true as in_a, | ||
false as in_b | ||
from a_except_b | ||
|
||
union all | ||
|
||
select | ||
*, | ||
false as in_a, | ||
true as in_b | ||
from b_except_a | ||
|
||
), | ||
|
||
summary_stats as ( | ||
select | ||
in_a, | ||
in_b, | ||
count(*) as count | ||
from all_records | ||
|
||
group by 1, 2 | ||
) | ||
-- select * from all_records | ||
-- where not (in_a and in_b) | ||
-- order by {{ primary_key ~ ", " if primary_key is not none }} in_a desc, in_b desc | ||
|
||
select | ||
*, | ||
-- TODO(morsapaes): Materialize doesn't support window functions yet, | ||
-- so adding a ugly hack. Once we do, revert to the original: | ||
-- round(100.0 * count / sum(count) over (), 2) as percent_of_total | ||
round(100.0 * count / (select sum(count) from summary_stats), 2) as percent_of_total | ||
|
||
from summary_stats | ||
order by in_a desc, in_b desc | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{% macro materialize__compare_relation_columns(a_relation, b_relation) %} | ||
|
||
with a_cols as ( | ||
{{ audit_helper.get_columns_in_relation_sql(a_relation) }} | ||
), | ||
|
||
b_cols as ( | ||
{{ audit_helper.get_columns_in_relation_sql(b_relation) }} | ||
) | ||
|
||
select | ||
column_name, | ||
a_cols.ordinal_position as a_ordinal_position, | ||
b_cols.ordinal_position as b_ordinal_position, | ||
a_cols.data_type as a_data_type, | ||
b_cols.data_type as b_data_type, | ||
coalesce(a_cols.ordinal_position = b_cols.ordinal_position, false) as has_ordinal_position_match, | ||
coalesce(a_cols.data_type = b_cols.data_type, false) as has_data_type_match | ||
from a_cols | ||
full outer join b_cols using (column_name) | ||
order by coalesce(a_cols.ordinal_position, b_cols.ordinal_position) | ||
|
||
{% endmacro %} | ||
|
||
|
||
{% macro materialize__get_columns_in_relation_sql(relation) %} | ||
{#- | ||
From: https://github.com/dbt-labs/dbt/blob/23484b18b71010f701b5312f920f04529ceaa6b2/plugins/postgres/dbt/include/postgres/macros/adapters.sql#L32 | ||
Edited to include ordinal_position | ||
-#} | ||
select | ||
ordinal_position, | ||
column_name, | ||
data_type, | ||
character_maximum_length, | ||
numeric_precision, | ||
numeric_scale | ||
|
||
from {{ relation.information_schema('columns') }} | ||
where table_name = '{{ relation.identifier }}' | ||
{% if relation.schema %} | ||
and table_schema = '{{ relation.schema }}' | ||
{% endif %} | ||
order by ordinal_position | ||
{% endmacro %} |
Oops, something went wrong.