From 19ff404636af93484910e702930cff70adb4134c Mon Sep 17 00:00:00 2001 From: Pradeep Srikakolapu Date: Wed, 27 Nov 2024 19:12:34 -0800 Subject: [PATCH] Addressing #243, #221, #228, #229, #232, #235 issues --- .../fabric/macros/adapters/catalog.sql | 4 +- dbt/include/fabric/macros/adapters/show.sql | 22 ++++---- .../materializations/snapshots/helpers.sql | 54 ++----------------- .../materializations/snapshots/snapshot.sql | 9 ++-- 4 files changed, 21 insertions(+), 68 deletions(-) diff --git a/dbt/include/fabric/macros/adapters/catalog.sql b/dbt/include/fabric/macros/adapters/catalog.sql index 6bd1398..555b5a7 100644 --- a/dbt/include/fabric/macros/adapters/catalog.sql +++ b/dbt/include/fabric/macros/adapters/catalog.sql @@ -96,7 +96,7 @@ c.column_id as column_index, t.name as column_type from sys.columns as c {{ information_schema_hints() }} - left join sys.types as t on c.system_type_id = t.system_type_id {{ information_schema_hints() }} + left join sys.types as t on c.system_type_id = t.system_type_id ) select @@ -223,7 +223,7 @@ c.column_id as column_index, t.name as column_type from sys.columns as c {{ information_schema_hints() }} - left join sys.types as t on c.system_type_id = t.system_type_id {{ information_schema_hints() }} + left join sys.types as t on c.system_type_id = t.system_type_id ) select diff --git a/dbt/include/fabric/macros/adapters/show.sql b/dbt/include/fabric/macros/adapters/show.sql index 7b3340e..d6e1e3d 100644 --- a/dbt/include/fabric/macros/adapters/show.sql +++ b/dbt/include/fabric/macros/adapters/show.sql @@ -1,16 +1,12 @@ {% macro fabric__get_limit_sql(sql, limit) %} - - {% if limit == -1 or limit is none %} - with model_limit_subq as ( - {{ sql }} - ) - select * - from model_limit_subq; - {% else -%} - with model_limit_subq as ( - {{ sql }} - ) - select top {{ limit }} * - from model_limit_subq; + {%- if limit == -1 or limit is none -%} + {{ sql }} + {#- Special processing if the last non-blank line starts with order by -#} + {%- elif 'order by' in sql.strip().splitlines()[-1].strip().lower() -%} + {{ sql }} + offset 0 rows fetch first {{ limit }} rows only + {%- else -%} + {{ sql }} + order by (select null) offset 0 rows fetch first {{ limit }} rows only {%- endif -%} {% endmacro %} diff --git a/dbt/include/fabric/macros/materializations/snapshots/helpers.sql b/dbt/include/fabric/macros/materializations/snapshots/helpers.sql index 29d4934..d5915e4 100644 --- a/dbt/include/fabric/macros/materializations/snapshots/helpers.sql +++ b/dbt/include/fabric/macros/materializations/snapshots/helpers.sql @@ -3,56 +3,12 @@ {% do drop_relation_if_exists(staging_relation) %} {% endmacro %} ---Due to Alter not being supported, have to rely on this for temporarily {% macro fabric__create_columns(relation, columns) %} - {# default__ macro uses "add column" - TSQL preferes just "add" - #} - - {% set columns %} - {% for column in columns %} - , CAST(NULL AS {{column.data_type}}) AS {{column.name}} - {% endfor %} - {% endset %} - - {% set tempTableName %} - [{{relation.database}}].[{{ relation.schema }}].[{{ relation.identifier }}_{{ range(1300, 19000) | random }}] - {% endset %} - {{ log("Creating new columns are not supported without dropping a table. Using random table as a temp table. - " ~ tempTableName) }} - - {% set tempTable %} - CREATE TABLE {{tempTableName}} - AS SELECT * {{columns}} FROM [{{relation.database}}].[{{ relation.schema }}].[{{ relation.identifier }}] {{ information_schema_hints() }} {{ apply_label() }} - {% endset %} - - {% call statement('create_temp_table') -%} - {{ tempTable }} - {%- endcall %} - - {% set dropTable %} - DROP TABLE [{{relation.database}}].[{{ relation.schema }}].[{{ relation.identifier }}] - {% endset %} - - {% call statement('drop_table') -%} - {{ dropTable }} - {%- endcall %} - - {% set createTable %} - CREATE TABLE {{ relation }} - AS SELECT * FROM {{tempTableName}} {{ information_schema_hints() }} {{ apply_label() }} - {% endset %} - - {% call statement('create_Table') -%} - {{ createTable }} - {%- endcall %} - - {% set dropTempTable %} - DROP TABLE {{tempTableName}} - {% endset %} - - {% call statement('drop_temp_table') -%} - {{ dropTempTable }} - {%- endcall %} + {% for column in columns %} + {% call statement() %} + alter table {{ relation.render() }} add "{{ column.name }}" {{ column.data_type }} NULL; + {% endcall %} + {% endfor %} {% endmacro %} {% macro fabric__get_true_sql() %} diff --git a/dbt/include/fabric/macros/materializations/snapshots/snapshot.sql b/dbt/include/fabric/macros/materializations/snapshots/snapshot.sql index 07c6789..b10ad03 100644 --- a/dbt/include/fabric/macros/materializations/snapshots/snapshot.sql +++ b/dbt/include/fabric/macros/materializations/snapshots/snapshot.sql @@ -31,10 +31,11 @@ -- Create a temporary view to manage if user SQl uses CTE {% set temp_snapshot_relation_sql = model['compiled_code'].replace("'", "''") %} - {% call statement('create temp_snapshot_relation') %} - EXEC('DROP VIEW IF EXISTS {{ temp_snapshot_relation.include(database=False) }};'); - EXEC('create view {{ temp_snapshot_relation.include(database=False) }} as {{ temp_snapshot_relation_sql }};'); - {% endcall %} + + {% call statement('create temp_snapshot_relation') -%} + {{ adapter.drop_relation(temp_snapshot_relation) }} + {{ get_create_view_as_sql(temp_snapshot_relation, temp_snapshot_relation_sql) }} + {%- endcall %} {% if not target_relation_exists %}