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

Commit 9001841

Browse files
author
Shangjie Chen
committed
Addressed commments from index team
1 parent 6c986fd commit 9001841

File tree

9 files changed

+257
-255
lines changed

9 files changed

+257
-255
lines changed

src/catalog/catalog.cpp

Lines changed: 169 additions & 180 deletions
Large diffs are not rendered by default.

src/executor/alter_executor.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ AlterExecutor::AlterExecutor(const planner::AbstractPlan *node,
2929
// Initialize executor
3030
// Nothing to initialize for now
3131
bool AlterExecutor::DInit() {
32-
LOG_TRACE("Initializing Alter Executer...");
32+
LOG_TRACE("Initializing Alter Executor...");
3333

3434
LOG_TRACE("Alter Executor initialized!");
3535
return true;
3636
}
3737

3838
bool AlterExecutor::DExecute() {
3939
LOG_TRACE("Executing Alter...");
40-
bool result = false;
40+
bool result;
4141
const planner::AlterPlan &node = GetPlanNode<planner::AlterPlan>();
4242
auto current_txn = executor_context_->GetTransaction();
4343
AlterType type = node.GetAlterTableType();
44+
45+
// TODO: grab per-table lock before execution.
4446
switch (type) {
4547
case AlterType::RENAME:
4648
result = RenameColumn(node, current_txn);
@@ -65,13 +67,13 @@ bool AlterExecutor::RenameColumn(
6567
auto old_column_name = node.GetOldName();
6668

6769
ResultType result = catalog::Catalog::GetInstance()->RenameColumn(
68-
database_name, table_name, old_column_name, new_column_name, schema_name, txn);
70+
database_name, table_name, old_column_name, new_column_name, schema_name,
71+
txn);
6972
txn->SetResult(result);
7073

7174
if (txn->GetResult() == ResultType::SUCCESS) {
7275
LOG_TRACE("Rename column succeeded!");
7376

74-
// TODO: Add on succeed logic if necessary
7577
executor_context_->num_processed = 1;
7678
} else {
7779
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());
@@ -133,8 +135,6 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
133135
return false;
134136
} else {
135137
columns[i] = std::move(change_col);
136-
// TODO: decide VARCHAR's size when change type
137-
// if (change_pair.second == type::TypeId::VARCHAR) {}
138138
}
139139
}
140140

@@ -168,13 +168,11 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
168168
case ResultType::SUCCESS:
169169
LOG_TRACE("Alter table succeed!");
170170

171-
// TODO: Add on succeed logic if necessary
172171
executor_context_->num_processed = 1;
173172
break;
174173
case ResultType::FAILURE:
175174
LOG_TRACE("Alter table failed!");
176175

177-
// TODO: Add on failed logic if necessary
178176
break;
179177
default:
180178
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());

src/include/catalog/column_catalog.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ 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 database_oid, oid_t table_oid, const std::string &column_name,
92+
bool RenameColumn(oid_t database_oid, oid_t table_oid,
93+
const std::string &column_name,
9394
const std::string &new_name,
9495
concurrency::TransactionContext *txn);
9596

src/include/concurrency/transaction_context.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ class TransactionContext : public Printable {
5151

5252
public:
5353
TransactionContext(const size_t thread_id, const IsolationLevelType isolation,
54-
const cid_t &read_id);
54+
const cid_t &read_id);
5555

5656
TransactionContext(const size_t thread_id, const IsolationLevelType isolation,
57-
const cid_t &read_id, const cid_t &commit_id);
58-
57+
const cid_t &read_id, const cid_t &commit_id);
58+
5959
TransactionContext(const size_t thread_id, const IsolationLevelType isolation,
60-
const cid_t &read_id, const cid_t &commit_id,
61-
const size_t read_write_set_size);
60+
const cid_t &read_id, const cid_t &commit_id,
61+
const size_t read_write_set_size);
6262

6363
/**
6464
* @brief Destroys the object.
@@ -126,8 +126,9 @@ class TransactionContext : public Printable {
126126
*
127127
* @return The query strings.
128128
*/
129-
inline const std::vector<std::string>& GetQueryStrings() const {
130-
return query_strings_; }
129+
inline const std::vector<std::string> &GetQueryStrings() const {
130+
return query_strings_;
131+
}
131132

132133
/**
133134
* @brief Sets the commit identifier.
@@ -142,7 +143,7 @@ class TransactionContext : public Printable {
142143
* @param[in] epoch_id The epoch identifier
143144
*/
144145
inline void SetEpochId(const eid_t epoch_id) { epoch_id_ = epoch_id; }
145-
146+
146147
/**
147148
* @brief Sets the timestamp.
148149
*
@@ -155,18 +156,18 @@ class TransactionContext : public Printable {
155156
*
156157
* @param[in] query_string The query string
157158
*/
158-
inline void AddQueryString(const char* query_string) {
159+
inline void AddQueryString(const char *query_string) {
159160
query_strings_.push_back(std::string(query_string));
160161
}
161162

162163
void RecordCreate(oid_t database_oid, oid_t table_oid, oid_t index_oid) {
163-
rw_object_set_.push_back(std::make_tuple(database_oid, table_oid,
164-
index_oid, DDLType::CREATE));
164+
rw_object_set_.push_back(
165+
std::make_tuple(database_oid, table_oid, index_oid, DDLType::CREATE));
165166
}
166167

167168
void RecordDrop(oid_t database_oid, oid_t table_oid, oid_t index_oid) {
168-
rw_object_set_.push_back(std::make_tuple(database_oid, table_oid,
169-
index_oid, DDLType::DROP));
169+
rw_object_set_.push_back(
170+
std::make_tuple(database_oid, table_oid, index_oid, DDLType::DROP));
170171
}
171172

172173
void RecordRead(const ItemPointer &);
@@ -177,10 +178,18 @@ class TransactionContext : public Printable {
177178

178179
void RecordInsert(const ItemPointer &);
179180

181+
/**
182+
* @brief Record drop of a data table.
183+
* @param table the table dropped.
184+
*/
180185
void RecordDropTable(storage::DataTable *table) {
181186
dropped_tables.push_back(table);
182187
}
183188

189+
/**
190+
* @brief Get the dropped tables.
191+
* @return vector of dropped tables.
192+
*/
184193
std::vector<storage::DataTable *> &GetDroppedTables() {
185194
return dropped_tables;
186195
}
@@ -338,8 +347,8 @@ class TransactionContext : public Printable {
338347
ReadWriteSet rw_set_;
339348
CreateDropSet rw_object_set_;
340349

341-
/**
342-
* this set contains data location that needs to be gc'd in the transaction.
350+
/**
351+
* this set contains data location that needs to be gc'd in the transaction.
343352
*/
344353
std::shared_ptr<GCSet> gc_set_;
345354
std::shared_ptr<GCObjectSet> gc_object_set_;

src/include/parser/alter_statement.h

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,66 @@
22
//
33
// Peloton
44
//
5-
// alter_function_statement.h
5+
// alter_statement.h
66
//
7-
// Identification: src/include/parser/alter_function_statement.h
7+
// Identification: src/include/parser/alter_statement.h
88
//
9-
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

1313
#pragma once
1414

15-
#include "parser/sql_statement.h"
1615
#include "common/sql_node_visitor.h"
16+
#include "parser/sql_statement.h"
1717
#include "parser/create_statement.h"
1818

1919
namespace peloton {
2020
namespace parser {
21+
2122
/**
2223
* @struct AlterTableStatement
2324
* @brief Represents "ALTER TABLE add column COLUMN_NAME COLUMN_TYPE"
24-
* TODO: add implementation of AlterTableStatement
2525
*/
2626
class AlterTableStatement : public TableRefStatement {
2727
public:
2828
enum class AlterTableType { INVALID = 0, ALTER = 1, RENAME = 2 };
29+
2930
AlterTableStatement(AlterTableType type)
3031
: TableRefStatement(StatementType::ALTER),
3132
type(type),
3233
oldName(nullptr),
3334
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>>);
35+
if (type == AlterTableType::RENAME) {
36+
dropped_names = nullptr;
37+
added_columns = nullptr;
38+
changed_type_columns = nullptr;
39+
} else {
40+
dropped_names = new std::vector<char *>;
41+
added_columns = new std::vector<std::unique_ptr<ColumnDefinition>>;
42+
changed_type_columns = new std::vector<std::unique_ptr<ColumnDefinition>>;
43+
}
4344
}
4445

4546
virtual ~AlterTableStatement() {
4647
if (added_columns != nullptr) {
4748
delete added_columns;
4849
}
4950
if (dropped_names != nullptr) {
50-
for (auto name : *dropped_names) delete name;
51+
for (auto name : *dropped_names) {
52+
delete name;
53+
}
5154
delete dropped_names;
5255
}
5356
if (changed_type_columns != nullptr) {
5457
delete changed_type_columns;
5558
}
56-
if (oldName) delete oldName;
57-
if (newName) delete newName;
59+
if (oldName) {
60+
delete oldName;
61+
}
62+
if (newName) {
63+
delete newName;
64+
}
5865
}
5966

6067
virtual void Accept(SqlNodeVisitor *v) override { v->Visit(this); }
@@ -68,10 +75,11 @@ class AlterTableStatement : public TableRefStatement {
6875
std::vector<std::unique_ptr<ColumnDefinition>> *added_columns;
6976

7077
std::vector<std::unique_ptr<ColumnDefinition>> *changed_type_columns;
78+
7179
// the name that needs to be changed
7280
char *oldName;
7381
char *newName;
7482
};
7583

7684
} // End parser namespace
77-
} // End peloton namespace
85+
} // End peloton namespace

src/include/parser/statements.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
// This is just for convenience
1616

17-
#include "analyze_statement.h"
1817
#include "alter_statement.h"
18+
#include "analyze_statement.h"
1919
#include "copy_statement.h"
2020
#include "create_function_statement.h"
2121
#include "create_statement.h"

src/include/planner/alter_plan.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
//
77
// Identification: src/include/parser/alter_plan.h
88
//
9-
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

1313
#pragma once
14-
#include "planner/abstract_plan.h"
14+
1515
#include "parser/alter_statement.h"
16+
#include "planner/abstract_plan.h"
17+
1618
namespace peloton {
1719

1820
namespace parser {
@@ -26,8 +28,8 @@ class DataTable;
2628
}
2729

2830
namespace planner {
31+
2932
/** @brief The plan used for altering
30-
* TODO: adding support for add/drop column
3133
*/
3234
class AlterPlan : public AbstractPlan {
3335
public:
@@ -39,11 +41,11 @@ class AlterPlan : public AbstractPlan {
3941
const std::unique_ptr<catalog::Schema> &added_columns,
4042
const std::unique_ptr<catalog::Schema> &changed_type_columns,
4143
AlterType a_type);
44+
4245
explicit AlterPlan(const std::string &database_name,
43-
const std::string &table_name,
44-
const std::vector<std::string> &old_names,
45-
const std::vector<std::string> &new_names,
46-
AlterType a_type);
46+
const std::string &table_name, const std::string &old_name,
47+
const std::string &new_name, AlterType a_type);
48+
4749
explicit AlterPlan(parser::AlterTableStatement *parse_tree);
4850

4951
virtual ~AlterPlan() {}
@@ -61,11 +63,12 @@ class AlterPlan : public AbstractPlan {
6163
std::unique_ptr<AbstractPlan> Copy() const {
6264
switch (this->type) {
6365
case AlterType::ALTER:
64-
return std::unique_ptr<AbstractPlan>(new AlterPlan(
65-
database_name, table_name, dropped_columns, added_columns, changed_type_columns, type));
66+
return std::unique_ptr<AbstractPlan>(
67+
new AlterPlan(database_name, table_name, dropped_columns,
68+
added_columns, changed_type_columns, type));
6669
case AlterType::RENAME:
6770
return std::unique_ptr<AbstractPlan>(new AlterPlan(
68-
database_name, table_name, old_names_, new_names_, type));
71+
database_name, table_name, old_name_, new_name_, type));
6972
default:
7073
LOG_ERROR("Not supported Copy of Alter type yet");
7174
return nullptr;
@@ -84,24 +87,24 @@ class AlterPlan : public AbstractPlan {
8487
return dropped_columns;
8588
}
8689

87-
const std::unique_ptr<catalog::Schema> &
88-
GetChangedTypeColumns() const {
90+
const std::unique_ptr<catalog::Schema> &GetChangedTypeColumns() const {
8991
return changed_type_columns;
9092
};
9193

9294
AlterType GetAlterTableType() const { return type; }
9395

9496
// function used for rename statement
95-
std::string GetOldName() const { return this->old_names_[0]; }
97+
std::string GetOldName() const { return this->old_name_; }
9698

9799
// function used for rename statement
98-
std::string GetNewName() const { return this->new_names_[0]; }
100+
std::string GetNewName() const { return this->new_name_; }
99101

100102
// return true if the alter plan is rename statement
101103
bool IsRename() const { return this->type == AlterType::RENAME; }
102104

103105
// return schema name
104106
std::string GetSchemaName() const { return this->schema_name; }
107+
105108
private:
106109
// Target Table
107110
storage::DataTable *target_table_ = nullptr;
@@ -121,8 +124,8 @@ class AlterPlan : public AbstractPlan {
121124
// changed-type columns, define the column you want to change type
122125
std::unique_ptr<catalog::Schema> changed_type_columns;
123126
// used for store rename function data
124-
std::vector<std::string> old_names_;
125-
std::vector<std::string> new_names_;
127+
std::string old_name_;
128+
std::string new_name_;
126129

127130
// Check to either AlterTable Table, INDEX or Rename
128131
AlterType type;

src/optimizer/optimizer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Identification: src/optimizer/optimizer.cpp
88
//
9-
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -147,7 +147,6 @@ unique_ptr<planner::AbstractPlan> Optimizer::HandleDDLStatement(
147147
auto stmt_type = tree->GetType();
148148
switch (stmt_type) {
149149
case StatementType::ALTER: {
150-
// TODO (shilun) adding support of Alter
151150
LOG_TRACE("Adding Alter Plan");
152151
unique_ptr<planner::AbstractPlan> alter_plan(
153152
new planner::AlterPlan((parser::AlterTableStatement *)tree));

0 commit comments

Comments
 (0)