Skip to content

Commit f6a58ee

Browse files
authored
Allow auto liquid clustering (#935)
1 parent 4b1d2d9 commit f6a58ee

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
## dbt-databricks 1.9.5 (TBD)
22

3+
### Features
4+
5+
- Add `auto_liquid_cluster` config to enable Auto Liquid Clustering for Delta-based dbt models ([935](https://github.com/databricks/dbt-databricks/pull/935))
6+
37
### Fixes
48

59
- table_format: iceberg is unblocked for snapshots ([930](https://github.com/databricks/dbt-databricks/pull/930))
610
- Fix for regression in glue table listing behavior ([934](https://github.com/databricks/dbt-databricks/pull/934))
711

12+
813
### Under the Hood
914

1015
- Collapsing to a single connection manager (since the old one no longer works) ([910](https://github.com/databricks/dbt-databricks/pull/910))

dbt/adapters/databricks/impl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class DatabricksConfig(AdapterConfig):
110110
partition_by: Optional[Union[list[str], str]] = None
111111
clustered_by: Optional[Union[list[str], str]] = None
112112
liquid_clustered_by: Optional[Union[list[str], str]] = None
113+
auto_liquid_cluster: Optional[bool] = None
113114
buckets: Optional[int] = None
114115
options: Optional[dict[str, str]] = None
115116
merge_update_columns: Optional[str] = None
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
{% macro liquid_clustered_cols() -%}
22
{%- set cols = config.get('liquid_clustered_by', validator=validation.any[list, basestring]) -%}
3+
{%- set auto_cluster = config.get('auto_liquid_cluster', validator=validation.any[boolean]) -%}
34
{%- if cols is not none %}
45
{%- if cols is string -%}
56
{%- set cols = [cols] -%}
67
{%- endif -%}
78
CLUSTER BY ({{ cols | join(', ') }})
9+
{%- elif auto_cluster -%}
10+
CLUSTER BY AUTO
811
{%- endif %}
912
{%- endmacro -%}
1013

1114
{% macro apply_liquid_clustered_cols(target_relation) -%}
1215
{%- set cols = config.get('liquid_clustered_by', validator=validation.any[list, basestring]) -%}
16+
{%- set auto_cluster = config.get('auto_liquid_cluster', validator=validation.any[boolean]) -%}
1317
{%- if cols is not none %}
1418
{%- if cols is string -%}
1519
{%- set cols = [cols] -%}
1620
{%- endif -%}
1721
{%- call statement('set_cluster_by_columns') -%}
1822
ALTER {{ target_relation.type }} {{ target_relation.render() }} CLUSTER BY ({{ cols | join(', ') }})
1923
{%- endcall -%}
24+
{%- elif auto_cluster -%}
25+
{%- call statement('set_cluster_by_auto') -%}
26+
ALTER {{ target_relation.type }} {{ target_relation.render() }} CLUSTER BY AUTO
27+
{%- endcall -%}
2028
{%- endif %}
2129
{%- endmacro -%}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{%- if var('DATABRICKS_SKIP_OPTIMIZE', 'false')|lower != 'true' and
77
var('databricks_skip_optimize', 'false')|lower != 'true' and
88
config.get('file_format', 'delta') == 'delta' -%}
9-
{%- if (config.get('zorder', False) or config.get('liquid_clustered_by', False)) -%}
9+
{%- if (config.get('zorder', False) or config.get('liquid_clustered_by', False)) or config.get('auto_liquid_cluster', False) -%}
1010
{%- call statement('run_optimize_stmt') -%}
1111
{{ get_optimize_sql(relation) }}
1212
{%- endcall -%}
@@ -17,8 +17,8 @@
1717
{%- macro get_optimize_sql(relation) %}
1818
optimize {{ relation }}
1919
{%- if config.get('zorder', False) and config.get('file_format', 'delta') == 'delta' %}
20-
{%- if config.get('liquid_clustered_by', False) %}
21-
{{ exceptions.warn("Both zorder and liquid_clustered_by are set but they are incompatible. zorder will be ignored.") }}
20+
{%- if config.get('liquid_clustered_by', False) or config.get('auto_liquid_cluster', False) %}
21+
{{ exceptions.warn("Both zorder and liquid_clustering are set but they are incompatible. zorder will be ignored.") }}
2222
{%- else %}
2323
{%- set zorder = config.get('zorder', none) %}
2424
{# TODO: predicates here? WHERE ... #}

tests/functional/adapter/liquid_clustering/fixtures.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
{{ config(materialized='incremental', liquid_clustered_by='id') }}
33
select 1 as id, 'Joe' as name
44
"""
5+
6+
auto_liquid_cluster_sql = """
7+
{{ config(materialized='incremental', auto_liquid_cluster=true) }}
8+
select 1 as id, 'Joe' as name
9+
"""

tests/functional/adapter/liquid_clustering/test_liquid_clustering.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ def models(self):
1515
def test_liquid_clustering(self, project):
1616
_, logs = util.run_dbt_and_capture(["--debug", "run"])
1717
assert "optimize" in logs
18+
19+
20+
class TestAutoLiquidClustering:
21+
@pytest.fixture(scope="class")
22+
def models(self):
23+
return {
24+
"liquid_clustering.sql": fixtures.liquid_cluster_sql,
25+
}
26+
27+
@pytest.mark.skip_profile("databricks_uc_cluster", "databricks_cluster")
28+
def test_liquid_clustering(self, project):
29+
_, logs = util.run_dbt_and_capture(["--debug", "run"])
30+
assert "optimize" in logs

tests/unit/macros/relations/test_table_macros.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ def test_macros_create_table_as_liquid_clusters(self, config, template_bundle):
180180

181181
assert sql == expected
182182

183+
def test_macros_create_table_as_liquid_cluster_auto(self, config, template_bundle):
184+
config["auto_liquid_cluster"] = True
185+
sql = self.render_create_table_as(template_bundle)
186+
expected = (
187+
f"create or replace table {template_bundle.relation} using"
188+
" delta CLUSTER BY AUTO as select 1"
189+
)
190+
191+
assert sql == expected
192+
183193
def test_macros_create_table_as_comment(self, config, template_bundle):
184194
config["persist_docs"] = {"relation": True}
185195
template_bundle.context["model"].description = "Description Test"

0 commit comments

Comments
 (0)