-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
update of macro for postgres/redshift use of unique_key as a list #4858
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
236d69f
pre-commit additions
McKnight-42 6d42c50
Merge branch 'main' of github.com:dbt-labs/dbt into mcknight/unique_k…
McKnight-42 b6bdedb
added changie changelog entry
McKnight-42 6f194b4
moving integration test over
McKnight-42 5aafc44
Pair programming
jtcohen6 ba986b6
Merge branch 'main' of github.com:dbt-labs/dbt into mcknight/unique_k…
McKnight-42 1ae3382
Merge branch 'mcknight/unique_key_as_lists' of github.com:dbt-labs/db…
McKnight-42 5cfa2cd
removing ref to mapping as seems to be unnecessary check, unique_key …
McKnight-42 c46327e
Merge branch 'main' of github.com:dbt-labs/dbt into mcknight/unique_k…
McKnight-42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
kind: Features | ||
body: Allow unique key to take a list implementation for postgres/redshift | ||
time: 2022-03-14T11:23:41.293726-05:00 | ||
custom: | ||
Author: McKnight-42 | ||
Issue: "4738" | ||
PR: "4858" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
test/integration/076_incremental_unique_id_test/models/duplicated_unary_unique_key_list.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
16 changes: 16 additions & 0 deletions
16
test/integration/076_incremental_unique_id_test/models/empty_str_unique_key.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
14 changes: 14 additions & 0 deletions
14
test/integration/076_incremental_unique_id_test/models/empty_unique_key_list.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
21 changes: 21 additions & 0 deletions
21
test/integration/076_incremental_unique_id_test/models/expected/one_str__overwrite.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'::varchar(2),'Suffolk'::varchar(12),'Boston'::varchar(12),'2020-02-12'::date | ||
McKnight-42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
union all | ||
select 'NJ'::varchar(2),'Mercer'::varchar(12),'Trenton'::varchar(12),'2022-01-01'::date | ||
union all | ||
select 'NY'::varchar(2),'Kings'::varchar(12),'Brooklyn'::varchar(12),'2021-04-02'::date | ||
union all | ||
select 'NY'::varchar(2),'New York'::varchar(12),'Manhattan'::varchar(12),'2021-04-01'::date | ||
union all | ||
select 'PA'::varchar(2),'Philadelphia'::varchar(12),'Philadelphia'::varchar(12),'2021-05-21'::date |
21 changes: 21 additions & 0 deletions
21
...ion/076_incremental_unique_id_test/models/expected/unique_key_list__inplace_overwrite.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'::varchar(2),'Suffolk'::varchar(12),'Boston'::varchar(12),'2020-02-12'::date | ||
union all | ||
select 'NJ'::varchar(2),'Mercer'::varchar(12),'Trenton'::varchar(12),'2022-01-01'::date | ||
union all | ||
select 'NY'::varchar(2),'Kings'::varchar(12),'Brooklyn'::varchar(12),'2021-04-02'::date | ||
union all | ||
select 'NY'::varchar(2),'New York'::varchar(12),'Manhattan'::varchar(12),'2021-04-01'::date | ||
union all | ||
select 'PA'::varchar(2),'Philadelphia'::varchar(12),'Philadelphia'::varchar(12),'2021-05-21'::date |
15 changes: 15 additions & 0 deletions
15
test/integration/076_incremental_unique_id_test/models/no_unique_key.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
21 changes: 21 additions & 0 deletions
21
test/integration/076_incremental_unique_id_test/models/nontyped_trinary_unique_key_list.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
16 changes: 16 additions & 0 deletions
16
test/integration/076_incremental_unique_id_test/models/not_found_unique_key.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
10 changes: 10 additions & 0 deletions
10
test/integration/076_incremental_unique_id_test/models/not_found_unique_key_list.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') }} |
21 changes: 21 additions & 0 deletions
21
test/integration/076_incremental_unique_id_test/models/str_unique_key.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
20 changes: 20 additions & 0 deletions
20
test/integration/076_incremental_unique_id_test/models/trinary_unique_key_list.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
20 changes: 20 additions & 0 deletions
20
test/integration/076_incremental_unique_id_test/models/unary_unique_key_list.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
12 changes: 12 additions & 0 deletions
12
test/integration/076_incremental_unique_id_test/seeds/add_new_rows.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
9 changes: 9 additions & 0 deletions
9
test/integration/076_incremental_unique_id_test/seeds/duplicate_insert.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
7
test/integration/076_incremental_unique_id_test/seeds/seed.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was worried that this may be a breaking change for any users who have defined a unique key named
"False"
. Putting aside how confusing that would be — we tested it out, and this logic should still work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also an empty string