Skip to content

Commit 37f24fb

Browse files
author
wumu.stt
committed
close #101 fix rename table failed
1 parent 55ca0c9 commit 37f24fb

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

polardbx-executor/src/main/java/com/alibaba/polardbx/executor/ddl/job/meta/TableMetaChanger.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ public static void renamePartitionTableMeta(Connection metaDbConn, String schema
353353
String tableListDataId = MetaDbDataIdBuilder.getTableListDataId(schemaName);
354354
String tableDataId = MetaDbDataIdBuilder.getTableDataId(schemaName, logicalTableName);
355355
String newTableDataId = MetaDbDataIdBuilder.getTableDataId(schemaName, newLogicalTableName);
356+
boolean renamePhyTable = executionContext.needToRenamePhyTables();
356357

357358
// Rename sequence if exists.
358359
SequenceBaseRecord sequenceRecord = null;
@@ -365,9 +366,13 @@ public static void renamePartitionTableMeta(Connection metaDbConn, String schema
365366
TableInfoManager tableInfoManager = new TableInfoManager();
366367
tableInfoManager.setConnection(metaDbConn);
367368

369+
// Replace with new physical table name
370+
if (renamePhyTable) {
371+
tableInfoManager.renamePartitionTablePhyTable(schemaName, logicalTableName, newLogicalTableName);
372+
}
373+
368374
// Replace with new table name
369-
tableInfoManager
370-
.renamePartitionTable(schemaName, logicalTableName, newLogicalTableName, sequenceRecord);
375+
tableInfoManager.renamePartitionTable(schemaName, logicalTableName, newLogicalTableName, sequenceRecord);
371376

372377
// Unregister the old table data id.
373378
CONFIG_MANAGER.unregister(tableDataId, metaDbConn);

polardbx-gms/src/main/java/com/alibaba/polardbx/gms/metadb/table/TableInfoManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
import com.alibaba.polardbx.gms.partition.TableLocalPartitionRecord;
3939
import com.alibaba.polardbx.gms.partition.TablePartRecordInfoContext;
4040
import com.alibaba.polardbx.gms.partition.TablePartitionAccessor;
41+
import com.alibaba.polardbx.gms.partition.TablePartitionConfig;
4142
import com.alibaba.polardbx.gms.partition.TablePartitionRecord;
43+
import com.alibaba.polardbx.gms.partition.TablePartitionSpecConfig;
4244
import com.alibaba.polardbx.gms.scheduler.FiredScheduledJobsAccessor;
4345
import com.alibaba.polardbx.gms.scheduler.ScheduledJobsAccessor;
4446
import com.alibaba.polardbx.gms.scheduler.ScheduledJobsRecord;
@@ -729,6 +731,16 @@ public void renamePartitionTable(String tableSchema, String tableName, String ne
729731
}
730732
}
731733

734+
public void renamePartitionTablePhyTable(String tableSchema, String tableName, String newTableName) {
735+
TablePartitionConfig tablePartitionConfig =
736+
tablePartitionAccessor.getTablePartitionConfig(tableSchema, tableName, false);
737+
for (TablePartitionSpecConfig item : tablePartitionConfig.getPartitionSpecConfigs()) {
738+
String phyTableName = item.getSpecConfigInfo().phyTable;
739+
String newPhyTableName = TStringUtil.replaceWithIgnoreCase(phyTableName, tableName, newTableName);
740+
tablePartitionAccessor.renamePhyTableName(item.getSpecConfigInfo().id, newPhyTableName);
741+
}
742+
}
743+
732744
public void setMultiWriteSourceColumn(String tableSchema, String tableName, String columnsName) {
733745
columnsAccessor.updateStatus(tableSchema, tableName, ImmutableList.of(columnsName),
734746
ColumnStatus.MULTI_WRITE_SOURCE.getValue());

polardbx-gms/src/main/java/com/alibaba/polardbx/gms/partition/TablePartitionAccessor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ public class TablePartitionAccessor extends AbstractAccessor {
178178
private static final String UPDATE_TABLES_RENAME =
179179
"update table_partitions set `table_name` = ? where table_schema=? and table_name=? ";
180180

181+
private static final String UPDATE_RENAME_PHYSICAL_TABLE =
182+
"update table_partitions set `phy_table` = ? where id = ? ";
183+
181184
private static final String GET_TABLE_PARTITIONS_BY_GID_AND_PART_FROM_DELTA_TABLE =
182185
"select " + ALL_COLUMNS + " from " + GmsSystemTables.TABLE_PARTITIONS_DELTA
183186
+ " where table_schema=? and group_id=? and part_name=? and part_level<>0";
@@ -1182,6 +1185,20 @@ public void rename(String tableSchema, String tableName, String newTableName) {
11821185
}
11831186
}
11841187

1188+
public void renamePhyTableName(Long id, String newPhyTableName) {
1189+
try {
1190+
Map<Integer, ParameterContext> params = new HashMap<>();
1191+
MetaDbUtil.setParameter(1, params, ParameterMethod.setString, newPhyTableName);
1192+
MetaDbUtil.setParameter(2, params, ParameterMethod.setLong, id);
1193+
MetaDbUtil.update(UPDATE_RENAME_PHYSICAL_TABLE, params, connection);
1194+
return;
1195+
} catch (Exception e) {
1196+
logger.error("Failed to query the system table 'table_partitions'", e);
1197+
throw new TddlRuntimeException(ErrorCode.ERR_GMS_ACCESS_TO_SYSTEM_TABLE, e,
1198+
e.getMessage());
1199+
}
1200+
}
1201+
11851202
private TablePartitionConfig getTablePartitionConfigInner(String dbName, String tbName, Connection metaDbConn,
11861203
boolean fromDeltaTable, boolean publicOnly) {
11871204

0 commit comments

Comments
 (0)