Skip to content

Commit d9817a0

Browse files
authored
Fix: drop temp tables in python model materialization v2 (#1026)
1 parent 1ccba21 commit d9817a0

6 files changed

Lines changed: 83 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Fixes
88

99
- Fix bug with multiple not_null constraints defined on the model level ([1008](https://github.com/databricks/dbt-databricks/pull/1008))
10+
- Fix bug with temp tables not being dropped after python model is materialized ([1010](https://github.com/databricks/dbt-databricks/issues/1010))
1011

1112
## dbt-databricks 1.10.1 (Apr 29, 2025)
1213

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
{% do apply_grants(target_relation, grant_config, should_revoke) %}
7474
{% do optimize(target_relation) %}
7575

76+
{% if language == 'python' %}
77+
{{ drop_relation_if_exists(intermediate_relation) }}
78+
{% endif %}
79+
7680
{{ run_post_hooks() }}
7781

7882
{% else %}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434

3535
{% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %}
3636
{{ apply_grants(target_relation, grant_config, should_revoke) }}
37+
38+
{% if language == 'python' %}
39+
{{ drop_relation_if_exists(intermediate_relation) }}
40+
{% endif %}
3741

3842
{{ run_post_hooks() }}
3943
{% else %}

tests/functional/adapter/incremental/fixtures.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ def model(dbt, spark):
549549
models:
550550
- name: tags
551551
config:
552+
unique_tmp_table_suffix: true
552553
tags: ["python"]
553554
databricks_tags:
554555
c: e

tests/functional/adapter/python_model/fixtures.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def model(dbt, spark):
8989
def model(dbt, spark):
9090
dbt.config(
9191
materialized='table',
92+
unique_tmp_table_suffix=True
9293
)
9394
data = [[1,2]] * 10
9495
return spark.createDataFrame(data, schema=['test1', 'test3'])
@@ -190,3 +191,26 @@ def model(dbt, spark):
190191
2,"Fang"
191192
3,"Elbert"
192193
"""
194+
195+
simple_incremental_python_model = """
196+
import pandas
197+
198+
def model(dbt, spark):
199+
dbt.config(
200+
materialized='incremental',
201+
)
202+
data = [[1,2]] * 5
203+
return spark.createDataFrame(data, schema=['test', 'test2'])
204+
"""
205+
206+
simple_incremental_python_model_v2 = """
207+
import pandas
208+
209+
def model(dbt, spark):
210+
dbt.config(
211+
materialized='incremental',
212+
unique_tmp_table_suffix=True,
213+
)
214+
data = [[1,2]] * 10
215+
return spark.createDataFrame(data, schema=['test', 'test2'])
216+
"""

tests/functional/adapter/python_model/test_python_model.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,22 @@
88
BasePythonIncrementalTests,
99
BasePythonModelTests,
1010
)
11+
from tests.functional.adapter.fixtures import MaterializationV2Mixin
1112
from tests.functional.adapter.python_model import fixtures as override_fixtures
1213

1314

15+
def verify_temp_tables_cleaned(project):
16+
verify_temp_table_cleaned(project, "*__dbt_stg")
17+
verify_temp_table_cleaned(project, "*__dbt_tmp")
18+
19+
20+
def verify_temp_table_cleaned(project, suffix):
21+
tmp_tables = project.run_sql(
22+
"SHOW TABLES IN {database}.{schema} LIKE '" + f"{suffix}'", fetch="all"
23+
)
24+
assert len(tmp_tables) == 0
25+
26+
1427
@pytest.mark.python
1528
@pytest.mark.skip_profile("databricks_uc_sql_endpoint")
1629
class TestPythonModel(BasePythonModelTests):
@@ -207,3 +220,39 @@ def test_workflow_run(self, project):
207220
"SELECT * FROM {database}.{schema}.my_workflow_model", fetch="all"
208221
)
209222
assert len(sql_results) == 10
223+
224+
225+
@pytest.mark.python
226+
@pytest.mark.skip_profile("databricks_uc_sql_endpoint")
227+
class TestChangingSchemaV2(MaterializationV2Mixin):
228+
@pytest.fixture(scope="class")
229+
def models(self):
230+
return {"simple_python_model.py": override_fixtures.simple_python_model}
231+
232+
def test_changing_unique_tmp_table_suffix(self, project):
233+
util.run_dbt(["run"])
234+
util.write_file(
235+
override_fixtures.simple_python_model_v2,
236+
project.project_root + "/models",
237+
"simple_python_model.py",
238+
)
239+
util.run_dbt(["run"])
240+
verify_temp_tables_cleaned(project)
241+
242+
243+
@pytest.mark.python
244+
@pytest.mark.skip_profile("databricks_uc_sql_endpoint")
245+
class TestChangingSchemaIncrementalV2(MaterializationV2Mixin):
246+
@pytest.fixture(scope="class")
247+
def models(self):
248+
return {"incremental_model.py": override_fixtures.simple_incremental_python_model}
249+
250+
def test_changing_unique_tmp_table_suffix(self, project):
251+
util.run_dbt(["run"])
252+
util.write_file(
253+
override_fixtures.simple_incremental_python_model_v2,
254+
project.project_root + "/models",
255+
"incremental_model.py",
256+
)
257+
util.run_dbt(["run"])
258+
verify_temp_tables_cleaned(project)

0 commit comments

Comments
 (0)