Skip to content

Commit

Permalink
update tblproperties
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-db committed Aug 12, 2024
1 parent 25807a8 commit be181ff
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 17 deletions.
1 change: 1 addition & 0 deletions dbt/adapters/databricks/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,4 +817,5 @@ def _describe_relation(

if not relation.is_hive_metastore():
results["information_schema.tags"] = adapter.execute_macro("fetch_tags", kwargs=kwargs)
results["show_tblproperties"] = adapter.execute_macro("fetch_tbl_properties", kwargs=kwargs)
return results
5 changes: 0 additions & 5 deletions dbt/adapters/databricks/relation_configs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ def get_diff(self, other: Self) -> Optional[Self]:
the complete config specified in the dbt project, so as to support rendering the `create`
as well as the `alter` statements, for the case where a different component requires full
refresh.
Consider updating tblproperties: we can apply only the differences to an existing relation,
but if some other modified component requires us to completely replace the relation, we
should still be able to construct appropriate create clause from the object returned by
this method.
"""

if self != other:
Expand Down
3 changes: 2 additions & 1 deletion dbt/adapters/databricks/relation_configs/incremental.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from dbt.adapters.databricks.relation_configs.base import DatabricksRelationChangeSet
from dbt.adapters.databricks.relation_configs.base import DatabricksRelationConfigBase
from dbt.adapters.databricks.relation_configs.tags import TagsProcessor
from dbt.adapters.databricks.relation_configs.tblproperties import TblPropertiesProcessor


class IncrementalTableConfig(DatabricksRelationConfigBase):
config_components = [TagsProcessor]
config_components = [TagsProcessor, TblPropertiesProcessor]

def get_changeset(
self, existing: "IncrementalTableConfig"
Expand Down
4 changes: 3 additions & 1 deletion dbt/adapters/databricks/relation_configs/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def get_diff(self, other: "TagsConfig") -> Optional["TagsConfig"]:
for k in other.set_tags.keys():
if k not in self.set_tags:
to_unset.append(k)
return TagsConfig(set_tags=self.set_tags, unset_tags=to_unset)
if self.set_tags or to_unset:
return TagsConfig(set_tags=self.set_tags, unset_tags=to_unset)
return None


class TagsProcessor(DatabricksComponentProcessor[TagsConfig]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
{%- endcall -%}
{% do persist_constraints(target_relation, model) %}
{% do apply_tags(target_relation, tags) %}
{%- if language == 'python' -%}
{%- do apply_tblproperties(target_relation, tblproperties) %}
{%- endif -%}

{%- elif existing_relation.is_view or existing_relation.is_materialized_view or existing_relation.is_streaming_table or should_full_refresh() -%}
{#-- Relation must be dropped & recreated --#}
{% set is_delta = (file_format == 'delta' and existing_relation.is_delta) %}
Expand Down Expand Up @@ -94,16 +98,19 @@
{%- endif -%}
{% do apply_liquid_clustered_cols(target_relation) %}
{% if _configuration_changes is not none %}
{% set tags = _configuration_changes.changes["tags"] %}
{% set tags = _configuration_changes.changes.get("tags", None) %}
{% set tblproperties = _configuration_changes.changes.get("tblproperties", None) %}
{% if tags is not none %}
{% do apply_tags(target_relation, tags.set_tags, tags.unset_tags) %}
{%- endif -%}
{% if tblproperties is not none %}
{% do apply_tblproperties(target_relation, tblproperties.tblproperties) %}
{%- endif -%}
{%- endif -%}
{%- endif -%}

{% set should_revoke = should_revoke(existing_relation, full_refresh_mode) %}
{% do apply_grants(target_relation, grant_config, should_revoke) %}
{% do apply_tblproperties_python(target_relation, tblproperties, language) %}
{% do persist_docs(target_relation, model) %}
{% do optimize(target_relation) %}

Expand Down
5 changes: 3 additions & 2 deletions dbt/include/databricks/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

{% set should_revoke = should_revoke(old_relation, full_refresh_mode=True) %}
{% do apply_grants(target_relation, grant_config, should_revoke) %}
{% do apply_tblproperties_python(target_relation, tblproperties, language) %}

{% if language=="python" %}
{% do apply_tblproperties(target_relation, tblproperties) %}
{% endif %}
{%- do apply_tags(target_relation, tags) -%}

{% do persist_docs(target_relation, model) %}
Expand Down
16 changes: 10 additions & 6 deletions dbt/include/databricks/macros/relations/tblproperties.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
{%- endif %}
{%- endmacro -%}

{% macro apply_tblproperties_python(relation, tblproperties, language) -%}
{%- if tblproperties and language == 'python' -%}
{%- call statement('python_tblproperties') -%}
alter table {{ relation }} set {{ tblproperties_clause() }}
{% macro apply_tblproperties(relation, tblproperties) -%}
{% if tblproperties %}
{%- call statement('apply_tblproperties') -%}
ALTER {{ relation.type }} {{ relation }} SET TBLPROPERTIES (
{% for tblproperty in tblproperties -%}
'{{ tblproperty }}' = '{{ tblproperties[tblproperty] }}' {%- if not loop.last %}, {% endif -%}
{%- endfor %}
)
{%- endcall -%}
{%- endif -%}
{%- endmacro -%}
{% endif %}
{%- endmacro -%}
55 changes: 55 additions & 0 deletions tests/functional/adapter/incremental/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from dbt.adapters.databricks.relation_configs import tblproperties


merge_update_columns_sql = """
{{ config(
materialized = 'incremental',
Expand Down Expand Up @@ -75,6 +78,36 @@
- name: color
"""

tblproperties_a = """
version: 2
models:
- name: merge_update_columns_sql
config:
tblproperties:
a: b
c: d
columns:
- name: id
- name: msg
- name: color
"""

tblproperties_b = """
version: 2
models:
- name: merge_update_columns_sql
config:
tblproperties:
c: e
d: f
columns:
- name: id
- name: msg
- name: color
"""

liquid_clustering_a = """
version: 2
Expand Down Expand Up @@ -313,6 +346,28 @@ def model(dbt, spark):
http_path: "{{ env_var('DBT_DATABRICKS_UC_CLUSTER_HTTP_PATH') }}"
"""

python_tblproperties_schema = """version: 2
models:
- name: tblproperties
config:
tags: ["python"]
tblproperties:
a: b
c: d
http_path: "{{ env_var('DBT_DATABRICKS_UC_CLUSTER_HTTP_PATH') }}"
"""

python_tblproperties_schema2 = """version: 2
models:
- name: tblproperties
config:
tags: ["python"]
tblproperties:
c: e
d: f
http_path: "{{ env_var('DBT_DATABRICKS_UC_CLUSTER_HTTP_PATH') }}"
"""

lc_python_schema = """version: 2
models:
- name: simple_python_model
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
from dbt.tests import util
from tests.functional.adapter.incremental import fixtures


class TestIncrementalTblproperties:
@pytest.fixture(scope="class")
def models(self):
return {
"merge_update_columns_sql.sql": fixtures.merge_update_columns_sql,
"schema.yml": fixtures.tblproperties_a,
}

# For now, only add/change, no deletes
def test_changing_tblproperties(self, project):
util.run_dbt(["run"])
util.write_file(fixtures.tblproperties_b, "models", "schema.yml")
util.run_dbt(["run"])
results = project.run_sql(
"show tblproperties {database}.{schema}.merge_update_columns_sql",
fetch="all",
)
results_dict = {}
for result in results:
results_dict[result.key] = result.value
assert results_dict["c"] == "e"
assert results_dict["d"] == "f"


@pytest.mark.skip_profile("databricks_cluster")
class TestIncrementalPythonTblproperties:
@pytest.fixture(scope="class")
def models(self):
return {
"tblproperties.py": fixtures.simple_python_model,
"schema.yml": fixtures.python_tblproperties_schema,
}

def test_changing_tblproperties(self, project):
util.run_dbt(["run"])
util.write_file(fixtures.python_tblproperties_schema2, "models", "schema.yml")
util.run_dbt(["run"])
results = project.run_sql(
"show tblproperties {database}.{schema}.tblproperties",
fetch="all",
)
results_dict = {}
for result in results:
results_dict[result.key] = result.value
assert results_dict["c"] == "e"
assert results_dict["d"] == "f"

0 comments on commit be181ff

Please sign in to comment.