Skip to content

Commit

Permalink
Jmcneill/expression is true tweak (dbt-labs#507)
Browse files Browse the repository at this point in the history
* Update README.md

* add some flexibility to expression_is_true execution plan and add a few new tests

* catch duplicate field name exception when the expression_is_true test is invoked

* expression is true - rename seeds, format sql and get rid of dupe column handler from PR comments

Co-authored-by: Joel Labes <joel.labes@dbtlabs.com>
  • Loading branch information
2 people authored and LewisDavies committed May 12, 2022
1 parent 2dfdb3c commit 55058e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
col_a,col_b,col_c
1,10,15
1,5,15
2,16,24
2,8,24
15 changes: 15 additions & 0 deletions integration_tests/models/generic_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ models:
expression: = 0.5
condition: col_a = 0.5

- name: data_test_expression_is_true_window_functions
tests:
- dbt_utils.expression_is_true:
expression: count(*) over (partition by col_a) = 2
- dbt_utils.expression_is_true:
expression: max(col_c) over (partition by col_a) = min(col_c) over (partition by col_a)
- dbt_utils.expression_is_true:
expression: sum(col_b) over (partition by col_a) = 15
condition: col_a = 1
columns:
- name: col_c
tests:
- dbt_utils.expression_is_true:
expression: = sum(col_b) over (partition by col_a)

- name: test_recency
tests:
- dbt_utils.recency:
Expand Down
21 changes: 12 additions & 9 deletions macros/generic_tests/expression_is_true.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
{% macro default__test_expression_is_true(model, expression, column_name, condition) %}

with meet_condition as (
select * from {{ model }} where {{ condition }}
select
*,
{% if column_name is none %}
{{ expression }}
{%- else %}
{{ column_name }} {{ expression }}
{%- endif %}
as _dbt_utils_test_expression_passed
from {{ model }}
where {{ condition }}
)

select
*
from meet_condition
{% if column_name is none %}
where not({{ expression }})
{%- else %}
where not({{ column_name }} {{ expression }})
{%- endif %}
select * from meet_condition
where not(_dbt_utils_test_expression_passed)

{% endmacro %}

0 comments on commit 55058e8

Please sign in to comment.