-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring to align with dbt-core organization: Part I (#525)
- Loading branch information
Showing
16 changed files
with
141 additions
and
120 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
9 changes: 0 additions & 9 deletions
9
...lude/databricks/macros/materializations/materialized_view/create_materialized_view_as.sql
This file was deleted.
Oops, something went wrong.
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,46 @@ | ||
{% macro databricks__alter_column_comment(relation, column_dict) %} | ||
{% if config.get('file_format', default='delta') in ['delta', 'hudi'] %} | ||
{% for column_name in column_dict %} | ||
{% set comment = column_dict[column_name]['description'] %} | ||
{% set escaped_comment = comment | replace('\'', '\\\'') %} | ||
{% set comment_query %} | ||
alter table {{ relation }} change column | ||
{{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }} | ||
comment '{{ escaped_comment }}'; | ||
{% endset %} | ||
{% do run_query(comment_query) %} | ||
{% endfor %} | ||
{% endif %} | ||
{% endmacro %} | ||
|
||
{% macro databricks__persist_docs(relation, model, for_relation, for_columns) -%} | ||
{% if for_columns and config.persist_column_docs() and model.columns %} | ||
{%- set existing_columns = adapter.get_columns_in_relation(relation) -%} | ||
{%- set columns_to_persist_docs = adapter.get_persist_doc_columns(existing_columns, model.columns) -%} | ||
{% do alter_column_comment(relation, columns_to_persist_docs) %} | ||
{% endif %} | ||
{% endmacro %} | ||
|
||
|
||
{% macro get_columns_comments(relation) -%} | ||
{% call statement('get_columns_comments', fetch_result=True) -%} | ||
describe table {{ relation }} | ||
{% endcall %} | ||
|
||
{% do return(load_result('get_columns_comments').table) %} | ||
{% endmacro %} | ||
|
||
{% macro get_column_comment_sql(column_name, column_dict) -%} | ||
{% if column_name in column_dict and column_dict[column_name]["description"] -%} | ||
{% set escaped_description = column_dict[column_name]["description"] | replace("'", "\\'") %} | ||
{% set column_comment_clause = "comment '" ~ escaped_description ~ "'" %} | ||
{%- endif -%} | ||
{{ adapter.quote(column_name) }} {{ column_comment_clause }} | ||
{% endmacro %} | ||
|
||
{% macro get_persist_docs_column_list(model_columns, query_columns) %} | ||
{% for column_name in query_columns %} | ||
{{ get_column_comment_sql(column_name, model_columns) }} | ||
{{- ", " if not loop.last else "" }} | ||
{% endfor %} | ||
{% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{% macro databricks__get_drop_sql(relation) -%} | ||
{%- if relation.is_materialized_view -%} | ||
{{ drop_materialized_view(relation) }} | ||
{%- elif relation.is_streaming_table-%} | ||
{{ drop_streaming_table(relation) }} | ||
{%- elif relation.is_view -%} | ||
{{ drop_view(relation) }} | ||
{%- else -%} | ||
{{ drop_table(relation) }} | ||
{%- endif -%} | ||
{% endmacro %} |
5 changes: 5 additions & 0 deletions
5
dbt/include/databricks/macros/relations/materialized_view/create.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,5 @@ | ||
{% macro databricks__get_create_materialized_view_as_sql(relation, sql) -%} | ||
create materialized view {{ relation }} | ||
as | ||
{{ sql }} | ||
{% endmacro %} |
3 changes: 3 additions & 0 deletions
3
dbt/include/databricks/macros/relations/materialized_view/drop.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,3 @@ | ||
{% macro databricks__drop_materialized_view(relation) -%} | ||
drop materialized view if exists {{ relation }} | ||
{%- endmacro %} |
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
dbt/include/databricks/macros/relations/streaming_table/create.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,9 @@ | ||
{% macro get_create_streaming_table_as_sql(relation, sql) -%} | ||
{{ adapter.dispatch('get_create_streaming_table_as_sql', 'dbt')(relation, sql) }} | ||
{%- endmacro %} | ||
|
||
{% macro databricks__get_create_streaming_table_as_sql(relation, sql) -%} | ||
create streaming table {{ relation }} | ||
as | ||
{{ sql }} | ||
{% endmacro %} |
7 changes: 7 additions & 0 deletions
7
dbt/include/databricks/macros/relations/streaming_table/drop.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,7 @@ | ||
{% macro drop_streaming_table(relation) -%} | ||
{{ return(adapter.dispatch('drop_streaming_table', 'dbt')(relation)) }} | ||
{%- endmacro %} | ||
|
||
{% macro default__drop_streaming_table(relation) -%} | ||
drop table if exists {{ relation }} | ||
{%- endmacro %} |
10 changes: 0 additions & 10 deletions
10
.../create_or_refresh_streaming_table_as.sql → ...ros/relations/streaming_table/refresh.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
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,3 @@ | ||
{% macro databricks__drop_table(relation) -%} | ||
drop table if exists {{ relation }} | ||
{%- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% macro databricks__create_view_as(relation, sql) -%} | ||
create or replace view {{ relation }} | ||
{% if config.persist_column_docs() -%} | ||
{% set model_columns = model.columns %} | ||
{% set query_columns = get_columns_in_query(sql) %} | ||
{% if query_columns %} | ||
( | ||
{{ get_persist_docs_column_list(model_columns, query_columns) }} | ||
) | ||
{% endif %} | ||
{% endif %} | ||
{{ comment_clause() }} | ||
{%- set contract_config = config.get('contract') -%} | ||
{% if contract_config and contract_config.enforced %} | ||
{{ get_assert_columns_equivalent(sql) }} | ||
{%- endif %} | ||
{{ tblproperties_clause() }} | ||
as | ||
{{ sql }} | ||
{% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro databricks__drop_view(relation) -%} | ||
drop view if exists {{ relation }} | ||
{%- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from mock import Mock | ||
import pytest | ||
|
||
from tests.unit.macros.base import MacroTestBase | ||
|
||
|
||
class TestCreateViewAs(MacroTestBase): | ||
@pytest.fixture(scope="class") | ||
def template_name(self) -> str: | ||
return "create.sql" | ||
|
||
@pytest.fixture(scope="class") | ||
def macro_folders_to_load(self) -> list: | ||
return ["macros", "macros/relations/view"] | ||
|
||
def render_create_view_as(self, template_bundle, sql="select 1"): | ||
return self.run_macro( | ||
template_bundle.template, | ||
"databricks__create_view_as", | ||
template_bundle.relation, | ||
sql, | ||
) | ||
|
||
def test_macros_create_view_as_tblproperties(self, config, template_bundle): | ||
config["tblproperties"] = {"tblproperties_to_view": "true"} | ||
template_bundle.context["get_columns_in_query"] = Mock(return_value=[]) | ||
|
||
sql = self.render_create_view_as(template_bundle) | ||
expected = ( | ||
f"create or replace view {template_bundle.relation} " | ||
"tblproperties ('tblproperties_to_view' = 'true' ) as select 1" | ||
) | ||
|
||
assert sql == expected |
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