Skip to content
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

Feature/fewer rows than #343

Merged
merged 4 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Make `expression_is_true` work as a column test (code originally in [#226](https://github.com/fishtown-analytics/dbt-utils/pull/226/) from [@elliottohara](https://github.com/elliottohara), merged via [#313])
* Allow individual columns in star macro to be aliased (code originally in [#230](https://github.com/fishtown-analytics/dbt-utils/pull/230/) from [@elliottohara](https://github.com/elliottohara), merged via [#245])
* Add new schema test, `not_accepted_values` ([#284](https://github.com/fishtown-analytics/dbt-utils/pull/284) [@JavierMonton](https://github.com/JavierMonton))
* Add new schema test, `fewer_rows_than` (code originally in [#221](https://github.com/fishtown-analytics/dbt-utils/pull/230/) from [@dmarts](https://github.com/dmarts), merged via [#343])


## Fixes
Expand Down
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Usage:
---
### Date/Time
#### date_spine ([source](macros/datetime/date_spine.sql))
This macro returns the sql required to build a date spine. The spine will include the `start_date` (if it is aligned to the `datepart`), but it will not include the `end_date`.
This macro returns the sql required to build a date spine. The spine will include the `start_date` (if it is aligned to the `datepart`), but it will not include the `end_date`.

Usage:
```
Expand Down Expand Up @@ -132,6 +132,20 @@ models:

```

#### fewer_rows_than ([source](macros/schema_tests/fewer_rows_than.sql))
This schema test asserts that this model has fewer rows than the referenced model.

Usage:
```yaml
version: 2

models:
- name: model_name
tests:
- dbt_utils.fewer_rows_than:
compare_model: ref('other_table_name')
```

#### equality ([source](macros/schema_tests/equality.sql))
This schema test asserts the equality of two relations. Optionally specify a subset of columns to compare.

Expand Down Expand Up @@ -181,13 +195,13 @@ models:

```

This macro can also be used at the column level. When this is done, the `expression` is evaluated against the column.
This macro can also be used at the column level. When this is done, the `expression` is evaluated against the column.

```yaml
version: 2
models:
models:
- name: model_name
columns:
columns:
- name: col_a
tests:
- dbt_utils.expression_is_true:
Expand All @@ -197,7 +211,7 @@ models:
- dbt_utils.expression_is_true:
expression: '= 1'
condition: col_a = 1

```


Expand Down Expand Up @@ -361,7 +375,7 @@ models:
upper_bound_column: ended_at
partition_by: customer_id
gaps: required

# test that each customer can have subscriptions that start and end on the same date
- name: subscriptions
tests:
Expand Down Expand Up @@ -497,9 +511,9 @@ constraint needs to be verified.


#### accepted_range ([source](macros/schema_tests/accepted_range.sql))
This test checks that a column's values fall inside an expected range. Any combination of `min_value` and `max_value` is allowed, and the range can be inclusive or exclusive. Provide a `where` argument to filter to specific records only.
This test checks that a column's values fall inside an expected range. Any combination of `min_value` and `max_value` is allowed, and the range can be inclusive or exclusive. Provide a `where` argument to filter to specific records only.

In addition to comparisons to a scalar value, you can also compare to another column's values. Any data type that supports the `>` or `<` operators can be compared, so you could also run tests like checking that all order dates are in the past.
In addition to comparisons to a scalar value, you can also compare to another column's values. Any data type that supports the `>` or `<` operators can be compared, so you could also run tests like checking that all order dates are in the past.

Usage:
```yaml
Expand All @@ -513,19 +527,19 @@ models:
- dbt_utils.accepted_range:
min_value: 0
inclusive: false

- name: account_created_at
tests:
- dbt_utils.accepted_range:
max_value: "getdate()"
#inclusive is true by default

- name: num_returned_orders
tests:
- dbt_utils.accepted_range:
min_value: 0
max_value: "num_orders"

- name: num_web_sessions
tests:
- dbt_utils.accepted_range:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
field
1
2
3
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
field
1
2
3
4
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with data as (

select * from {{ ref('data_test_fewer_rows_than_table_1') }}

)

select
field
from data
43 changes: 43 additions & 0 deletions macros/schema_tests/fewer_rows_than.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% macro test_fewer_rows_than(model) %}
{{ return(adapter.dispatch('test_fewer_rows_than', packages = dbt_utils._get_utils_namespaces())(model, combination_of_columns, quote_columns, where)) }}
{% endmacro %}

{% macro default__test_fewer_rows_than(model) %}

{% set compare_model = kwargs.get('compare_model', kwargs.get('arg')) %}

with a as (

select count(*) as count_ourmodel from {{ model }}

),
b as (

select count(*) as count_comparisonmodel from {{ compare_model }}

),
counts as (

select
(select count_ourmodel from a) as count_model_with_fewer_rows,
(select count_comparisonmodel from b) as count_model_with_more_rows

),
final as (

select
case
-- fail the test if we have more rows than the reference model and return the row count delta
when count_model_with_fewer_rows > count_model_with_more_rows then (count_model_with_fewer_rows - count_model_with_more_rows)
-- fail the test if they are the same number
when count_model = count_comparison then 1
-- pass the test if the delta is positive (i.e. return the number 0)
else 0
end as row_count_delta
from counts

)

select row_count_delta from final

{% endmacro %}