Skip to content

Commit a8cd583

Browse files
committed
Support databricks_tags for MV/STs
1 parent 92f1442 commit a8cd583

13 files changed

Lines changed: 63 additions & 15 deletions

File tree

dbt/adapters/databricks/relation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class DatabricksRelationType(StrEnum):
4343
MetricView = "metric_view"
4444
Unknown = "unknown"
4545

46+
def render(self) -> str:
47+
"""Return the type formatted for SQL statements (replace underscores with spaces)"""
48+
return self.value.replace("_", " ")
49+
4650

4751
class DatabricksTableType(StrEnum):
4852
External = "external"

dbt/include/databricks/macros/adapters/persist_docs.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ COMMENT ON COLUMN {{ column_path }} IS '{{ escaped_comment }}'
2828
{% endmacro %}
2929

3030
{% macro alter_relation_comment_sql(relation, description) %}
31-
COMMENT ON {{ relation.type.upper() }} {{ relation.render() }} IS '{{ description | replace("'", "\\'") }}'
31+
COMMENT ON {{ relation.type.render().upper() }} {{ relation.render() }} IS '{{ description | replace("'", "\\'") }}'
3232
{% endmacro %}
3333

3434
{% macro alter_column_comments(relation, column_dict) %}

dbt/include/databricks/macros/materializations/materialized_view.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@
6464
{{ run_hooks(pre_hooks, inside_transaction=True) }}
6565

6666
{% set grant_config = config.get('grants') %}
67+
{% set tags = config.get('databricks_tags') %}
6768

6869
{{ execute_multiple_statements(build_sql) }}
6970

71+
{%- do apply_tags(target_relation, tags) -%}
72+
7073
{% set column_tags = adapter.get_column_tags_from_model(config.model) %}
7174
{% if column_tags %}
7275
{{ apply_column_tags(target_relation, column_tags) }}

dbt/include/databricks/macros/materializations/streaming_table.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@
6363
{{ run_hooks(pre_hooks, inside_transaction=True) }}
6464

6565
{% set grant_config = config.get('grants') %}
66+
{% set tags = config.get('databricks_tags') %}
6667

6768
{{ execute_multiple_statements(build_sql) }}
6869

70+
{%- do apply_tags(target_relation, tags) -%}
71+
6972
{% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %}
7073
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
7174

dbt/include/databricks/macros/relations/components/column_mask.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
{%- endmacro -%}
4242

4343
{% macro alter_drop_column_mask(relation, column) -%}
44-
ALTER {{ relation.type }} {{ relation.render() }}
44+
ALTER {{ relation.type.render() }} {{ relation.render() }}
4545
ALTER COLUMN `{{ column }}`
4646
DROP MASK;
4747
{%- endmacro -%}
4848

4949
{% macro alter_set_column_mask(relation, column, mask) -%}
50-
ALTER {{ relation.type }} {{ relation.render() }}
50+
ALTER {{ relation.type.render() }} {{ relation.render() }}
5151
ALTER COLUMN `{{ column }}`
5252
SET MASK {{ mask.function }}
5353
{%- if mask.using_columns %}

dbt/include/databricks/macros/relations/components/column_tags.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
{%- if relation.type == 'view' -%}
3939
ALTER TABLE {{ relation.render() }}
4040
{%- else -%}
41-
ALTER {{ relation.type | replace('_', ' ') }} {{ relation.render() }}
41+
ALTER {{ relation.type.render() }} {{ relation.render() }}
4242
{%- endif -%}
4343
ALTER COLUMN `{{ column }}`
4444
SET TAGS (

dbt/include/databricks/macros/relations/components/constraints.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,23 @@
119119
{%- endmacro -%}
120120

121121
{% macro alter_set_non_null_constraint(relation, column) -%}
122-
ALTER {{ relation.type }} {{ relation.render() }} ALTER COLUMN {{ column }} SET NOT NULL;
122+
ALTER {{ relation.type.render() }} {{ relation.render() }} ALTER COLUMN {{ column }} SET NOT NULL;
123123
{%- endmacro -%}
124124

125125
{% macro alter_unset_non_null_constraint(relation, column) -%}
126-
ALTER {{ relation.type }} {{ relation.render() }} ALTER COLUMN {{ column }} DROP NOT NULL;
126+
ALTER {{ relation.type.render() }} {{ relation.render() }} ALTER COLUMN {{ column }} DROP NOT NULL;
127127
{%- endmacro -%}
128128

129129
{% macro alter_set_constraint(relation, constraint) -%}
130-
ALTER {{ relation.type }} {{ relation.render() }} ADD {{ constraint.render() }};
130+
ALTER {{ relation.type.render() }} {{ relation.render() }} ADD {{ constraint.render() }};
131131
{%- endmacro -%}
132132

133133
{% macro alter_unset_constraint(relation, constraint) -%}
134134
{% set constraint_type = constraint.type %}
135135
{% if constraint_type == 'primary_key' %}
136136
{# Need to only add CASCADE to PK constraints because dropping check constraints break when adding CASCADE #}
137-
ALTER {{ relation.type }} {{ relation.render() }} DROP CONSTRAINT {{ constraint.name }} CASCADE;
137+
ALTER {{ relation.type.render() }} {{ relation.render() }} DROP CONSTRAINT {{ constraint.name }} CASCADE;
138138
{% else %}
139-
ALTER {{ relation.type }} {{ relation.render() }} DROP CONSTRAINT IF EXISTS {{ constraint.name }};
139+
ALTER {{ relation.type.render() }} {{ relation.render() }} DROP CONSTRAINT IF EXISTS {{ constraint.name }};
140140
{% endif %}
141141
{%- endmacro -%}

dbt/include/databricks/macros/relations/components/query.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{% endmacro %}
99

1010
{% macro get_alter_query_sql(target_relation, query) -%}
11-
ALTER {{ target_relation.type|upper }} {{ target_relation.render() }} AS (
11+
ALTER {{ target_relation.type.render()|upper }} {{ target_relation.render() }} AS (
1212
{{ query }}
1313
)
1414
{%- endmacro %}

dbt/include/databricks/macros/relations/liquid_clustering.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
{%- set auto_cluster = liquid_clustering.auto_cluster -%}
1717
{%- if cols and cols != [] %}
1818
{%- call statement('set_cluster_by_columns') -%}
19-
ALTER {{ target_relation.type }} {{ target_relation.render() }} CLUSTER BY ({{ cols | join(', ') }})
19+
ALTER {{ target_relation.type.render() }} {{ target_relation.render() }} CLUSTER BY ({{ cols | join(', ') }})
2020
{%- endcall -%}
2121
{%- elif auto_cluster -%}
2222
{%- call statement('set_cluster_by_auto') -%}
23-
ALTER {{ target_relation.type }} {{ target_relation.render() }} CLUSTER BY AUTO
23+
ALTER {{ target_relation.type.render() }} {{ target_relation.render() }} CLUSTER BY AUTO
2424
{%- endcall -%}
2525
{% else %}
2626
{%- call statement('unset_cluster_by') -%}
27-
ALTER {{ target_relation.type }} {{ target_relation.render() }} CLUSTER BY NONE
27+
ALTER {{ target_relation.type.render() }} {{ target_relation.render() }} CLUSTER BY NONE
2828
{%- endcall -%}
2929
{%- endif %}
3030
{%- endmacro -%}

dbt/include/databricks/macros/relations/tags.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
{%- endmacro -%}
3030

3131
{% macro alter_set_tags(relation, tags) -%}
32-
ALTER {{ relation.type }} {{ relation.render() }} SET TAGS (
32+
ALTER {{ relation.type.render() }} {{ relation.render() }} SET TAGS (
3333
{% for tag in tags -%}
3434
'{{ tag }}' = '{{ tags[tag] }}' {%- if not loop.last %}, {% endif -%}
3535
{%- endfor %}

0 commit comments

Comments
 (0)