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

Commit 4e4dfdc

Browse files
author
Dean Chen
committed
addressed comments and fix varchar
1 parent ae65f67 commit 4e4dfdc

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
@@ -804,10 +804,9 @@ std::shared_ptr<TableCatalogObject> Catalog::GetTableObject(
804804
* @param txn the transaction Context
805805
* @return TransactionContext ResultType(SUCCESS or FAILURE)
806806
*/
807-
ResultType Catalog::AlterTable(
808-
UNUSED_ATTRIBUTE oid_t database_oid, UNUSED_ATTRIBUTE oid_t table_oid,
809-
UNUSED_ATTRIBUTE std::unique_ptr<catalog::Schema> &new_schema,
810-
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn) {
807+
ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid,
808+
std::unique_ptr<catalog::Schema> &new_schema,
809+
concurrency::TransactionContext *txn) {
811810
LOG_TRACE("AlterTable in Catalog");
812811

813812
if (txn == nullptr)
@@ -864,7 +863,6 @@ ResultType Catalog::AlterTable(
864863
old_index->GetMetadata()->GetIndexType(),
865864
old_index->GetMetadata()->GetIndexConstraintType(),
866865
new_schema.get(),
867-
// catalog::Schema::CopySchema(old_index->GetKeySchema()),
868866
catalog::Schema::CopySchema(new_schema.get(), new_key_attrs),
869867
new_key_attrs, old_index->GetMetadata()->HasUniqueKeys());
870868

@@ -917,29 +915,26 @@ ResultType Catalog::AlterTable(
917915
new_column_id < new_schema->GetColumnCount(); new_column_id++) {
918916
auto it = column_map.find(new_column_id);
919917
type::Value val;
920-
auto cast_flag = false;
921918
if (it == column_map.end()) {
922919
// new column, set value to null
923920
val = type::ValueFactory::GetNullValueByType(
924921
new_schema->GetColumn(new_column_id).GetType());
925922
} else {
926923
// otherwise, copy value in old table
927-
// TODO: Change type if necessary
928924
val = result_tile->GetValue(i, it->second);
929-
if (new_schema->GetColumn(new_column_id).GetType() != old_schema->GetColumn(it->second).GetType()) {
930-
//change the value's type
931-
LOG_TRACE("CASTED: %s TO %s", val.GetInfo().c_str(),new_schema->GetColumn(new_column_id).GetInfo()
932-
.c_str());
933-
auto casted_val = val.CastAs(new_schema->GetColumn(new_column_id).GetType());
934-
cast_flag = true;
925+
if (new_schema->GetColumn(new_column_id).GetType() !=
926+
old_schema->GetColumn(it->second).GetType()) {
927+
// change the value's type
928+
LOG_TRACE(
929+
"CASTED: %s TO %s", val.GetInfo().c_str(),
930+
new_schema->GetColumn(new_column_id).GetInfo().c_str());
931+
auto casted_val =
932+
val.CastAs(new_schema->GetColumn(new_column_id).GetType());
935933
tuple->SetValue(new_column_id, casted_val, pool_.get());
934+
} else {
935+
tuple->SetValue(new_column_id, val, pool_.get());
936936
}
937937
}
938-
if (!cast_flag) {
939-
tuple->SetValue(new_column_id, val, pool_.get());
940-
} else {
941-
LOG_TRACE("CASTED: %s", val.GetInfo().c_str());
942-
}
943938
}
944939
// insert new tuple into new table
945940
planner::InsertPlan node(new_table, std::move(tuple));
@@ -955,19 +950,19 @@ ResultType Catalog::AlterTable(
955950
oid_t column_offset = 0;
956951
for (auto new_column : new_schema->GetColumns()) {
957952
catalog::ColumnCatalog::GetInstance()->InsertColumn(
958-
new_table->GetOid(), new_column.GetName(), column_offset,
953+
table_oid, new_column.GetName(), column_offset,
959954
new_column.GetOffset(), new_column.GetType(),
960955
new_column.IsInlined(), new_column.GetConstraints(), pool_.get(),
961956
txn);
962957
column_offset++;
963958
}
964-
// Record table drop
959+
// TODO: Add gc logic
965960
// txn->RecordDrop(database_oid, old_table->GetOid(), INVALID_OID);
966961

967962
// Final step of physical change should be moved to commit time
968963
database->ReplaceTableWithOid(table_oid, new_table);
969964

970-
LOG_TRACE("Alter table with oid %d", new_table->GetOid());
965+
LOG_TRACE("Alter table with oid %d succeed.", table_oid);
971966
} catch (CatalogException &e) {
972967
LOG_TRACE("Alter table failed.");
973968
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)