-
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 #4 from fishtown-analytics/feature/compare-queries
Add compare_queries macro
- Loading branch information
Showing
5 changed files
with
145 additions
and
82 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% set a_query %} | ||
select * from {{ ref('data_compare_relations__a_relation') }} | ||
{% endset %} | ||
|
||
{% set b_query %} | ||
select * from {{ ref('data_compare_relations__b_relation') }} | ||
{% endset %} | ||
|
||
{{ audit_helper.compare_queries( | ||
a_query=a_query, | ||
b_query=b_query, | ||
primary_key="order_id" | ||
) }} |
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,82 @@ | ||
{% macro 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 * 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