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

remove CTEs for schema tests #536

Merged
merged 3 commits into from
Sep 21, 2017
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## dbt 0.9.0 (unreleased)

### Improvements

- Replace schema test CTEs with subqueries to speed them up for Postgres ([#536](https://github.com/fishtown-analytics/dbt/pull/536))

### New Contributors

- @ronnyli
- https://github.com/fishtown-analytics/dbt/pull/536

## dbt 0.9.0 Alpha 2 (September 20, 2017)

### Overview
Expand Down
13 changes: 2 additions & 11 deletions dbt/include/global_project/macros/schema_tests/not_null.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@

{% macro test_not_null(model, arg) %}

with validation as (

select
{{ arg }} as not_null_field

from {{ model }}

)

select count(*)
from validation
where not_null_field is null
from {{ model }}
where {{ arg }} is null

{% endmacro %}

21 changes: 6 additions & 15 deletions dbt/include/global_project/macros/schema_tests/relationships.sql
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@

{% macro test_relationships(model, field, to, from) %}

with parent as (

select
{{ field }} as id

from {{ to }}

),

child as (
select count(*)
from (

select
{{ from }} as id

from {{ model }}
where {{ from }} is not null
and {{ from }} not in (select {{ field }}
from {{ to }})

)

select count(*)
from child
where id is not null
and id not in (select id from parent)
) validation_errors

{% endmacro %}
21 changes: 5 additions & 16 deletions dbt/include/global_project/macros/schema_tests/unique.sql
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@

{% macro test_unique(model, arg) %}

with validation as (
select count(*)
from (

select
{{ arg }} as unique_field
{{ arg }}

from {{ model }}
where {{ arg }} is not null

),

validation_errors as (

select
unique_field

from validation
group by unique_field
group by {{ arg }}
having count(*) > 1

)

select count(*)
from validation_errors
) validation_errors

{% endmacro %}