-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Matthew McKnight <matthew.mcknight@dbtlabs.com>
- Loading branch information
1 parent
1af94de
commit 5c7aa7f
Showing
20 changed files
with
726 additions
and
83 deletions.
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,6 @@ | ||
kind: Features | ||
body: dbt clone | ||
time: 2023-06-16T10:48:49.079961-05:00 | ||
custom: | ||
Author: jtcohen6 aranke McKnight-42 | ||
Issue: "7258" |
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
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
7 changes: 7 additions & 0 deletions
7
core/dbt/include/global_project/macros/materializations/models/clone/can_clone_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,7 @@ | ||
{% macro can_clone_table() %} | ||
{{ return(adapter.dispatch('can_clone_table', 'dbt')()) }} | ||
{% endmacro %} | ||
|
||
{% macro default__can_clone_table() %} | ||
{{ return(False) }} | ||
{% endmacro %} |
62 changes: 62 additions & 0 deletions
62
core/dbt/include/global_project/macros/materializations/models/clone/clone.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,62 @@ | ||
{%- materialization clone, default -%} | ||
|
||
{%- set relations = {'relations': []} -%} | ||
|
||
{%- if not defer_relation -%} | ||
-- nothing to do | ||
{{ log("No relation found in state manifest for " ~ model.unique_id, info=True) }} | ||
{{ return(relations) }} | ||
{%- endif -%} | ||
|
||
{%- set existing_relation = load_cached_relation(this) -%} | ||
|
||
{%- if existing_relation and not flags.FULL_REFRESH -%} | ||
-- noop! | ||
{{ log("Relation " ~ existing_relation ~ " already exists", info=True) }} | ||
{{ return(relations) }} | ||
{%- endif -%} | ||
|
||
{%- set other_existing_relation = load_cached_relation(defer_relation) -%} | ||
|
||
-- If this is a database that can do zero-copy cloning of tables, and the other relation is a table, then this will be a table | ||
-- Otherwise, this will be a view | ||
|
||
{% set can_clone_table = can_clone_table() %} | ||
|
||
{%- if other_existing_relation and other_existing_relation.type == 'table' and can_clone_table -%} | ||
|
||
{%- set target_relation = this.incorporate(type='table') -%} | ||
{% if existing_relation is not none and not existing_relation.is_table %} | ||
{{ log("Dropping relation " ~ existing_relation ~ " because it is of type " ~ existing_relation.type) }} | ||
{{ drop_relation_if_exists(existing_relation) }} | ||
{% endif %} | ||
|
||
-- as a general rule, data platforms that can clone tables can also do atomic 'create or replace' | ||
{% call statement('main') %} | ||
{{ create_or_replace_clone(target_relation, defer_relation) }} | ||
{% endcall %} | ||
|
||
{% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %} | ||
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %} | ||
{% do persist_docs(target_relation, model) %} | ||
|
||
{{ return({'relations': [target_relation]}) }} | ||
|
||
{%- else -%} | ||
|
||
{%- set target_relation = this.incorporate(type='view') -%} | ||
|
||
-- reuse the view materialization | ||
-- TODO: support actual dispatch for materialization macros | ||
-- Tracking ticket: https://github.com/dbt-labs/dbt-core/issues/7799 | ||
{% set search_name = "materialization_view_" ~ adapter.type() %} | ||
{% if not search_name in context %} | ||
{% set search_name = "materialization_view_default" %} | ||
{% endif %} | ||
{% set materialization_macro = context[search_name] %} | ||
{% set relations = materialization_macro() %} | ||
{{ return(relations) }} | ||
|
||
{%- endif -%} | ||
|
||
{%- endmaterialization -%} |
7 changes: 7 additions & 0 deletions
7
...t/include/global_project/macros/materializations/models/clone/create_or_replace_clone.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,7 @@ | ||
{% macro create_or_replace_clone(this_relation, defer_relation) %} | ||
{{ return(adapter.dispatch('create_or_replace_clone', 'dbt')(this_relation, defer_relation)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__create_or_replace_clone(this_relation, defer_relation) %} | ||
create or replace table {{ this_relation }} clone {{ defer_relation }} | ||
{% endmacro %} |
Oops, something went wrong.