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

Commit 36f111a

Browse files
author
DeanChensj
authored
Merge pull request #11 from sxzh93/add_column_parser_planner
added parser support for change column type and add column
2 parents 66220b5 + 32ec8fd commit 36f111a

File tree

7 files changed

+233
-186
lines changed

7 files changed

+233
-186
lines changed

src/common/internal_types.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ std::string AlterTypeToString(AlterType type) {
426426
case AlterType::RENAME: {
427427
return "RENAME";
428428
}
429+
case AlterType::ALTER: {
430+
return "ALTER";
431+
}
429432
default: {
430433
throw ConversionException(
431434
StringUtil::Format("No string conversion for AlterType value '%d'",
@@ -440,6 +443,8 @@ AlterType StringToAlterType(const std::string &str) {
440443
return AlterType::INVALID;
441444
} else if (upper_str == "RENAME") {
442445
return AlterType::RENAME;
446+
} else if (upper_str == "ALTER") {
447+
return AlterType::ALTER;
443448
} else {
444449
throw ConversionException(StringUtil::Format(
445450
"No AlterType conversion from string '%s'", upper_str.c_str()));

src/executor/alter_executor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace executor {
2323
AlterExecutor::AlterExecutor(const planner::AbstractPlan *node,
2424
ExecutorContext *executor_context)
2525
: AbstractExecutor(node, executor_context),
26-
isAlter_(!reinterpret_cast<const planner::AlterPlan *>(node)->IsRename()) {}
26+
isAlter_(
27+
!reinterpret_cast<const planner::AlterPlan *>(node)->IsRename()) {}
2728

2829
// Initialize executor
2930
// Nothing to initialize for now
@@ -46,7 +47,7 @@ bool AlterExecutor::DExecute() {
4647
auto current_txn = executor_context_->GetTransaction();
4748
AlterType type = node.GetAlterTableType();
4849
switch (type) {
49-
case AlterType::DROP:
50+
case AlterType::ALTER:
5051
result = DropColumn(node, current_txn);
5152
break;
5253
default:

src/include/common/internal_types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,7 @@ 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
649+
ALTER = 2
651650
};
652651
std::string AlterTypeToString(AlterType type);
653652
AlterType StringToAlterType(const std::string &str);
@@ -1230,7 +1229,8 @@ std::ostream &operator<<(std::ostream &os, const RWType &type);
12301229
typedef CuckooMap<ItemPointer, RWType, ItemPointerHasher, ItemPointerComparator>
12311230
ReadWriteSet;
12321231

1233-
typedef tbb::concurrent_unordered_set<ItemPointer, ItemPointerHasher, ItemPointerComparator> WriteSet;
1232+
typedef tbb::concurrent_unordered_set<ItemPointer, ItemPointerHasher,
1233+
ItemPointerComparator> WriteSet;
12341234

12351235
// this enum is to identify why the version should be GC'd.
12361236
enum class GCVersionType {

src/include/parser/alter_statement.h

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "parser/sql_statement.h"
1616
#include "common/sql_node_visitor.h"
17+
#include "parser/create_statement.h"
1718

1819
namespace peloton {
1920
namespace parser {
@@ -24,39 +25,50 @@ namespace parser {
2425
*/
2526
class AlterTableStatement : public TableRefStatement {
2627
public:
27-
enum class AlterTableType { INVALID = 0, ADD = 1, DROP = 2, RENAME = 3 };
28+
enum class AlterTableType { INVALID = 0, ALTER = 1, RENAME = 2 };
2829
AlterTableStatement(AlterTableType type)
2930
: TableRefStatement(StatementType::ALTER),
3031
type(type),
31-
names(new std::vector<char*>),
3232
oldName(nullptr),
33-
newName(nullptr)
34-
{};
33+
newName(nullptr) {
34+
dropped_names =
35+
type == AlterTableType::RENAME ? nullptr : (new std::vector<char *>);
36+
added_columns = type == AlterTableType::RENAME
37+
? nullptr
38+
: (new std::vector<std::unique_ptr<ColumnDefinition>>);
39+
changed_type_columns =
40+
type == AlterTableType::RENAME
41+
? nullptr
42+
: (new std::vector<std::unique_ptr<ColumnDefinition>>);
43+
}
3544

3645
virtual ~AlterTableStatement() {
37-
/*if (columns != nullptr) {
38-
for (auto col : *columns) delete col;
39-
delete columns;
40-
}*/
41-
if (names != nullptr) {
42-
for (auto name : *names) delete name;
43-
delete names;
46+
if (added_columns != nullptr) {
47+
delete added_columns;
48+
}
49+
if (dropped_names != nullptr) {
50+
for (auto name : *dropped_names) delete name;
51+
delete dropped_names;
52+
}
53+
if (changed_type_columns != nullptr) {
54+
delete changed_type_columns;
4455
}
4556
if (oldName) delete oldName;
4657
if (newName) delete newName;
4758
}
4859

49-
virtual void Accept(SqlNodeVisitor* v) override { v->Visit(this); }
60+
virtual void Accept(SqlNodeVisitor *v) override { v->Visit(this); }
5061

5162
AlterTableType type;
5263

5364
// Dropped columns
54-
std::vector<char*>* names;
65+
std::vector<char *> *dropped_names;
5566

5667
// Added columns
57-
//std::vector<ColumnDefinition*>* columns;
68+
std::vector<std::unique_ptr<ColumnDefinition>> *added_columns;
5869

59-
// the name that needs to be changed
70+
std::vector<std::unique_ptr<ColumnDefinition>> *changed_type_columns;
71+
// the name that needs to be changed
6072
char *oldName;
6173
char *newName;
6274
};

src/include/planner/alter_plan.h

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ class AlterPlan : public AbstractPlan {
3333
public:
3434
AlterPlan() = delete;
3535

36-
explicit AlterPlan(const std::string &database_name,
37-
const std::string &table_name,
38-
//std::unique_ptr<catalog::Schema> added_columns,
39-
const std::vector<std::string> &dropped_columns,
40-
AlterType a_type);
36+
explicit AlterPlan(
37+
const std::string &database_name, const std::string &table_name,
38+
const std::vector<std::string> &dropped_columns,
39+
const std::vector<std::unique_ptr<catalog::Schema>> &added_columns,
40+
AlterType a_type);
4141
explicit AlterPlan(const std::string &database_name,
4242
const std::string &table_name,
4343
const std::vector<std::string> &old_names,
44-
const std::vector<std::string> &new_names, AlterType a_type);
44+
const std::vector<std::string> &new_names,
45+
AlterType a_type);
4546
explicit AlterPlan(parser::AlterTableStatement *parse_tree);
4647

4748
virtual ~AlterPlan() {}
@@ -52,49 +53,47 @@ class AlterPlan : public AbstractPlan {
5253

5354
const std::string GetInfo() const {
5455
return StringUtil::Format("AlterPlan table:%s, database:%s",
55-
this->table_name.c_str(), this->database_name.c_str());
56+
this->table_name.c_str(),
57+
this->database_name.c_str());
5658
}
5759

5860
std::unique_ptr<AbstractPlan> Copy() const {
59-
switch(this->type) {
60-
case AlterType::DROP:
61-
case AlterType::ADD:
62-
return std::unique_ptr<AbstractPlan>(
63-
new AlterPlan(database_name, table_name,
64-
//std::unique_ptr<catalog::Schema>(
65-
// catalog::Schema::CopySchema(added_columns)),
66-
dropped_columns, type));
67-
case AlterType::RENAME:
68-
return std::unique_ptr<AbstractPlan>(
69-
new AlterPlan(database_name, table_name, old_names_, new_names_, type));
70-
default:
71-
LOG_ERROR("Not supported Copy of Alter type yet");
72-
return nullptr;
61+
switch (this->type) {
62+
case AlterType::ALTER:
63+
return std::unique_ptr<AbstractPlan>(new AlterPlan(
64+
database_name, table_name, dropped_columns, added_columns, type));
65+
case AlterType::RENAME:
66+
return std::unique_ptr<AbstractPlan>(new AlterPlan(
67+
database_name, table_name, old_names_, new_names_, type));
68+
default:
69+
LOG_ERROR("Not supported Copy of Alter type yet");
70+
return nullptr;
7371
}
7472
}
7573

7674
std::string GetTableName() const { return table_name; }
7775

7876
std::string GetDatabaseName() const { return database_name; }
7977

80-
//catalog::Schema *GetAddedColumns() const { return added_columns; }
78+
// catalog::Schema *GetAddedColumns() const { return added_columns; }
8179

8280
const std::vector<std::string> &GetDroppedColumns() const {
8381
return dropped_columns;
8482
}
8583

8684
AlterType GetAlterTableType() const { return type; }
8785

88-
//function used for rename statement
86+
// function used for rename statement
8987
std::string GetOldName() const { return this->old_names_[0]; }
9088

91-
//function used for rename statement
89+
// function used for rename statement
9290
std::string GetNewName() const { return this->new_names_[0]; }
9391

94-
//return true if the alter plan is rename statement
95-
bool IsRename() const { return this->type==AlterType::RENAME;}
96-
private:
97-
// Target Table
92+
// return true if the alter plan is rename statement
93+
bool IsRename() const { return this->type == AlterType::RENAME; }
94+
95+
private:
96+
// Target Table
9897
storage::DataTable *target_table_ = nullptr;
9998

10099
// Table Name
@@ -104,17 +103,17 @@ class AlterPlan : public AbstractPlan {
104103
std::string database_name;
105104

106105
// Schema delta, define the column txn want to add
107-
// catalog::Schema *added_columns;
106+
std::vector<std::unique_ptr<catalog::Schema>> added_columns;
108107
// dropped_column, define the column you want to drop
109108
std::vector<std::string> dropped_columns;
110-
111-
//used for store rename function data
109+
// changed-type columns, define the column you want to change type
110+
std::vector<std::pair<std::string, type::TypeId>> changed_type_columns;
111+
// used for store rename function data
112112
std::vector<std::string> old_names_;
113113
std::vector<std::string> new_names_;
114114

115115
// Check to either AlterTable Table, INDEX or Rename
116116
AlterType type;
117-
118117
};
119118
}
120119
}

0 commit comments

Comments
 (0)