Skip to content

Commit 87deee1

Browse files
authored
Merge pull request #262 from microsoft/v1.9.1
Addressing issues - #260, #258, #257, #256, #254, #250, #239 issues
2 parents ed77d7c + 648f3e2 commit 87deee1

File tree

10 files changed

+92
-118
lines changed

10 files changed

+92
-118
lines changed

dbt/adapters/fabric/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "1.9.0"
1+
version = "1.9.1"

dbt/adapters/fabric/relation_configs/policies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class FabricRelationType(StrEnum):
88
Table = "table"
99
View = "view"
10+
Ephemeral = "ephemeral"
1011
CTE = "cte"
1112

1213

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
{% macro fabric__get_empty_subquery_sql(select_sql, select_sql_header=none) %}
2-
{% if select_sql.strip().lower().startswith('with') %}
3-
{{ select_sql }}
4-
{% else -%}
5-
select * from (
2+
3+
with __dbt_sbq_tmp as (
64
{{ select_sql }}
7-
) dbt_sbq_tmp
8-
where 1 = 0
9-
{%- endif -%}
5+
)
6+
select * from __dbt_sbq_tmp
7+
where 0 = 1
108

119
{% endmacro %}
1210

1311
{% macro fabric__get_columns_in_relation(relation) -%}
1412
{% set query_label = apply_label() %}
1513
{% call statement('get_columns_in_relation', fetch_result=True) %}
16-
14+
{{ get_use_database_sql(relation.database) }}
1715
with mapping as (
1816
select
1917
row_number() over (partition by object_name(c.object_id) order by c.column_id) as ordinal_position,
@@ -22,7 +20,7 @@
2220
c.max_length as character_maximum_length,
2321
c.precision as numeric_precision,
2422
c.scale as numeric_scale
25-
from [{{ 'tempdb' if '#' in relation.identifier else relation.database }}].sys.columns c {{ information_schema_hints() }}
23+
from sys.columns c {{ information_schema_hints() }}
2624
inner join sys.types t {{ information_schema_hints() }}
2725
on c.user_type_id = t.user_type_id
2826
where c.object_id = object_id('{{ 'tempdb..' ~ relation.include(database=false, schema=false) if '#' in relation.identifier else relation }}')
@@ -46,11 +44,16 @@
4644
{% macro fabric__get_columns_in_query(select_sql) %}
4745
{% set query_label = apply_label() %}
4846
{% call statement('get_columns_in_query', fetch_result=True, auto_begin=False) -%}
49-
select TOP 0 * from (
47+
48+
with __dbt_sbq as
49+
(
5050
{{ select_sql }}
51-
) as __dbt_sbq
51+
)
52+
select top 0 *
53+
from __dbt_sbq
5254
where 0 = 1
5355
{{ query_label }}
56+
5457
{% endcall %}
5558

5659
{{ return(load_result('get_columns_in_query').table.columns | map(attribute='name') | list) }}
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
{% macro fabric__create_table_as(temporary, relation, sql) -%}
2-
{% set query_label = apply_label() %}
3-
{% set tmp_vw_relation = relation.incorporate(path={"identifier": relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
4-
{% do adapter.drop_relation(tmp_vw_relation) %}
5-
{{ get_create_view_as_sql(tmp_vw_relation, sql) }}
62

3+
{% set query_label = apply_label() %}
74
{% set contract_config = config.get('contract') %}
8-
{% if contract_config.enforced %}
5+
6+
{% if sql.strip().lower().startswith('with') and contract_config.enforced %}
7+
8+
{{ exceptions.raise_compiler_error(
9+
"As contract is enforced and the model is using CTE, INSERT INTO is not supported with CTE. Either do not enforce contract or change the model"
10+
) }}
11+
12+
{%- elif not sql.strip().lower().startswith('with') and contract_config.enforced %}
913

1014
CREATE TABLE {{relation}}
1115
{{ build_columns_constraints(relation) }}
1216
{{ get_assert_columns_equivalent(sql) }}
17+
1318
{% set listColumns %}
1419
{% for column in model['columns'] %}
1520
{{ "["~column~"]" }}{{ ", " if not loop.last }}
1621
{% endfor %}
1722
{%endset%}
1823

24+
{% set tmp_vw_relation = relation.incorporate(path={"identifier": relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%}
25+
{% do adapter.drop_relation(tmp_vw_relation) %}
26+
{{ get_create_view_as_sql(tmp_vw_relation, sql) }}
27+
1928
INSERT INTO {{relation}} ({{listColumns}})
2029
SELECT {{listColumns}} FROM {{tmp_vw_relation}} {{ query_label }}
2130

2231
{%- else %}
32+
2333
{%- set query_label_option = query_label.replace("'", "''") -%}
24-
EXEC('CREATE TABLE {{relation}} AS SELECT * FROM {{tmp_vw_relation}} {{ query_label_option }}');
34+
{%- set sql_with_quotes = sql.replace("'", "''") -%}
35+
EXEC('CREATE TABLE {{relation}} AS {{sql_with_quotes}} {{ query_label_option }}');
36+
2537
{% endif %}
2638
{% endmacro %}

dbt/include/fabric/macros/materializations/snapshots/snapshot.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
type='view') -%}
3131

3232
-- Create a temporary view to manage if user SQl uses CTE
33-
{% set temp_snapshot_relation_sql = model['compiled_code'].replace("'", "''") %}
33+
{% set temp_snapshot_relation_sql = model['compiled_code'] %}
3434
{{ adapter.drop_relation(temp_snapshot_relation) }}
3535

3636
{% call statement('create temp_snapshot_relation') -%}

dbt/include/fabric/macros/materializations/tests/helpers.sql

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,59 @@
1-
{% macro get_fabric_test_sql(database, schema, main_sql, fail_calc, warn_if, error_if, limit) -%}
2-
{{ log ("local_md5(model.name): "~ local_md5(model.name))}}
3-
{{ log ("local_md5(invocation_id): "~ local_md5(invocation_id))}}
4-
{% set testview %}
1+
{% macro fabric__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
52

6-
[{{ schema }}.testview_{{ local_md5(model.name ~ invocation_id) }}]
7-
{% endset %}
8-
9-
{% set sql = main_sql.replace("'", "''")%}
10-
{{ get_use_database_sql(database) }}
11-
EXEC('create view {{testview}} as {{ sql }};')
3+
with test_main_sql as (
4+
{{ main_sql }}
5+
),
6+
dbt_internal_test as (
7+
select {{ "top (" ~ limit ~ ')' if limit != none }} * from test_main_sql
8+
)
129
select
1310
{{ fail_calc }} as failures,
1411
case when {{ fail_calc }} {{ warn_if }}
1512
then 'true' else 'false' end as should_warn,
1613
case when {{ fail_calc }} {{ error_if }}
1714
then 'true' else 'false' end as should_error
18-
from (
19-
select {{ "top (" ~ limit ~ ')' if limit != none }} * from {{testview}}
20-
) dbt_internal_test;
15+
from dbt_internal_test
16+
17+
{%- endmacro %}
18+
19+
20+
{% macro fabric__get_unit_test_sql(main_sql, expected_fixture_sql, expected_column_names) -%}
21+
-- Build actual result given inputs
22+
WITH dbt_internal_unit_test_actual AS (
23+
24+
WITH main_sql AS (
25+
{{ main_sql }}
26+
)
27+
SELECT
28+
{% for expected_column_name in expected_column_names %}
29+
{{ expected_column_name }}{% if not loop.last -%},{% endif %}
30+
{%- endfor -%},
31+
{{ dbt.string_literal("actual") }} AS {{ adapter.quote("actual_or_expected") }}
32+
FROM main_sql
33+
),
34+
35+
-- Build expected result
36+
dbt_internal_unit_test_expected AS (
37+
38+
WITH expected_fixture_sql AS (
39+
{{ expected_fixture_sql }}
40+
)
41+
SELECT
42+
{% for expected_column_name in expected_column_names %}
43+
{{ expected_column_name }}{% if not loop.last -%}, {% endif %}
44+
{%- endfor -%},
45+
{{ dbt.string_literal("expected") }} AS {{ adapter.quote("actual_or_expected") }}
46+
FROM expected_fixture_sql
47+
)
48+
49+
-- Union actual and expected results
50+
SELECT * FROM dbt_internal_unit_test_actual
51+
UNION ALL
52+
SELECT * FROM dbt_internal_unit_test_expected
2153

22-
{{ get_use_database_sql(database) }}
23-
EXEC('drop view {{testview}};')
2454
{%- endmacro %}
2555

56+
2657
{% macro fabric__generate_schema_name(custom_schema_name, node) -%}
2758

2859
{%- set default_schema = target.schema -%}

dbt/include/fabric/macros/materializations/tests/test.sql

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% macro fabric__split_part(string_text, delimiter_text, part_number) %}
2+
{% if part_number >= 0 %}
3+
(select value from string_split({{ string_text }}, {{ delimiter_text }}, 1) where ordinal = {{ part_number }})
4+
{% else %}
5+
(select value from string_split({{ string_text }}, {{ delimiter_text }}, 1)
6+
where ordinal = len(replace({{ string_text }}, {{delimiter_text}}, '')) + 1 + {{ part_number }})
7+
{% endif %}
8+
{% endmacro %}

dev_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# install latest changes in dbt-core
22
# TODO: how to automate switching from develop to version branches?
33
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core
4-
git+https://github.com/dbt-labs/dbt-adapters.git
4+
git+https://github.com/dbt-labs/dbt-adapters.git#egg=dbt-adapters&subdirectory=dbt-adapters
55
git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter
66

77
pytest==8.0.1

tests/functional/adapter/test_constraints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def models(self):
480480
@pytest.fixture(scope="class")
481481
def expected_sql(self):
482482
return """
483-
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) ) INSERT INTO <model_identifier> ( [id], [color], [date_day] ) SELECT [id], [color], [date_day] FROM <model_identifier>
483+
create table <model_identifier>(id int not null,color varchar(100),date_day varchar(100))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;'); insert into <model_identifier>([id],[color],[date_day])select [id],[color],[date_day] from <model_identifier>
484484
"""
485485

486486
# EXEC('DROP view IF EXISTS <model_identifier>
@@ -549,7 +549,7 @@ def models(self):
549549
@pytest.fixture(scope="class")
550550
def expected_sql(self):
551551
return """
552-
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) ) INSERT INTO <model_identifier> ( [id], [color], [date_day] ) SELECT [id], [color], [date_day] FROM <model_identifier>
552+
create table <model_identifier>(id int not null,color varchar(100),date_day varchar(100))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;'); insert into <model_identifier>([id],[color],[date_day])select [id],[color],[date_day] from <model_identifier>
553553
"""
554554

555555
# EXEC('DROP view IF EXISTS <model_identifier>

0 commit comments

Comments
 (0)