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

Commit 4d00de1

Browse files
author
DeanChensj
authored
Merge pull request #17 from sxzh93/change_type
added change type, changed logic in alter_executor, varchar now has default length.
2 parents fdce5cf + 4e4dfdc commit 4d00de1

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

src/catalog/catalog.cpp

Lines changed: 17 additions & 13 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

@@ -923,14 +921,20 @@ ResultType Catalog::AlterTable(
923921
new_schema->GetColumn(new_column_id).GetType());
924922
} else {
925923
// otherwise, copy value in old table
926-
// TODO: Change type if necessary
927924
val = result_tile->GetValue(i, it->second);
928-
if (new_schema->GetColumn(new_column_id).GetType() != old_schema->GetColumn(it->second).GetType()) {
929-
//change the value's type
930-
val = val.CastAs(new_schema->GetColumn(new_column_id).GetType());
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());
933+
tuple->SetValue(new_column_id, casted_val, pool_.get());
934+
} else {
935+
tuple->SetValue(new_column_id, val, pool_.get());
931936
}
932937
}
933-
tuple->SetValue(new_column_id, val, pool_.get());
934938
}
935939
// insert new tuple into new table
936940
planner::InsertPlan node(new_table, std::move(tuple));
@@ -952,13 +956,13 @@ ResultType Catalog::AlterTable(
952956
txn);
953957
column_offset++;
954958
}
959+
// TODO: Add gc logic
960+
// txn->RecordDrop(database_oid, old_table->GetOid(), INVALID_OID);
955961

956962
// Final step of physical change should be moved to commit time
957963
database->ReplaceTableWithOid(table_oid, new_table);
958964

959-
// Record table drop
960-
txn->RecordDrop(database_oid, table_oid, INVALID_OID);
961-
965+
LOG_TRACE("Alter table with oid %d succeed.", table_oid);
962966
} catch (CatalogException &e) {
963967
LOG_TRACE("Alter table failed.");
964968
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: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
114114
}
115115
std::unique_ptr<catalog::Schema> temp_schema(
116116
catalog::Schema::CopySchema(old_schema, column_ids));
117-
117+
auto columns = temp_schema->GetColumns();
118118
// Step 2: change column type if exists
119119
for (auto change_pair : node.GetChangedTypeColumns()) {
120120
bool is_found = false;
121121
oid_t i = 0;
122-
for (; i < temp_schema->GetColumnCount(); ++i) {
123-
if (temp_schema->GetColumn(i).GetName() == change_pair.first) {
122+
for (; i < columns.size(); ++i) {
123+
if (columns[i].GetName() == change_pair.first) {
124124
is_found = true;
125125
break;
126126
}
@@ -131,7 +131,12 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
131131
txn->SetResult(ResultType::FAILURE);
132132
return false;
133133
} else {
134-
temp_schema->ChangeColumnType(i, change_pair.second);
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) {}
135140
}
136141
}
137142

@@ -143,10 +148,8 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
143148
add_columns.push_back(column);
144149
}
145150
}
146-
std::unique_ptr<catalog::Schema> add_column_schema(
147-
new catalog::Schema(add_columns));
148151
// Check if added column exists
149-
for (auto new_column : add_column_schema->GetColumns()) {
152+
for (auto new_column : add_columns) {
150153
for (auto old_column : old_schema->GetColumns()) {
151154
if (new_column.GetName() == old_column.GetName()) {
152155
LOG_TRACE("Add column failed: Column %s already exists",
@@ -156,10 +159,9 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
156159
}
157160
}
158161
}
159-
162+
columns.insert(columns.end(), add_columns.begin(), add_columns.end());
160163
// Construct new schema
161-
std::unique_ptr<catalog::Schema> new_schema(catalog::Schema::AppendSchema(
162-
temp_schema.get(), add_column_schema.get()));
164+
std::unique_ptr<catalog::Schema> new_schema(new catalog::Schema(columns));
163165

164166
// Copy and replace table content to new schema in catalog
165167
ResultType result = catalog::Catalog::GetInstance()->AlterTable(

src/parser/postgresparser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,7 @@ parser::AlterTableStatement *PostgresParser::AlterTransform(Node *root) {
18111811
std::unique_ptr<ColumnDefinition> col_def(
18121812
new ColumnDefinition(cmd->name, type_id));
18131813
result->changed_type_columns->push_back(std::move(col_def));
1814+
break;
18141815
}
18151816
default: {
18161817
throw NotImplementedException(StringUtil::Format(

src/storage/database.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ storage::DataTable *Database::ReplaceTableWithOid(
112112
const oid_t table_oid, storage::DataTable *new_table) {
113113
{
114114
std::lock_guard<std::mutex> lock(database_mutex);
115+
codegen::QueryCache::Instance().Remove(table_oid);
115116

116117
oid_t table_offset = 0;
117118
for (auto table : tables) {

0 commit comments

Comments
 (0)