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

Commit

Permalink
addressed the modification from cmu-db master
Browse files Browse the repository at this point in the history
  • Loading branch information
Dingshilun committed May 5, 2018
1 parent a57a5df commit 5b9818c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 37 deletions.
52 changes: 24 additions & 28 deletions src/catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ std::shared_ptr<SystemCatalogs> Catalog::GetSystemCatalogs(
* @param txn the transaction Context
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid, const std::string &schema_name,
std::unique_ptr<catalog::Schema> &new_schema,
concurrency::TransactionContext *txn) {
LOG_TRACE("AlterTable in Catalog");
Expand All @@ -960,6 +960,7 @@ ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
try {
auto old_table = database->GetTableWithOid(table_oid);
auto old_schema = old_table->GetSchema();
auto pg_index = catalog_map_[database_oid]->GetIndexCatalog();

// Step 1: build empty table with new schema
bool own_schema = true;
Expand All @@ -968,14 +969,12 @@ ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
database_oid, table_oid,
catalog::Schema::CopySchema(new_schema.get()), old_table->GetName(),
DEFAULT_TUPLES_PER_TILEGROUP, own_schema, adapt_table);

// Step 2: Copy indexes
auto old_index_oids =
IndexCatalog::GetInstance()->GetIndexObjects(table_oid, txn);
auto old_index_oids = pg_index->GetIndexObjects(table_oid, txn);
for (auto index_oid_pair : old_index_oids) {
oid_t index_oid = index_oid_pair.first;
// delete record in pg_index
IndexCatalog::GetInstance()->DeleteIndex(index_oid, txn);
pg_index->DeleteIndex(index_oid, txn);
// Check if all indexed columns still exists
auto old_index = old_table->GetIndexWithOid(index_oid);
bool index_exist = true;
Expand Down Expand Up @@ -1014,14 +1013,13 @@ ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
new_table->AddIndex(new_index);

// reinsert record into pg_index
IndexCatalog::GetInstance()->InsertIndex(
index_oid, old_index->GetName(), table_oid,
pg_index->InsertIndex(
index_oid, old_index->GetName(), table_oid, schema_name,
old_index->GetMetadata()->GetIndexType(),
old_index->GetMetadata()->GetIndexConstraintType(),
old_index->GetMetadata()->HasUniqueKeys(), new_key_attrs,
pool_.get(), txn);
}

std::unique_ptr<executor::ExecutorContext> context(
new executor::ExecutorContext(txn, {}));
// Step 3: build column mapping between old table and new table
Expand Down Expand Up @@ -1089,10 +1087,12 @@ ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
// Step 5: delete all the column(attribute) records in pg_attribute
// and reinsert them using new schema(column offset needs to change
// accordingly)
catalog::ColumnCatalog::GetInstance()->DeleteColumns(table_oid, txn);
auto pg_attributes =
catalog_map_[database_oid]->GetColumnCatalog();
pg_attributes->DeleteColumns(table_oid, txn);
oid_t column_offset = 0;
for (auto new_column : new_schema->GetColumns()) {
catalog::ColumnCatalog::GetInstance()->InsertColumn(
pg_attributes->InsertColumn(
table_oid, new_column.GetName(), column_offset,
new_column.GetOffset(), new_column.GetType(),
new_column.IsInlined(), new_column.GetConstraints(), pool_.get(),
Expand Down Expand Up @@ -1143,20 +1143,10 @@ ResultType Catalog::AddColumn(
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/

ResultType Catalog::DropColumn(const std::string &database_name,
const std::string &table_name,
const std::vector<std::string> &columns,
concurrency::TransactionContext *txn) {
try {
oid_t table_oid = Catalog::GetInstance()
->GetTableObject(database_name, table_name, txn)
->GetTableOid();
for (std::string name : columns) {
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, name, txn);
}
} catch (CatalogException &e) {
return ResultType::FAILURE;
}
ResultType Catalog::DropColumn(UNUSED_ATTRIBUTE const std::string &database_name,
UNUSED_ATTRIBUTE const std::string &table_name,
UNUSED_ATTRIBUTE const std::vector<std::string> &columns,
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn) {
return ResultType::SUCCESS;
}

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

try {
// Get table from the name
auto table = Catalog::GetInstance()->GetTableWithName(database_name,
auto table = Catalog::GetInstance()->GetTableWithName(database_name, schema_name,
table_name, txn);
auto schema = table->GetSchema();

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

// Modify the pg_table
oid_t table_oid = Catalog::GetInstance()
->GetTableObject(database_name, table_name, txn)
->GetTableObject(database_name, schema_name, table_name, txn)
->GetTableOid();
bool res = catalog::ColumnCatalog::GetInstance()->RenameColumn(
table_oid, old_name, new_name, txn);
oid_t database_oid = Catalog::GetInstance()
->GetTableObject(database_name, schema_name, table_name, txn)
->GetDatabaseOid();
auto pg_attributes =
catalog_map_[database_oid]->GetColumnCatalog();
bool res = pg_attributes->RenameColumn(
database_oid, table_oid, old_name, new_name, txn);
if (!res) {
throw CatalogException("Change Column name failed.");
}
Expand Down
5 changes: 3 additions & 2 deletions src/catalog/column_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ ColumnCatalog::GetColumnObjects(oid_t table_oid,
* @brief Rename the column name to a new name.
* @ return whether the update succeed
*/
bool ColumnCatalog::RenameColumn(oid_t table_oid,
bool ColumnCatalog::RenameColumn(oid_t database_oid,
oid_t table_oid,
const std::string &column_name,
const std::string &new_name,
concurrency::TransactionContext *txn) {
Expand All @@ -275,7 +276,7 @@ bool ColumnCatalog::RenameColumn(oid_t table_oid,
oid_t index_offset = IndexId::PRIMARY_KEY;

auto table_object =
TableCatalog::GetInstance()->GetTableObject(table_oid, txn);
Catalog::GetInstance()->GetTableObject(database_oid, table_oid, txn);
table_object->EvictColumnObject(column_name);

return UpdateWithIndexScan(update_columns, update_values, scan_values,
Expand Down
11 changes: 6 additions & 5 deletions src/executor/alter_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ bool AlterExecutor::RenameColumn(
peloton::concurrency::TransactionContext *txn) {
auto database_name = node.GetDatabaseName();
auto table_name = node.GetTableName();
auto schema_name = node.GetSchemaName();
auto new_column_name = node.GetNewName();
auto old_column_name = node.GetOldName();

ResultType result = catalog::Catalog::GetInstance()->RenameColumn(
database_name, table_name, old_column_name, new_column_name, txn);
database_name, table_name, old_column_name, new_column_name, schema_name, txn);
txn->SetResult(result);

if (txn->GetResult() == ResultType::SUCCESS) {
Expand All @@ -82,14 +83,14 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
peloton::concurrency::TransactionContext *txn) {
auto database_name = node.GetDatabaseName();
auto table_name = node.GetTableName();

auto schema_name = node.GetSchemaName();
auto table_catalog_obj = catalog::Catalog::GetInstance()->GetTableObject(
database_name, table_name, txn);
database_name, schema_name, table_name, txn);
oid_t database_oid = table_catalog_obj->GetDatabaseOid();
oid_t table_oid = table_catalog_obj->GetTableOid();

auto old_table = catalog::Catalog::GetInstance()->GetTableWithName(
database_name, table_name, txn);
database_name, schema_name, table_name, txn);
auto old_schema = old_table->GetSchema();
std::vector<oid_t> column_ids;

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

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

switch (txn->GetResult()) {
Expand Down
3 changes: 2 additions & 1 deletion src/include/catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Catalog {
//===--------------------------------------------------------------------===//
// ALTER TABLE
//===--------------------------------------------------------------------===//
ResultType AlterTable(oid_t database_oid, oid_t table_oid,
ResultType AlterTable(oid_t database_oid, oid_t table_oid, const std::string &schema_name,
std::unique_ptr<catalog::Schema> &new_schema,
concurrency::TransactionContext *txn);

Expand All @@ -206,6 +206,7 @@ class Catalog {
const std::string &table_name,
const std::string &old_name,
const std::string &new_name,
const std::string &schema_name,
concurrency::TransactionContext *txn);

/*
Expand Down
2 changes: 1 addition & 1 deletion src/include/catalog/column_catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ColumnCatalog : public AbstractCatalog {
bool DeleteColumn(oid_t table_oid, const std::string &column_name,
concurrency::TransactionContext *txn);
bool DeleteColumns(oid_t table_oid, concurrency::TransactionContext *txn);
bool RenameColumn(oid_t table_oid, const std::string &column_name,
bool RenameColumn(oid_t database_oid, oid_t table_oid, const std::string &column_name,
const std::string &new_name,
concurrency::TransactionContext *txn);

Expand Down
4 changes: 4 additions & 0 deletions src/include/planner/alter_plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class AlterPlan : public AbstractPlan {
// return true if the alter plan is rename statement
bool IsRename() const { return this->type == AlterType::RENAME; }

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

// Schema Name
std::string schema_name;
// Schema delta, define the column txn want to add
std::unique_ptr<catalog::Schema> added_columns;
// dropped_column, define the column you want to drop
Expand Down
6 changes: 6 additions & 0 deletions src/parser/postgresparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,9 @@ parser::AlterTableStatement *PostgresParser::AlterTransform(Node *root) {
result->table_info_.get()->database_name =
strdup(relation->catalogname);
}
if (relation->schemaname) {
result->table_info_.get()->schema_name = strdup(relation->schemaname);
}
if (newRoot->subname) {
result->oldName = strdup(newRoot->subname);
}
Expand Down Expand Up @@ -1823,6 +1826,9 @@ parser::AlterTableStatement *PostgresParser::AlterTransform(Node *root) {
result->table_info_.get()->database_name =
strdup(relation->catalogname);
}
if (relation->schemaname) {
result->table_info_.get()->schema_name = strdup(relation->schemaname);
}

for (auto cell = newRoot->cmds->head; cell != NULL; cell = cell->next) {
auto cmd = reinterpret_cast<AlterTableCmd *>(cell->data.ptr_value);
Expand Down
1 change: 1 addition & 0 deletions src/planner/alter_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ AlterPlan::AlterPlan(const std::string &database_name,
AlterPlan::AlterPlan(parser::AlterTableStatement *parse_tree) {
table_name = std::string(parse_tree->GetTableName());
database_name = std::string(parse_tree->GetDatabaseName());
schema_name = std::string(parse_tree->GetSchemaName());
switch (parse_tree->type) {
case parser::AlterTableStatement::AlterTableType::RENAME: {
old_names_.emplace_back(std::string{parse_tree->oldName});
Expand Down

0 comments on commit 5b9818c

Please sign in to comment.