Skip to content

Commit

Permalink
handling temp tables and views #188
Browse files Browse the repository at this point in the history
  • Loading branch information
prdpsvs committed Jun 21, 2024
1 parent 3fca56e commit 640f4be
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
{%- set full_refresh_mode = (should_full_refresh()) -%}
{% set target_relation = this.incorporate(type='table') %}
{%- set relation = load_cached_relation(this) -%}
{%- set temp_relation = make_temp_relation(target_relation)-%}
{%- set existing_relation = none %}
{% if relation.type == 'table' %}
{% set existing_relation = target_relation %}
{% elif relation.type == 'view' %}
{% set existing_relation = get_or_create_relation(relation.database, relation.schema, relation.identifier, relation.type)[1] %}
{#-- Can't overwrite a view with a table - we must drop --#}
{{ log("Dropping relation " ~ existing_relation ~ " because it is a view and target is a table.") }}
{{ adapter.drop_relation(existing_relation) }}
{% endif %}

-- configs
Expand All @@ -20,29 +22,30 @@

{{ run_hooks(pre_hooks, inside_transaction=True) }}

{% if existing_relation is none %}
{%- call statement('main') -%}
{{ get_create_table_as_sql(False, target_relation, sql)}}
{%- endcall -%}
{% if existing_relation is none or full_refresh_mode or existing_relation.is_view %}

{% elif existing_relation.is_view %}
{#-- Can't overwrite a view with a table - we must drop --#}
{{ log("Dropping relation " ~ target_relation ~ " because it is a view and this model is a table.") }}
{{ adapter.drop_relation(existing_relation) }}
{% set tmp_vw_relation = target_relation.incorporate(path={"identifier": target_relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
-- Dropping temp view relation if it exists
{{ adapter.drop_relation(tmp_vw_relation) }}

{%- call statement('main') -%}
{{ get_create_table_as_sql(False, target_relation, sql)}}
{%- endcall -%}

{% elif full_refresh_mode %}
{%- call statement('main') -%}
{{ get_create_table_as_sql(False, target_relation, sql)}}
{%- endcall -%}
-- Dropping temp view relation
{{ adapter.drop_relation(tmp_vw_relation) }}

{% else %}

{%- set temp_relation = make_temp_relation(target_relation)-%}
{% set tmp_tble_vw_relation = temp_relation.incorporate(path={"identifier": temp_relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
-- Dropping temp view relation if it exists
{{ adapter.drop_relation(tmp_tble_vw_relation) }}

{%- call statement('create_tmp_relation') -%}
{{ get_create_table_as_sql(True, temp_relation, sql)}}
{%- endcall -%}
{{ adapter.drop_relation(tmp_tble_vw_relation) }}
{% do adapter.expand_target_column_types(
from_relation=temp_relation,
to_relation=target_relation) %}
Expand All @@ -59,9 +62,10 @@
{%- call statement('main') -%}
{{ strategy_sql_macro_func(strategy_arg_dict) }}
{%- endcall -%}

{{ adapter.drop_relation(temp_relation) }}
{% endif %}

{{ adapter.drop_relation(temp_relation) }}
{{ run_hooks(post_hooks, inside_transaction=True) }}
{% set target_relation = target_relation.incorporate(type='table') %}
{% set should_revoke = should_revoke(existing_relation, full_refresh_mode) %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% macro fabric__create_table_as(temporary, relation, sql) -%}

{% set tmp_vw_relation = relation.incorporate(path={"identifier": relation.identifier ~ '_vw'}, type='view')-%}
{% do adapter.drop_relation(tmp_vw_relation) %}
{% set tmp_vw_relation = relation.incorporate(path={"identifier": relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
{{ get_create_view_as_sql(tmp_vw_relation, sql) }}

{% set contract_config = config.get('contract') %}
Expand All @@ -22,6 +21,6 @@
{%- else %}
EXEC('CREATE TABLE {{relation}} AS SELECT * FROM {{tmp_vw_relation}};');
{% endif %}

-- To clean up test_store tests
{% do adapter.drop_relation(tmp_vw_relation) %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
-- Drop temp relation if it exists before materializing temp relation
{{ adapter.drop_relation(temp_relation) }}

{% set tmp_vw_relation = temp_relation.incorporate(path={"identifier": temp_relation.identifier ~ '_vw'}, type='view')-%}
{% set tmp_vw_relation = temp_relation.incorporate(path={"identifier": temp_relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}

{{ adapter.drop_relation(tmp_vw_relation) }}

{{ run_hooks(pre_hooks, inside_transaction=False) }}
-- `BEGIN` happens here:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,17 @@
{% macro build_snapshot_staging_table(strategy, temp_snapshot_relation, target_relation) %}
{% set temp_relation = make_temp_relation(target_relation) %}
{% set select = snapshot_staging_table(strategy, temp_snapshot_relation, target_relation) %}

{% set tmp_tble_vw_relation = temp_relation.incorporate(path={"identifier": temp_relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
-- Dropping temp view relation if it exists
{{ adapter.drop_relation(tmp_tble_vw_relation) }}

{% call statement('build_snapshot_staging_relation') %}
{{ get_create_table_as_sql(True, temp_relation, select) }}
{% endcall %}

-- Dropping temp view relation if it exists
{{ adapter.drop_relation(tmp_tble_vw_relation) }}

{% do return(temp_relation) %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
{% set tmp_relation_view = target_relation.incorporate(path={"identifier": target_relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
-- Fabric & Synapse adapters use temp relation because of lack of CTE support for CTE in CTAS, Insert
-- drop temp relation if exists
{% do adapter.drop_relation(tmp_relation_view) %}
{{ adapter.drop_relation(tmp_relation_view) }}
{% set final_sql = get_create_table_as_sql(False, target_relation, build_sql) %}
{% do adapter.drop_relation(tmp_relation_view) %}
{{ adapter.drop_relation(tmp_relation_view) }}

{% else %}

Expand Down Expand Up @@ -88,7 +88,7 @@
{{ final_sql }}
{% endcall %}

{% do adapter.drop_relation(temp_snapshot_relation) %}
{{ adapter.drop_relation(temp_snapshot_relation) }}
{% set should_revoke = should_revoke(target_relation_exists, full_refresh_mode=False) %}
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

Expand Down

0 comments on commit 640f4be

Please sign in to comment.