-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add percentage support for error_if, warn_if #5172
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] == '%' %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you imagine someone wanting to define a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We took a list at that this morning, I think I probably could think of someone wanting to do this (in a rare case), but it wasn't clear to me why the current approach wouldn't work (given that thresholds should work in the same way). Could you help me understand how that might work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case where a singular test depends on multiple nodes, I'm not sure how we can tell which node should represent the denominator? I also think, in a singular test, you're writing all the SQL yourself, it's pretty trivial to add a |
||
{% set threshold = threshold[:-1] %} | ||
{% set pct_division = '/ (select count(*) from ' ~ ref(model_name) ~ ') * 100' %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
||
{% else %} | ||
{% set pct_division = '' %} | ||
{% endif %} | ||
{% do return((threshold, pct_division)) %} | ||
{% endmacro %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two thoughts:
file_key_name
issources.source_name
(and missing the name of the specific source table), so I don't think it would work as currently constituted. We could updatefile_key_name
to includesources.source_name.source_table
.where
config, and we're calculating%
, I think we would want to filter both the numerator and denominator by thatwhere
config. You could reproduce that logic here (config.get("where")
), but all it all it seems preferable if there were some where to access the rendered version of{{ model }}
(from the generic test query) or{{ test_metadata.kwargs.model }}
(from the test node), since that's really the thing we want.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point on the 'where' @jtcohen6, we totally overlooked that! We've added that in, but not pushed the change yet, while we wait to hear from the language team on how we might approach getting the non-model resources appropriately.