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

Commit 764fcfb

Browse files
author
Dean Chen
committed
addressed comments and fix varchar
1 parent 6d1961c commit 764fcfb

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

src/catalog/catalog.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -947,10 +947,9 @@ std::shared_ptr<SystemCatalogs> Catalog::GetSystemCatalogs(
947947
* @param txn the transaction Context
948948
* @return TransactionContext ResultType(SUCCESS or FAILURE)
949949
*/
950-
ResultType Catalog::AlterTable(
951-
UNUSED_ATTRIBUTE oid_t database_oid, UNUSED_ATTRIBUTE oid_t table_oid,
952-
UNUSED_ATTRIBUTE std::unique_ptr<catalog::Schema> &new_schema,
953-
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn) {
950+
ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
951+
std::unique_ptr<catalog::Schema> &new_schema,
952+
concurrency::TransactionContext *txn) {
954953
LOG_TRACE("AlterTable in Catalog");
955954

956955
if (txn == nullptr)
@@ -1007,7 +1006,6 @@ ResultType Catalog::AlterTable(
10071006
old_index->GetMetadata()->GetIndexType(),
10081007
old_index->GetMetadata()->GetIndexConstraintType(),
10091008
new_schema.get(),
1010-
// catalog::Schema::CopySchema(old_index->GetKeySchema()),
10111009
catalog::Schema::CopySchema(new_schema.get(), new_key_attrs),
10121010
new_key_attrs, old_index->GetMetadata()->HasUniqueKeys());
10131011

@@ -1060,29 +1058,26 @@ ResultType Catalog::AlterTable(
10601058
new_column_id < new_schema->GetColumnCount(); new_column_id++) {
10611059
auto it = column_map.find(new_column_id);
10621060
type::Value val;
1063-
auto cast_flag = false;
10641061
if (it == column_map.end()) {
10651062
// new column, set value to null
10661063
val = type::ValueFactory::GetNullValueByType(
10671064
new_schema->GetColumn(new_column_id).GetType());
10681065
} else {
10691066
// otherwise, copy value in old table
1070-
// TODO: Change type if necessary
10711067
val = result_tile->GetValue(i, it->second);
1072-
if (new_schema->GetColumn(new_column_id).GetType() != old_schema->GetColumn(it->second).GetType()) {
1073-
//change the value's type
1074-
LOG_TRACE("CASTED: %s TO %s", val.GetInfo().c_str(),new_schema->GetColumn(new_column_id).GetInfo()
1075-
.c_str());
1076-
auto casted_val = val.CastAs(new_schema->GetColumn(new_column_id).GetType());
1077-
cast_flag = true;
1068+
if (new_schema->GetColumn(new_column_id).GetType() !=
1069+
old_schema->GetColumn(it->second).GetType()) {
1070+
// change the value's type
1071+
LOG_TRACE(
1072+
"CASTED: %s TO %s", val.GetInfo().c_str(),
1073+
new_schema->GetColumn(new_column_id).GetInfo().c_str());
1074+
auto casted_val =
1075+
val.CastAs(new_schema->GetColumn(new_column_id).GetType());
10781076
tuple->SetValue(new_column_id, casted_val, pool_.get());
1077+
} else {
1078+
tuple->SetValue(new_column_id, val, pool_.get());
10791079
}
10801080
}
1081-
if (!cast_flag) {
1082-
tuple->SetValue(new_column_id, val, pool_.get());
1083-
} else {
1084-
LOG_TRACE("CASTED: %s", val.GetInfo().c_str());
1085-
}
10861081
}
10871082
// insert new tuple into new table
10881083
planner::InsertPlan node(new_table, std::move(tuple));
@@ -1098,19 +1093,19 @@ ResultType Catalog::AlterTable(
10981093
oid_t column_offset = 0;
10991094
for (auto new_column : new_schema->GetColumns()) {
11001095
catalog::ColumnCatalog::GetInstance()->InsertColumn(
1101-
new_table->GetOid(), new_column.GetName(), column_offset,
1096+
table_oid, new_column.GetName(), column_offset,
11021097
new_column.GetOffset(), new_column.GetType(),
11031098
new_column.IsInlined(), new_column.GetConstraints(), pool_.get(),
11041099
txn);
11051100
column_offset++;
11061101
}
1107-
// Record table drop
1102+
// TODO: Add gc logic
11081103
// txn->RecordDrop(database_oid, old_table->GetOid(), INVALID_OID);
11091104

11101105
// Final step of physical change should be moved to commit time
11111106
database->ReplaceTableWithOid(table_oid, new_table);
11121107

1113-
LOG_TRACE("Alter table with oid %d", new_table->GetOid());
1108+
LOG_TRACE("Alter table with oid %d succeed.", table_oid);
11141109
} catch (CatalogException &e) {
11151110
LOG_TRACE("Alter table failed.");
11161111
return ResultType::FAILURE;

src/catalog/column.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void Column::SetInlined() {
3434
switch (column_type) {
3535
case type::TypeId::VARCHAR:
3636
case type::TypeId::VARBINARY:
37+
is_inlined = false;
3738
break; // No change of inlined setting
3839

3940
default:

src/executor/alter_executor.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,12 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
131131
txn->SetResult(ResultType::FAILURE);
132132
return false;
133133
} else {
134-
if (change_pair.second != type::TypeId::VARCHAR) {
135-
columns[i].SetType(change_pair.second);
136-
columns[i].SetLength(type::VarlenType::GetTypeSize(change_pair.second));
137-
} else {
138-
//TODO decide VARCHAR's size when change type
139-
//It is broken now!
140-
columns[i].SetType(change_pair.second);
141-
//columns[i].SetLength(type::VarlenType::GetTypeSize(change_pair.second));
142-
columns[i].SetInlined();
143-
}
134+
columns[i].SetType(change_pair.second);
135+
columns[i].SetInlined();
136+
columns[i].SetLength(type::VarlenType::GetTypeSize(change_pair.second));
137+
138+
// TODO: decide VARCHAR's size when change type
139+
// if (change_pair.second == type::TypeId::VARCHAR) {}
144140
}
145141
}
146142

0 commit comments

Comments
 (0)