diff --git a/.changes/unreleased/Features-20220427-095609.yaml b/.changes/unreleased/Features-20220427-095609.yaml new file mode 100644 index 00000000000..97fe0fd5eaf --- /dev/null +++ b/.changes/unreleased/Features-20220427-095609.yaml @@ -0,0 +1,7 @@ +kind: Features +body: Add support for percentages in error_if, warn_if +time: 2022-04-27T09:56:09.707636+10:00 +custom: + Author: jaypeedevlin ehmartens + Issue: "4723" + PR: "5172" diff --git a/core/dbt/include/global_project/macros/materializations/tests/helpers.sql b/core/dbt/include/global_project/macros/materializations/tests/helpers.sql index efc55288076..2592af3c92e 100644 --- a/core/dbt/include/global_project/macros/materializations/tests/helpers.sql +++ b/core/dbt/include/global_project/macros/materializations/tests/helpers.sql @@ -3,12 +3,27 @@ {%- endmacro %} {% macro default__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%} + {% set model_name = model.file_key_name.split('.')[1] %} + {% set warn_if, warn_pct_division = get_pct_division(warn_if, model_name) %} + {% set error_if, error_pct_division = get_pct_division(error_if, model_name) %} + select {{ fail_calc }} as failures, - {{ fail_calc }} {{ warn_if }} as should_warn, - {{ fail_calc }} {{ error_if }} as should_error + {{ fail_calc }} {{ warn_pct_division }} {{ warn_if }} as should_warn, + {{ fail_calc }} {{ error_pct_division }} {{ error_if }} as should_error from ( {{ main_sql }} {{ "limit " ~ limit if limit != none }} ) dbt_internal_test {%- endmacro %} + + +{% macro get_pct_division(threshold, model_name) %} + {% if threshold[-1] == '%' %} + {% set threshold = threshold[:-1] %} + {% set pct_division = '/ (select count(*) from ' ~ ref(model_name) ~ ') * 100' %} + {% else %} + {% set pct_division = '' %} + {% endif %} + {% do return((threshold, pct_division)) %} +{% endmacro %}