From 9c8139f1e2d39d0c487df5e5387e58eabee388db Mon Sep 17 00:00:00 2001 From: Zhenbang <122523068+zli06160@users.noreply.github.com> Date: Tue, 22 Aug 2023 15:36:51 +0200 Subject: [PATCH] fix: distributed_table materialization issue (#184) --- dbt/include/clickhouse/macros/adapters.sql | 2 +- .../clickhouse/macros/materializations/distributed_table.sql | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/dbt/include/clickhouse/macros/adapters.sql b/dbt/include/clickhouse/macros/adapters.sql index 8ef23409..2d5301b6 100644 --- a/dbt/include/clickhouse/macros/adapters.sql +++ b/dbt/include/clickhouse/macros/adapters.sql @@ -106,7 +106,7 @@ {% macro exchange_tables_atomic(old_relation, target_relation, obj_types='TABLES') %} - {%- if adapter.get_clickhouse_cluster_name() is not none and obj_types == 'TABLES' %} + {%- if adapter.get_clickhouse_cluster_name() is not none and obj_types == 'TABLES' and 'Replicated' in engine_clause() %} {% do run_query("SYSTEM SYNC REPLICA " + on_cluster_clause() + target_relation.schema + '.' + target_relation.identifier) %} {%- endif %} diff --git a/dbt/include/clickhouse/macros/materializations/distributed_table.sql b/dbt/include/clickhouse/macros/materializations/distributed_table.sql index 4713db71..dfccc0d7 100644 --- a/dbt/include/clickhouse/macros/materializations/distributed_table.sql +++ b/dbt/include/clickhouse/macros/materializations/distributed_table.sql @@ -46,13 +46,14 @@ {% elif existing_relation.can_exchange %} -- We can do an atomic exchange, so no need for an intermediate {% call statement('main') -%} - {% do run_query(create_empty_table_from_relation(backup_relation, view_relation)) or '' %} + {{ create_empty_table_from_relation(backup_relation, view_relation) }} {%- endcall %} - {% do exchange_tables_atomic(backup_relation, existing_relation) %} + {% do exchange_tables_atomic(backup_relation, existing_relation_local) %} {% else %} {% do run_query(create_empty_table_from_relation(intermediate_relation, view_relation)) or '' %} {{ adapter.rename_relation(existing_relation_local, backup_relation) }} {{ adapter.rename_relation(intermediate_relation, target_relation_local) }} + {{ create_distributed_table(target_relation, target_relation_local) }} {% endif %} {% do run_query(clickhouse__insert_into(target_relation, sql)) or '' %} {{ drop_relation_if_exists(view_relation) }}