Skip to content

Commit 4ed06da

Browse files
prdpsvsjtcohen6
andauthored
Updating dispatching methods to ensure dbt-synapse adapter can use ad… (#178)
* Updating dispatching methods to ensure dbt-synapse adapter can use adapter dispatch methods * additional changes * Updating dbt-fabric adapter to stay compatible with dbt-synapse adapter * changes to drop_relation_if_exists * Updated tests and drop_relation * adding temp relation to table materialization to support temp view drop relation * adding a log * dropping and creating temp relation in table materialization and removing it from adapter create_table_as.sql macro * removing hardcoded fabric__ references to not to break dbt-synapse and dbt-sqlserver adapters * moving temp relation drop to table, incremental and snapshot materializations * adding drop relation to create_table_as to support test_store_tests * adding log statements * removing comments * Update * Update dbt/include/fabric/macros/adapters/columns.sql Co-authored-by: Jeremy Cohen <[email protected]> * Update dbt/include/fabric/macros/materializations/models/incremental/incremental.sql Co-authored-by: Jeremy Cohen <[email protected]> * Resolving comments --------- Co-authored-by: Jeremy Cohen <[email protected]>
1 parent 45dacbc commit 4ed06da

29 files changed

+171
-288
lines changed

.github/workflows/integration-tests-azure.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ jobs:
1111
name: Regular
1212
strategy:
1313
fail-fast: false
14+
max-parallel: 1
1415
matrix:
15-
python_version: ["3.8", "3.9", "3.10", "3.11"]
1616
profile: ["ci_azure_auto"]
17+
python_version: ["3.11"]
1718
msodbc_version: ["17", "18"]
18-
max-parallel: 1
19+
1920
runs-on: ubuntu-latest
2021
container:
2122
image: ghcr.io/${{ github.repository }}:CI-${{ matrix.python_version }}-msodbc${{ matrix.msodbc_version }}

dbt/adapters/fabric/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "1.8.5"
1+
version = "1.8.6"

dbt/adapters/fabric/fabric_connection_manager.py

+6
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ def open(cls, connection: Connection) -> Connection:
323323

324324
con_str.append(f"Database={credentials.database}")
325325

326+
#Enabling trace flag
327+
if credentials.trace_flag:
328+
con_str.append("SQL_ATTR_TRACE=SQL_OPT_TRACE_ON")
329+
else:
330+
con_str.append("SQL_ATTR_TRACE=SQL_OPT_TRACE_OFF")
331+
326332
assert credentials.authentication is not None
327333

328334
if "ActiveDirectory" in credentials.authentication:

dbt/adapters/fabric/fabric_credentials.py

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class FabricCredentials(Credentials):
1313
UID: Optional[str] = None
1414
PWD: Optional[str] = None
1515
windows_login: Optional[bool] = False
16+
trace_flag: Optional[bool] = False
1617
tenant_id: Optional[str] = None
1718
client_id: Optional[str] = None
1819
client_secret: Optional[str] = None
@@ -36,6 +37,7 @@ class FabricCredentials(Credentials):
3637
"app_secret": "client_secret",
3738
"TrustServerCertificate": "trust_cert",
3839
"schema_auth": "schema_authorization",
40+
"SQL_ATTR_TRACE": "trace_flag",
3941
}
4042

4143
@property
@@ -63,6 +65,7 @@ def _connection_keys(self):
6365
"retries",
6466
"login_timeout",
6567
"query_timeout",
68+
"trace_flag",
6669
)
6770

6871
@property

dbt/include/fabric/macros/adapters/columns.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454

5555
{% macro fabric__alter_column_type(relation, column_name, new_column_type) %}
5656

57-
{%- set table_name= tmp_relation.include(database=False).include(schema=False)-%}
58-
{%- set schema_name = tmp_relation.include(database=False).include(identifier=False) -%}
57+
{%- set table_name= relation.identifier -%}
58+
{%- set schema_name = relation.schema -%}
5959

6060
{% set generate_tmp_relation_script %}
6161
SELECT TRIM(REPLACE(STRING_AGG(ColumnName + ' ', ',-'), '-', CHAR(10))) AS ColumnDef

dbt/include/fabric/macros/adapters/metadata.sql

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
information_schema
1010
{%- endmacro %}
1111

12+
{% macro get_use_database_sql(database) %}
13+
{{ return(adapter.dispatch('get_use_database_sql', 'dbt')(database)) }}
14+
{% endmacro %}
15+
16+
{%- macro fabric__get_use_database_sql(database) -%}
17+
USE [{{database}}];
18+
{%- endmacro -%}
19+
1220
{% macro fabric__list_schemas(database) %}
1321
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
1422
select name as [schema]
@@ -27,7 +35,7 @@
2735

2836
{% macro fabric__list_relations_without_caching(schema_relation) -%}
2937
{% call statement('list_relations_without_caching', fetch_result=True) -%}
30-
USE [{{ schema_relation.database }}];
38+
{{ get_use_database_sql(schema_relation.database) }}
3139
with base as (
3240
select
3341
DB_NAME() as [database],
@@ -51,7 +59,7 @@
5159

5260
{% macro fabric__get_relation_without_caching(schema_relation) -%}
5361
{% call statement('get_relation_without_caching', fetch_result=True) -%}
54-
USE [{{ schema_relation.database }}];
62+
{{ get_use_database_sql(schema_relation.database) }}
5563
with base as (
5664
select
5765
DB_NAME() as [database],

dbt/include/fabric/macros/adapters/relation.sql

+23-30
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,44 @@
66
{{ return(temp_relation) }}
77
{% endmacro %}
88

9-
{% macro fabric__drop_relation(relation) -%}
10-
{% call statement('drop_relation', auto_begin=False) -%}
11-
{{ fabric__drop_relation_script(relation) }}
12-
{%- endcall %}
13-
{% endmacro %}
14-
15-
{% macro fabric__drop_relation_script(relation) -%}
16-
17-
{% if relation.type == 'view' -%}
9+
{% macro fabric__get_drop_sql(relation) -%}
10+
{% if relation.type == 'view' -%}
1811
{% call statement('find_references', fetch_result=true) %}
19-
USE [{{ relation.database }}];
20-
select
21-
sch.name as schema_name,
22-
obj.name as view_name
23-
from sys.sql_expression_dependencies refs
24-
inner join sys.objects obj
25-
on refs.referencing_id = obj.object_id
26-
inner join sys.schemas sch
27-
on obj.schema_id = sch.schema_id
28-
where refs.referenced_database_name = '{{ relation.database }}'
29-
and refs.referenced_schema_name = '{{ relation.schema }}'
30-
and refs.referenced_entity_name = '{{ relation.identifier }}'
31-
and refs.referencing_class = 1
32-
and obj.type = 'V'
12+
{{ get_use_database_sql(relation.database) }}
13+
select
14+
sch.name as schema_name,
15+
obj.name as view_name
16+
from sys.sql_expression_dependencies refs
17+
inner join sys.objects obj
18+
on refs.referencing_id = obj.object_id
19+
inner join sys.schemas sch
20+
on obj.schema_id = sch.schema_id
21+
where refs.referenced_database_name = '{{ relation.database }}'
22+
and refs.referenced_schema_name = '{{ relation.schema }}'
23+
and refs.referenced_entity_name = '{{ relation.identifier }}'
24+
and refs.referencing_class = 1
25+
and obj.type = 'V'
3326
{% endcall %}
3427
{% set references = load_result('find_references')['data'] %}
3528
{% for reference in references -%}
36-
-- dropping referenced view {{ reference[0] }}.{{ reference[1] }}
37-
{{ fabric__drop_relation_script(relation.incorporate(
38-
type="view",
39-
path={"schema": reference[0], "identifier": reference[1]})) }}
29+
-- dropping referenced view {{ reference[0] }}.{{ reference[1] }}
30+
{% do adapter.drop_relation
31+
(api.Relation.create(
32+
identifier = reference[1], schema = reference[0], database = relation.database, type='view'
33+
))%}
4034
{% endfor %}
4135
{% elif relation.type == 'table'%}
4236
{% set object_id_type = 'U' %}
43-
4437
{%- else -%}
4538
{{ exceptions.raise_not_implemented('Invalid relation being dropped: ' ~ relation) }}
4639
{% endif %}
47-
USE [{{ relation.database }}];
40+
{{ get_use_database_sql(relation.database) }}
4841
EXEC('DROP {{ relation.type }} IF EXISTS {{ relation.include(database=False) }};');
4942
{% endmacro %}
5043

5144
{% macro fabric__rename_relation(from_relation, to_relation) -%}
5245
{% call statement('rename_relation') -%}
53-
USE [{{ from_relation.database }}];
46+
{{ get_use_database_sql(from_relation.database) }}
5447
EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}'
5548
{%- endcall %}
5649
{% endmacro %}

dbt/include/fabric/macros/adapters/schema.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
{% macro fabric__create_schema_with_authorization(relation, schema_authorization) -%}
1212
{% call statement('create_schema') -%}
13-
USE [{{ relation.database }}];
13+
{{ get_use_database_sql(relation.database) }}
1414
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
1515
BEGIN
1616
EXEC('CREATE SCHEMA [{{ relation.schema }}] AUTHORIZATION [{{ schema_authorization }}]')
@@ -27,7 +27,7 @@
2727
identifier=row[1],
2828
type=row[3]
2929
) -%}
30-
{% do drop_relation(schema_relation) %}
30+
{% do adapter.drop_relation(schema_relation) %}
3131
{%- endfor %}
3232

3333
{% call statement('drop_schema') -%}

dbt/include/fabric/macros/materializations/models/incremental/incremental.sql

+21-26
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
21
{% materialization incremental, adapter='fabric' -%}
32

43
{%- set full_refresh_mode = (should_full_refresh()) -%}
54
{% set target_relation = this.incorporate(type='table') %}
6-
{%- set relations_list = fabric__get_relation_without_caching(target_relation) -%}
5+
{%- set relation = load_cached_relation(this) -%}
76

87
{%- set existing_relation = none %}
9-
{% if (relations_list|length == 1) and (relations_list[0][2] == target_relation.schema)
10-
and (relations_list[0][1] == target_relation.identifier) and (relations_list[0][3] == target_relation.type)%}
8+
{% if relation.type == 'table' %}
119
{% set existing_relation = target_relation %}
12-
{% elif (relations_list|length == 1) and (relations_list[0][2] == target_relation.schema)
13-
and (relations_list[0][1] == target_relation.identifier) and (relations_list[0][3] != target_relation.type) %}
14-
{% set existing_relation = get_or_create_relation(relations_list[0][0], relations_list[0][2] , relations_list[0][1] , relations_list[0][3])[1] %}
10+
{% elif relation.type == 'view' %}
11+
{% set existing_relation = get_or_create_relation(relation.database, relation.schema, relation.identifier, relation.type)[1] %}
1512
{% endif %}
1613

17-
{{ log("Full refresh mode" ~ full_refresh_mode)}}
18-
{{ log("existing relation : "~existing_relation ~ " type "~ existing_relation.type ~ " is view? "~existing_relation.is_view) }}
19-
{{ log("target relation: " ~target_relation ~ " type "~ target_relation.type ~ " is view? "~target_relation.is_view) }}
20-
2114
-- configs
2215
{%- set unique_key = config.get('unique_key') -%}
2316
{% set incremental_strategy = config.get('incremental_strategy') or 'default' %}
@@ -28,35 +21,39 @@
2821

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

31-
{% if existing_relation is none %}
24+
-- naming a temp relation
25+
{% set tmp_relation_view = target_relation.incorporate(path={"identifier": target_relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
3226

27+
-- Fabric & Synapse adapters use temp relation because of lack of CTE support for CTE in CTAS, Insert
28+
-- drop temp relation if exists
29+
{% do adapter.drop_relation(tmp_relation_view) %}
30+
31+
{% if existing_relation is none %}
3332
{%- call statement('main') -%}
34-
{{ fabric__create_table_as(False, target_relation, sql)}}
33+
{{ get_create_table_as_sql(False, target_relation, sql)}}
3534
{%- endcall -%}
3635

3736
{% elif existing_relation.is_view %}
38-
3937
{#-- Can't overwrite a view with a table - we must drop --#}
4038
{{ log("Dropping relation " ~ target_relation ~ " because it is a view and this model is a table.") }}
41-
{{ drop_relation_if_exists(existing_relation) }}
39+
{% do adapter.drop_relation(existing_relation) %}
40+
4241
{%- call statement('main') -%}
43-
{{ fabric__create_table_as(False, target_relation, sql)}}
42+
{{ get_create_table_as_sql(False, target_relation, sql)}}
4443
{%- endcall -%}
4544

4645
{% elif full_refresh_mode %}
47-
4846
{%- call statement('main') -%}
49-
{{ fabric__create_table_as(False, target_relation, sql)}}
47+
{{ get_create_table_as_sql(False, target_relation, sql)}}
5048
{%- endcall -%}
5149

5250
{% else %}
53-
5451
{%- call statement('create_tmp_relation') -%}
55-
{{ fabric__create_table_as(True, temp_relation, sql)}}
52+
{{ get_create_table_as_sql(True, temp_relation, sql)}}
5653
{%- endcall -%}
5754
{% do adapter.expand_target_column_types(
58-
from_relation=temp_relation,
59-
to_relation=target_relation) %}
55+
from_relation=temp_relation,
56+
to_relation=target_relation) %}
6057
{#-- Process schema changes. Returns dict of changes if successful. Use source columns for upserting/merging --#}
6158
{% set dest_columns = process_schema_changes(on_schema_change, temp_relation, existing_relation) %}
6259
{% if not dest_columns %}
@@ -72,14 +69,12 @@
7269
{%- endcall -%}
7370
{% endif %}
7471

75-
{% do drop_relation_if_exists(temp_relation) %}
72+
{% do adapter.drop_relation(tmp_relation_view) %}
73+
{% do adapter.drop_relation(temp_relation) %}
7674
{{ run_hooks(post_hooks, inside_transaction=True) }}
77-
7875
{% set target_relation = target_relation.incorporate(type='table') %}
79-
8076
{% set should_revoke = should_revoke(existing_relation, full_refresh_mode) %}
8177
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
82-
8378
{% do persist_docs(target_relation, model) %}
8479
{% do adapter.commit() %}
8580
{{ return({'relations': [target_relation]}) }}

dbt/include/fabric/macros/materializations/models/table/clone.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
{%- set target_relation = this.incorporate(type='table') -%}
2525

2626
{% call statement('main') %}
27-
{{ fabric__drop_relation_script(target_relation) }}
27+
{% do adapter.drop_relation(target_relation) %}
2828
{{ create_or_replace_clone(target_relation, defer_relation) }}
2929
{% endcall %}
3030
{{ return({'relations': [target_relation]}) }}

dbt/include/fabric/macros/materializations/models/table/columns_spec_ddl.sql

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
{% macro build_columns_constraints(relation) %}
2+
{{ return(adapter.dispatch('build_columns_constraints', 'dbt')(relation)) }}
3+
{% endmacro %}
4+
15
{% macro fabric__build_columns_constraints(relation) %}
26
{# loop through user_provided_columns to create DDL with data types and constraints #}
37
{%- set raw_column_constraints = adapter.render_raw_columns_constraints(raw_columns=model['columns']) -%}
@@ -8,6 +12,10 @@
812
)
913
{% endmacro %}
1014

15+
{% macro build_model_constraints(relation) %}
16+
{{ return(adapter.dispatch('build_model_constraints', 'dbt')(relation)) }}
17+
{% endmacro %}
18+
1119
{% macro fabric__build_model_constraints(relation) %}
1220
{# loop through user_provided_columns to create DDL with data types and constraints #}
1321
{%- set raw_model_constraints = adapter.render_raw_model_constraints(raw_constraints=model['constraints']) -%}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
{% macro fabric__create_table_as(temporary, relation, sql) -%}
22

3-
{% set tmp_relation = relation.incorporate(
4-
path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'},
5-
type='view')-%}
6-
{% do run_query(fabric__drop_relation_script(tmp_relation)) %}
3+
{% set tmp_relation = relation.incorporate(path={"identifier": relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
4+
{{ get_create_view_as_sql(tmp_relation, sql) }}
75

8-
{% set contract_config = config.get('contract') %}
9-
10-
{{ fabric__create_view_as(tmp_relation, sql) }}
6+
{% set contract_config = config.get('contract') %}
117
{% if contract_config.enforced %}
128

139
CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}]
14-
{{ fabric__build_columns_constraints(relation) }}
10+
{{ build_columns_constraints(relation) }}
1511
{{ get_assert_columns_equivalent(sql) }}
16-
1712
{% set listColumns %}
1813
{% for column in model['columns'] %}
1914
{{ "["~column~"]" }}{{ ", " if not loop.last }}
@@ -24,9 +19,7 @@
2419
({{listColumns}}) SELECT {{listColumns}} FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}];
2520

2621
{%- else %}
27-
EXEC('CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}] AS (SELECT * FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]);');
22+
EXEC('CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}] AS (SELECT * FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]);');
2823
{% endif %}
29-
30-
{{ fabric__drop_relation_script(tmp_relation) }}
31-
24+
{% do adapter.drop_relation(tmp_relation)%}
3225
{% endmacro %}

0 commit comments

Comments
 (0)