Skip to content

Commit

Permalink
moving temp relation drop to table, incremental and snapshot material…
Browse files Browse the repository at this point in the history
…izations
  • Loading branch information
prdpsvs committed May 27, 2024
1 parent 916d48d commit 7bde13e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@

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

-- naming a temp relation
{% 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) %}

{% if existing_relation is none %}
{%- call statement('main') -%}
{{ get_create_table_as_sql(False, target_relation, sql)}}
Expand Down Expand Up @@ -63,14 +70,12 @@
{%- endcall -%}
{% endif %}

{% do adapter.drop_relation(tmp_relation_view) %}
{% do 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) %}
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

{% do persist_docs(target_relation, model) %}
{% do adapter.commit() %}
{{ return({'relations': [target_relation]}) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% endmacro %}

{% macro build_model_constraints(relation) %}
{{ return(adapter.dispatch('build_columns_constraints', 'dbt')(relation)) }}
{{ return(adapter.dispatch('build_model_constraints', 'dbt')(relation)) }}
{% endmacro %}

{%- macro default__build_model_constraints(relation) -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{% macro fabric__create_table_as(temporary, relation, sql) -%}

{% set tmp_relation = relation.incorporate(
path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'},
type='view')-%}
{% set tmp_relation = relation.incorporate(path={"identifier": relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
{{ get_create_view_as_sql(tmp_relation, sql) }}

{% set contract_config = config.get('contract') %}
{% if contract_config.enforced %}

CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}]
{{ fabric__build_columns_constraints(relation) }}
{{ build_columns_constraints(relation) }}
{{ get_assert_columns_equivalent(sql) }}
{% set listColumns %}
{% for column in model['columns'] %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@
{% if not target_relation_exists %}

{% set build_sql = build_snapshot_table(strategy, temp_snapshot_relation) %}

-- naming a temp relation
{% 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) %}
{% set final_sql = get_create_table_as_sql(False, target_relation, build_sql) %}
{% do adapter.drop_relation(tmp_relation_view) %}

{% else %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test__fabric__list_relations_without_caching(self, project):
str(kwargs),
]
)
assert "n_relations: 2" in log_output
assert "n_relations: 1" in log_output


class TestListRelationsWithoutCachingFull:
Expand Down Expand Up @@ -157,4 +157,4 @@ def test__fabric__list_relations_without_caching(self, project):
str(kwargs),
]
)
assert "n_relations: 12" in log_output
assert f"n_relations: {NUM_EXPECTED_RELATIONS}" in log_output

0 comments on commit 7bde13e

Please sign in to comment.