diff --git a/dbt/include/athena/macros/materializations/models/table/table.sql b/dbt/include/athena/macros/materializations/models/table/table.sql index f371c5a7..7f9856e4 100644 --- a/dbt/include/athena/macros/materializations/models/table/table.sql +++ b/dbt/include/athena/macros/materializations/models/table/table.sql @@ -82,27 +82,36 @@ {{ create_table_as(False, target_relation, sql) }} {%- endcall %} {%- else -%} - {%- if tmp_relation is not none -%} - {%- do drop_relation(tmp_relation) -%} - {%- endif -%} - - {%- set old_relation_bkp = make_temp_relation(old_relation, '__bkp') -%} - -- If we have this, it means that at least the first renaming occurred but there was an issue - -- afterwards, therefore we are in weird state. The easiest and cleanest should be to remove - -- the backup relation. It won't have an impact because since we are in the else condition, - -- that means that old relation exists therefore no downtime yet. - {%- if old_relation_bkp is not none -%} - {%- do drop_relation(old_relation_bkp) -%} + {%- if old_relation.is_view -%} + {%- call statement('main') -%} + {{ create_table_as(False, tmp_relation, sql) }} + {%- endcall -%} + {%- do drop_relation(old_relation) -%} + {%- do rename_relation(tmp_relation, target_relation) -%} + {%- else -%} + + {%- if tmp_relation is not none -%} + {%- do drop_relation(tmp_relation) -%} + {%- endif -%} + + {%- set old_relation_bkp = make_temp_relation(old_relation, '__bkp') -%} + -- If we have this, it means that at least the first renaming occurred but there was an issue + -- afterwards, therefore we are in weird state. The easiest and cleanest should be to remove + -- the backup relation. It won't have an impact because since we are in the else condition, + -- that means that old relation exists therefore no downtime yet. + {%- if old_relation_bkp is not none -%} + {%- do drop_relation(old_relation_bkp) -%} + {%- endif -%} + + {%- call statement('main') -%} + {{ create_table_as(False, tmp_relation, sql) }} + {%- endcall -%} + + {{ rename_relation(old_relation, old_relation_bkp) }} + {{ rename_relation(tmp_relation, target_relation) }} + + {{ drop_relation(old_relation_bkp) }} {%- endif -%} - - {%- call statement('main') -%} - {{ create_table_as(False, tmp_relation, sql) }} - {%- endcall -%} - - {{ rename_relation(old_relation, old_relation_bkp) }} - {{ rename_relation(tmp_relation, target_relation) }} - - {{ drop_relation(old_relation_bkp) }} {%- endif -%} {%- endif -%} diff --git a/tests/functional/adapter/test_change_relation_types.py b/tests/functional/adapter/test_change_relation_types.py new file mode 100644 index 00000000..047cac75 --- /dev/null +++ b/tests/functional/adapter/test_change_relation_types.py @@ -0,0 +1,26 @@ +import pytest + +from dbt.tests.adapter.relations.test_changing_relation_type import ( + BaseChangeRelationTypeValidator, +) + + +class TestChangeRelationTypesHive(BaseChangeRelationTypeValidator): + pass + + +class TestChangeRelationTypesIceberg(BaseChangeRelationTypeValidator): + @pytest.fixture(scope="class") + def project_config_update(self): + return { + "models": { + "+table_type": "iceberg", + } + } + + def test_changing_materialization_changes_relation_type(self, project): + self._run_and_check_materialization("view") + self._run_and_check_materialization("table") + self._run_and_check_materialization("view") + # skip incremntal that doesn't work with Iceberg + self._run_and_check_materialization("table", extra_args=["--full-refresh"])