-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdistributed_alter_table.cpp
More file actions
77 lines (61 loc) · 2.92 KB
/
Copy pathdistributed_alter_table.cpp
File metadata and controls
77 lines (61 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "distributed_alter_table.hpp"
#include "distributed_client.hpp"
#include "duckdb/catalog/catalog.hpp"
#include "duckdb/catalog/catalog_entry/duck_table_entry.hpp"
#include "duckdb/catalog/catalog_transaction.hpp"
#include "duckdb/execution/execution_context.hpp"
#include "duckdb/logging/logger.hpp"
#include "duckdb/main/client_context.hpp"
#include "duckdb/main/database.hpp"
#include "duckdb/parser/parsed_data/alter_table_info.hpp"
#include "utils/catalog_utils.hpp"
namespace duckdb {
namespace {
// Global source state for tracking remote ALTER TABLE execution.
class RemoteAlterTableGlobalState : public GlobalSourceState {
public:
RemoteAlterTableGlobalState() : executed(false) {
}
bool executed;
mutex lock;
idx_t MaxThreads() override {
return 1; // Single-threaded execution
}
};
} // namespace
PhysicalRemoteAlterTableOperator::PhysicalRemoteAlterTableOperator(PhysicalPlan &physical_plan,
unique_ptr<AlterTableInfo> info_p,
string catalog_name_p, string schema_name_p,
string table_name_p, idx_t estimated_cardinality)
: PhysicalOperator(physical_plan, PhysicalOperatorType::EXTENSION, {LogicalType {LogicalTypeId::BIGINT}},
estimated_cardinality),
info(std::move(info_p)), catalog_name(std::move(catalog_name_p)), schema_name(std::move(schema_name_p)),
table_name(std::move(table_name_p)) {
}
unique_ptr<GlobalSourceState> PhysicalRemoteAlterTableOperator::GetGlobalSourceState(ClientContext &context) const {
return make_uniq<RemoteAlterTableGlobalState>();
}
SourceResultType PhysicalRemoteAlterTableOperator::GetDataInternal(ExecutionContext &context, DataChunk &chunk,
OperatorSourceInput &input) const {
auto &gstate = input.global_state.Cast<RemoteAlterTableGlobalState>();
auto &db_instance = DatabaseInstance::GetDatabase(context.client);
// Execute the ALTER TABLE on the remote server.
lock_guard<mutex> lock(gstate.lock);
if (gstate.executed) {
return SourceResultType::FINISHED;
}
// Generate ALTER TABLE SQL and remove catalog prefix for remote execution.
string alter_sql = SanitizeQuery(info->ToString(), catalog_name);
DUCKDB_LOG_DEBUG(db_instance, StringUtil::Format("Executing ALTER TABLE on remote server: %s", alter_sql));
auto &catalog = Catalog::GetCatalog(context.client, catalog_name);
auto &client = GetDistributedClient(catalog);
auto result = client.ExecuteSQL(alter_sql);
if (result->HasError()) {
throw Exception(ExceptionType::CATALOG, "Failed to alter table on server: " + result->GetError());
}
// Now apply the alteration locally to update the catalog.
catalog.Alter(context.client, *info);
gstate.executed = true;
return SourceResultType::FINISHED;
}
} // namespace duckdb