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

Commit 66220b5

Browse files
author
DeanChensj
authored
Merge pull request #10 from sxzh93/address-comment
Addressed comments in test and catalog; Remove LOG_LEVEL change; Change change column parameter. Fix compile error.
2 parents d1c8437 + 1ce6ed0 commit 66220b5

File tree

6 files changed

+83
-40
lines changed

6 files changed

+83
-40
lines changed

script/testing/junit/AlterTableTest.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void Teardown() throws SQLException {
6666
* Insert 1 tuple, rename the column, and see if the change is visible.
6767
*/
6868
@Test
69-
public void test_RenameColumn_1() throws SQLException {
69+
public void test_RenameCol_Base() throws SQLException {
7070
conn.createStatement().execute(SQL_RENAME_COLUMN);
7171
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
7272
rs.next();
@@ -80,7 +80,7 @@ public void test_RenameColumn_1() throws SQLException {
8080
* Rename a column that does not exists, should throw exception
8181
*/
8282
@Test
83-
public void test_RenameColumn_2() throws SQLException {
83+
public void test_RenameCol_NotExist() throws SQLException {
8484
String sql = "ALTER TABLE foo RENAME a to b;";
8585

8686
// Old column does not exist
@@ -89,10 +89,10 @@ public void test_RenameColumn_2() throws SQLException {
8989
}
9090

9191
/**
92-
* Rename a column that does not exists, should throw exception
92+
* Rename a column to a name that already exists, should throw exception
9393
*/
9494
@Test
95-
public void test_RenameColumn_3() throws SQLException {
95+
public void test_RenameCol_Exist() throws SQLException {
9696
String sql = "ALTER TABLE foo RENAME year to id;";
9797

9898
// New column already exists
@@ -104,7 +104,7 @@ public void test_RenameColumn_3() throws SQLException {
104104
* Two transactions try to rename at the same time, should throw exception
105105
*/
106106
@Test
107-
public void test_RenameColumn_4() throws SQLException {
107+
public void test_RenameCol_Concurrent() throws SQLException {
108108
conn.setAutoCommit(false);
109109
conn2.setAutoCommit(false);
110110

@@ -118,8 +118,13 @@ public void test_RenameColumn_4() throws SQLException {
118118
}
119119

120120
// The following tests are currently broken.
121+
//
122+
// /**
123+
// * 2 transactions, t2 reads the table between t1 executes the rename
124+
// * and commits the changes.
125+
// */
121126
// @Test
122-
// public void test_RenameColumn_5() throws SQLException {
127+
// public void test_RenameCol_ReadBeforeCommit() throws SQLException {
123128
// conn.setAutoCommit(false);
124129
// conn2.setAutoCommit(false);
125130
//
@@ -138,11 +143,11 @@ public void test_RenameColumn_4() throws SQLException {
138143
// }
139144
//
140145
// /**
141-
// * 2 transactions, t2 read the table before and after t1 change the column
142-
// * name and should not see the changes.
146+
// * 2 transactions, t2 reads the table before and after t1 changes the
147+
// * column name and should not see the changes.
143148
// */
144149
// @Test
145-
// public void test_RenameColumn_6() throws SQLException {
150+
// public void test_RenameCol_ReadBeforeAndAfterCommit() throws SQLException {
146151
// conn.setAutoCommit(false);
147152
// conn2.setAutoCommit(false);
148153
//
@@ -164,9 +169,12 @@ public void test_RenameColumn_4() throws SQLException {
164169
// assertNoMoreRows(rs_2);
165170
// conn2.commit();
166171
// }
167-
172+
//
173+
// /**
174+
// * 2 transactions, t2 reads the table after t1 commits the changes.
175+
// */
168176
// @Test
169-
// public void test_RenameColumn_7() throws SQLException {
177+
// public void test_RenameCol_ReadAfterCommit() throws SQLException {
170178
// conn.setAutoCommit(false);
171179
// conn2.setAutoCommit(false);
172180
//

src/catalog/catalog.cpp

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,14 @@ std::shared_ptr<TableCatalogObject> Catalog::GetTableObject(
791791
//===--------------------------------------------------------------------===//
792792
// ALTER TABLE
793793
//===--------------------------------------------------------------------===//
794-
/* Helper function for alter table, called internally
794+
795+
/**
796+
* @brief Helper function for alter table, called internally
797+
* @param database_oid database to which the table belongs to
798+
* @param table_oid table to which the column belongs to
799+
* @param new_schema the new table schema
800+
* @param txn the transaction Context
801+
* @return TransactionContext ResultType(SUCCESS or FAILURE)
795802
*/
796803
ResultType Catalog::AlterTable(
797804
UNUSED_ATTRIBUTE oid_t database_oid, UNUSED_ATTRIBUTE oid_t table_oid,
@@ -803,14 +810,33 @@ ResultType Catalog::AlterTable(
803810
return ResultType::SUCCESS;
804811
}
805812

813+
/**
814+
* @brief Add new columns to the table.
815+
* @param database_name database to which the table belongs to
816+
* @param table_name table to which the column belongs to
817+
* @param columns the column to be added
818+
* @param txn the transaction Context
819+
* @return TransactionContext ResultType(SUCCESS or FAILURE)
820+
*
821+
*/
806822
ResultType Catalog::AddColumn(
807823
UNUSED_ATTRIBUTE const std::string &database_name,
808824
UNUSED_ATTRIBUTE const std::string &table_name,
809825
UNUSED_ATTRIBUTE const std::vector<std::string> &columns,
810826
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn) {
827+
// TODO: perform ADD Operation
811828
return ResultType::SUCCESS;
812829
}
813830

831+
/**
832+
* @brief Drop the column from the table.
833+
* @param database_name database to which the table belongs to
834+
* @param table_name table to which the columns belong to
835+
* @param columns the columns to be dropped
836+
* @param txn the transaction Context
837+
* @return TransactionContext ResultType(SUCCESS or FAILURE)
838+
*/
839+
814840
ResultType Catalog::DropColumn(
815841
const std::string &database_name,
816842
const std::string &table_name,
@@ -829,21 +855,29 @@ ResultType Catalog::DropColumn(
829855
return ResultType::SUCCESS;
830856
}
831857

832-
ResultType Catalog::ChangeColumnName(const std::string &database_name,
833-
const std::string &table_name,
834-
const std::vector<std::string> &old_names,
835-
const std::vector<std::string> &names,
836-
concurrency::TransactionContext *txn) {
858+
/**
859+
* @brief Change the column name in the catalog.
860+
* @param database_name database to which the table belongs to
861+
* @param table_name table to which the column belongs to
862+
* @param columns the column to be dropped
863+
* @param txn the transaction Context
864+
* @return TransactionContext ResultType(SUCCESS or FAILURE)
865+
*/
866+
ResultType Catalog::RenameColumn(const std::string &database_name,
867+
const std::string &table_name,
868+
const std::string &old_name,
869+
const std::string &new_name,
870+
concurrency::TransactionContext *txn) {
837871
if (txn == nullptr) {
838872
throw CatalogException("Change Column requires transaction.");
839873
}
840874

841-
if (old_names.size() == 0 || names.size() == 0) {
842-
throw CatalogException("No names are given.");
875+
if (new_name.size() == 0) {
876+
throw CatalogException("Name can not be empty string.");
843877
}
844878

845-
LOG_TRACE("Change Column Name %s to %s", old_names[0].c_str(),
846-
names[0].c_str());
879+
LOG_TRACE("Change Column Name %s to %s", old_name.c_str(),
880+
new_name.c_str());
847881

848882
try {
849883
// Get table from the name
@@ -854,23 +888,23 @@ ResultType Catalog::ChangeColumnName(const std::string &database_name,
854888
// Currently we only support change the first column name!
855889

856890
// Check the validity of old name and the new name
857-
oid_t columnId = schema->GetColumnID(names[0]);
891+
oid_t columnId = schema->GetColumnID(new_name);
858892
if (columnId != INVALID_OID) {
859893
throw CatalogException("New column already exists in the table.");
860894
}
861-
columnId = schema->GetColumnID(old_names[0]);
895+
columnId = schema->GetColumnID(old_name);
862896
if (columnId == INVALID_OID) {
863897
throw CatalogException("Old column does not exist in the table.");
864898
}
865899

866900
// Change column name in the global schema
867-
schema->ChangeColumnName(columnId, names[0]);
901+
schema->ChangeColumnName(columnId, new_name);
868902

869903
// Change cached ColumnCatalog
870904
oid_t table_oid = Catalog::GetInstance()
871905
->GetTableObject(database_name, table_name, txn)
872906
->GetTableOid();
873-
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, old_names[0],
907+
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, old_name,
874908
txn);
875909
auto new_column = schema->GetColumn(columnId);
876910
catalog::ColumnCatalog::GetInstance()->InsertColumn(

src/executor/alter_executor.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bool AlterExecutor::DInit() {
3535
}
3636

3737
bool AlterExecutor::DExecute() {
38-
LOG_TRACE("Executing Drop...");
38+
LOG_TRACE("Executing Alter...");
3939
bool result = false;
4040
if (!isAlter_) {
4141
const planner::AlterPlan &node = GetPlanNode<planner::AlterPlan>();
@@ -66,16 +66,15 @@ bool AlterExecutor::RenameColumn(
6666
auto new_column_name = node.GetNewName();
6767
auto old_column_name = node.GetOldName();
6868

69-
std::vector<std::string> old_names = {old_column_name};
70-
std::vector<std::string> names = {new_column_name};
71-
ResultType result = catalog::Catalog::GetInstance()->ChangeColumnName(
72-
database_name, table_name, old_names, names, txn);
69+
ResultType result = catalog::Catalog::GetInstance()->RenameColumn(
70+
database_name, table_name, old_column_name, new_column_name, txn);
7371
txn->SetResult(result);
7472

7573
if (txn->GetResult() == ResultType::SUCCESS) {
7674
LOG_TRACE("Rename column succeeded!");
7775

78-
// TODO Add on succeed logic if necessary
76+
// TODO: Add on succeed logic if necessary
77+
executor_context_->num_processed = 1;
7978
} else {
8079
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());
8180
}
@@ -95,7 +94,8 @@ bool AlterExecutor::DropColumn(const peloton::planner::AlterPlan &node,
9594
if (txn->GetResult() == ResultType::SUCCESS) {
9695
LOG_TRACE("Drop column succeed!");
9796

98-
// TODO Add on succeed logic if necessary
97+
// TODO: Add on succeed logic if necessary
98+
executor_context_->num_processed = 1;
9999
} else {
100100
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());
101101
}

src/include/catalog/catalog.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ class Catalog {
194194
const std::vector<std::string> &columns,
195195
concurrency::TransactionContext *txn);
196196

197-
ResultType ChangeColumnName(const std::string &database_name,
198-
const std::string &table_name,
199-
const std::vector<std::string> &old_columns,
200-
const std::vector<std::string> &names,
201-
concurrency::TransactionContext *txn);
197+
ResultType RenameColumn(const std::string &database_name,
198+
const std::string &table_name,
199+
const std::string &old_name,
200+
const std::string &new_name,
201+
concurrency::TransactionContext *txn);
202202

203203
//===--------------------------------------------------------------------===//
204204
// DEPRECATED FUNCTIONS

src/include/common/logger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace peloton {
4646

4747
#define LOG_LOG_TIME_FORMAT "%Y-%m-%d %H:%M:%S"
4848
#define LOG_OUTPUT_STREAM stdout
49-
#define LOG_LEVEL LOG_LEVEL_TRACE
49+
5050
// Compile Option
5151
#ifndef LOG_LEVEL
5252
// TODO : any way to use pragma message in GCC?

src/include/parser/alter_statement.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
namespace peloton {
1919
namespace parser {
2020
/**
21-
* @struct AlterTableStatement
21+
* @class AlterTableStatement
2222
* @brief Represents "ALTER TABLE add column COLUMN_NAME COLUMN_TYPE"
2323
* TODO: add implementation of AlterTableStatement
2424
*/
25-
struct AlterTableStatement : TableRefStatement {
25+
class AlterTableStatement : public TableRefStatement {
26+
public:
2627
enum class AlterTableType { INVALID = 0, ADD = 1, DROP = 2, RENAME = 3 };
2728
AlterTableStatement(AlterTableType type)
2829
: TableRefStatement(StatementType::ALTER),

0 commit comments

Comments
 (0)