@@ -947,10 +947,9 @@ std::shared_ptr<SystemCatalogs> Catalog::GetSystemCatalogs(
947
947
* @param txn the transaction Context
948
948
* @return TransactionContext ResultType(SUCCESS or FAILURE)
949
949
*/
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) {
954
953
LOG_TRACE (" AlterTable in Catalog" );
955
954
956
955
if (txn == nullptr )
@@ -1007,7 +1006,6 @@ ResultType Catalog::AlterTable(
1007
1006
old_index->GetMetadata ()->GetIndexType (),
1008
1007
old_index->GetMetadata ()->GetIndexConstraintType (),
1009
1008
new_schema.get (),
1010
- // catalog::Schema::CopySchema(old_index->GetKeySchema()),
1011
1009
catalog::Schema::CopySchema (new_schema.get (), new_key_attrs),
1012
1010
new_key_attrs, old_index->GetMetadata ()->HasUniqueKeys ());
1013
1011
@@ -1060,29 +1058,26 @@ ResultType Catalog::AlterTable(
1060
1058
new_column_id < new_schema->GetColumnCount (); new_column_id++) {
1061
1059
auto it = column_map.find (new_column_id);
1062
1060
type::Value val;
1063
- auto cast_flag = false ;
1064
1061
if (it == column_map.end ()) {
1065
1062
// new column, set value to null
1066
1063
val = type::ValueFactory::GetNullValueByType (
1067
1064
new_schema->GetColumn (new_column_id).GetType ());
1068
1065
} else {
1069
1066
// otherwise, copy value in old table
1070
- // TODO: Change type if necessary
1071
1067
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 ());
1078
1076
tuple->SetValue (new_column_id, casted_val, pool_.get ());
1077
+ } else {
1078
+ tuple->SetValue (new_column_id, val, pool_.get ());
1079
1079
}
1080
1080
}
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
- }
1086
1081
}
1087
1082
// insert new tuple into new table
1088
1083
planner::InsertPlan node (new_table, std::move (tuple));
@@ -1098,19 +1093,19 @@ ResultType Catalog::AlterTable(
1098
1093
oid_t column_offset = 0 ;
1099
1094
for (auto new_column : new_schema->GetColumns ()) {
1100
1095
catalog::ColumnCatalog::GetInstance ()->InsertColumn (
1101
- new_table-> GetOid () , new_column.GetName (), column_offset,
1096
+ table_oid , new_column.GetName (), column_offset,
1102
1097
new_column.GetOffset (), new_column.GetType (),
1103
1098
new_column.IsInlined (), new_column.GetConstraints (), pool_.get (),
1104
1099
txn);
1105
1100
column_offset++;
1106
1101
}
1107
- // Record table drop
1102
+ // TODO: Add gc logic
1108
1103
// txn->RecordDrop(database_oid, old_table->GetOid(), INVALID_OID);
1109
1104
1110
1105
// Final step of physical change should be moved to commit time
1111
1106
database->ReplaceTableWithOid (table_oid, new_table);
1112
1107
1113
- LOG_TRACE (" Alter table with oid %d" , new_table-> GetOid () );
1108
+ LOG_TRACE (" Alter table with oid %d succeed. " , table_oid );
1114
1109
} catch (CatalogException &e) {
1115
1110
LOG_TRACE (" Alter table failed." );
1116
1111
return ResultType::FAILURE;
0 commit comments