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

Commit 5b9818c

Browse files
committed
addressed the modification from cmu-db master
1 parent a57a5df commit 5b9818c

File tree

8 files changed

+47
-37
lines changed

8 files changed

+47
-37
lines changed

src/catalog/catalog.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ std::shared_ptr<SystemCatalogs> Catalog::GetSystemCatalogs(
947947
* @param txn the transaction Context
948948
* @return TransactionContext ResultType(SUCCESS or FAILURE)
949949
*/
950-
ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
950+
ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid, const std::string &schema_name,
951951
std::unique_ptr<catalog::Schema> &new_schema,
952952
concurrency::TransactionContext *txn) {
953953
LOG_TRACE("AlterTable in Catalog");
@@ -960,6 +960,7 @@ ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
960960
try {
961961
auto old_table = database->GetTableWithOid(table_oid);
962962
auto old_schema = old_table->GetSchema();
963+
auto pg_index = catalog_map_[database_oid]->GetIndexCatalog();
963964

964965
// Step 1: build empty table with new schema
965966
bool own_schema = true;
@@ -968,14 +969,12 @@ ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
968969
database_oid, table_oid,
969970
catalog::Schema::CopySchema(new_schema.get()), old_table->GetName(),
970971
DEFAULT_TUPLES_PER_TILEGROUP, own_schema, adapt_table);
971-
972972
// Step 2: Copy indexes
973-
auto old_index_oids =
974-
IndexCatalog::GetInstance()->GetIndexObjects(table_oid, txn);
973+
auto old_index_oids = pg_index->GetIndexObjects(table_oid, txn);
975974
for (auto index_oid_pair : old_index_oids) {
976975
oid_t index_oid = index_oid_pair.first;
977976
// delete record in pg_index
978-
IndexCatalog::GetInstance()->DeleteIndex(index_oid, txn);
977+
pg_index->DeleteIndex(index_oid, txn);
979978
// Check if all indexed columns still exists
980979
auto old_index = old_table->GetIndexWithOid(index_oid);
981980
bool index_exist = true;
@@ -1014,14 +1013,13 @@ ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
10141013
new_table->AddIndex(new_index);
10151014

10161015
// reinsert record into pg_index
1017-
IndexCatalog::GetInstance()->InsertIndex(
1018-
index_oid, old_index->GetName(), table_oid,
1016+
pg_index->InsertIndex(
1017+
index_oid, old_index->GetName(), table_oid, schema_name,
10191018
old_index->GetMetadata()->GetIndexType(),
10201019
old_index->GetMetadata()->GetIndexConstraintType(),
10211020
old_index->GetMetadata()->HasUniqueKeys(), new_key_attrs,
10221021
pool_.get(), txn);
10231022
}
1024-
10251023
std::unique_ptr<executor::ExecutorContext> context(
10261024
new executor::ExecutorContext(txn, {}));
10271025
// Step 3: build column mapping between old table and new table
@@ -1089,10 +1087,12 @@ ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
10891087
// Step 5: delete all the column(attribute) records in pg_attribute
10901088
// and reinsert them using new schema(column offset needs to change
10911089
// accordingly)
1092-
catalog::ColumnCatalog::GetInstance()->DeleteColumns(table_oid, txn);
1090+
auto pg_attributes =
1091+
catalog_map_[database_oid]->GetColumnCatalog();
1092+
pg_attributes->DeleteColumns(table_oid, txn);
10931093
oid_t column_offset = 0;
10941094
for (auto new_column : new_schema->GetColumns()) {
1095-
catalog::ColumnCatalog::GetInstance()->InsertColumn(
1095+
pg_attributes->InsertColumn(
10961096
table_oid, new_column.GetName(), column_offset,
10971097
new_column.GetOffset(), new_column.GetType(),
10981098
new_column.IsInlined(), new_column.GetConstraints(), pool_.get(),
@@ -1143,20 +1143,10 @@ ResultType Catalog::AddColumn(
11431143
* @return TransactionContext ResultType(SUCCESS or FAILURE)
11441144
*/
11451145

1146-
ResultType Catalog::DropColumn(const std::string &database_name,
1147-
const std::string &table_name,
1148-
const std::vector<std::string> &columns,
1149-
concurrency::TransactionContext *txn) {
1150-
try {
1151-
oid_t table_oid = Catalog::GetInstance()
1152-
->GetTableObject(database_name, table_name, txn)
1153-
->GetTableOid();
1154-
for (std::string name : columns) {
1155-
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, name, txn);
1156-
}
1157-
} catch (CatalogException &e) {
1158-
return ResultType::FAILURE;
1159-
}
1146+
ResultType Catalog::DropColumn(UNUSED_ATTRIBUTE const std::string &database_name,
1147+
UNUSED_ATTRIBUTE const std::string &table_name,
1148+
UNUSED_ATTRIBUTE const std::vector<std::string> &columns,
1149+
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn) {
11601150
return ResultType::SUCCESS;
11611151
}
11621152

@@ -1172,6 +1162,7 @@ ResultType Catalog::RenameColumn(const std::string &database_name,
11721162
const std::string &table_name,
11731163
const std::string &old_name,
11741164
const std::string &new_name,
1165+
const std::string &schema_name,
11751166
concurrency::TransactionContext *txn) {
11761167
if (txn == nullptr) {
11771168
throw CatalogException("Change Column requires transaction.");
@@ -1185,7 +1176,7 @@ ResultType Catalog::RenameColumn(const std::string &database_name,
11851176

11861177
try {
11871178
// Get table from the name
1188-
auto table = Catalog::GetInstance()->GetTableWithName(database_name,
1179+
auto table = Catalog::GetInstance()->GetTableWithName(database_name, schema_name,
11891180
table_name, txn);
11901181
auto schema = table->GetSchema();
11911182

@@ -1206,10 +1197,15 @@ ResultType Catalog::RenameColumn(const std::string &database_name,
12061197

12071198
// Modify the pg_table
12081199
oid_t table_oid = Catalog::GetInstance()
1209-
->GetTableObject(database_name, table_name, txn)
1200+
->GetTableObject(database_name, schema_name, table_name, txn)
12101201
->GetTableOid();
1211-
bool res = catalog::ColumnCatalog::GetInstance()->RenameColumn(
1212-
table_oid, old_name, new_name, txn);
1202+
oid_t database_oid = Catalog::GetInstance()
1203+
->GetTableObject(database_name, schema_name, table_name, txn)
1204+
->GetDatabaseOid();
1205+
auto pg_attributes =
1206+
catalog_map_[database_oid]->GetColumnCatalog();
1207+
bool res = pg_attributes->RenameColumn(
1208+
database_oid, table_oid, old_name, new_name, txn);
12131209
if (!res) {
12141210
throw CatalogException("Change Column name failed.");
12151211
}

src/catalog/column_catalog.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ ColumnCatalog::GetColumnObjects(oid_t table_oid,
256256
* @brief Rename the column name to a new name.
257257
* @ return whether the update succeed
258258
*/
259-
bool ColumnCatalog::RenameColumn(oid_t table_oid,
259+
bool ColumnCatalog::RenameColumn(oid_t database_oid,
260+
oid_t table_oid,
260261
const std::string &column_name,
261262
const std::string &new_name,
262263
concurrency::TransactionContext *txn) {
@@ -275,7 +276,7 @@ bool ColumnCatalog::RenameColumn(oid_t table_oid,
275276
oid_t index_offset = IndexId::PRIMARY_KEY;
276277

277278
auto table_object =
278-
TableCatalog::GetInstance()->GetTableObject(table_oid, txn);
279+
Catalog::GetInstance()->GetTableObject(database_oid, table_oid, txn);
279280
table_object->EvictColumnObject(column_name);
280281

281282
return UpdateWithIndexScan(update_columns, update_values, scan_values,

src/executor/alter_executor.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ bool AlterExecutor::RenameColumn(
6060
peloton::concurrency::TransactionContext *txn) {
6161
auto database_name = node.GetDatabaseName();
6262
auto table_name = node.GetTableName();
63+
auto schema_name = node.GetSchemaName();
6364
auto new_column_name = node.GetNewName();
6465
auto old_column_name = node.GetOldName();
6566

6667
ResultType result = catalog::Catalog::GetInstance()->RenameColumn(
67-
database_name, table_name, old_column_name, new_column_name, txn);
68+
database_name, table_name, old_column_name, new_column_name, schema_name, txn);
6869
txn->SetResult(result);
6970

7071
if (txn->GetResult() == ResultType::SUCCESS) {
@@ -82,14 +83,14 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
8283
peloton::concurrency::TransactionContext *txn) {
8384
auto database_name = node.GetDatabaseName();
8485
auto table_name = node.GetTableName();
85-
86+
auto schema_name = node.GetSchemaName();
8687
auto table_catalog_obj = catalog::Catalog::GetInstance()->GetTableObject(
87-
database_name, table_name, txn);
88+
database_name, schema_name, table_name, txn);
8889
oid_t database_oid = table_catalog_obj->GetDatabaseOid();
8990
oid_t table_oid = table_catalog_obj->GetTableOid();
9091

9192
auto old_table = catalog::Catalog::GetInstance()->GetTableWithName(
92-
database_name, table_name, txn);
93+
database_name, schema_name, table_name, txn);
9394
auto old_schema = old_table->GetSchema();
9495
std::vector<oid_t> column_ids;
9596

@@ -160,7 +161,7 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
160161

161162
// Copy and replace table content to new schema in catalog
162163
ResultType result = catalog::Catalog::GetInstance()->AlterTable(
163-
database_oid, table_oid, new_schema, txn);
164+
database_oid, table_oid, schema_name, new_schema, txn);
164165
txn->SetResult(result);
165166

166167
switch (txn->GetResult()) {

src/include/catalog/catalog.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class Catalog {
188188
//===--------------------------------------------------------------------===//
189189
// ALTER TABLE
190190
//===--------------------------------------------------------------------===//
191-
ResultType AlterTable(oid_t database_oid, oid_t table_oid,
191+
ResultType AlterTable(oid_t database_oid, oid_t table_oid, const std::string &schema_name,
192192
std::unique_ptr<catalog::Schema> &new_schema,
193193
concurrency::TransactionContext *txn);
194194

@@ -206,6 +206,7 @@ class Catalog {
206206
const std::string &table_name,
207207
const std::string &old_name,
208208
const std::string &new_name,
209+
const std::string &schema_name,
209210
concurrency::TransactionContext *txn);
210211

211212
/*

src/include/catalog/column_catalog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class ColumnCatalog : public AbstractCatalog {
8989
bool DeleteColumn(oid_t table_oid, const std::string &column_name,
9090
concurrency::TransactionContext *txn);
9191
bool DeleteColumns(oid_t table_oid, concurrency::TransactionContext *txn);
92-
bool RenameColumn(oid_t table_oid, const std::string &column_name,
92+
bool RenameColumn(oid_t database_oid, oid_t table_oid, const std::string &column_name,
9393
const std::string &new_name,
9494
concurrency::TransactionContext *txn);
9595

src/include/planner/alter_plan.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class AlterPlan : public AbstractPlan {
100100
// return true if the alter plan is rename statement
101101
bool IsRename() const { return this->type == AlterType::RENAME; }
102102

103+
// return schema name
104+
std::string GetSchemaName() const { return this->schema_name; }
103105
private:
104106
// Target Table
105107
storage::DataTable *target_table_ = nullptr;
@@ -110,6 +112,8 @@ class AlterPlan : public AbstractPlan {
110112
// Database Name
111113
std::string database_name;
112114

115+
// Schema Name
116+
std::string schema_name;
113117
// Schema delta, define the column txn want to add
114118
std::unique_ptr<catalog::Schema> added_columns;
115119
// dropped_column, define the column you want to drop

src/parser/postgresparser.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,9 @@ parser::AlterTableStatement *PostgresParser::AlterTransform(Node *root) {
17961796
result->table_info_.get()->database_name =
17971797
strdup(relation->catalogname);
17981798
}
1799+
if (relation->schemaname) {
1800+
result->table_info_.get()->schema_name = strdup(relation->schemaname);
1801+
}
17991802
if (newRoot->subname) {
18001803
result->oldName = strdup(newRoot->subname);
18011804
}
@@ -1823,6 +1826,9 @@ parser::AlterTableStatement *PostgresParser::AlterTransform(Node *root) {
18231826
result->table_info_.get()->database_name =
18241827
strdup(relation->catalogname);
18251828
}
1829+
if (relation->schemaname) {
1830+
result->table_info_.get()->schema_name = strdup(relation->schemaname);
1831+
}
18261832

18271833
for (auto cell = newRoot->cmds->head; cell != NULL; cell = cell->next) {
18281834
auto cmd = reinterpret_cast<AlterTableCmd *>(cell->data.ptr_value);

src/planner/alter_plan.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ AlterPlan::AlterPlan(const std::string &database_name,
4848
AlterPlan::AlterPlan(parser::AlterTableStatement *parse_tree) {
4949
table_name = std::string(parse_tree->GetTableName());
5050
database_name = std::string(parse_tree->GetDatabaseName());
51+
schema_name = std::string(parse_tree->GetSchemaName());
5152
switch (parse_tree->type) {
5253
case parser::AlterTableStatement::AlterTableType::RENAME: {
5354
old_names_.emplace_back(std::string{parse_tree->oldName});

0 commit comments

Comments
 (0)