Skip to content

Commit

Permalink
Refactoring to align with dbt-core organization: Part I (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-db authored Dec 6, 2023
2 parents 04d04a8 + 4b77c28 commit 67c8f52
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 120 deletions.
84 changes: 0 additions & 84 deletions dbt/include/databricks/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,57 +62,6 @@
{%- endif %}
{%- 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 %}

{% 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 %}

{% 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 %}

{# Persist table-level and column-level constraints. #}
{% macro persist_constraints(relation, model) %}
{{ return(adapter.dispatch('persist_constraints', 'dbt')(relation, model)) }}
Expand Down Expand Up @@ -431,23 +380,6 @@
{% do return(load_result('show_views').table) %}
{% endmacro %}


{% macro databricks__drop_relation(relation) -%}
{%- if relation.is_materialized_view -%}
{% call statement('drop_relation', auto_begin=False) -%}
drop materialized view if exists {{ relation }}
{%- endcall %}
{%- elif relation.is_streaming_table-%}
{% call statement('drop_relation', auto_begin=False) -%}
drop table if exists {{ relation }}
{%- endcall %}
{%- else -%}
{% call statement('drop_relation', auto_begin=False) -%}
drop {{ relation.type }} if exists {{ relation }}
{%- endcall %}
{%- endif -%}
{% endmacro %}

{% macro databricks__generate_database_name(custom_database_name=none, node=none) -%}
{%- set default_database = target.database -%}
{%- if custom_database_name is none -%}
Expand Down Expand Up @@ -490,19 +422,3 @@
) -%}
{% do return([false, new_relation]) %}
{% 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 %}

This file was deleted.

2 changes: 0 additions & 2 deletions dbt/include/databricks/macros/materializations/view.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{% materialization view, adapter='databricks' -%}
{# copied from macro create_or_replace_view #}

{%- set identifier = model['alias'] -%}

{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
Expand Down
46 changes: 46 additions & 0 deletions dbt/include/databricks/macros/persist_docs.sql
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 %}
11 changes: 11 additions & 0 deletions dbt/include/databricks/macros/relations/drop.sql
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 %}
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 %}
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 %}
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 %}
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 %}
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
{% 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 %}

{% macro get_refresh_streaming_table_as_sql(relation, sql) -%}
{{ adapter.dispatch('get_refresh_streaming_table_as_sql', 'dbt')(relation, sql) }}
{%- endmacro %}
Expand Down
3 changes: 3 additions & 0 deletions dbt/include/databricks/macros/relations/table/drop.sql
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 %}
20 changes: 20 additions & 0 deletions dbt/include/databricks/macros/relations/view/create.sql
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 %}
3 changes: 3 additions & 0 deletions dbt/include/databricks/macros/relations/view/drop.sql
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 %}
34 changes: 34 additions & 0 deletions tests/unit/macros/relations/test_view_macros.py
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
15 changes: 0 additions & 15 deletions tests/unit/macros/test_adapters_macros.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from mock import MagicMock
import pytest

from tests.unit.macros.base import MacroTestBase
Expand All @@ -9,20 +8,6 @@ class TestDatabricksMacros(MacroTestBase):
def template_name(self) -> str:
return "adapters.sql"

def test_macros_create_view_as_tblproperties(self, config, template_bundle):
config["tblproperties"] = {"tblproperties_to_view": "true"}
template_bundle.context["model"].alias = "my_table"
template_bundle.context["get_columns_in_query"] = MagicMock(return_value=[])
sql = self.run_macro(
template_bundle.template, "databricks__create_view_as", "my_table", "select 1"
)
expected = (
"create or replace view my_table "
"tblproperties ('tblproperties_to_view' = 'true' ) as select 1"
)

assert sql == expected

def test_macros_get_optimize_sql(self, config, template_bundle):
config["zorder"] = "foo"
sql = self.render_bundle(template_bundle, "get_optimize_sql")
Expand Down

0 comments on commit 67c8f52

Please sign in to comment.