forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
equality.sql
74 lines (43 loc) · 1.14 KB
/
equality.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{% macro test_equality(model) %}
{% set compare_model = kwargs.get('compare_model', kwargs.get('arg')) %}
{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
{%- if not execute -%}
{{ return('') }}
{% endif %}
-- setup
{% set schema = model.schema %}
{% set model_a_name = model.name %}
{% set dest_columns = adapter.get_columns_in_table(schema, model_a_name) %}
{% set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') %}
-- core SQL
with a as (
select * from {{ model }}
),
b as (
select * from {{ compare_model }}
),
a_minus_b as (
select {{dest_cols_csv}} from a
except
select {{dest_cols_csv}} from b
),
b_minus_a as (
select {{dest_cols_csv}} from b
except
select {{dest_cols_csv}} from a
),
unioned as (
select * from a_minus_b
union all
select * from b_minus_a
),
final as (
select (select count(*) from unioned) +
(select abs(
(select count(*) from a_minus_b) -
(select count(*) from b_minus_a)
))
as count
)
select count from final
{% endmacro %}