diff --git a/dbms/src/Storages/Transaction/SchemaBuilder.cpp b/dbms/src/Storages/Transaction/SchemaBuilder.cpp index 4322cdcd4d6..93ba4d8a5fc 100644 --- a/dbms/src/Storages/Transaction/SchemaBuilder.cpp +++ b/dbms/src/Storages/Transaction/SchemaBuilder.cpp @@ -2,9 +2,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -86,6 +88,7 @@ inline AlterCommands detectSchemaChanges(Logger * log, const TableInfo & table_i { const auto & column_info = std::find_if(table_info.columns.begin(), table_info.columns.end(), [&](const ColumnInfo & column_info_) { // TODO: Check primary key. + // TODO: Support Rename Column; return column_info_.id == orig_column_info.id && column_info_.tp != orig_column_info.tp; }); @@ -201,12 +204,15 @@ void SchemaBuilder::applyDiff(const SchemaDiff & diff) case SchemaActionAddColumn: case SchemaActionDropColumn: case SchemaActionModifyColumn: - case SchemaActionRenameTable: case SchemaActionSetDefaultValue: { applyAlterTable(di, diff.table_id); break; } + case SchemaActionRenameTable: + { + applyRenameTable(di, diff.old_schema_id, diff.table_id); + } case SchemaActionAddTablePartition: { //applyAddPartition(di, diff.table_id); @@ -234,6 +240,66 @@ void SchemaBuilder::applyDiff(const SchemaDiff & diff) } } +void SchemaBuilder::applyRenameTable(DBInfoPtr db_info, DatabaseID old_db_id, TableID table_id) +{ + DBInfoPtr old_db_info; + if (db_info->id == old_db_id) + { + old_db_info = db_info; + } + else + { + auto db = getter.getDatabase(old_db_id); + if (db == nullptr) + { + throw Exception("miss old db id " + std::to_string(old_db_id)); + } + old_db_info = db; + } + + auto table_info = getter.getTableInfo(db_info->id, table_id); + if (table_info == nullptr) + { + throw Exception("miss old table id in TiKV " + std::to_string(table_id)); + } + + auto & tmt_context = context.getTMTContext(); + auto storage_to_rename = tmt_context.getStorages().get(table_id).get(); + if (storage_to_rename == nullptr) + { + throw Exception("miss old table id in Flash " + std::to_string(table_id)); + } + + applyRenameTableImpl(old_db_info->name, db_info->name, storage_to_rename->getTableName(), table_info->name); +} + +void SchemaBuilder::applyRenameTableImpl(const String & old_db, const String & new_db, const String & old_table, const String & new_table) +{ + if (old_db == new_db && old_table == new_table) + { + LOG_INFO(log, "The " + old_db + "." + old_table + " has been renamed, nothing needs to do"); + return; + } + + auto rename = std::make_shared(); + + ASTRenameQuery::Table from; + from.database = old_db; + from.table = old_table; + + ASTRenameQuery::Table to; + to.database = new_db; + to.table = new_table; + + ASTRenameQuery::Element elem; + elem.from = from; + elem.to = to; + + rename->elements.emplace_back(elem); + + InterpreterRenameQuery(rename, context).execute(); +} + bool SchemaBuilder::applyCreateSchema(DatabaseID schema_id) { auto db = getter.getDatabase(schema_id); diff --git a/dbms/src/Storages/Transaction/SchemaBuilder.h b/dbms/src/Storages/Transaction/SchemaBuilder.h index 60c374e2115..acd5d70ffe8 100644 --- a/dbms/src/Storages/Transaction/SchemaBuilder.h +++ b/dbms/src/Storages/Transaction/SchemaBuilder.h @@ -52,6 +52,10 @@ struct SchemaBuilder void applyCreateTableImpl(const TiDB::DBInfo & db_info, TiDB::TableInfo & table_info); void applyDropTableImpl(const String &, const String &); + + void applyRenameTable(TiDB::DBInfoPtr db_info, TiDB::DatabaseID old_db_id, TiDB::TableID table_id); + + void applyRenameTableImpl(const String & old_db, const String & new_db, const String & old_table, const String & new_table); }; } // namespace DB