From 916d48d016a66a8f01eb39106109a5f7355d965b Mon Sep 17 00:00:00 2001 From: Pradeep Srikakolapu Date: Mon, 27 May 2024 11:26:35 -0700 Subject: [PATCH] removing hardcoded fabric__ references to not to break dbt-synapse and dbt-sqlserver adapters --- .../models/incremental/incremental.sql | 20 ++----------------- .../models/table/columns_spec_ddl.sql | 14 +++++++++++++ .../models/table/create_table_as.sql | 17 ++++++---------- .../materializations/models/table/table.sql | 2 +- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/dbt/include/fabric/macros/materializations/models/incremental/incremental.sql b/dbt/include/fabric/macros/materializations/models/incremental/incremental.sql index 698b213..5fce98c 100644 --- a/dbt/include/fabric/macros/materializations/models/incremental/incremental.sql +++ b/dbt/include/fabric/macros/materializations/models/incremental/incremental.sql @@ -11,22 +11,6 @@ {% elif (relation.type != target_relation.type) and (relation.identifier == target_relation.identifier) and (relation.schema == target_relation.schema) and (relation.database == target_relation.database) %} {% set existing_relation = get_or_create_relation(relation.database, relation.schema, relation.identifier, relation.type)[1] %} {% endif %} - {# {%- set relations_list = fabric__get_relation_without_caching(target_relation) -%} - {%- set existing_relation = none %} - {% if (relations_list|length == 1) and (relations_list[0][2] == target_relation.schema) - and (relations_list[0][1] == target_relation.identifier) and (relations_list[0][3] == target_relation.type)%} - {% set existing_relation = target_relation %} - {% elif (relations_list|length == 1) and (relations_list[0][2] == target_relation.schema) - and (relations_list[0][1] == target_relation.identifier) and (relations_list[0][3] != target_relation.type) %} - {% set existing_relation = get_or_create_relation(relations_list[0][0], relations_list[0][2] , relations_list[0][1] , relations_list[0][3])[1] %} - {% endif %} #} - - {# {{ log("Full refresh mode" ~ full_refresh_mode)}} - {{ log("existing relation : "~existing_relation ~ " type "~ existing_relation.type ~ " is view? "~existing_relation.is_view) }} - {{ log("target relation: " ~target_relation ~ " type "~ target_relation.type ~ " is view? "~target_relation.is_view) }} #} - - {{ log("existing relation : "~existing_relation ~ " type "~ existing_relation.type ~ " is view? "~existing_relation.is_view) }} - {{ log("target relation: " ~target_relation ~ " type "~ target_relation.type ~ " is view? "~target_relation.is_view) }} -- configs {%- set unique_key = config.get('unique_key') -%} @@ -62,8 +46,8 @@ {{ get_create_table_as_sql(True, temp_relation, sql)}} {%- endcall -%} {% do adapter.expand_target_column_types( - from_relation=temp_relation, - to_relation=target_relation) %} + from_relation=temp_relation, + to_relation=target_relation) %} {#-- Process schema changes. Returns dict of changes if successful. Use source columns for upserting/merging --#} {% set dest_columns = process_schema_changes(on_schema_change, temp_relation, existing_relation) %} {% if not dest_columns %} diff --git a/dbt/include/fabric/macros/materializations/models/table/columns_spec_ddl.sql b/dbt/include/fabric/macros/materializations/models/table/columns_spec_ddl.sql index 83188c9..1fcc779 100644 --- a/dbt/include/fabric/macros/materializations/models/table/columns_spec_ddl.sql +++ b/dbt/include/fabric/macros/materializations/models/table/columns_spec_ddl.sql @@ -1,3 +1,10 @@ +{% macro build_columns_constraints(relation) %} + {{ return(adapter.dispatch('build_columns_constraints', 'dbt')(relation)) }} +{% endmacro %} + +{%- macro default__build_columns_constraints(relation) -%} +{%- endmacro -%} + {% macro fabric__build_columns_constraints(relation) %} {# loop through user_provided_columns to create DDL with data types and constraints #} {%- set raw_column_constraints = adapter.render_raw_columns_constraints(raw_columns=model['columns']) -%} @@ -8,6 +15,13 @@ ) {% endmacro %} +{% macro build_model_constraints(relation) %} + {{ return(adapter.dispatch('build_columns_constraints', 'dbt')(relation)) }} +{% endmacro %} + +{%- macro default__build_model_constraints(relation) -%} +{%- endmacro -%} + {% macro fabric__build_model_constraints(relation) %} {# loop through user_provided_columns to create DDL with data types and constraints #} {%- set raw_model_constraints = adapter.render_raw_model_constraints(raw_constraints=model['constraints']) -%} diff --git a/dbt/include/fabric/macros/materializations/models/table/create_table_as.sql b/dbt/include/fabric/macros/materializations/models/table/create_table_as.sql index 7c281f7..9f301e3 100644 --- a/dbt/include/fabric/macros/materializations/models/table/create_table_as.sql +++ b/dbt/include/fabric/macros/materializations/models/table/create_table_as.sql @@ -1,18 +1,16 @@ {% macro fabric__create_table_as(temporary, relation, sql) -%} - {% set tmp_relation = relation.incorporate( - path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'}, - type='view')-%} - {% do adapter.drop_relation(tmp_relation) %} - {% set contract_config = config.get('contract') %} - + {% set tmp_relation = relation.incorporate( + path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'}, + 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) }} {{ get_assert_columns_equivalent(sql) }} - {% set listColumns %} {% for column in model['columns'] %} {{ "["~column~"]" }}{{ ", " if not loop.last }} @@ -23,9 +21,6 @@ ({{listColumns}}) SELECT {{listColumns}} FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]; {%- else %} - EXEC('CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}] AS (SELECT * FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]);'); + EXEC('CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}] AS (SELECT * FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]);'); {% endif %} - - {% do adapter.drop_relation(tmp_relation) %} - {% endmacro %} diff --git a/dbt/include/fabric/macros/materializations/models/table/table.sql b/dbt/include/fabric/macros/materializations/models/table/table.sql index ebe9530..2594baa 100644 --- a/dbt/include/fabric/macros/materializations/models/table/table.sql +++ b/dbt/include/fabric/macros/materializations/models/table/table.sql @@ -57,7 +57,7 @@ {% endif %} -- Add constraints including FK relation. - {{ fabric__build_model_constraints(target_relation) }} + {{ build_model_constraints(target_relation) }} {{ run_hooks(post_hooks, inside_transaction=False) }} {{ return({'relations': [target_relation]}) }}