Skip to content

Commit eef6fda

Browse files
authored
Merge pull request #245 from microsoft/v1.8.0
V1.8.0 release
2 parents 071fb4c + da2b8a9 commit eef6fda

File tree

17 files changed

+112
-314
lines changed

17 files changed

+112
-314
lines changed

dbt/adapters/synapse/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "1.8.0rc3"
1+
version = "1.8.0"

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
path={"identifier": relation.identifier.replace("#", "") ~ '_tmp_tbl_hack'},
88
type='table')-%}
99

10-
{% do drop_relation(tmp_tbl_hack) %}
10+
{% do adapter.drop_relation(tmp_tbl_hack) %}
1111
{% set sql_create %}
1212
CREATE TABLE {{ tmp_tbl_hack }}
1313
WITH(
@@ -23,7 +23,7 @@
2323
{% call statement() -%} {{ sql_create }} {%- endcall %}
2424

2525
{% set output = get_columns_in_relation(tmp_tbl_hack) %}
26-
{% do drop_relation(tmp_tbl_hack) %}
26+
{% do adapter.drop_relation(tmp_tbl_hack) %}
2727
{{ return(output) }}
2828
{% endif %}
2929

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

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
{%- macro synapse__get_use_database_sql(database) -%}
2+
{%- endmacro -%}
3+
4+
{%- macro default__get_use_database_sql(database) -%}
5+
{{ return('') }}
6+
{%- endmacro -%}
7+
18
{% macro synapse__list_schemas(database) %}
29
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
310
select name as [schema]
@@ -8,6 +15,7 @@
815

916
{% macro synapse__list_relations_without_caching(schema_relation) %}
1017
{% call statement('list_relations_without_caching', fetch_result=True) -%}
18+
{{ get_use_database_sql(schema_relation.database) }}
1119
select
1220
table_catalog as [database],
1321
table_name as [name],
@@ -26,6 +34,7 @@
2634

2735
{% macro synapse__get_relation_without_caching(schema_relation) -%}
2836
{% call statement('list_relations_without_caching', fetch_result=True) -%}
37+
{{ get_use_database_sql(schema_relation.database) }}
2938
select
3039
table_catalog as [database],
3140
table_name as [name],

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

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
{% macro synapse__drop_relation(relation) -%}
2-
{% call statement('drop_relation', auto_begin=False) -%}
3-
{{ synapse__drop_relation_script(relation) }}
4-
{%- endcall %}
5-
{% endmacro %}
6-
7-
{% macro synapse__drop_relation_script(relation) -%}
8-
{% if relation is not none %}
1+
{% macro synapse__get_drop_sql(relation) -%}
92
{% if relation.type == 'view' or relation.type == 'materialized_view' -%}
103
{% set object_id_type = 'V' %}
114
{% elif relation.type == 'table'%}
125
{% set object_id_type = 'U' %}
136
{%- else -%} invalid target name
147
{% endif %}
15-
if object_id ('{{ relation }}','{{ object_id_type }}') is not null
16-
{% if relation.type == 'view' or relation.type == 'materialized_view' -%}
17-
begin
18-
drop view {{ relation }}
19-
end
20-
{% elif relation.type == 'table' %}
21-
begin
22-
drop {{ relation.type }} {{ relation }}
23-
end
24-
{% endif %}
25-
{% else %}
26-
-- no object to drop
27-
select 1 as nothing
28-
{% endif %}
8+
if object_id ('{{ relation }}','{{ object_id_type }}') is not null
9+
drop {{ relation.type }} {{ relation }}
2910
{% endmacro %}
3011

3112
{% macro synapse__rename_relation(from_relation, to_relation) -%}

dbt/include/synapse/macros/adapters/replace.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,24 @@
2626
{{ get_create_intermediate_sql(target_relation, sql) }};
2727
{{ get_create_backup_sql(existing_relation) }};
2828
{{ get_rename_intermediate_sql(target_relation) }};
29-
{{ synapse__drop_relation(existing_relation) }}
29+
{% do adapter.drop_relation(existing_relation) %}
3030

3131
{# /* create target_relation as an intermediate relation, then swap it out with the existing one without using a backup */ #}
3232
{%- elif target_relation.can_be_renamed -%}
3333
{{ get_create_intermediate_sql(target_relation, sql) }};
34-
{{ synapse__drop_relation(existing_relation) }};
34+
{% do adapter.drop_relation(existing_relation) %}
3535
{{ get_rename_intermediate_sql(target_relation) }}
3636

3737
{# /* create target_relation in place by first backing up the existing relation */ #}
3838
{%- elif existing_relation.can_be_renamed -%}
3939
{{ get_create_backup_sql(existing_relation) }};
4040
{{ get_create_sql(target_relation, sql) }};
41-
{{ synapse__drop_relation(existing_relation) }}
41+
{% do adapter.drop_relation(existing_relation) %}
4242

4343
{# /* no renaming is allowed, so just drop and create */ #}
4444
{%- else -%}
4545
{{ synapse__drop_relation(existing_relation) }};
46+
{% do adapter.drop_relation(existing_relation) %}
4647
{{ get_create_sql(target_relation, sql) }}
4748

4849
{%- endif -%}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
{% macro synapse__create_schema_with_authorization(relation, schema_authorization) -%}
1111
{% call statement('create_schema') -%}
12+
1213
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
1314
BEGIN
1415
EXEC('CREATE SCHEMA [{{ relation.schema }}] AUTHORIZATION [{{ schema_authorization }}]')
@@ -25,7 +26,7 @@
2526
identifier=row[1],
2627
type=row[3]
2728
) -%}
28-
{% do drop_relation(schema_relation) %}
29+
{% do adapter.drop_relation(schema_relation) %}
2930
{%- endfor %}
3031

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

dbt/include/synapse/macros/materializations/models/materialized_view/materialized_view.sql

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
they return none
3333

3434
-- drop the temp relations if they exist already in the database
35-
{{ synapse__drop_relation(preexisting_backup_relation) }}
36-
{{ synapse__drop_relation(preexisting_intermediate_relation) }}
35+
{% do adapter.drop_relation(preexisting_backup_relation) %}
36+
{% do adapter.drop_relation(preexisting_intermediate_relation) %}
37+
3738

3839
{{ run_hooks(pre_hooks, inside_transaction=False) }}
3940

@@ -43,8 +44,9 @@
4344
{% macro materialized_view_teardown(backup_relation, intermediate_relation, post_hooks) %}
4445

4546
-- drop the temp relations if they exist to leave the database clean for the next run
46-
{{ synapse__drop_relation_script(backup_relation) }}
47-
{{ synapse__drop_relation_script(intermediate_relation) }}
47+
48+
{% do adapter.drop_relation(backup_relation) %}
49+
{% do adapter.drop_relation(intermediate_relation) %}
4850

4951
{{ run_hooks(post_hooks, inside_transaction=False) }}
5052

Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1+
-- Need to check why model contract are not enforced.
2+
-- TODO: Is it because Synapse uses Fabric table materialization and usage of this macro build model constraints?
13
{% macro synapse__create_table_as(temporary, relation, sql) -%}
2-
{%- set index = config.get('index', default="CLUSTERED COLUMNSTORE INDEX") -%}
3-
{%- set dist = config.get('dist', default="ROUND_ROBIN") -%}
4-
{% set tmp_relation = relation.incorporate(
5-
path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'},
6-
type='view')-%}
7-
{%- set temp_view_sql = sql.replace("'", "''") -%}
4+
{%- set index = config.get('index', default="CLUSTERED COLUMNSTORE INDEX") -%}
5+
{%- set dist = config.get('dist', default="ROUND_ROBIN") -%}
6+
{% set tmp_relation = relation.incorporate(path={"identifier": relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
7+
{%- set temp_view_sql = sql.replace("'", "''") -%}
88

9-
{{ synapse__drop_relation_script(tmp_relation) }}
9+
{{ get_create_view_as_sql(tmp_relation, sql) }}
10+
{% set contract_config = config.get('contract') %}
1011

11-
{{ synapse__drop_relation_script(relation) }}
12-
13-
{{ synapse__create_view_as(tmp_relation, sql) }}
14-
15-
{% set contract_config = config.get('contract') %}
16-
17-
{% if contract_config.enforced %}
12+
{% if contract_config.enforced %}
1813

1914
{{exceptions.warn("Model contracts cannot be enforced by <adapter>!")}}
2015

2116
CREATE TABLE [{{relation.schema}}].[{{relation.identifier}}]
2217
{{ synapse__build_columns_constraints(tmp_relation) }}
2318
WITH(
24-
DISTRIBUTION = {{dist}},
25-
{{index}}
19+
DISTRIBUTION = {{dist}},
20+
{{index}}
2621
)
2722
{{ get_assert_columns_equivalent(sql) }}
28-
2923
{% set listColumns %}
3024
{% for column in model['columns'] %}
3125
{{ "["~column~"]" }}{{ ", " if not loop.last }}
@@ -34,11 +28,8 @@
3428

3529
INSERT INTO [{{relation.schema}}].[{{relation.identifier}}]
3630
({{listColumns}}) SELECT {{listColumns}} FROM [{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]
37-
3831
{%- else %}
3932
EXEC('CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}]WITH(DISTRIBUTION = {{dist}},{{index}}) AS (SELECT * FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]);');
4033
{% endif %}
41-
42-
{{ synapse__drop_relation_script(tmp_relation) }}
43-
34+
{% do adapter.drop_relation(tmp_relation)%}
4435
{% endmacro %}

dbt/include/synapse/macros/materializations/models/view/create_view_as.sql

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{% macro synapse__create_view_as(relation, sql) -%}
22

33
{%- set temp_view_sql = sql.replace("'", "''") -%}
4-
54
{% set contract_config = config.get('contract') %}
65

7-
86
{% if contract_config.enforced %}
9-
{{ exceptions.warn("Model contracts cannot be enforced by <adapter>!" }}
7+
{{ exceptions.warn("Model contracts cannot be enforced by <adapter>!") }}
108
{{ get_assert_columns_equivalent(sql) }}
119
{%- endif %}
1210

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{% macro synapse__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
2+
3+
-- Create target schema in synapse db if it does not
4+
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ target.schema }}')
5+
BEGIN
6+
EXEC('CREATE SCHEMA [{{ target.schema }}]')
7+
END
8+
9+
{% if main_sql.strip().lower().startswith('with') %}
10+
{% set testview %}
11+
{{ target.schema }}.testview_{{ range(1300, 19000) | random }}
12+
{% endset %}
13+
14+
{% set sql = main_sql.replace("'", "''")%}
15+
EXEC('create view {{testview}} as {{ sql }};')
16+
select
17+
{{ "top (" ~ limit ~ ')' if limit != none }}
18+
{{ fail_calc }} as failures,
19+
case when {{ fail_calc }} {{ warn_if }}
20+
then 'true' else 'false' end as should_warn,
21+
case when {{ fail_calc }} {{ error_if }}
22+
then 'true' else 'false' end as should_error
23+
from (
24+
select * from {{testview}}
25+
) dbt_internal_test;
26+
27+
EXEC('drop view {{testview}};')
28+
29+
{% else -%}
30+
select
31+
{{ "top (" ~ limit ~ ')' if limit != none }}
32+
{{ fail_calc }} as failures,
33+
case when {{ fail_calc }} {{ warn_if }}
34+
then 'true' else 'false' end as should_warn,
35+
case when {{ fail_calc }} {{ error_if }}
36+
then 'true' else 'false' end as should_error
37+
from (
38+
{{ main_sql }}
39+
) dbt_internal_test
40+
{%- endif -%}
41+
{%- endmacro %}

dev_requirements.txt

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# install latest changes in dbt-core
22
# TODO: how to automate switching from develop to version branches?
3-
git+https://github.com/dbt-labs/dbt-core.git@fc431010ef0bd11ee6a502fc6c9e5e3e75c5d72d#egg=dbt-core&subdirectory=core
4-
git+https://github.com/dbt-labs/dbt-adapters.git@4c289b150853b94beb67921f2a8dd203abe53cbe
5-
git+https://github.com/dbt-labs/dbt-adapters.git@4c289b150853b94beb67921f2a8dd203abe53cbe#subdirectory=dbt-tests-adapter
3+
git+https://github.com/dbt-labs/[email protected]#egg=dbt-core&subdirectory=core
4+
git+https://github.com/dbt-labs/dbt-adapters.git
5+
git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter
6+
git+https://github.com/dbt-labs/dbt-common.git
67

78
pytest==8.0.1
89
twine==5.0.0
@@ -11,7 +12,8 @@ pre-commit==3.5.0;python_version<"3.9"
1112
pre-commit==3.6.2;python_version>="3.9"
1213
pytest-dotenv==0.5.2
1314
aiohttp==3.8.3
14-
azure-mgmt-synapse==2.0.0
15+
#azure-mgmt-synapse==2.0.0
1516
flaky==3.7.0
1617
pytest-xdist==3.5.0
1718
-e .
19+
#-e /mnt/c/users/pvenkat/repos/dbt-fabric

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"Sam Debruyn",
1717
]
1818
dbt_version = "1.8"
19-
dbt_fabric_requirement = "dbt-fabric==1.8.2"
19+
dbt_fabric_requirement = "dbt-fabric==1.8.6"
2020
description = """An Azure Synapse adapter plugin for dbt"""
2121

2222
this_directory = os.path.abspath(os.path.dirname(__file__))

tests/functional/adapter/test_constraints.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -648,29 +648,34 @@ def models(self):
648648
@pytest.fixture(scope="class")
649649
def expected_sql(self):
650650
return """
651-
if object_id <model_identifier> is not null begin drop view <model_identifier> end
652-
if object_id <model_identifier> is not null begin drop table <model_identifier> end
653-
exec(\'create view <model_identifier> as select \'\'blue\'\' as "from",1 as id,\'\'2019-01-01\'\' as date_day;\');
654-
create table <model_identifier>([id] int not null,[from] varchar(100)not null,[date_day] varchar(100))
655-
with(distribution = round_robin,heap)
656-
insert into <model_identifier>([id],[from],[date_day])
657-
select [id],[from],[date_day] from <model_identifier>
658-
if object_id <model_identifier> is not null begin drop view <model_identifier> end
651+
exec(\'create view <model_identifier> as select \'\'blue\'\' as "from",1 as id,\'\'2019-01-01\'\' as date_day;\'); create table <model_identifier>([id] int not null,[from] varchar(100)not null,[date_day] varchar(100))with(distribution = round_robin,heap)insert into <model_identifier>([id],[from],[date_day])select [id],[from],[date_day] from <model_identifier>
659652
"""
660653

661654

662655
class TestTableConstraintsRuntimeDdlEnforcementSynapse(BaseConstraintsRuntimeDdlEnforcement):
663-
pass
656+
@pytest.fixture(scope="class")
657+
def expected_sql(self):
658+
return """
659+
exec('create view <model_identifier> as -- depends_on: <foreign_key_model_identifier> select ''blue'' as color,1 as id,''2019-01-01'' as date_day;'); create table <model_identifier>([id] int not null,[color] varchar(100),[date_day] varchar(100))with(distribution = round_robin,heap)insert into <model_identifier>([id],[color],[date_day])select [id],[color],[date_day] from <model_identifier>
660+
"""
664661

665662

666663
class TestIncrementalConstraintsRuntimeDdlEnforcementSynapse(
667664
BaseIncrementalConstraintsRuntimeDdlEnforcement
668665
):
669-
pass
666+
@pytest.fixture(scope="class")
667+
def expected_sql(self):
668+
return """
669+
exec('create view <model_identifier> as -- depends_on: <foreign_key_model_identifier> select ''blue'' as color,1 as id,''2019-01-01'' as date_day;'); create table <model_identifier>([id] int not null,[color] varchar(100),[date_day] varchar(100))with(distribution = round_robin,heap)insert into <model_identifier>([id],[color],[date_day])select [id],[color],[date_day] from <model_identifier>
670+
"""
670671

671672

672673
class TestModelConstraintsRuntimeEnforcementSynapse(BaseModelConstraintsRuntimeEnforcement):
673-
pass
674+
@pytest.fixture(scope="class")
675+
def expected_sql(self):
676+
return """
677+
exec('create view <model_identifier> as -- depends_on: <foreign_key_model_identifier> select ''blue'' as color,1 as id,''2019-01-01'' as date_day;'); create table <model_identifier>([id] int not null,[color] varchar(100),[date_day] varchar(100))with(distribution = round_robin,heap)insert into <model_identifier>([id],[color],[date_day])select [id],[color],[date_day] from <model_identifier>
678+
"""
674679

675680

676681
class TestTableConstraintsColumnsEqualSynapse(BaseTableConstraintsColumnsEqual):

0 commit comments

Comments
 (0)