forked from dbt-labs/dbt-snowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADAP-821: Refactor prep for dynamic table / table/view swap (dbt-labs…
…#751) * restructure macros directory, create separate files and directories for each ddl statement and relation type
- Loading branch information
1 parent
4ea39ca
commit 2b1809b
Showing
14 changed files
with
184 additions
and
191 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,7 @@ | ||
kind: Under the Hood | ||
body: Restructure macros files - move relation ddl statements into separate files | ||
and directories | ||
time: 2023-08-21T23:09:21.317975-04:00 | ||
custom: | ||
Author: mikealfare | ||
Issue: "750" |
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
89 changes: 0 additions & 89 deletions
89
dbt/include/snowflake/macros/materializations/dynamic_table/ddl.sql
This file was deleted.
Oops, something went wrong.
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 @@ | ||
{% macro snowflake__drop_relation(relation) -%} | ||
{%- if relation.is_dynamic_table -%} | ||
{% call statement('drop_relation', auto_begin=False) -%} | ||
drop dynamic table if exists {{ relation }} | ||
{%- endcall %} | ||
{%- else -%} | ||
{{- default__drop_relation(relation) -}} | ||
{%- endif -%} | ||
{% endmacro %} |
5 changes: 5 additions & 0 deletions
5
dbt/include/snowflake/macros/relations/dynamic_table/_replace.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,5 @@ | ||
{% macro snowflake__get_replace_dynamic_table_as_sql(target_relation, sql, existing_relation, backup_relation, intermediate_relation) -%} | ||
{{- log('Applying REPLACE to: ' ~ target_relation) -}} | ||
{{ snowflake__get_drop_dynamic_table_sql(existing_relation) }}; | ||
{{ snowflake__get_create_dynamic_table_as_sql(target_relation, sql) }} | ||
{%- endmacro %} |
27 changes: 27 additions & 0 deletions
27
dbt/include/snowflake/macros/relations/dynamic_table/alter.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,27 @@ | ||
{% macro snowflake__get_alter_dynamic_table_as_sql( | ||
target_relation, | ||
configuration_changes, | ||
sql, | ||
existing_relation, | ||
backup_relation, | ||
intermediate_relation | ||
) -%} | ||
{{- log('Applying ALTER to: ' ~ target_relation) -}} | ||
|
||
{% if configuration_changes.requires_full_refresh %} | ||
{{- snowflake__get_replace_dynamic_table_as_sql(target_relation, sql, existing_relation, backup_relation, intermediate_relation) -}} | ||
|
||
{% else %} | ||
|
||
{%- set target_lag = configuration_changes.target_lag -%} | ||
{%- if target_lag -%}{{- log('Applying UPDATE TARGET_LAG to: ' ~ existing_relation) -}}{%- endif -%} | ||
{%- set snowflake_warehouse = configuration_changes.snowflake_warehouse -%} | ||
{%- if snowflake_warehouse -%}{{- log('Applying UPDATE WAREHOUSE to: ' ~ existing_relation) -}}{%- endif -%} | ||
|
||
alter dynamic table {{ existing_relation }} set | ||
{% if target_lag %}target_lag = '{{ target_lag.context }}'{% endif %} | ||
{% if snowflake_warehouse %}warehouse = {{ snowflake_warehouse.context }}{% endif %} | ||
|
||
{%- endif -%} | ||
|
||
{%- endmacro %} |
13 changes: 13 additions & 0 deletions
13
dbt/include/snowflake/macros/relations/dynamic_table/create.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,13 @@ | ||
{% macro snowflake__get_create_dynamic_table_as_sql(relation, sql) -%} | ||
{{- log('Applying CREATE to: ' ~ relation) -}} | ||
|
||
create or replace dynamic table {{ relation }} | ||
target_lag = '{{ config.get("target_lag") }}' | ||
warehouse = {{ config.get("snowflake_warehouse") }} | ||
as ( | ||
{{ sql }} | ||
) | ||
; | ||
{{ snowflake__refresh_dynamic_table(relation) }} | ||
|
||
{%- endmacro %} |
19 changes: 19 additions & 0 deletions
19
dbt/include/snowflake/macros/relations/dynamic_table/describe.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,19 @@ | ||
{% macro snowflake__describe_dynamic_table(relation) %} | ||
{%- set _dynamic_table_sql -%} | ||
show dynamic tables | ||
like '{{ relation.identifier }}' | ||
in schema {{ relation.database }}.{{ relation.schema }} | ||
; | ||
select | ||
"name", | ||
"schema_name", | ||
"database_name", | ||
"text", | ||
"target_lag", | ||
"warehouse" | ||
from table(result_scan(last_query_id())) | ||
{%- endset %} | ||
{% set _dynamic_table = run_query(_dynamic_table_sql) %} | ||
|
||
{% do return({'dynamic_table': _dynamic_table}) %} | ||
{% endmacro %} |
3 changes: 3 additions & 0 deletions
3
dbt/include/snowflake/macros/relations/dynamic_table/drop.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,3 @@ | ||
{% macro snowflake__get_drop_dynamic_table_sql(relation) %} | ||
drop dynamic table if exists {{ relation }} | ||
{% endmacro %} |
5 changes: 5 additions & 0 deletions
5
dbt/include/snowflake/macros/relations/dynamic_table/refresh.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,5 @@ | ||
{% macro snowflake__refresh_dynamic_table(relation) -%} | ||
{{- log('Applying REFRESH to: ' ~ relation) -}} | ||
|
||
alter dynamic table {{ relation }} refresh | ||
{%- endmacro %} |
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,5 @@ | ||
{% macro snowflake__rename_relation(from_relation, to_relation) -%} | ||
{% call statement('rename_relation') -%} | ||
alter table {{ from_relation }} rename to {{ to_relation }} | ||
{%- endcall %} | ||
{% endmacro %} |
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,54 @@ | ||
{% macro snowflake__create_table_as(temporary, relation, compiled_code, language='sql') -%} | ||
{%- if language == 'sql' -%} | ||
{%- set transient = config.get('transient', default=true) -%} | ||
{%- set cluster_by_keys = config.get('cluster_by', default=none) -%} | ||
{%- set enable_automatic_clustering = config.get('automatic_clustering', default=false) -%} | ||
{%- set copy_grants = config.get('copy_grants', default=false) -%} | ||
|
||
{%- if cluster_by_keys is not none and cluster_by_keys is string -%} | ||
{%- set cluster_by_keys = [cluster_by_keys] -%} | ||
{%- endif -%} | ||
{%- if cluster_by_keys is not none -%} | ||
{%- set cluster_by_string = cluster_by_keys|join(", ")-%} | ||
{% else %} | ||
{%- set cluster_by_string = none -%} | ||
{%- endif -%} | ||
{%- set sql_header = config.get('sql_header', none) -%} | ||
|
||
{{ sql_header if sql_header is not none }} | ||
|
||
create or replace {% if temporary -%} | ||
temporary | ||
{%- elif transient -%} | ||
transient | ||
{%- endif %} table {{ relation }} | ||
{%- set contract_config = config.get('contract') -%} | ||
{%- if contract_config.enforced -%} | ||
{{ get_assert_columns_equivalent(sql) }} | ||
{{ get_table_columns_and_constraints() }} | ||
{% set compiled_code = get_select_subquery(compiled_code) %} | ||
{% endif %} | ||
{% if copy_grants and not temporary -%} copy grants {%- endif %} as | ||
( | ||
{%- if cluster_by_string is not none -%} | ||
select * from ( | ||
{{ compiled_code }} | ||
) order by ({{ cluster_by_string }}) | ||
{%- else -%} | ||
{{ compiled_code }} | ||
{%- endif %} | ||
); | ||
{% if cluster_by_string is not none and not temporary -%} | ||
alter table {{relation}} cluster by ({{cluster_by_string}}); | ||
{%- endif -%} | ||
{% if enable_automatic_clustering and cluster_by_string is not none and not temporary -%} | ||
alter table {{relation}} resume recluster; | ||
{%- endif -%} | ||
|
||
{%- elif language == 'python' -%} | ||
{{ py_write_table(compiled_code=compiled_code, target_relation=relation, temporary=temporary) }} | ||
{%- else -%} | ||
{% do exceptions.raise_compiler_error("snowflake__create_table_as macro didn't get supported language, it got %s" % language) %} | ||
{%- endif -%} | ||
|
||
{% endmacro %} |
Oops, something went wrong.