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

Test incremental model with unique key both as string and list #96

Merged
merged 10 commits into from
Feb 22, 2022
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
## dbt-snowflake 1.0.0 (Release TBD)
## dbt-snowflake 1.1.0 (TBD)

### Features
- Adds tests for incremental model unique key parameter ([#91](https://github.com/dbt-labs/dbt-snowflake/issues/91))

### Fixes
- Add unique\_id field to docs generation test catalogs; a follow-on PR to core PR ([#4168](https://github.com/dbt-labs/dbt-core/pull/4618))

## dbt-snowflake 1.0.0 (December 3rd, 2021)

## dbt-snowflake 1.0.0rc2 (November 24, 2021)

### Fixes
- Apply query tags for Seed and Snapshot materialisations ([#20](https://github.com/dbt-labs/dbt-snowflake/issues/20), [#48](https://github.com/dbt-labs/dbt-snowflake/issues/48))
- Adds column-level comments to Snowflake views ([#17](https://github.com/dbt-labs/dbt-snowflake/issues/17))
- Add unique\_id field to docs generation test catalogs; a follow-on PR to core PR ([#4168](https://github.com/dbt-labs/dbt-core/pull/4618))

### Under the hood
- Resolves an issue caused when the Snowflake OCSP server is not accessible, by exposing the `insecure_mode` boolean avalable in the Snowflake python connector ([#31](https://github.com/dbt-labs/dbt-snowflake/issues/31), [#49](https://github.com/dbt-labs/dbt-snowflake/pull/49))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def rendered_tst_config(self, **updates):
'database': None,
'schema': 'dbt_test__audit',
'alias': None,
'meta': {},
'meta': {}
}
result.update(updates)
return result
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{
config(
materialized='incremental',
unique_key=['state', 'state']
)
}}

select
state::varchar(2) as state,
county::varchar(12) as county,
city::varchar(12) as city,
last_visit_date::date as last_visit_date
from {{ ref('seed') }}

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- ensure model with empty string unique key should build normally

{{
config(
materialized='incremental',
unique_key=''
)
}}

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

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- model with empty list unique key should build normally

{{
config(
materialized='incremental',
unique_key=[]
)
}}

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

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{
config(
materialized='table'
)
}}

select
'CT'::varchar(2) as state,
'Hartford'::varchar(12) as county,
'Hartford'::varchar(12) as city,
'2022-02-14'::date as last_visit_date
union all
select 'MA','Suffolk','Boston','2020-02-12'
union all
select 'NJ','Mercer','Trenton','2022-01-01'
union all
select 'NY','Kings','Brooklyn','2021-04-02'
union all
select 'NY','New York','Manhattan','2021-04-01'
union all
select 'PA','Philadelphia','Philadelphia','2021-05-21'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{
config(
materialized='table'
)
}}

select
'CT'::varchar(2) as state,
'Hartford'::varchar(12) as county,
'Hartford'::varchar(12) as city,
'2022-02-14'::date as last_visit_date
union all
select 'MA','Suffolk','Boston','2020-02-12'
union all
select 'NJ','Mercer','Trenton','2022-01-01'
union all
select 'NY','Kings','Brooklyn','2021-04-02'
union all
select 'NY','New York','Manhattan','2021-04-01'
union all
select 'PA','Philadelphia','Philadelphia','2021-05-21'
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- no specified unique key should cause no special build behavior

{{
config(
materialized='incremental'
)
}}

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

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- a multi-argument unique key list should see overwriting on rows in the model
-- where all unique key fields apply
-- N.B. needed for direct comparison with seed

{{
config(
materialized='incremental',
unique_key=['state', 'county', 'city']
)
}}

select
state as state,
county as county,
city as city,
last_visit_date as last_visit_date
from {{ ref('seed') }}

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- a model with a unique key not found in the table itself will error out

{{
config(
materialized='incremental',
unique_key='thisisnotacolumn'
)
}}

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

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- a unique key list with any element not in the model itself should error out

{{
config(
materialized='incremental',
unique_key=['state', 'thisisnotacolumn']
)
}}

select * from {{ ref('seed') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- a unique key with a string should trigger to overwrite behavior when
-- the source has entries in conflict (i.e. more than one row per unique key
-- combination)

{{
config(
materialized='incremental',
unique_key='state'
)
}}

select
state::varchar(2) as state,
county::varchar(12) as county,
city::varchar(12) as city,
last_visit_date::date as last_visit_date
from {{ ref('seed') }}

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- a multi-argument unique key list should see overwriting on rows in the model
-- where all unique key fields apply

{{
config(
materialized='incremental',
unique_key=['state', 'county', 'city']
)
}}

select
state::varchar(2) as state,
county::varchar(12) as county,
city::varchar(12) as city,
last_visit_date::date as last_visit_date
from {{ ref('seed') }}

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- a one argument unique key list should result in overwritting semantics for
-- that one matching field

{{
config(
materialized='incremental',
unique_key=['state']
)
}}

select
state::varchar(2) as state,
county::varchar(12) as county,
city::varchar(12) as city,
last_visit_date::date as last_visit_date
from {{ ref('seed') }}

{% if is_incremental() %}
where last_visit_date > (select max(last_visit_date) from {{ this }})
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Insert statement which when applied to seed.csv sees incremental model
-- grow in size while not (necessarily) diverging from the seed itself.

-- insert two new rows, both of which should be in incremental model
-- with any unique columns
insert into {schema}.seed
(state, county, city, last_visit_date)
values ('WA','King','Seattle','2022-02-01');

insert into {schema}.seed
(state, county, city, last_visit_date)
values ('CA','Los Angeles','Los Angeles','2022-02-01');
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Insert statement which when applied to seed.csv triggers the inplace
-- overwrite strategy of incremental models. Seed and incremental model
-- diverge.

-- insert new row, which should not be in incremental model
-- with primary or first three columns unique
insert into {schema}.seed
(state, county, city, last_visit_date)
values ('CT','Hartford','Hartford','2022-02-14');
7 changes: 7 additions & 0 deletions tests/integration/incremental_unique_id_test/seeds/seed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
state,county,city,last_visit_date
CT,Hartford,Hartford,2020-09-23
MA,Suffolk,Boston,2020-02-12
NJ,Mercer,Trenton,2022-01-01
NY,Kings,Brooklyn,2021-04-02
NY,New York,Manhattan,2021-04-01
PA,Philadelphia,Philadelphia,2021-05-21
Loading