Skip to content

Commit 78c3079

Browse files
authored
Revert REPLACE USING usage in insert overwrite (databricks#1138)
2 parents 922969c + 1c79493 commit 78c3079

6 files changed

Lines changed: 52 additions & 138 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
### Features
44
- Support column tags for views using `ALTER TABLE`
55

6+
### Under the hood
7+
- Revert `REPLACE USING` syntax being used for insert overwrite ([1025](https://github.com/databricks/dbt-databricks/issues/1025))
8+
69
## dbt-databricks 1.10.8 (August 4, 2025)
710

811
### Features

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
{%- else -%}
5353
{{ log("Existing relation found, proceeding with incremental work")}}
5454
{#-- Set Overwrite Mode to DYNAMIC for subsequent incremental operations --#}
55-
{%- if incremental_strategy == 'insert_overwrite' and partition_by and adapter.compare_dbr_version(16, 3) < 0 -%}
55+
{%- if incremental_strategy == 'insert_overwrite' and partition_by -%}
5656
{{ set_overwrite_mode('DYNAMIC') }}
5757
{%- endif -%}
5858
{#-- Relation must be merged --#}
@@ -118,7 +118,7 @@
118118
{% do persist_docs(target_relation, model, for_relation=language=='python') %}
119119
{%- else -%}
120120
{#-- Set Overwrite Mode to DYNAMIC for subsequent incremental operations --#}
121-
{%- if incremental_strategy == 'insert_overwrite' and partition_by and adapter.compare_dbr_version(16, 3) < 0 -%}
121+
{%- if incremental_strategy == 'insert_overwrite' and partition_by -%}
122122
{{ set_overwrite_mode('DYNAMIC') }}
123123
{%- endif -%}
124124
{#-- Relation must be merged --#}
@@ -177,7 +177,7 @@
177177
{{ run_hooks(post_hooks) }}
178178
{%- endif -%}
179179

180-
{%- if incremental_strategy == 'insert_overwrite' and not full_refresh and adapter.compare_dbr_version(16, 3) < 0 -%}
180+
{%- if incremental_strategy == 'insert_overwrite' and not full_refresh -%}
181181
{{ set_overwrite_mode('STATIC') }}
182182
{%- endif -%}
183183

@@ -191,7 +191,7 @@
191191
set spark.sql.sources.partitionOverwriteMode = {{ value }}
192192
{%- endcall -%}
193193
{% else %}
194-
{{ exceptions.warn("INSERT OVERWRITE is supported on SQL warehouses with DBR 16.3+. On older DBR versions, this strategy would be equivalent to using the table materialization.") }}
194+
{{ exceptions.warn("INSERT OVERWRITE is only properly supported on all-purpose clusters. On SQL Warehouses, this strategy would be equivalent to using the table materialization.") }}
195195
{% endif %}
196196
{% endmacro %}
197197

@@ -216,4 +216,4 @@
216216
{%- set configuration_changes = model_config.get_changeset(existing_config) -%}
217217
{{ apply_config_changeset(target_relation, model, configuration_changes) }}
218218
{% endif %}
219-
{% endmacro %}
219+
{% endmacro %}

dbt/include/databricks/macros/materializations/incremental/strategies.sql

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,9 @@
3838
{%- endfor -%}
3939
{%- set dest_cols_csv = dest_columns | join(', ') -%}
4040
{%- set source_cols_csv = common_columns | join(', ') -%}
41-
42-
{%- if adapter.compare_dbr_version(16, 3) >= 0 -%}
43-
{{ get_insert_replace_using_sql(source_relation, target_relation, source_cols_csv) }}
44-
{%- else -%}
45-
{#-- Use traditional INSERT OVERWRITE for older DBR versions --#}
46-
insert overwrite table {{ target_relation }}
47-
{{ partition_cols(label="partition") }}
48-
select {{ source_cols_csv }} from {{ source_relation }}
49-
{%- endif -%}
50-
{% endmacro %}
51-
52-
{% macro get_insert_replace_using_sql(source_relation, target_relation, source_cols_csv) %}
53-
{%- set partition_by = config.get('partition_by') -%}
54-
{%- if partition_by -%}
55-
{%- if partition_by is string -%}
56-
{%- set partition_by = [partition_by] -%}
57-
{%- endif -%}
58-
{%- set partition_cols_csv = partition_by | join(', ') -%}
59-
insert into table {{ target_relation }}
60-
replace using ({{ partition_cols_csv }})
61-
select {{ source_cols_csv }} from {{ source_relation }}
62-
{%- else -%}
63-
{#-- Fallback to regular insert if no partitions defined --#}
64-
insert overwrite table {{ target_relation }}
65-
select {{ source_cols_csv }} from {{ source_relation }}
66-
{%- endif -%}
41+
insert overwrite table {{ target_relation }}
42+
{{ partition_cols(label="partition") }}
43+
select {{source_cols_csv}} from {{ source_relation }}
6744
{% endmacro %}
6845

6946
{% macro get_replace_where_sql(args_dict) -%}

dbt/include/databricks/macros/materializations/incremental/validate.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
You can only choose this strategy when file_format is set to 'delta'
2525
{%- endset %}
2626

27+
{% set invalid_insert_overwrite_endpoint_msg -%}
28+
Invalid incremental strategy provided: {{ raw_strategy }}
29+
You cannot use this strategy when connecting via warehouse
30+
Use the 'merge' or 'replace_where' strategy instead
31+
{%- endset %}
32+
2733
{% if raw_strategy not in adapter.valid_incremental_strategies() %}
2834
{{ log("WARNING - You are using an unsupported incremental strategy: " ~ raw_strategy) }}
2935
{{ log("You can ignore this warning if you are using a custom incremental strategy") }}

tests/functional/adapter/incremental/test_incremental_strategies.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def project_config_update(self):
7070
}
7171

7272

73+
@pytest.mark.skip_profile("databricks_uc_sql_endpoint")
7374
class InsertOverwriteBase(IncrementalBase):
7475
@pytest.fixture(scope="class")
7576
def seeds(self):
@@ -92,10 +93,12 @@ def test_incremental(self, project):
9293
util.check_relations_equal(project.adapter, ["overwrite_model", "overwrite_expected"])
9394

9495

96+
@pytest.mark.skip_profile("databricks_uc_sql_endpoint")
9597
class TestInsertOverwriteDelta(InsertOverwriteBase):
9698
pass
9799

98100

101+
@pytest.mark.skip_profile("databricks_uc_sql_endpoint")
99102
class TestInsertOverwriteWithPartitionsDelta(InsertOverwriteBase):
100103
@pytest.fixture(scope="class")
101104
def project_config_update(self):
@@ -117,6 +120,7 @@ def test_incremental(self, project):
117120
util.check_relations_equal(project.adapter, ["overwrite_model", "upsert_expected"])
118121

119122

123+
@pytest.mark.skip_profile("databricks_uc_sql_endpoint")
120124
class TestInsertOverwriteChangeSchema(InsertOverwriteBase):
121125
@pytest.fixture(scope="class")
122126
def models(self):
@@ -174,6 +178,37 @@ def test_incremental(self, project):
174178
util.check_relations_equal(project.adapter, ["overwrite_model", "upsert_expected"])
175179

176180

181+
# Insert overwrite in SQL warehouse is expected to behave like a table materialization
182+
# We support this as a short term hack for customers who want the side effect of reusing
183+
# the same table on subsequent runs
184+
@pytest.mark.skip_profile("databricks_uc_cluster", "databricks_cluster")
185+
class TestInsertOverwriteSqlWarehouse(IncrementalBase):
186+
@pytest.fixture(scope="class")
187+
def project_config_update(self):
188+
return {
189+
"models": {
190+
"+incremental_strategy": "insert_overwrite",
191+
"+partition_by": "id",
192+
},
193+
}
194+
195+
@pytest.fixture(scope="class")
196+
def seeds(self):
197+
return {
198+
"overwrite_expected.csv": fixtures.overwrite_expected,
199+
}
200+
201+
@pytest.fixture(scope="class")
202+
def models(self):
203+
return {
204+
"overwrite_model.sql": fixtures.base_model,
205+
}
206+
207+
def test_incremental(self, project):
208+
self.seed_and_run_twice()
209+
util.check_relations_equal(project.adapter, ["overwrite_model", "overwrite_expected"])
210+
211+
177212
@pytest.mark.external
178213
@pytest.mark.skip("This test is not repeatable due to external location")
179214
class TestInsertOverwriteParquet(InsertOverwriteBase):

tests/unit/macros/materializations/incremental/test_insert_overwrite_macros.py

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)