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

Commit 6d1961c

Browse files
DingshilunDean Chen
authored andcommitted
added change type, changed logic in alter_executor, varchar still not suppport
1 parent 56a2e44 commit 6d1961c

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
@@ -1060,6 +1060,7 @@ ResultType Catalog::AlterTable(
10601060
new_column_id < new_schema->GetColumnCount(); new_column_id++) {
10611061
auto it = column_map.find(new_column_id);
10621062
type::Value val;
1063+
auto cast_flag = false;
10631064
if (it == column_map.end()) {
10641065
// new column, set value to null
10651066
val = type::ValueFactory::GetNullValueByType(
@@ -1070,10 +1071,18 @@ ResultType Catalog::AlterTable(
10701071
val = result_tile->GetValue(i, it->second);
10711072
if (new_schema->GetColumn(new_column_id).GetType() != old_schema->GetColumn(it->second).GetType()) {
10721073
//change the value's type
1073-
val = val.CastAs(new_schema->GetColumn(new_column_id).GetType());
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;
1078+
tuple->SetValue(new_column_id, casted_val, pool_.get());
10741079
}
10751080
}
1076-
tuple->SetValue(new_column_id, val, pool_.get());
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+
}
10771086
}
10781087
// insert new tuple into new table
10791088
planner::InsertPlan node(new_table, std::move(tuple));
@@ -1089,19 +1098,19 @@ ResultType Catalog::AlterTable(
10891098
oid_t column_offset = 0;
10901099
for (auto new_column : new_schema->GetColumns()) {
10911100
catalog::ColumnCatalog::GetInstance()->InsertColumn(
1092-
table_oid, new_column.GetName(), column_offset,
1101+
new_table->GetOid(), new_column.GetName(), column_offset,
10931102
new_column.GetOffset(), new_column.GetType(),
10941103
new_column.IsInlined(), new_column.GetConstraints(), pool_.get(),
10951104
txn);
10961105
column_offset++;
10971106
}
1107+
// Record table drop
1108+
// txn->RecordDrop(database_oid, old_table->GetOid(), INVALID_OID);
10981109

10991110
// Final step of physical change should be moved to commit time
11001111
database->ReplaceTableWithOid(table_oid, new_table);
11011112

1102-
// Record table drop
1103-
txn->RecordDrop(database_oid, table_oid, INVALID_OID);
1104-
1113+
LOG_TRACE("Alter table with oid %d", new_table->GetOid());
11051114
} catch (CatalogException &e) {
11061115
LOG_TRACE("Alter table failed.");
11071116
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
@@ -1855,6 +1855,7 @@ parser::AlterTableStatement *PostgresParser::AlterTransform(Node *root) {
18551855
std::unique_ptr<ColumnDefinition> col_def(
18561856
new ColumnDefinition(cmd->name, type_id));
18571857
result->changed_type_columns->push_back(std::move(col_def));
1858+
break;
18581859
}
18591860
default: {
18601861
throw NotImplementedException(StringUtil::Format(

src/storage/database.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ storage::DataTable *Database::ReplaceTableWithOid(
104104
const oid_t table_oid, storage::DataTable *new_table) {
105105
{
106106
std::lock_guard<std::mutex> lock(database_mutex);
107+
codegen::QueryCache::Instance().Remove(table_oid);
107108

108109
oid_t table_offset = 0;
109110
for (auto table : tables) {

0 commit comments

Comments
 (0)