diff --git a/src/Storages/MaterializedView/RefreshTask.cpp b/src/Storages/MaterializedView/RefreshTask.cpp index fdf3948de70..fa5b3df75a2 100644 --- a/src/Storages/MaterializedView/RefreshTask.cpp +++ b/src/Storages/MaterializedView/RefreshTask.cpp @@ -188,6 +188,11 @@ RefreshTask::ExecutionResult RefreshTask::executeRefresh() while (!interrupt_execution.load() && not_finished) not_finished = refresh_executor->executeStep(interrupt_execution); + auto defer = make_scope_guard([this] + { + canceled.store(false); + interrupt_execution.store(false); + }); if (!not_finished) return ExecutionResult::Finished; if (interrupt_execution.load() && !canceled.load()) @@ -206,9 +211,6 @@ void RefreshTask::initializeRefresh(std::shared_ptrpipeline.setProgressCallback([this](const Progress & progress){ progressCallback(progress); }); - canceled.store(false); - interrupt_execution.store(false); - refresh_executor.emplace(refresh_block->pipeline); } diff --git a/src/Storages/StorageMaterializedView.cpp b/src/Storages/StorageMaterializedView.cpp index 42191bde90e..0939ddcd91a 100644 --- a/src/Storages/StorageMaterializedView.cpp +++ b/src/Storages/StorageMaterializedView.cpp @@ -315,7 +315,7 @@ std::shared_ptr StorageMaterializedView::prepareRefreshQuery() c return insert_query; } -StorageID StorageMaterializedView::exchangeTargetTable(const StorageID & fresh_table) +StorageID StorageMaterializedView::exchangeTargetTable(StorageID fresh_table) { auto stale_table_id = getTargetTableId(); @@ -326,7 +326,9 @@ StorageID StorageMaterializedView::exchangeTargetTable(const StorageID & fresh_t target_db->renameTable( rename_ctx, fresh_table.table_name, *db, stale_table_id.table_name, /*exchange=*/true, /*dictionary=*/false); - setTargetTableId(fresh_table); + std::swap(stale_table_id.database_name, fresh_table.database_name); + std::swap(stale_table_id.table_name, fresh_table.table_name); + setTargetTableId(std::move(fresh_table)); return stale_table_id; } diff --git a/src/Storages/StorageMaterializedView.h b/src/Storages/StorageMaterializedView.h index f3b777d34fa..ff7b50340c7 100644 --- a/src/Storages/StorageMaterializedView.h +++ b/src/Storages/StorageMaterializedView.h @@ -121,7 +121,7 @@ private: StorageID createFreshTable() const; std::shared_ptr prepareRefreshQuery() const; - StorageID exchangeTargetTable(const StorageID & fresh_table); + StorageID exchangeTargetTable(StorageID fresh_table); StorageID getTargetTableId() const; void setTargetTableId(StorageID id);