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

Commit 8cdea77

Browse files
author
Dean Chen
committed
Addressed comments in test and catalog
1 parent 844e564 commit 8cdea77

File tree

5 files changed

+78
-36
lines changed

5 files changed

+78
-36
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: 49 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,37 +810,65 @@ 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
UNUSED_ATTRIBUTE const std::string &database_name,
816842
UNUSED_ATTRIBUTE const std::string &table_name,
817843
UNUSED_ATTRIBUTE const std::vector<std::string> &columns,
818844
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn) {
845+
// TODO: perform DROP Operation
819846
return ResultType::SUCCESS;
820847
}
821848

822-
ResultType Catalog::ChangeColumnName(const std::string &database_name,
823-
const std::string &table_name,
824-
const std::vector<std::string> &old_names,
825-
const std::vector<std::string> &names,
826-
concurrency::TransactionContext *txn) {
849+
/**
850+
* @brief Change the column name in the catalog.
851+
* @param database_name database to which the table belongs to
852+
* @param table_name table to which the column belongs to
853+
* @param columns the column to be dropped
854+
* @param txn the transaction Context
855+
* @return TransactionContext ResultType(SUCCESS or FAILURE)
856+
*/
857+
ResultType Catalog::RenameColumn(const std::string &database_name,
858+
const std::string &table_name,
859+
const std::string &old_name,
860+
const std::string &new_name,
861+
concurrency::TransactionContext *txn) {
827862
if (txn == nullptr) {
828863
throw CatalogException("Change Column requires transaction.");
829864
}
830865

831-
if (old_names.size() == 0 || names.size() == 0) {
832-
throw CatalogException("No names are given.");
866+
if (new_name.size() == 0) {
867+
throw CatalogException("Name can not be empty string.");
833868
}
834869

835-
LOG_TRACE("Change Column Name %s to %s", old_names[0].c_str(),
836-
names[0].c_str());
870+
LOG_TRACE("Change Column Name %s to %s", old_name.c_str(),
871+
new_name.c_str());
837872

838873
try {
839874
// Get table from the name
@@ -844,23 +879,23 @@ ResultType Catalog::ChangeColumnName(const std::string &database_name,
844879
// Currently we only support change the first column name!
845880

846881
// Check the validity of old name and the new name
847-
oid_t columnId = schema->GetColumnID(names[0]);
882+
oid_t columnId = schema->GetColumnID(new_name);
848883
if (columnId != INVALID_OID) {
849884
throw CatalogException("New column already exists in the table.");
850885
}
851-
columnId = schema->GetColumnID(old_names[0]);
886+
columnId = schema->GetColumnID(old_name);
852887
if (columnId == INVALID_OID) {
853888
throw CatalogException("Old column does not exist in the table.");
854889
}
855890

856891
// Change column name in the global schema
857-
schema->ChangeColumnName(columnId, names[0]);
892+
schema->ChangeColumnName(columnId, new_name);
858893

859894
// Change cached ColumnCatalog
860895
oid_t table_oid = Catalog::GetInstance()
861896
->GetTableObject(database_name, table_name, txn)
862897
->GetTableOid();
863-
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, old_names[0],
898+
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, old_name,
864899
txn);
865900
auto new_column = schema->GetColumn(columnId);
866901
catalog::ColumnCatalog::GetInstance()->InsertColumn(

src/executor/alter_executor.cpp

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

3636
bool AlterExecutor::DExecute() {
37-
LOG_TRACE("Executing Drop...");
37+
LOG_TRACE("Executing Alter...");
3838
bool result = false;
3939
const planner::RenamePlan &node = GetPlanNode<planner::RenamePlan>();
4040
auto current_txn = executor_context_->GetTransaction();
@@ -60,16 +60,15 @@ bool AlterExecutor::RenameColumn(
6060
auto new_column_name = node.GetNewName();
6161
auto old_column_name = node.GetOldName();
6262

63-
std::vector<std::string> old_names = {old_column_name};
64-
std::vector<std::string> names = {new_column_name};
65-
ResultType result = catalog::Catalog::GetInstance()->ChangeColumnName(
66-
database_name, table_name, old_names, names, txn);
63+
ResultType result = catalog::Catalog::GetInstance()->RenameColumn(
64+
database_name, table_name, old_column_name, new_column_name, txn);
6765
txn->SetResult(result);
6866

6967
if (txn->GetResult() == ResultType::SUCCESS) {
7068
LOG_TRACE("Rename column succeeded!");
7169

7270
// Add on succeed logic if necessary
71+
executor_context_->num_processed = 1;
7372
} else {
7473
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());
7574
}

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?

0 commit comments

Comments
 (0)