Skip to content

Commit 3d02772

Browse files
authored
Merge pull request #67 from leoebfolsom/lf/fix-incorrect-null-to-missing-match
bug fix: account for incorrect comparisons involving null values
2 parents 470b72a + 1dbaaf9 commit 3d02772

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

integration_tests/seeds/data_compare_all_columns__albertsons_produce.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ id,fruit,ripeness
66
5,orange,orange
77
6,,brown
88
7,orange,orange
9-
9,apple,mushy
9+
9,apple,mushy
10+
10,apple,
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
primary_key,column_name,perfect_match,null_in_a,null_in_b,missing_from_a,missing_from_b,conflicting_values
22
8,ID,false,false,false,false,true,false
33
9,ID,false,false,false,true,false,false
4+
10,ID,false,false,false,true,false,false
45
6,FRUIT,false,false,true,false,false,false
56
8,FRUIT,false,false,false,false,true,false
67
9,FRUIT,false,false,false,true,false,false
8+
10,FRUIT,false,false,false,true,false,false
79
2,RIPENESS,false,false,false,false,false,true
810
7,RIPENESS,false,true,false,false,false,false
911
8,RIPENESS,false,false,false,false,true,false
10-
9,RIPENESS,false,false,false,true,false,false
12+
9,RIPENESS,false,false,false,true,false,false
13+
10,RIPENESS,false,false,false,true,false,false

macros/compare_column_values_verbose.sql

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,27 @@ b_query as (
2020
'{{ column_to_compare }}' as column_name,
2121
{% endif %}
2222

23-
coalesce(a_query.{{ column_to_compare }} = b_query.{{ column_to_compare }},
24-
(a_query.{{ column_to_compare }} is null and b_query.{{ column_to_compare }} is null),
25-
false) as perfect_match,
23+
coalesce(
24+
a_query.{{ column_to_compare }} = b_query.{{ column_to_compare }} and
25+
a_query.{{ primary_key }} is not null and b_query.{{ primary_key }} is not null,
26+
(a_query.{{ column_to_compare }} is null and b_query.{{ column_to_compare }} is null),
27+
false
28+
) as perfect_match,
2629
a_query.{{ column_to_compare }} is null and a_query.{{ primary_key }} is not null as null_in_a,
2730
b_query.{{ column_to_compare }} is null and b_query.{{ primary_key }} is not null as null_in_b,
2831
a_query.{{ primary_key }} is null as missing_from_a,
2932
b_query.{{ primary_key }} is null as missing_from_b,
30-
coalesce(a_query.{{ column_to_compare }} != b_query.{{ column_to_compare }} and
31-
(a_query.{{ column_to_compare }} is not null or b_query.{{ column_to_compare }} is not null), false)
32-
as conflicting_values
33-
-- considered a conflict if the values do not match AND at least one of the values is not null.
33+
coalesce(
34+
a_query.{{ primary_key }} is not null and b_query.{{ primary_key }} is not null and
35+
-- ensure that neither value is missing before considering it a conflict
36+
(
37+
a_query.{{ column_to_compare }} != b_query.{{ column_to_compare }} or -- two not-null values that do not match
38+
(a_query.{{ column_to_compare }} is not null and b_query.{{ column_to_compare }} is null) or -- null in b and not null in a
39+
(a_query.{{ column_to_compare }} is null and b_query.{{ column_to_compare }} is not null) -- null in a and not null in b
40+
),
41+
false
42+
) as conflicting_values
43+
-- considered a conflict if the values do not match AND at least one of the values is not null.
3444

3545
from a_query
3646

0 commit comments

Comments
 (0)