-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from microsoft/dbtsynapse1.7_dataroots
v1.7.0rc1
- Loading branch information
Showing
25 changed files
with
1,392 additions
and
20,048 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version = "1.6.0rc1" | ||
version = "1.7.0rc1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
{% macro synapse__get_catalog_tables_sql(information_schemas) -%} | ||
-- avoid with statement to be able to wrap into CTE (limitation of Synapse) | ||
|
||
SELECT | ||
table_name, | ||
table_schema, | ||
principal_name, | ||
table_type | ||
FROM ( | ||
SELECT | ||
table_name, | ||
schema_name AS table_schema, | ||
COALESCE(relations.principal_id, schemas.principal_id) AS owner_principal_id, | ||
table_type | ||
FROM ( | ||
SELECT | ||
name AS table_name, | ||
schema_id AS schema_id, | ||
principal_id AS principal_id, | ||
'BASE TABLE' AS table_type | ||
FROM | ||
sys.tables {{ information_schema_hints() }} | ||
UNION ALL | ||
SELECT | ||
name AS table_name, | ||
schema_id AS schema_id, | ||
principal_id AS principal_id, | ||
'VIEW' AS table_type | ||
FROM | ||
sys.views {{ information_schema_hints() }} | ||
) AS relations | ||
JOIN ( | ||
SELECT | ||
name AS schema_name, | ||
schema_id AS schema_id, | ||
principal_id AS principal_id | ||
FROM | ||
sys.schemas {{ information_schema_hints() }} | ||
) AS schemas ON relations.schema_id = schemas.schema_id | ||
) AS relations_with_metadata | ||
JOIN ( | ||
SELECT | ||
name AS principal_name, | ||
principal_id AS principal_id | ||
FROM | ||
sys.database_principals {{ information_schema_hints() }} | ||
) AS principals ON relations_with_metadata.owner_principal_id = principals.principal_id | ||
|
||
{% endmacro %} | ||
|
||
{% macro synapse__get_catalog_columns_sql(information_schemas) -%} | ||
|
||
select | ||
table_catalog as table_database, | ||
table_schema, | ||
table_name, | ||
column_name, | ||
ordinal_position as column_index, | ||
data_type as column_type | ||
from INFORMATION_SCHEMA.COLUMNS {{ information_schema_hints() }} | ||
|
||
{% endmacro %} | ||
|
||
{% macro synapse__get_catalog_schemas_where_clause_sql(schemas) -%} | ||
where ({%- for schema in schemas -%} | ||
upper(table_schema) = upper('{{ schema }}'){%- if not loop.last %} or {% endif -%} | ||
{%- endfor -%}) | ||
{%- endmacro %} | ||
|
||
{% macro synapse__get_catalog_relations_where_clause_sql(relations) %} | ||
|
||
where ( | ||
{%- for relation in relations -%} | ||
{% if relation.schema and relation.identifier %} | ||
( | ||
upper(table_schema) = upper('{{ relation.schema }}') | ||
and upper(table_name) = upper('{{ relation.identifier }}') | ||
) | ||
{% elif relation.schema %} | ||
( | ||
upper(table_schema) = upper('{{ relation.schema }}') | ||
) | ||
{% else %} | ||
{% do exceptions.raise_compiler_error( | ||
'`get_catalog_relations` requires a list of relations, each with a schema' | ||
) %} | ||
{% endif %} | ||
|
||
{%- if not loop.last %} or {% endif -%} | ||
{%- endfor -%} | ||
) | ||
|
||
{% endmacro %} | ||
|
||
{% macro synapse__get_catalog_results_sql() -%} | ||
select | ||
cols.table_database, | ||
tv.table_schema, | ||
tv.table_name, | ||
tv.table_type, | ||
null as table_comment, | ||
tv.principal_name as table_owner, | ||
cols.column_name, | ||
cols.column_index, | ||
cols.column_type, | ||
null as column_comment | ||
from tables as tv | ||
join columns as cols on tv.table_schema = cols.table_schema and tv.table_name = cols.table_name | ||
order by cols.column_index | ||
{%- endmacro %} | ||
|
||
-- combine everything into the get_catalog_(relations) macro | ||
{% macro synapse__get_catalog(information_schema, schemas) -%} | ||
|
||
{% set query %} | ||
with tables as ( | ||
{{ synapse__get_catalog_tables_sql(information_schema) }} | ||
{{ synapse__get_catalog_schemas_where_clause_sql(schemas) }} | ||
), | ||
columns as ( | ||
{{ synapse__get_catalog_columns_sql(information_schema) }} | ||
{{ synapse__get_catalog_schemas_where_clause_sql(schemas) }} | ||
) | ||
{{ synapse__get_catalog_results_sql() }} | ||
{%- endset -%} | ||
|
||
{{ return(run_query(query)) }} | ||
|
||
{%- endmacro %} | ||
|
||
{% macro synapse__get_catalog_relations(information_schema, relations) -%} | ||
|
||
{% set query %} | ||
with tables as ( | ||
{{ synapse__get_catalog_tables_sql(information_schema) }} | ||
{{ synapse__get_catalog_relations_where_clause_sql(relations) }} | ||
), | ||
columns as ( | ||
{{ synapse__get_catalog_columns_sql(information_schema) }} | ||
{{ synapse__get_catalog_relations_where_clause_sql(relations) }} | ||
) | ||
{{ synapse__get_catalog_results_sql() }} | ||
{%- endset -%} | ||
|
||
{{ return(run_query(query)) }} | ||
|
||
{%- endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{% macro get_replace_sql(existing_relation, target_relation, sql) %} | ||
{{- log('Applying REPLACE to: ' ~ existing_relation) -}} | ||
{{- adapter.dispatch('get_replace_sql', 'synapse')(existing_relation, target_relation, sql) -}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro synapse__get_replace_sql(existing_relation, target_relation, sql) %} | ||
|
||
{# /* use a create or replace statement if possible */ #} | ||
|
||
{% set is_replaceable = existing_relation.type == target_relation_type and existing_relation.can_be_replaced %} | ||
|
||
{% if is_replaceable and existing_relation.is_view %} | ||
{{ get_replace_view_sql(target_relation, sql) }} | ||
|
||
{% elif is_replaceable and existing_relation.is_table %} | ||
{{ get_replace_table_sql(target_relation, sql) }} | ||
|
||
{% elif is_replaceable and existing_relation.is_materialized_view %} | ||
{{ get_replace_materialized_view_sql(target_relation, sql) }} | ||
|
||
{# /* a create or replace statement is not possible, so try to stage and/or backup to be safe */ #} | ||
|
||
{# /* create target_relation as an intermediate relation, then swap it out with the existing one using a backup */ #} | ||
{%- elif target_relation.can_be_renamed and existing_relation.can_be_renamed -%} | ||
{{ get_create_intermediate_sql(target_relation, sql) }}; | ||
{{ get_create_backup_sql(existing_relation) }}; | ||
{{ get_rename_intermediate_sql(target_relation) }}; | ||
{{ synapse__drop_relation(existing_relation) }} | ||
|
||
{# /* create target_relation as an intermediate relation, then swap it out with the existing one without using a backup */ #} | ||
{%- elif target_relation.can_be_renamed -%} | ||
{{ get_create_intermediate_sql(target_relation, sql) }}; | ||
{{ synapse__drop_relation(existing_relation) }}; | ||
{{ get_rename_intermediate_sql(target_relation) }} | ||
|
||
{# /* create target_relation in place by first backing up the existing relation */ #} | ||
{%- elif existing_relation.can_be_renamed -%} | ||
{{ get_create_backup_sql(existing_relation) }}; | ||
{{ get_create_sql(target_relation, sql) }}; | ||
{{ synapse__drop_relation(existing_relation) }} | ||
|
||
{# /* no renaming is allowed, so just drop and create */ #} | ||
{%- else -%} | ||
{{ synapse__drop_relation(existing_relation) }}; | ||
{{ get_create_sql(target_relation, sql) }} | ||
|
||
{%- endif -%} | ||
|
||
{% endmacro %} |
30 changes: 30 additions & 0 deletions
30
.../synapse/macros/materializations/models/materialized_view/create_materialized_view_as.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{% macro ref(model_name) %} | ||
|
||
{% do return(builtins.ref(model_name).include(database=false)) %} | ||
|
||
{% endmacro %} | ||
|
||
|
||
{% macro synapse__get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) %} | ||
{# Synapse does not have ALTER...RENAME function, so use synapse__rename_relation_script #} | ||
|
||
{%- set dist = config.get('dist', default="ROUND_ROBIN") -%} | ||
EXEC(' | ||
CREATE materialized view [{{intermediate_relation.schema}}].[{{intermediate_relation.identifier}}] | ||
WITH ( DISTRIBUTION = {{dist}} ) | ||
AS {{ sql }} | ||
'); | ||
|
||
{{ synapse__rename_relation_script(existing_relation, backup_relation) }} | ||
{{ synapse__rename_relation_script(intermediate_relation, relation) }} | ||
|
||
{% endmacro %} | ||
|
||
{% macro synapse__get_create_materialized_view_as_sql(relation, sql) %} | ||
{%- set dist = config.get('dist', default="ROUND_ROBIN") -%} | ||
|
||
CREATE materialized view [{{relation.schema}}].[{{relation.identifier}}] | ||
WITH ( DISTRIBUTION = {{dist}} ) | ||
AS {{ sql }} | ||
|
||
{% endmacro %} |
Oops, something went wrong.