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

Commit f7af80f

Browse files
committed
added alter table drop column parser, planner, and catalog support, waiting for executor
1 parent 6a294fc commit f7af80f

File tree

5 files changed

+115
-25
lines changed

5 files changed

+115
-25
lines changed

src/catalog/catalog.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,10 +812,20 @@ ResultType Catalog::AddColumn(
812812
}
813813

814814
ResultType Catalog::DropColumn(
815-
UNUSED_ATTRIBUTE const std::string &database_name,
816-
UNUSED_ATTRIBUTE const std::string &table_name,
817-
UNUSED_ATTRIBUTE const std::vector<std::string> &columns,
818-
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn) {
815+
const std::string &database_name,
816+
const std::string &table_name,
817+
const std::vector<std::string> &columns,
818+
concurrency::TransactionContext *txn) {
819+
try {
820+
oid_t table_oid = Catalog::GetInstance()
821+
->GetTableObject(database_name, table_name, txn)
822+
->GetTableOid();
823+
for (std::string name: columns) {
824+
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, name, txn);
825+
}
826+
} catch(CatalogException &e){
827+
return ResultType::FAILURE;
828+
}
819829
return ResultType::SUCCESS;
820830
}
821831

src/include/common/internal_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,8 @@ std::ostream &operator<<(std::ostream &os, const DropType &type);
646646
enum class AlterType {
647647
INVALID = INVALID_TYPE_ID, // invalid alter type
648648
RENAME = 1, // rename table, column, database...
649+
ADD = 2,
650+
DROP = 3
649651
};
650652
std::string AlterTypeToString(AlterType type);
651653
AlterType StringToAlterType(const std::string &str);

src/optimizer/optimizer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ unique_ptr<planner::AbstractPlan> Optimizer::HandleDDLStatement(
149149
switch (stmt_type) {
150150
case StatementType::ALTER: {
151151
// TODO (shilun) adding support of Alter
152-
LOG_TRACE("TO BE Implemented...");
152+
LOG_TRACE("Adding Alter Plan");
153+
unique_ptr<planner::AbstractPlan> alter_plan(
154+
new planner::AlterPlan((parser::AlterTableStatement *)tree));
155+
ddl_plan = move(alter_plan);
153156
break;
154157
}
155158
case StatementType::RENAME: {

src/parser/postgresparser.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,13 +1732,13 @@ parser::TransactionStatement *PostgresParser::TransactionTransform(
17321732
parser::AlterTableStatement *PostgresParser::AlterTransform(
17331733
AlterTableStmt *root) {
17341734
// TODO (shilun) adding alter type check
1735-
parser::AlterTableStatement *result = new AlterTableStatement(
1736-
parser::AlterTableStatement::AlterTableType::INVALID);
1735+
// Currently we only support add/drop column type
1736+
parser::AlterTableStatement* result =
1737+
new AlterTableStatement(AlterTableStatement::AlterTableType::INVALID);
17371738

17381739
// Get database and table name
1739-
RangeVar *relation = root->relation;
1740-
result->table_info_ =
1741-
std::unique_ptr<parser::TableInfo>(new parser::TableInfo());
1740+
RangeVar* relation = root->relation;
1741+
result->table_info_ = std::unique_ptr<parser::TableInfo>(new parser::TableInfo());
17421742
if (relation->relname) {
17431743
result->table_info_->table_name = strdup(relation->relname);
17441744
}
@@ -1747,22 +1747,22 @@ parser::AlterTableStatement *PostgresParser::AlterTransform(
17471747
}
17481748

17491749
for (auto cell = root->cmds->head; cell != NULL; cell = cell->next) {
1750-
auto cmd = reinterpret_cast<AlterTableCmd *>(cell->data.ptr_value);
1750+
auto cmd = reinterpret_cast<AlterTableCmd*>(cell->data.ptr_value);
17511751
switch (cmd->subtype) {
1752-
/*case AT_AddColumn: {
1753-
auto column =
1754-
ColumnDefTransform(reinterpret_cast<ColumnDef*>(cmd->def));
1755-
result->columns->push_back(column);
1756-
break;
1757-
}
1758-
case AT_DropColumn:
1759-
result->names->push_back(strdup(cmd->name));
1760-
break;
1761-
case AT_AlterColumnGenericOptions:*/
1762-
default: {
1763-
throw NotImplementedException(StringUtil::Format(
1764-
"Alter Table type %d not supported yet...\n", cmd->subtype));
1765-
}
1752+
/*case AT_AddColumn: {
1753+
auto column =
1754+
ColumnDefTransform(reinterpret_cast<ColumnDef*>(cmd->def));
1755+
result->columns->push_back(column);
1756+
break;
1757+
}*/
1758+
case AT_DropColumn:
1759+
result->names->push_back(strdup(cmd->name));
1760+
result->type = AlterTableStatement::AlterTableType::DROP;
1761+
break;
1762+
default: {
1763+
throw NotImplementedException(StringUtil::Format(
1764+
"Alter Table type %d not supported yet...\n", cmd->subtype));
1765+
}
17661766
}
17671767
}
17681768
return result;

src/planner/alter_plan.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// alter_table_plan.cpp
6+
//
7+
// Identification: src/planner/alter_table_plan.cpp
8+
//
9+
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "planner/alter_plan.h"
14+
15+
#include "catalog/column.h"
16+
#include "catalog/schema.h"
17+
#include "parser/alter_statement.h"
18+
#include "storage/data_table.h"
19+
20+
namespace peloton {
21+
namespace planner {
22+
23+
AlterPlan::AlterPlan(const std::string &database_name,
24+
const std::string &table_name,
25+
//std::unique_ptr<catalog::Schema> added_columns,
26+
const std::vector<std::string> &dropped_columns,
27+
AlterType a_type)
28+
: table_name(table_name),
29+
database_name(database_name),
30+
//added_columns(added_columns.release()),
31+
dropped_columns(dropped_columns),
32+
type(a_type) {}
33+
34+
AlterPlan::AlterPlan(parser::AlterTableStatement *parse_tree) {
35+
table_name = std::string(parse_tree->GetTableName());
36+
database_name = std::string(parse_tree->GetDatabaseName());
37+
switch (parse_tree->type){
38+
case parser::AlterTableStatement::AlterTableType::DROP:
39+
for (auto col : *parse_tree->names) {
40+
LOG_TRACE("Drooped column name: %s", col);
41+
dropped_columns.push_back(std::string(col));
42+
type = AlterType::DROP;
43+
}
44+
case parser::AlterTableStatement::AlterTableType::ADD:
45+
default:
46+
LOG_TRACE("Not Implemented the plan yet!");
47+
}
48+
//std::vector<catalog::Column> columns;
49+
// case 1: add column(column name + column data type)
50+
//if (parse_tree->type == AlterTableType::COLUMN) {
51+
// Add columns: traverse through vector of ColumnDefinition
52+
/*for (auto col : *parse_tree->columns) {
53+
type::TypeId val = col->GetValueType(col->type);
54+
LOG_TRACE("Column name: %s", col->name);
55+
56+
bool is_inline = (val == type::TypeId::VARCHAR) ? false : true;
57+
auto column = catalog::Column(val, type::Type::GetTypeSize(val),
58+
std::string(col->name), is_inline);
59+
LOG_TRACE("Column is_line: %d", is_inline);
60+
// Add not_null constraints
61+
if (col->not_null) {
62+
catalog::Constraint constraint(ConstraintType::NOTNULL, "con_not_null");
63+
column.AddConstraint(constraint);
64+
}
65+
columns.push_back(column);
66+
}
67+
added_columns = new catalog::Schema(columns);*/
68+
69+
// Drop columns: traverse through vector of char*(column name)
70+
71+
}
72+
73+
74+
} // namespace planner
75+
} // namespace peloton

0 commit comments

Comments
 (0)