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

Support quoted column names in sequential_values test #479

Merged
merged 5 commits into from
Feb 18, 2022
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
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For compatibility details between versions of dbt-core and dbt-utils, [see this

**[Schema tests](#schema-tests)**
- [equal_rowcount](#equal_rowcount-source)
- [fewer_rows_than](#fewer_rows_than-source)
- [equality](#equality-source)
- [expression_is_true](#expression_is_true-source)
- [recency](#recency-source)
Expand Down Expand Up @@ -228,6 +229,8 @@ models:
#### unique_where ([source](macros/schema_tests/test_unique_where.sql))
This test validates that there are no duplicate values present in a field for a subset of rows by specifying a `where` clause.

*Warning*: This test is no longer supported. Starting in dbt v0.20.0, the built-in `unique` test supports a `where` config. [See the dbt docs for more details](https://docs.getdbt.com/reference/resource-configs/where).

**Usage:**
```yaml
version: 2
Expand All @@ -244,6 +247,8 @@ models:
#### not_null_where ([source](macros/schema_tests/test_not_null_where.sql))
This test validates that there are no null values present in a column for a subset of rows by specifying a `where` clause.

*Warning*: This test is no longer supported. Starting in dbt v0.20.0, the built-in `not_null` test supports a `where` config. [See the dbt docs for more details](https://docs.getdbt.com/reference/resource-configs/where).

**Usage:**
```yaml
version: 2
Expand Down Expand Up @@ -657,17 +662,25 @@ This macro returns a dictionary from a sql query, so that you don't need to inte

**Usage:**
```
-- Returns a dictionary of the users table where the state is California
{% set california_cities = dbt_utils.get_query_results_as_dict("select * from" ~ ref('cities') ~ "where state = 'CA' and city is not null ") %}
{% set sql_statement %}
select city, state from {{ ref('users) }}
{% endset %}

{%- set places = dbt_utils.get_query_results_as_dict(sql_statement) -%}

select
city,
{% for city in california_cities %}
sum(case when city = {{ city }} then 1 else 0 end) as users_in_{{ city }},
{% endfor %}
count(*) as total
from {{ ref('users') }}

group by 1
{% for city in places['CITY'] | unique -%}
sum(case when city = '{{ city }}' then 1 else 0 end) as users_in_{{ dbt_utils.slugify(city) }},
{% endfor %}

{% for state in places['STATE'] | unique -%}
sum(case when state = '{{ state }}' then 1 else 0 end) as users_in_{{ state }},
{% endfor %}

count(*) as total_total

from {{ ref('users') }}
```

### SQL generators
Expand Down Expand Up @@ -1042,7 +1055,7 @@ select
order_id,
{%- for payment_method in payment_methods %}
sum(case when payment_method = '{{ payment_method }}' then amount end)
as {{ slugify(payment_method) }}_amount,
as {{ dbt_utils.slugify(payment_method) }}_amount,

{% endfor %}
...
Expand Down
8 changes: 5 additions & 3 deletions macros/schema_tests/sequential_values.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

{% macro default__test_sequential_values(model, column_name, interval=1, datepart=None) %}

{% set previous_column_name = "previous_" ~ dbt_utils.slugify(column_name) %}

with windowed as (

select
{{ column_name }},
lag({{ column_name }}) over (
order by {{ column_name }}
) as previous_{{ column_name }}
) as {{ previous_column_name }}
from {{ model }}
),

Expand All @@ -21,9 +23,9 @@ validation_errors as (
*
from windowed
{% if datepart %}
where not(cast({{ column_name }} as {{ dbt_utils.type_timestamp() }})= cast({{ dbt_utils.dateadd(datepart, interval, 'previous_' + column_name) }} as {{ dbt_utils.type_timestamp() }}))
where not(cast({{ column_name }} as {{ dbt_utils.type_timestamp() }})= cast({{ dbt_utils.dateadd(datepart, interval, previous_column_name) }} as {{ dbt_utils.type_timestamp() }}))
{% else %}
where not({{ column_name }} = previous_{{ column_name }} + {{ interval }})
where not({{ column_name }} = {{ previous_column_name }} + {{ interval }})
{% endif %}
)

Expand Down