Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit a1e7265

Browse files
authored
Merge pull request #16 from sxzh93/alter_executor
add alter executor
2 parents 21ee68d + 9904788 commit a1e7265

File tree

7 files changed

+136
-43
lines changed

7 files changed

+136
-43
lines changed

src/catalog/catalog.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,10 @@ std::shared_ptr<TableCatalogObject> Catalog::GetTableObject(
804804
* @param txn the transaction Context
805805
* @return TransactionContext ResultType(SUCCESS or FAILURE)
806806
*/
807-
ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
808-
std::unique_ptr<catalog::Schema> new_schema,
809-
concurrency::TransactionContext *txn) {
807+
ResultType Catalog::AlterTable(
808+
UNUSED_ATTRIBUTE oid_t database_oid, UNUSED_ATTRIBUTE oid_t table_oid,
809+
UNUSED_ATTRIBUTE std::unique_ptr<catalog::Schema> &new_schema,
810+
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn) {
810811
LOG_TRACE("AlterTable in Catalog");
811812

812813
if (txn == nullptr)

src/executor/alter_executor.cpp

Lines changed: 113 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
#include "executor/alter_executor.h"
1414

1515
#include "catalog/catalog.h"
16+
#include "catalog/table_catalog.h"
1617
#include "common/logger.h"
1718
#include "executor/executor_context.h"
19+
#include "storage/data_table.h"
1820

1921
namespace peloton {
2022
namespace executor {
2123

2224
// Constructor for alter table executor
2325
AlterExecutor::AlterExecutor(const planner::AbstractPlan *node,
2426
ExecutorContext *executor_context)
25-
: AbstractExecutor(node, executor_context),
26-
isAlter_(
27-
!reinterpret_cast<const planner::AlterPlan *>(node)->IsRename()) {}
27+
: AbstractExecutor(node, executor_context) {}
2828

2929
// Initialize executor
3030
// Nothing to initialize for now
@@ -38,24 +38,20 @@ bool AlterExecutor::DInit() {
3838
bool AlterExecutor::DExecute() {
3939
LOG_TRACE("Executing Alter...");
4040
bool result = false;
41-
if (!isAlter_) {
42-
const planner::AlterPlan &node = GetPlanNode<planner::AlterPlan>();
43-
auto current_txn = executor_context_->GetTransaction();
44-
result = RenameColumn(node, current_txn);
45-
} else {
46-
const planner::AlterPlan &node = GetPlanNode<planner::AlterPlan>();
47-
auto current_txn = executor_context_->GetTransaction();
48-
AlterType type = node.GetAlterTableType();
49-
switch (type) {
50-
case AlterType::ALTER:
51-
result = DropColumn(node, current_txn);
52-
break;
53-
default:
54-
throw NotImplementedException(StringUtil::Format(
55-
"Alter Type not supported, %s", AlterTypeToString(type).c_str()));
56-
}
41+
const planner::AlterPlan &node = GetPlanNode<planner::AlterPlan>();
42+
auto current_txn = executor_context_->GetTransaction();
43+
AlterType type = node.GetAlterTableType();
44+
switch (type) {
45+
case AlterType::RENAME:
46+
result = RenameColumn(node, current_txn);
47+
break;
48+
case AlterType::ALTER:
49+
result = AlterTable(node, current_txn);
50+
break;
51+
default:
52+
throw NotImplementedException(StringUtil::Format(
53+
"Alter Type not supported, %s", AlterTypeToString(type).c_str()));
5754
}
58-
5955
return result;
6056
}
6157

@@ -82,23 +78,109 @@ bool AlterExecutor::RenameColumn(
8278
return false;
8379
}
8480

85-
bool AlterExecutor::DropColumn(const peloton::planner::AlterPlan &node,
81+
bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
8682
peloton::concurrency::TransactionContext *txn) {
8783
auto database_name = node.GetDatabaseName();
8884
auto table_name = node.GetTableName();
89-
auto drop_columns = node.GetDroppedColumns();
9085

91-
ResultType result = catalog::Catalog::GetInstance()->DropColumn(
92-
database_name, table_name, drop_columns, txn);
93-
txn->SetResult(result);
86+
auto table_catalog_obj = catalog::Catalog::GetInstance()->GetTableObject(
87+
database_name, table_name, txn);
88+
oid_t database_oid = table_catalog_obj->GetDatabaseOid();
89+
oid_t table_oid = table_catalog_obj->GetTableOid();
90+
91+
auto old_table = catalog::Catalog::GetInstance()->GetTableWithName(
92+
database_name, table_name, txn);
93+
auto old_schema = old_table->GetSchema();
94+
std::vector<oid_t> column_ids;
95+
96+
// Step 1: remove drop columns from old schema
97+
for (oid_t i = 0; i < old_schema->GetColumnCount(); ++i) {
98+
bool is_found = false;
99+
for (auto drop_column : node.GetDroppedColumns()) {
100+
if (old_schema->GetColumn(i).GetName() == drop_column) {
101+
is_found = true;
102+
}
103+
}
104+
if (!is_found) {
105+
column_ids.push_back(i);
106+
}
107+
}
108+
// Check if dropped column exists
109+
if (column_ids.size() + node.GetDroppedColumns().size() !=
110+
old_schema->GetColumnCount()) {
111+
LOG_TRACE("Dropped column not exists");
112+
txn->SetResult(ResultType::FAILURE);
113+
return false;
114+
}
115+
std::unique_ptr<catalog::Schema> temp_schema(
116+
catalog::Schema::CopySchema(old_schema, column_ids));
117+
118+
// Step 2: change column type if exists
119+
for (auto change_pair : node.GetChangedTypeColumns()) {
120+
bool is_found = false;
121+
oid_t i = 0;
122+
for (; i < temp_schema->GetColumnCount(); ++i) {
123+
if (temp_schema->GetColumn(i).GetName() == change_pair.first) {
124+
is_found = true;
125+
break;
126+
}
127+
}
128+
if (!is_found) {
129+
LOG_TRACE("Change column type failed: Column %s does not exists",
130+
change_pair.first.c_str());
131+
txn->SetResult(ResultType::FAILURE);
132+
return false;
133+
} else {
134+
temp_schema->ChangeColumnType(i, change_pair.second);
135+
}
136+
}
94137

95-
if (txn->GetResult() == ResultType::SUCCESS) {
96-
LOG_TRACE("Drop column succeed!");
138+
// Step 3: append add column to new schema
139+
// construct add column schema
140+
std::vector<catalog::Column> add_columns;
141+
for (size_t i = 0; i < node.GetAddedColumns().size(); ++i) {
142+
for (auto column : node.GetAddedColumns()[i]->GetColumns()) {
143+
add_columns.push_back(column);
144+
}
145+
}
146+
std::unique_ptr<catalog::Schema> add_column_schema(
147+
new catalog::Schema(add_columns));
148+
// Check if added column exists
149+
for (auto new_column : add_column_schema->GetColumns()) {
150+
for (auto old_column : old_schema->GetColumns()) {
151+
if (new_column.GetName() == old_column.GetName()) {
152+
LOG_TRACE("Add column failed: Column %s already exists",
153+
new_column.GetName().c_str());
154+
txn->SetResult(ResultType::FAILURE);
155+
return false;
156+
}
157+
}
158+
}
97159

98-
// TODO: Add on succeed logic if necessary
99-
executor_context_->num_processed = 1;
100-
} else {
101-
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());
160+
// Construct new schema
161+
std::unique_ptr<catalog::Schema> new_schema(catalog::Schema::AppendSchema(
162+
temp_schema.get(), add_column_schema.get()));
163+
164+
// Copy and replace table content to new schema in catalog
165+
ResultType result = catalog::Catalog::GetInstance()->AlterTable(
166+
database_oid, table_oid, new_schema, txn);
167+
txn->SetResult(result);
168+
169+
switch (txn->GetResult()) {
170+
case ResultType::SUCCESS:
171+
LOG_TRACE("Alter table succeed!");
172+
173+
// TODO: Add on succeed logic if necessary
174+
executor_context_->num_processed = 1;
175+
break;
176+
case ResultType::FAILURE:
177+
LOG_TRACE("Alter table failed!");
178+
179+
// TODO: Add on failed logic if necessary
180+
break;
181+
default:
182+
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());
183+
break;
102184
}
103185
return false;
104186
}

src/include/catalog/catalog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class Catalog {
181181
// ALTER TABLE
182182
//===--------------------------------------------------------------------===//
183183
ResultType AlterTable(oid_t database_oid, oid_t table_oid,
184-
std::unique_ptr<catalog::Schema> new_schema,
184+
std::unique_ptr<catalog::Schema> &new_schema,
185185
concurrency::TransactionContext *txn);
186186

187187
ResultType AddColumn(const std::string &database_name,

src/include/catalog/column.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ class Column : public Printable {
3333
// Nothing to see...
3434
}
3535

36-
Column(type::TypeId value_type, size_t column_length,
37-
std::string column_name, bool is_inlined = false,
38-
oid_t column_offset = INVALID_OID)
36+
Column(type::TypeId value_type, size_t column_length, std::string column_name,
37+
bool is_inlined = false, oid_t column_offset = INVALID_OID)
3938
: column_name(column_name),
4039
column_type(value_type),
4140
fixed_length(INVALID_OID),
@@ -78,6 +77,8 @@ class Column : public Printable {
7877

7978
inline type::TypeId GetType() const { return column_type; }
8079

80+
inline void SetType(const type::TypeId &new_type) { column_type = new_type; }
81+
8182
inline bool IsInlined() const { return is_inlined; }
8283

8384
inline bool IsPrimary() const { return is_primary_; }

src/include/catalog/schema.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ class Schema : public Printable {
149149
columns[column_id].column_name = new_name;
150150
}
151151

152+
inline void ChangeColumnType(const oid_t column_id,
153+
const type::TypeId &new_type) {
154+
columns[column_id].SetType(new_type);
155+
}
156+
152157
inline oid_t GetUninlinedColumn(const oid_t column_id) const {
153158
return uninlined_columns[column_id];
154159
}

src/include/executor/alter_executor.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ class AlterExecutor : public AbstractExecutor {
4444
bool RenameColumn(const planner::AlterPlan &node,
4545
concurrency::TransactionContext *txn);
4646

47-
bool DropColumn(const planner::AlterPlan &node,
47+
bool AlterTable(const planner::AlterPlan &node,
4848
concurrency::TransactionContext *txn);
49-
50-
private:
51-
bool isAlter_;
5249
};
5350

5451
} // executor

src/include/planner/alter_plan.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,19 @@ class AlterPlan : public AbstractPlan {
7575

7676
std::string GetDatabaseName() const { return database_name; }
7777

78-
// catalog::Schema *GetAddedColumns() const { return added_columns; }
78+
const std::vector<std::unique_ptr<catalog::Schema>> &GetAddedColumns() const {
79+
return added_columns;
80+
}
7981

8082
const std::vector<std::string> &GetDroppedColumns() const {
8183
return dropped_columns;
8284
}
8385

86+
const std::vector<std::pair<std::string, type::TypeId>> &
87+
GetChangedTypeColumns() const {
88+
return changed_type_columns;
89+
};
90+
8491
AlterType GetAlterTableType() const { return type; }
8592

8693
// function used for rename statement

0 commit comments

Comments
 (0)