-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix/snowflake view transactions #940
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
afe1489
Fix snowflake view swaps... and a lot of other things too
drewbanin dad9970
account for view --> table switch
drewbanin b33dbf0
fix tests
drewbanin 457df4d
handle table --> view switch on snowflake
drewbanin 7961300
pr feedback
drewbanin dbb32e9
Merge branch 'development' into fix/snowflake-view-transactions
drewbanin 3601f1c
Merge branch 'development' into fix/snowflake-view-transactions
drewbanin 78fd05a
Merge branch 'development' into fix/snowflake-view-transactions
drewbanin 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
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
93 changes: 93 additions & 0 deletions
93
dbt/include/global_project/macros/materializations/table/snowflake_table.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,93 @@ | ||
{% materialization table, adapter='snowflake' %} | ||
{%- set identifier = model['alias'] -%} | ||
{%- set tmp_identifier = identifier + '__dbt_tmp' -%} | ||
{%- set backup_identifier = identifier + '__dbt_backup' -%} | ||
{%- set non_destructive_mode = (flags.NON_DESTRUCTIVE == True) -%} | ||
|
||
{%- set existing_relations = adapter.list_relations(schema=schema) -%} | ||
{%- set old_relation = adapter.get_relation(relations_list=existing_relations, | ||
schema=schema, identifier=identifier) -%} | ||
{%- set target_relation = api.Relation.create(identifier=identifier, | ||
schema=schema, type='table') -%} | ||
{%- set intermediate_relation = api.Relation.create(identifier=tmp_identifier, | ||
schema=schema, type='table') -%} | ||
|
||
/* | ||
See ../view/view.sql for more information about this relation. | ||
*/ | ||
{%- set backup_relation = api.Relation.create(identifier=backup_identifier, | ||
schema=schema, type=(old_relation.type or 'table')) -%} | ||
|
||
{%- set exists_as_table = (old_relation is not none and old_relation.is_table) -%} | ||
{%- set exists_as_view = (old_relation is not none and old_relation.is_view) -%} | ||
{%- set create_as_temporary = (exists_as_table and non_destructive_mode) -%} | ||
|
||
|
||
-- drop the temp relations if they exists for some reason | ||
{{ adapter.drop_relation(intermediate_relation) }} | ||
{{ adapter.drop_relation(backup_relation) }} | ||
|
||
-- setup: if the target relation already exists, truncate or drop it (if it's a view) | ||
{% if non_destructive_mode -%} | ||
{% if exists_as_table -%} | ||
{{ adapter.truncate_relation(old_relation) }} | ||
{% elif exists_as_view -%} | ||
{{ adapter.drop_relation(old_relation) }} | ||
{%- set old_relation = none -%} | ||
{%- endif %} | ||
{%- endif %} | ||
|
||
{{ run_hooks(pre_hooks, inside_transaction=False) }} | ||
|
||
-- `BEGIN` happens here: | ||
{{ run_hooks(pre_hooks, inside_transaction=True) }} | ||
|
||
-- build model | ||
{% call statement('main') -%} | ||
{%- if non_destructive_mode -%} | ||
{%- if old_relation is not none -%} | ||
{{ create_table_as(create_as_temporary, intermediate_relation, sql) }} | ||
|
||
{% set dest_columns = adapter.get_columns_in_table(schema, identifier) %} | ||
{% set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') %} | ||
|
||
insert into {{ target_relation }} ({{ dest_cols_csv }}) ( | ||
select {{ dest_cols_csv }} | ||
from {{ intermediate_relation.include(schema=(not create_as_temporary)) }} | ||
); | ||
{%- else -%} | ||
{{ create_table_as(create_as_temporary, target_relation, sql) }} | ||
{%- endif -%} | ||
{%- else -%} | ||
{{ create_table_as(create_as_temporary, intermediate_relation, sql) }} | ||
{%- endif -%} | ||
{%- endcall %} | ||
|
||
-- cleanup | ||
{% if non_destructive_mode -%} | ||
-- noop | ||
{%- else -%} | ||
{% if old_relation is not none %} | ||
{% if old_relation.type == 'view' %} | ||
{#-- This is the primary difference between Snowflake and Redshift. Renaming this view | ||
-- would cause an error if the view has become invalid due to upstream schema changes #} | ||
{{ log("Dropping relation " ~ old_relation ~ " because it is a view and this model is a table.") }} | ||
{{ drop_relation_if_exists(old_relation) }} | ||
{% else %} | ||
{{ adapter.rename_relation(target_relation, backup_relation) }} | ||
{% endif %} | ||
{% endif %} | ||
|
||
{{ adapter.rename_relation(intermediate_relation, target_relation) }} | ||
{%- endif %} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=True) }} | ||
|
||
-- `COMMIT` happens here | ||
{{ adapter.commit() }} | ||
|
||
-- finally, drop the existing/backup relation after the commit | ||
{{ drop_relation_if_exists(backup_relation) }} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=False) }} | ||
{% endmaterialization %} |
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
41 changes: 0 additions & 41 deletions
41
dbt/include/global_project/macros/materializations/view/bigquery_view.sql
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
dbt/include/global_project/macros/materializations/view/bq_snowflake_view.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,98 @@ | ||
|
||
{% macro handle_existing_table(full_refresh, non_destructive_mode, old_relation) %} | ||
{{ adapter_macro("dbt.handle_existing_table", full_refresh, non_destructive_mode, old_relation) }} | ||
{% endmacro %} | ||
|
||
{% macro default__handle_existing_table(full_refresh, non_destructive_mode, old_relation) %} | ||
{%- if not non_destructive_mode -%} | ||
{{ adapter.drop_relation(old_relation) }} | ||
{%- endif -%} | ||
{% endmacro %} | ||
|
||
{% macro bigquery__handle_existing_table(full_refresh, non_destructive_mode, old_relation) %} | ||
{%- if full_refresh and not non_destructive_mode -%} | ||
{{ adapter.drop_relation(old_relation) }} | ||
{%- else -%} | ||
{{ exceptions.relation_wrong_type(old_relation, 'view') }} | ||
{%- endif -%} | ||
{% endmacro %} | ||
|
||
|
||
{# /* | ||
Core materialization implementation. BigQuery and Snowflake are similar | ||
because both can use `create or replace view` where the resulting view schema | ||
is not necessarily the same as the existing view. On Redshift, this would | ||
result in: ERROR: cannot change number of columns in view | ||
|
||
This implementation is superior to the create_temp, swap_with_existing, drop_old | ||
paradigm because transactions don't run DDL queries atomically on Snowflake. By using | ||
`create or replace view`, the materialization becomes atomic in nature. | ||
*/ | ||
#} | ||
|
||
{% macro impl_view_materialization(run_outside_transaction_hooks=True) %} | ||
{%- set identifier = model['alias'] -%} | ||
{%- set non_destructive_mode = (flags.NON_DESTRUCTIVE == True) -%} | ||
|
||
{%- set old_relation = adapter.get_relation( | ||
schema=schema, identifier=identifier) -%} | ||
|
||
{%- set exists_as_view = (old_relation is not none and old_relation.is_view) -%} | ||
|
||
{%- set target_relation = api.Relation.create( | ||
identifier=identifier, schema=schema, | ||
type='view') -%} | ||
|
||
{%- set should_ignore = non_destructive_mode and exists_as_view %} | ||
{%- set has_transactional_hooks = (hooks | selectattr('transaction', 'equalto', True) | list | length) > 0 %} | ||
|
||
{% if run_outside_transaction_hooks %} | ||
-- no transactions on BigQuery | ||
{{ run_hooks(pre_hooks, inside_transaction=False) }} | ||
{% endif %} | ||
|
||
-- `BEGIN` happens here on Snowflake | ||
{{ run_hooks(pre_hooks, inside_transaction=True) }} | ||
|
||
-- If there's a table with the same name and we weren't told to full refresh, | ||
-- that's an error. If we were told to full refresh, drop it. This behavior differs | ||
-- for Snowflake and BigQuery, so multiple dispatch is used. | ||
{%- if old_relation is not none and old_relation.is_table -%} | ||
{{ handle_existing_table(flags.FULL_REFRESH, non_destructive_mode, old_relation) }} | ||
{%- endif -%} | ||
|
||
-- build model | ||
{% if non_destructive_mode -%} | ||
{% call noop_statement('main', status="PASS", res=None) -%} | ||
-- Not running : non-destructive mode | ||
{{ sql }} | ||
{%- endcall %} | ||
{%- else -%} | ||
{% call statement('main') -%} | ||
{{ create_view_as(target_relation, sql) }} | ||
{%- endcall %} | ||
{%- endif %} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=True) }} | ||
|
||
{# | ||
-- Don't commit in non-destructive mode _unless_ there are in-transaction hooks | ||
-- TODO : Figure out some other way of doing this that isn't as fragile | ||
#} | ||
{% if has_transactional_hooks or not should_ignore %} | ||
{{ adapter.commit() }} | ||
{% endif %} | ||
|
||
{% if run_outside_transaction_hooks %} | ||
-- No transactions on BigQuery | ||
{{ run_hooks(post_hooks, inside_transaction=False) }} | ||
{% endif %} | ||
{% endmacro %} | ||
|
||
{% materialization view, adapter='bigquery' -%} | ||
{{ impl_view_materialization(run_outside_transaction_hooks=False) }} | ||
{%- endmaterialization %} | ||
|
||
{% materialization view, adapter='snowflake' -%} | ||
{{ impl_view_materialization() }} | ||
{%- endmaterialization %} |
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
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
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
4 changes: 4 additions & 0 deletions
4
test/integration/036_snowflake_view_dependency_test/data/people.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,4 @@ | ||
id,name | ||
1,Drew | ||
2,Jake | ||
3,Connor |
Empty file.
9 changes: 9 additions & 0 deletions
9
test/integration/036_snowflake_view_dependency_test/models/base_table.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 @@ | ||
|
||
{{ config(materialized='table') }} | ||
|
||
select * | ||
{% if var('add_table_field', False) %} | ||
, 1 as new_field | ||
{% endif %} | ||
|
||
from {{ ref('people') }} |
8 changes: 8 additions & 0 deletions
8
test/integration/036_snowflake_view_dependency_test/models/dependent_model.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,8 @@ | ||
|
||
{% if var('dependent_type', 'view') == 'view' %} | ||
{{ config(materialized='view') }} | ||
{% else %} | ||
{{ config(materialized='table') }} | ||
{% endif %} | ||
|
||
select * from {{ ref('base_table') }} |
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'm thinking about this quite a bit, and I am not sure about the split here. Should snowflake really behave this fundamentally differently from bigquery? Never raise and ignore the full refresh flag?
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.
The decision to raise this error on BigQuery was, I think, a shortsighted one. The error we throw here is very specifically intended to avoid clobbering an ingestion-time partitioned table with a view by accident. This could happen if you switch the type of your materialization from
table
toview
. We don't do this check anywhere else in dbt, and ingestion-time partitioned tables are mostly superseded by column partitioning, so I don't know that too many people are actually being helped out by this error in practice.I'm in favor of dropping the exception if @cmcarthur is into it as well. I agree that it's a thorny maintenance burden and has little utility.
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.
If you remove the exception you also get to remove
handle_existing_table
entirely, and removing code is always a win.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.
@drewbanin I defer totally to you on it. IMO it's totally fine to handle these kinds of errors situationally based on the adapter. We added this error for a really good reason, namely building time partitioned tables was costly, and dropping it unintentionally would cause users a lot of pain. If you believe that this isn't going to cause pain then I'm on board with removing it