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

Commit 253b471

Browse files
author
Shangjie Chen
committed
new implement of rename
1 parent 36f111a commit 253b471

File tree

5 files changed

+167
-66
lines changed

5 files changed

+167
-66
lines changed

src/catalog/abstract_catalog.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "executor/index_scan_executor.h"
3434
#include "executor/insert_executor.h"
3535
#include "executor/seq_scan_executor.h"
36+
#include "executor/update_executor.h"
3637

3738
#include "storage/database.h"
3839
#include "storage/storage_manager.h"
@@ -166,6 +167,73 @@ bool AbstractCatalog::DeleteWithIndexScan(
166167
return status;
167168
}
168169

170+
/* @brief Update specific columns using index scan
171+
* @param update_columns Columns to be updated
172+
* @param update_values Values to be updated
173+
* @param scan_values Value to be scaned (used in index scan)
174+
* @param index_offset Offset of index for scan
175+
* @return true if successfully executes
176+
*/
177+
bool AbstractCatalog::UpdateWithIndexScan(
178+
std::vector<oid_t> update_columns, std::vector<type::Value> update_values,
179+
std::vector<type::Value> scan_values, oid_t index_offset,
180+
concurrency::TransactionContext *txn) {
181+
if (txn == nullptr) {
182+
throw CatalogException("Scan table requires transaction");
183+
}
184+
185+
std::unique_ptr<executor::ExecutorContext> context(
186+
new executor::ExecutorContext(txn));
187+
// Construct index scan executor
188+
auto index = catalog_table_->GetIndex(index_offset);
189+
oid_t index_oid = index->GetOid();
190+
std::vector<oid_t> key_column_offsets =
191+
index->GetMetadata()->GetKeySchema()->GetIndexedColumns();
192+
PELOTON_ASSERT(scan_values.size() == key_column_offsets.size());
193+
std::vector<ExpressionType> expr_types(scan_values.size(),
194+
ExpressionType::COMPARE_EQUAL);
195+
std::vector<expression::AbstractExpression *> runtime_keys;
196+
197+
planner::IndexScanPlan::IndexScanDesc index_scan_desc(
198+
index_oid, key_column_offsets, expr_types, scan_values, runtime_keys);
199+
200+
planner::IndexScanPlan index_scan_node(catalog_table_, nullptr,
201+
update_columns, index_scan_desc);
202+
203+
executor::IndexScanExecutor index_scan_executor(&index_scan_node,
204+
context.get());
205+
// Construct update executor
206+
TargetList target_list;
207+
DirectMapList direct_map_list;
208+
209+
size_t column_count = catalog_table_->GetSchema()->GetColumnCount();
210+
for (size_t col_itr = 0; col_itr < column_count; col_itr++) {
211+
// Skip any column for update
212+
if (std::find(std::begin(update_columns), std::end(update_columns),
213+
col_itr) == std::end(update_columns)) {
214+
direct_map_list.emplace_back(col_itr, std::make_pair(0, col_itr));
215+
}
216+
}
217+
218+
PELOTON_ASSERT(update_columns.size() == update_values.size());
219+
for (size_t i = 0; i < update_values.size(); i++) {
220+
planner::DerivedAttribute update_attribute{
221+
new expression::ConstantValueExpression(update_values[i])};
222+
target_list.emplace_back(update_columns[i], update_attribute);
223+
}
224+
225+
std::unique_ptr<const planner::ProjectInfo> project_info(
226+
new planner::ProjectInfo(std::move(target_list),
227+
std::move(direct_map_list)));
228+
planner::UpdatePlan update_node(catalog_table_, std::move(project_info));
229+
230+
executor::UpdateExecutor update_executor(&update_node, context.get());
231+
update_executor.AddChild(&index_scan_executor);
232+
// Execute
233+
update_executor.Init();
234+
return update_executor.Execute();
235+
}
236+
169237
/*@brief Index scan helper function
170238
* @param column_offsets Column ids for search (projection)
171239
* @param index_offset Offset of index for scan

src/catalog/catalog.cpp

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -615,18 +615,18 @@ ResultType Catalog::DropIndex(oid_t index_oid,
615615

616616
ResultType Catalog::DropIndex(const std::string &index_name,
617617
concurrency::TransactionContext *txn) {
618-
if(txn == nullptr) {
619-
throw CatalogException("Do not have transaction to drop index " +
620-
index_name);
621-
}
622-
auto index_object = catalog::IndexCatalog::GetInstance()->GetIndexObject(
623-
index_name, txn);
624-
if(index_object == nullptr) {
625-
throw CatalogException("Index name " + index_name + " cannot be found");
626-
}
627-
ResultType result = DropIndex(index_object->GetIndexOid(), txn);
618+
if (txn == nullptr) {
619+
throw CatalogException("Do not have transaction to drop index " +
620+
index_name);
621+
}
622+
auto index_object =
623+
catalog::IndexCatalog::GetInstance()->GetIndexObject(index_name, txn);
624+
if (index_object == nullptr) {
625+
throw CatalogException("Index name " + index_name + " cannot be found");
626+
}
627+
ResultType result = DropIndex(index_object->GetIndexOid(), txn);
628628

629-
return result;
629+
return result;
630630
}
631631

632632
//===--------------------------------------------------------------------===//
@@ -837,19 +837,18 @@ ResultType Catalog::AddColumn(
837837
* @return TransactionContext ResultType(SUCCESS or FAILURE)
838838
*/
839839

840-
ResultType Catalog::DropColumn(
841-
const std::string &database_name,
842-
const std::string &table_name,
843-
const std::vector<std::string> &columns,
844-
concurrency::TransactionContext *txn) {
840+
ResultType Catalog::DropColumn(const std::string &database_name,
841+
const std::string &table_name,
842+
const std::vector<std::string> &columns,
843+
concurrency::TransactionContext *txn) {
845844
try {
846845
oid_t table_oid = Catalog::GetInstance()
847-
->GetTableObject(database_name, table_name, txn)
848-
->GetTableOid();
849-
for (std::string name: columns) {
846+
->GetTableObject(database_name, table_name, txn)
847+
->GetTableOid();
848+
for (std::string name : columns) {
850849
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, name, txn);
851850
}
852-
} catch(CatalogException &e){
851+
} catch (CatalogException &e) {
853852
return ResultType::FAILURE;
854853
}
855854
return ResultType::SUCCESS;
@@ -876,8 +875,7 @@ ResultType Catalog::RenameColumn(const std::string &database_name,
876875
throw CatalogException("Name can not be empty string.");
877876
}
878877

879-
LOG_TRACE("Change Column Name %s to %s", old_name.c_str(),
880-
new_name.c_str());
878+
LOG_TRACE("Change Column Name %s to %s", old_name.c_str(), new_name.c_str());
881879

882880
try {
883881
// Get table from the name
@@ -900,17 +898,15 @@ ResultType Catalog::RenameColumn(const std::string &database_name,
900898
// Change column name in the global schema
901899
schema->ChangeColumnName(columnId, new_name);
902900

903-
// Change cached ColumnCatalog
901+
// Modify the pg_table
904902
oid_t table_oid = Catalog::GetInstance()
905903
->GetTableObject(database_name, table_name, txn)
906904
->GetTableOid();
907-
catalog::ColumnCatalog::GetInstance()->DeleteColumn(table_oid, old_name,
908-
txn);
909-
auto new_column = schema->GetColumn(columnId);
910-
catalog::ColumnCatalog::GetInstance()->InsertColumn(
911-
table_oid, new_column.GetName(), columnId, new_column.GetOffset(),
912-
new_column.GetType(), new_column.IsInlined(),
913-
new_column.GetConstraints(), pool_.get(), txn);
905+
bool res = catalog::ColumnCatalog::GetInstance()->RenameColumn(
906+
table_oid, old_name, new_name, txn);
907+
if (!res) {
908+
throw CatalogException("Change Column name failed.");
909+
}
914910

915911
} catch (CatalogException &e) {
916912
return ResultType::FAILURE;
@@ -1195,11 +1191,11 @@ void Catalog::InitializeFunctions() {
11951191
/**
11961192
* decimal functions
11971193
*/
1198-
AddBuiltinFunction(
1199-
"abs", {type::TypeId::DECIMAL}, type::TypeId::DECIMAL, internal_lang,
1200-
"Abs", function::BuiltInFuncType{OperatorId::Abs,
1201-
function::DecimalFunctions::_Abs},
1202-
txn);
1194+
AddBuiltinFunction("abs", {type::TypeId::DECIMAL}, type::TypeId::DECIMAL,
1195+
internal_lang, "Abs",
1196+
function::BuiltInFuncType{
1197+
OperatorId::Abs, function::DecimalFunctions::_Abs},
1198+
txn);
12031199
AddBuiltinFunction(
12041200
"sqrt", {type::TypeId::TINYINT}, type::TypeId::DECIMAL, internal_lang,
12051201
"Sqrt", function::BuiltInFuncType{OperatorId::Sqrt,
@@ -1236,33 +1232,29 @@ void Catalog::InitializeFunctions() {
12361232
/**
12371233
* integer functions
12381234
*/
1239-
AddBuiltinFunction(
1240-
"abs", {type::TypeId::TINYINT}, type::TypeId::TINYINT,
1241-
internal_lang, "Abs",
1242-
function::BuiltInFuncType{OperatorId::Abs,
1243-
function::DecimalFunctions::_Abs},
1244-
txn);
1235+
AddBuiltinFunction("abs", {type::TypeId::TINYINT}, type::TypeId::TINYINT,
1236+
internal_lang, "Abs",
1237+
function::BuiltInFuncType{
1238+
OperatorId::Abs, function::DecimalFunctions::_Abs},
1239+
txn);
12451240

1246-
AddBuiltinFunction(
1247-
"abs", {type::TypeId::SMALLINT}, type::TypeId::SMALLINT,
1248-
internal_lang, "Abs",
1249-
function::BuiltInFuncType{OperatorId::Abs,
1250-
function::DecimalFunctions::_Abs},
1251-
txn);
1241+
AddBuiltinFunction("abs", {type::TypeId::SMALLINT},
1242+
type::TypeId::SMALLINT, internal_lang, "Abs",
1243+
function::BuiltInFuncType{
1244+
OperatorId::Abs, function::DecimalFunctions::_Abs},
1245+
txn);
12521246

1253-
AddBuiltinFunction(
1254-
"abs", {type::TypeId::INTEGER}, type::TypeId::INTEGER,
1255-
internal_lang, "Abs",
1256-
function::BuiltInFuncType{OperatorId::Abs,
1257-
function::DecimalFunctions::_Abs},
1258-
txn);
1247+
AddBuiltinFunction("abs", {type::TypeId::INTEGER}, type::TypeId::INTEGER,
1248+
internal_lang, "Abs",
1249+
function::BuiltInFuncType{
1250+
OperatorId::Abs, function::DecimalFunctions::_Abs},
1251+
txn);
12591252

1260-
AddBuiltinFunction(
1261-
"abs", {type::TypeId::BIGINT}, type::TypeId::BIGINT,
1262-
internal_lang, "Abs",
1263-
function::BuiltInFuncType{OperatorId::Abs,
1264-
function::DecimalFunctions::_Abs},
1265-
txn);
1253+
AddBuiltinFunction("abs", {type::TypeId::BIGINT}, type::TypeId::BIGINT,
1254+
internal_lang, "Abs",
1255+
function::BuiltInFuncType{
1256+
OperatorId::Abs, function::DecimalFunctions::_Abs},
1257+
txn);
12661258

12671259
AddBuiltinFunction(
12681260
"floor", {type::TypeId::INTEGER}, type::TypeId::DECIMAL,

src/catalog/column_catalog.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ ColumnCatalogObject::ColumnCatalogObject(executor::LogicalTile *tile,
4141
is_not_null(tile->GetValue(tupleId, ColumnCatalog::ColumnId::IS_NOT_NULL)
4242
.GetAs<bool>()) {}
4343

44-
ColumnCatalog *ColumnCatalog::GetInstance(storage::Database *pg_catalog,
45-
type::AbstractPool *pool,
46-
concurrency::TransactionContext *txn) {
44+
ColumnCatalog *ColumnCatalog::GetInstance(
45+
storage::Database *pg_catalog, type::AbstractPool *pool,
46+
concurrency::TransactionContext *txn) {
4747
static ColumnCatalog column_catalog{pg_catalog, pool, txn};
4848
return &column_catalog;
4949
}
@@ -250,5 +250,35 @@ ColumnCatalog::GetColumnObjects(oid_t table_oid,
250250
return table_object->GetColumnObjects();
251251
}
252252

253+
/*
254+
* @brief Rename the column name to a new name.
255+
* @ return whether the update succeed
256+
*/
257+
bool ColumnCatalog::RenameColumn(oid_t table_oid,
258+
const std::string &column_name,
259+
const std::string &new_name,
260+
concurrency::TransactionContext *txn) {
261+
std::vector<oid_t> update_columns({ColumnId::COLUMN_NAME});
262+
263+
std::vector<type::Value> update_values;
264+
update_values.push_back(type::ValueFactory::GetVarcharValue(new_name).Copy());
265+
266+
// values to execute index scan
267+
std::vector<type::Value> scan_values;
268+
scan_values.push_back(type::ValueFactory::GetIntegerValue(table_oid).Copy());
269+
scan_values.push_back(
270+
type::ValueFactory::GetVarcharValue(column_name, nullptr).Copy());
271+
272+
// Index of table_oid & column_name
273+
oid_t index_offset = IndexId::PRIMARY_KEY;
274+
275+
auto table_object =
276+
TableCatalog::GetInstance()->GetTableObject(table_oid, txn);
277+
table_object->EvictColumnObject(column_name);
278+
279+
return UpdateWithIndexScan(update_columns, update_values, scan_values,
280+
index_offset, txn);
281+
}
282+
253283
} // namespace catalog
254284
} // namespace peloton

src/include/catalog/abstract_catalog.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class AbstractCatalog {
6262
bool DeleteWithIndexScan(oid_t index_offset, std::vector<type::Value> values,
6363
concurrency::TransactionContext *txn);
6464

65+
bool UpdateWithIndexScan(std::vector<oid_t> update_columns,
66+
std::vector<type::Value> update_values,
67+
std::vector<type::Value> scan_values,
68+
oid_t index_offset,
69+
concurrency::TransactionContext *txn);
70+
6571
std::unique_ptr<std::vector<std::unique_ptr<executor::LogicalTile>>>
6672
GetResultWithIndexScan(std::vector<oid_t> column_offsets, oid_t index_offset,
6773
std::vector<type::Value> values,

src/include/catalog/column_catalog.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class ColumnCatalog : public AbstractCatalog {
7070

7171
public:
7272
// Global Singleton, only the first call requires passing parameters.
73-
static ColumnCatalog *GetInstance(storage::Database *pg_catalog = nullptr,
74-
type::AbstractPool *pool = nullptr,
75-
concurrency::TransactionContext *txn = nullptr);
73+
static ColumnCatalog *GetInstance(
74+
storage::Database *pg_catalog = nullptr,
75+
type::AbstractPool *pool = nullptr,
76+
concurrency::TransactionContext *txn = nullptr);
7677

7778
~ColumnCatalog();
7879

@@ -86,10 +87,14 @@ class ColumnCatalog : public AbstractCatalog {
8687
oid_t column_id, oid_t column_offset,
8788
type::TypeId column_type, bool is_inlined,
8889
const std::vector<Constraint> &constraints,
89-
type::AbstractPool *pool, concurrency::TransactionContext *txn);
90+
type::AbstractPool *pool,
91+
concurrency::TransactionContext *txn);
9092
bool DeleteColumn(oid_t table_oid, const std::string &column_name,
9193
concurrency::TransactionContext *txn);
9294
bool DeleteColumns(oid_t table_oid, concurrency::TransactionContext *txn);
95+
bool RenameColumn(oid_t table_oid, const std::string &column_name,
96+
const std::string &new_name,
97+
concurrency::TransactionContext *txn);
9398

9499
private:
95100
//===--------------------------------------------------------------------===//

0 commit comments

Comments
 (0)