Skip to content

Commit

Permalink
Addressing #243, #221, #228, #229, #232, #235 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
prdpsvs committed Nov 28, 2024
1 parent 9c85649 commit 19ff404
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 68 deletions.
4 changes: 2 additions & 2 deletions dbt/include/fabric/macros/adapters/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions dbt/include/fabric/macros/adapters/show.sql
Original file line number Diff line number Diff line change
@@ -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 %}
54 changes: 5 additions & 49 deletions dbt/include/fabric/macros/materializations/snapshots/helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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() %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down

0 comments on commit 19ff404

Please sign in to comment.