Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
passing fixtures
  • Loading branch information
benc-db committed Jul 27, 2023
1 parent 65138f6 commit ad46db5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
72 changes: 72 additions & 0 deletions dbt/include/databricks/macros/materializations/clone.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{% macro databricks__can_clone_table() %}
{{ return(True) }}
{% endmacro %}

{% macro databricks__create_or_replace_clone(this_relation, defer_relation) %}
create or replace
table {{ this_relation }}
shallow clone {{ defer_relation }}
{% endmacro %}

{%- materialization clone, adapter='databricks' -%}

{%- set relations = {'relations': []} -%}

{%- if not defer_relation -%}
-- nothing to do
{{ log("No relation found in state manifest for " ~ model.unique_id, info=True) }}
{{ return(relations) }}
{%- endif -%}

{%- set existing_relation = load_cached_relation(this) -%}

{%- if existing_relation and not flags.FULL_REFRESH -%}
-- noop!
{{ log("Relation " ~ existing_relation ~ " already exists", info=True) }}
{{ return(relations) }}
{%- endif -%}

{%- set other_existing_relation = load_cached_relation(defer_relation) -%}
{%- set file_format = config.get('file_format', validator=validation.any[basestring]) -%}

-- If this is a database that can do zero-copy cloning of tables, and the other relation is a table, then this will be a table
-- Otherwise, this will be a view

{% set can_clone_table = can_clone_table() %}

{%- if other_existing_relation and other_existing_relation.type == 'table' and can_clone_table -%}

{%- set target_relation = this.incorporate(type='table') -%}
{% if existing_relation is not none and not existing_relation.is_table %}
{{ log("Dropping relation " ~ existing_relation ~ " because it is of type " ~ existing_relation.type) }}
{{ drop_relation_if_exists(existing_relation) }}
{% endif %}

-- as a general rule, data platforms that can clone tables can also do atomic 'create or replace'
{% call statement('main') %}
{{ create_or_replace_clone(target_relation, defer_relation) }}
{% endcall %}

{% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %}
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
{% do persist_docs(target_relation, model) %}

{{ return({'relations': [target_relation]}) }}

{%- else -%}

{%- set target_relation = this.incorporate(type='view') -%}

-- reuse the view materialization
-- TODO: support actual dispatch for materialization macros
-- Tracking ticket: https://github.com/dbt-labs/dbt-core/issues/7799
{% set search_name = "materialization_view_" ~ adapter.type() %}
{% if not search_name in context %}
{% set search_name = "materialization_view_default" %}
{% endif %}
{% set materialization_macro = context[search_name] %}
{% set relations = materialization_macro() %}
{{ return(relations) }}
{% endif %}

{%- endmaterialization -%}
1 change: 0 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pytz
tox>=3.2.0
types-requests

dbt-spark==1.6.0rc1
dbt-core==1.6.0rc1
dbt-tests-adapter==1.6.0rc1
# git+https://github.com/dbt-labs/[email protected]#egg=dbt-spark
Expand Down
20 changes: 20 additions & 0 deletions tests/functional/adapter/test_dbt_clone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
from dbt.tests.adapter.dbt_clone.test_dbt_clone import BaseClonePossible


class TestDatabricksClonePossible(BaseClonePossible):
@pytest.fixture(autouse=True)
def clean_up(self, project):
yield
with project.adapter.connection_named("__test"):
relation = project.adapter.Relation.create(
database=project.database, schema=f"{project.test_schema}_seeds"
)
project.adapter.drop_schema(relation)

relation = project.adapter.Relation.create(
database=project.database, schema=project.test_schema
)
project.adapter.drop_schema(relation)

pass

0 comments on commit ad46db5

Please sign in to comment.