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

Commit ae65f67

Browse files
committed
added change type, changed logic in alter_executor, varchar still not suppport
1 parent fdce5cf commit ae65f67

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

src/catalog/catalog.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ ResultType Catalog::AlterTable(
917917
new_column_id < new_schema->GetColumnCount(); new_column_id++) {
918918
auto it = column_map.find(new_column_id);
919919
type::Value val;
920+
auto cast_flag = false;
920921
if (it == column_map.end()) {
921922
// new column, set value to null
922923
val = type::ValueFactory::GetNullValueByType(
@@ -927,10 +928,18 @@ ResultType Catalog::AlterTable(
927928
val = result_tile->GetValue(i, it->second);
928929
if (new_schema->GetColumn(new_column_id).GetType() != old_schema->GetColumn(it->second).GetType()) {
929930
//change the value's type
930-
val = val.CastAs(new_schema->GetColumn(new_column_id).GetType());
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;
935+
tuple->SetValue(new_column_id, casted_val, pool_.get());
931936
}
932937
}
933-
tuple->SetValue(new_column_id, val, pool_.get());
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+
}
934943
}
935944
// insert new tuple into new table
936945
planner::InsertPlan node(new_table, std::move(tuple));
@@ -946,19 +955,19 @@ ResultType Catalog::AlterTable(
946955
oid_t column_offset = 0;
947956
for (auto new_column : new_schema->GetColumns()) {
948957
catalog::ColumnCatalog::GetInstance()->InsertColumn(
949-
table_oid, new_column.GetName(), column_offset,
958+
new_table->GetOid(), new_column.GetName(), column_offset,
950959
new_column.GetOffset(), new_column.GetType(),
951960
new_column.IsInlined(), new_column.GetConstraints(), pool_.get(),
952961
txn);
953962
column_offset++;
954963
}
964+
// Record table drop
965+
// txn->RecordDrop(database_oid, old_table->GetOid(), INVALID_OID);
955966

956967
// Final step of physical change should be moved to commit time
957968
database->ReplaceTableWithOid(table_oid, new_table);
958969

959-
// Record table drop
960-
txn->RecordDrop(database_oid, table_oid, INVALID_OID);
961-
970+
LOG_TRACE("Alter table with oid %d", new_table->GetOid());
962971
} catch (CatalogException &e) {
963972
LOG_TRACE("Alter table failed.");
964973
return ResultType::FAILURE;

src/executor/alter_executor.cpp

Lines changed: 16 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,16 @@ 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+
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+
}
135144
}
136145
}
137146

@@ -143,10 +152,8 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
143152
add_columns.push_back(column);
144153
}
145154
}
146-
std::unique_ptr<catalog::Schema> add_column_schema(
147-
new catalog::Schema(add_columns));
148155
// Check if added column exists
149-
for (auto new_column : add_column_schema->GetColumns()) {
156+
for (auto new_column : add_columns) {
150157
for (auto old_column : old_schema->GetColumns()) {
151158
if (new_column.GetName() == old_column.GetName()) {
152159
LOG_TRACE("Add column failed: Column %s already exists",
@@ -156,10 +163,9 @@ bool AlterExecutor::AlterTable(const peloton::planner::AlterPlan &node,
156163
}
157164
}
158165
}
159-
166+
columns.insert(columns.end(), add_columns.begin(), add_columns.end());
160167
// Construct new schema
161-
std::unique_ptr<catalog::Schema> new_schema(catalog::Schema::AppendSchema(
162-
temp_schema.get(), add_column_schema.get()));
168+
std::unique_ptr<catalog::Schema> new_schema(new catalog::Schema(columns));
163169

164170
// Copy and replace table content to new schema in catalog
165171
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)