-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed materialization for stage to become create if not exists
added in external tables capabilities
- Loading branch information
1 parent
3e2f7f9
commit a22ea42
Showing
7 changed files
with
92 additions
and
5 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
38 changes: 38 additions & 0 deletions
38
macros/source_tables/snowflake/snowflake__create_external_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,38 @@ | ||
{% macro snowflake__create_external_table(source_node) %} | ||
|
||
{%- set columns = source_node.columns.values() -%} | ||
{%- set external = source_node.external -%} | ||
{%- set partitions = external.partitions -%} | ||
|
||
{%- set is_csv = dbt_external_tables.is_csv(external.file_format) -%} | ||
|
||
{# https://docs.snowflake.net/manuals/sql-reference/sql/create-external-table.html #} | ||
{# This assumes you have already created an external stage #} | ||
CREATE OR REPLACE EXTERNAL TABLE {{source(source_node.source_name, source_node.name)}} | ||
( | ||
file_name VARCHAR(500) AS metadata$filename, | ||
load_date TIMESTAMP_LTZ(7) AS current_timestamp{{- ',' if partitions or columns|length > 0 -}} | ||
{%- if columns or partitions -%} | ||
{%- if partitions -%}{%- for partition in partitions %} | ||
{{partition.name}} {{partition.data_type}} AS {{partition.expression}}{{- ',' if not loop.last or columns|length > 0 -}} | ||
{%- endfor -%}{%- endif -%} | ||
{%- for column in columns %} | ||
{%- set column_quoted = adapter.quote(column.name) if column.quote else column.name %} | ||
{%- set col_expression -%} | ||
{%- set col_id = 'value:c' ~ loop.index if is_csv else 'value:' ~ column_quoted -%} | ||
(case when is_null_value({{col_id}}) or lower({{col_id}}) = 'null' then null else {{col_id}} end) | ||
{%- endset %} | ||
{{column_quoted}} {{column.data_type}} AS ({{col_expression}}::{{column.data_type}}) | ||
{{- ',' if not loop.last -}} | ||
{% endfor %} | ||
{%- endif -%} | ||
) | ||
{% if partitions %} PARTITION BY ({{partitions|map(attribute='name')|join(', ')}}) {% endif %} | ||
LOCATION = {{external.location}} {# stage #} | ||
{% if external.auto_refresh in (true, false) -%} | ||
AUTO_REFRESH = {{external.auto_refresh}} | ||
{%- endif %} | ||
{% if external.pattern -%} PATTERN = '{{external.pattern}}' {%- endif %} | ||
{% if external.integration -%} INTEGRATION = '{{external.integration}}' {%- endif %} | ||
FILE_FORMAT = {{external.file_format}} | ||
{% endmacro %} |
37 changes: 37 additions & 0 deletions
37
macros/source_tables/snowflake/snowflake__get_external_build_plan.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,37 @@ | ||
{% macro snowflake__get_external_build_plan(source_node) %} | ||
|
||
{% set build_plan = [] %} | ||
|
||
{% set old_relation = adapter.get_relation( | ||
database = source_node.database, | ||
schema = source_node.schema, | ||
identifier = source_node.identifier | ||
) %} | ||
|
||
{% set create_or_replace = (old_relation is none or var('ext_full_refresh', false)) %} | ||
|
||
{% if source_node.external.get('snowpipe', none) is not none %} | ||
|
||
{% if create_or_replace %} | ||
{% set build_plan = build_plan + [ | ||
dbt_external_tables.snowflake_create_empty_table(source_node), | ||
dbt_external_tables.snowflake_get_copy_sql(source_node, explicit_transaction=true), | ||
dbt_external_tables.snowflake_create_snowpipe(source_node) | ||
] %} | ||
{% else %} | ||
{% set build_plan = build_plan + dbt_external_tables.snowflake_refresh_snowpipe(source_node) %} | ||
{% endif %} | ||
|
||
{% else %} | ||
|
||
{% if create_or_replace %} | ||
{% set build_plan = build_plan + [dbt_dataengineers_materilizations.snowflake__create_external_table(source_node)] %} | ||
{% else %} | ||
{% set build_plan = build_plan + dbt_external_tables.refresh_external_table(source_node) %} | ||
{% endif %} | ||
|
||
{% endif %} | ||
|
||
{% do return(build_plan) %} | ||
|
||
{% 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
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
8 changes: 8 additions & 0 deletions
8
macros/stages/snowflake/snowflake_create_stages_if_not_exist_statement.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 @@ | ||
{%- macro snowflake_create_stages_if_not_exist_statement(relation, sql) -%} | ||
|
||
{{ log("Creating stages " ~ relation) }} | ||
CREATE STAGE IF NOT EXISTS {{ relation.include(database=(not temporary), schema=(not temporary)) }} | ||
{{ sql }} | ||
; | ||
|
||
{%- 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,3 @@ | ||
packages: | ||
- package: dbt-labs/dbt_external_tables | ||
version: 0.8.0 |