Skip to content

Commit

Permalink
Merge pull request #44 from calogica/fix/issue-41
Browse files Browse the repository at this point in the history
Adds better support for integer division issue in equal_expression macro
  • Loading branch information
clausherther authored Apr 19, 2021
2 parents 1a9f0fb + 3478249 commit fd8985f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
6 changes: 6 additions & 0 deletions integration_tests/models/schema_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ models:
expression: sum(col_numeric_a)
compare_model: ref("data_test")
group_by: [idx]
- dbt_expectations.equal_expression:
expression: sum(col_numeric_a)
compare_expression: sum(col_numeric_a * .5)
compare_model: ref("data_test")
group_by: [idx]
tolerance_percent: .5
- dbt_expectations.expect_column_pair_values_to_be_in_set:
column_A: col_numeric_a
column_B: col_numeric_b
Expand Down
32 changes: 23 additions & 9 deletions macros/schema_tests/_generalized/equal_expression.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,34 @@
row_condition=None,
compare_row_condition=None,
tolerance=0.0,
tolerance_percent=None) -%}
{{ adapter.dispatch('test_equal_expression', packages = dbt_expectations._get_namespaces()) (model, expression,
tolerance_percent=None,
return_difference=False
) -%}

{{ adapter.dispatch('test_equal_expression', packages = dbt_expectations._get_namespaces()) (
model, expression,
compare_model,
compare_expression,
group_by,
compare_group_by,
row_condition,
compare_row_condition,
tolerance,
tolerance_percent) }}
tolerance_percent,
return_difference) }}
{%- endmacro %}

{%- macro default__test_equal_expression(model, expression,
{%- macro default__test_equal_expression(
model, expression,
compare_model,
compare_expression,
group_by,
compare_group_by,
row_condition,
compare_row_condition,
tolerance,
tolerance_percent) -%}
tolerance_percent,
return_difference) -%}

{%- set compare_model = model if not compare_model else compare_model -%}
{%- set compare_expression = expression if not compare_expression else compare_expression -%}
Expand All @@ -73,7 +80,7 @@
b.expression as compare_expression,
abs(coalesce(a.expression, 0) - coalesce(b.expression, 0)) as expression_difference,
abs(coalesce(a.expression, 0) - coalesce(b.expression, 0))/
nullif(a.expression, 0) as expression_difference_percent
nullif(a.expression * 1.0, 0) as expression_difference_percent
from
a
full outer join
Expand All @@ -84,10 +91,17 @@
)
-- DEBUG:
-- select * from final
select count(*) from final
select
{% if return_difference %}
coalesce(sum(expression_difference), 0)
{% else %}
count(*)
{% endif %}
from final
where
expression_difference > {{ tolerance }}
{% if tolerance_percent %}
and expression_difference_percent > {{ tolerance_percent }}
expression_difference_percent > {{ tolerance_percent }}
{% else %}
expression_difference > {{ tolerance }}
{% endif %}
{%- endmacro -%}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- macro test_expect_table_row_count_to_equal_other_table_times_factor(model, compare_model, factor) -%}
{{ dbt_expectations.test_equal_expression(model, "count(*)", compare_model=compare_model, compare_expression="count(*) * " + factor|string) }}
{{ dbt_expectations.test_equal_expression(model, "count(*)", compare_model=compare_model, compare_expression="count(*) * " + factor|string, return_difference=True) }}
{%- endmacro -%}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- macro test_expect_table_row_count_to_equal_other_table(model, compare_model) -%}
{{ dbt_expectations.test_equal_expression(model, "count(*)", compare_model=compare_model) }}
{{ dbt_expectations.test_equal_expression(model, "count(*)", compare_model=compare_model, return_difference=True) }}
{%- endmacro -%}

0 comments on commit fd8985f

Please sign in to comment.