@@ -804,10 +804,9 @@ std::shared_ptr<TableCatalogObject> Catalog::GetTableObject(
804
804
* @param txn the transaction Context
805
805
* @return TransactionContext ResultType(SUCCESS or FAILURE)
806
806
*/
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) {
811
810
LOG_TRACE (" AlterTable in Catalog" );
812
811
813
812
if (txn == nullptr )
@@ -864,7 +863,6 @@ ResultType Catalog::AlterTable(
864
863
old_index->GetMetadata ()->GetIndexType (),
865
864
old_index->GetMetadata ()->GetIndexConstraintType (),
866
865
new_schema.get (),
867
- // catalog::Schema::CopySchema(old_index->GetKeySchema()),
868
866
catalog::Schema::CopySchema (new_schema.get (), new_key_attrs),
869
867
new_key_attrs, old_index->GetMetadata ()->HasUniqueKeys ());
870
868
@@ -917,29 +915,26 @@ ResultType Catalog::AlterTable(
917
915
new_column_id < new_schema->GetColumnCount (); new_column_id++) {
918
916
auto it = column_map.find (new_column_id);
919
917
type::Value val;
920
- auto cast_flag = false ;
921
918
if (it == column_map.end ()) {
922
919
// new column, set value to null
923
920
val = type::ValueFactory::GetNullValueByType (
924
921
new_schema->GetColumn (new_column_id).GetType ());
925
922
} else {
926
923
// otherwise, copy value in old table
927
- // TODO: Change type if necessary
928
924
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 ());
935
933
tuple->SetValue (new_column_id, casted_val, pool_.get ());
934
+ } else {
935
+ tuple->SetValue (new_column_id, val, pool_.get ());
936
936
}
937
937
}
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
- }
943
938
}
944
939
// insert new tuple into new table
945
940
planner::InsertPlan node (new_table, std::move (tuple));
@@ -955,19 +950,19 @@ ResultType Catalog::AlterTable(
955
950
oid_t column_offset = 0 ;
956
951
for (auto new_column : new_schema->GetColumns ()) {
957
952
catalog::ColumnCatalog::GetInstance ()->InsertColumn (
958
- new_table-> GetOid () , new_column.GetName (), column_offset,
953
+ table_oid , new_column.GetName (), column_offset,
959
954
new_column.GetOffset (), new_column.GetType (),
960
955
new_column.IsInlined (), new_column.GetConstraints (), pool_.get (),
961
956
txn);
962
957
column_offset++;
963
958
}
964
- // Record table drop
959
+ // TODO: Add gc logic
965
960
// txn->RecordDrop(database_oid, old_table->GetOid(), INVALID_OID);
966
961
967
962
// Final step of physical change should be moved to commit time
968
963
database->ReplaceTableWithOid (table_oid, new_table);
969
964
970
- LOG_TRACE (" Alter table with oid %d" , new_table-> GetOid () );
965
+ LOG_TRACE (" Alter table with oid %d succeed. " , table_oid );
971
966
} catch (CatalogException &e) {
972
967
LOG_TRACE (" Alter table failed." );
973
968
return ResultType::FAILURE;
0 commit comments