-
Notifications
You must be signed in to change notification settings - Fork 42
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 #92 from dbt-labs/add-detect-column-differences
Add `compare_which_columns_differ`
- Loading branch information
Showing
9 changed files
with
157 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% set a_relation=ref('data_compare_which_columns_differ_a')%} | ||
|
||
{% set b_relation=ref('data_compare_which_columns_differ_b') %} | ||
|
||
-- lowercase for CI | ||
|
||
select | ||
lower(column_name) as column_name, | ||
has_difference | ||
from ( | ||
|
||
{{ audit_helper.compare_which_columns_differ( | ||
a_relation=a_relation, | ||
b_relation=b_relation, | ||
primary_key="id" | ||
) }} | ||
) as macro_output |
18 changes: 18 additions & 0 deletions
18
integration_tests/models/compare_which_columns_differ_exclude_cols.sql
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,18 @@ | ||
{% set a_relation=ref('data_compare_which_columns_differ_a')%} | ||
|
||
{% set b_relation=ref('data_compare_which_columns_differ_b') %} | ||
|
||
|
||
select | ||
lower(column_name) as column_name, | ||
has_difference | ||
from ( | ||
|
||
{{ audit_helper.compare_which_columns_differ( | ||
a_relation=a_relation, | ||
b_relation=b_relation, | ||
primary_key="id", | ||
exclude_columns=["becomes_null"] | ||
) }} | ||
|
||
) as macro_output |
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
5 changes: 5 additions & 0 deletions
5
integration_tests/seeds/data_compare_which_columns_differ_a.csv
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,5 @@ | ||
id,value_changes,becomes_null,becomes_not_null,does_not_change | ||
1,pink,22,a,dave | ||
2,blue,33,,dave | ||
3,green,44,c,dave | ||
4,yellow,55,d,dave |
5 changes: 5 additions & 0 deletions
5
integration_tests/seeds/data_compare_which_columns_differ_b.csv
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,5 @@ | ||
id,value_changes,becomes_null,becomes_not_null,does_not_change | ||
1,red,22,a,dave | ||
2,blue,,b,dave | ||
3,green,44,c,dave | ||
4,yellow,55,d,dave |
6 changes: 6 additions & 0 deletions
6
integration_tests/seeds/expected_results__compare_which_columns_differ.csv
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,6 @@ | ||
column_name,has_difference | ||
id,false | ||
value_changes,true | ||
becomes_null,true | ||
becomes_not_null,true | ||
does_not_change,false |
5 changes: 5 additions & 0 deletions
5
integration_tests/seeds/expected_results__compare_which_columns_differ_exclude_cols.csv
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,5 @@ | ||
column_name,has_difference | ||
id,false | ||
value_changes,true | ||
becomes_not_null,true | ||
does_not_change,false |
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,46 @@ | ||
{% macro compare_which_columns_differ(a_relation, b_relation, primary_key, exclude_columns=[]) %} | ||
{{ return(adapter.dispatch('compare_which_columns_differ', 'audit_helper')(a_relation, b_relation, primary_key, exclude_columns)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__compare_which_columns_differ(a_relation, b_relation, primary_key, exclude_columns=[]) %} | ||
|
||
{% set column_names = dbt_utils.get_filtered_columns_in_relation(from=a_relation, except=exclude_columns) %} | ||
|
||
with bool_or as ( | ||
|
||
select | ||
true as anchor | ||
{% for column in column_names %} | ||
{% set column_name = adapter.quote(column) %} | ||
{% set compare_statement %} | ||
((a.{{ column_name }} != b.{{ column_name }}) | ||
or (a.{{ column_name }} is null and b.{{ column_name }} is not null) | ||
or (a.{{ column_name }} is not null and b.{{ column_name }} is null)) | ||
{% endset %} | ||
|
||
, {{ dbt.bool_or(compare_statement) }} as {{ column | lower }}_has_difference | ||
|
||
{% endfor %} | ||
from {{ a_relation }} as a | ||
inner join {{ b_relation }} as b | ||
on a.{{ primary_key }} = b.{{ primary_key }} | ||
|
||
) | ||
|
||
{% for column in column_names %} | ||
|
||
select | ||
'{{ column }}' as column_name, | ||
{{ column | lower }}_has_difference as has_difference | ||
|
||
from bool_or | ||
|
||
{% if not loop.last %} | ||
|
||
union all | ||
|
||
{% endif %} | ||
|
||
{% endfor %} | ||
|
||
{% endmacro %} |