Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix duplicate column names added when index changed in DDL
Browse files Browse the repository at this point in the history
terrymanu committed Dec 10, 2024
1 parent 56bcafa commit fb69b11
Showing 4 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -51,8 +51,7 @@ public final class MySQLMetaDataLoader implements DialectMetaDataLoader {
private static final String ORDER_BY_ORDINAL_POSITION = " ORDER BY ORDINAL_POSITION";

private static final String TABLE_META_DATA_NO_ORDER =
"SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_KEY, EXTRA, COLLATION_NAME, ORDINAL_POSITION, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "
+ "WHERE TABLE_SCHEMA=?";
"SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_KEY, EXTRA, COLLATION_NAME, ORDINAL_POSITION, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns WHERE TABLE_SCHEMA=?";

private static final String TABLE_META_DATA_SQL = TABLE_META_DATA_NO_ORDER + ORDER_BY_ORDINAL_POSITION;

Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@
import com.google.common.base.Preconditions;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
@@ -48,11 +51,14 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
}
String actualSchemaName = sqlStatement.getIndex().get().getOwner().map(optional -> optional.getIdentifier().getValue().toLowerCase()).orElse(schemaName);
String indexName = sqlStatement.getIndex().get().getIndexName().getIdentifier().getValue();
Optional<String> logicTableName = findLogicTableName(database.getSchema(actualSchemaName), indexName);
ShardingSpherePreconditions.checkState(database.containsSchema(actualSchemaName), () -> new SchemaNotFoundException(actualSchemaName));
ShardingSphereSchema schema = database.getSchema(actualSchemaName);
Optional<String> logicTableName = findLogicTableName(schema, indexName);
// TODO define IndexNotFoundException
Preconditions.checkState(logicTableName.isPresent(), "Can not find logic table by index '%s' of schema '%s'.", indexName, schemaName);
ShardingSphereTable table = database.getSchema(actualSchemaName).getTable(logicTableName.get());
Preconditions.checkNotNull(table, "Can not get the table '%s' meta data!", logicTableName.get());
ShardingSphereTable newTable = newShardingSphereTable(table);
ShardingSpherePreconditions.checkState(schema.containsTable(logicTableName.get()), () -> new TableNotFoundException(logicTableName.get()));
ShardingSphereTable table = schema.getTable(logicTableName.get());
ShardingSphereTable newTable = new ShardingSphereTable(table.getName(), table.getAllColumns(), table.getAllIndexes(), table.getAllConstraints(), table.getType());
newTable.removeIndex(indexName);
String renameIndexName = renameIndex.get().getIndexName().getIdentifier().getValue();
newTable.putIndex(new ShardingSphereIndex(renameIndexName, new LinkedList<>(), false));
@@ -65,14 +71,6 @@ private Optional<String> findLogicTableName(final ShardingSphereSchema schema, f
return schema.getAllTables().stream().filter(each -> each.containsIndex(indexName)).findFirst().map(ShardingSphereTable::getName);
}

private ShardingSphereTable newShardingSphereTable(final ShardingSphereTable table) {
ShardingSphereTable result = new ShardingSphereTable(table.getName(), table.getAllColumns(), table.getAllIndexes(), table.getAllConstraints(), table.getType());
result.getColumnNames().addAll(table.getColumnNames());
result.getVisibleColumns().addAll(table.getVisibleColumns());
result.getPrimaryKeyColumns().addAll(table.getPrimaryKeyColumns());
return result;
}

@Override
public Class<AlterIndexStatement> getType() {
return AlterIndexStatement.class;
Original file line number Diff line number Diff line change
@@ -21,8 +21,12 @@
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO;
import org.apache.shardingsphere.infra.metadata.database.schema.util.IndexMetaDataUtils;
@@ -44,23 +48,20 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
String indexName = null == sqlStatement.getIndex()
? IndexMetaDataUtils.getGeneratedLogicIndexName(sqlStatement.getColumns())
: sqlStatement.getIndex().getIndexName().getIdentifier().getValue();
// TODO define IndexNotFoundException
Preconditions.checkArgument(!Strings.isNullOrEmpty(indexName), "Index name is not exist.");
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue();
ShardingSphereTable table = newShardingSphereTable(database.getSchema(schemaName).getTable(tableName));
table.putIndex(new ShardingSphereIndex(indexName, new LinkedList<>(), false));
ShardingSpherePreconditions.checkState(database.containsSchema(schemaName), () -> new SchemaNotFoundException(schemaName));
ShardingSphereSchema schema = database.getSchema(schemaName);
ShardingSpherePreconditions.checkState(schema.containsTable(tableName), () -> new TableNotFoundException(tableName));
ShardingSphereTable table = schema.getTable(tableName);
ShardingSphereTable newTable = new ShardingSphereTable(table.getName(), table.getAllColumns(), table.getAllIndexes(), table.getAllConstraints(), table.getType());
newTable.putIndex(new ShardingSphereIndex(indexName, new LinkedList<>(), false));
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName);
alterSchemaMetaDataPOJO.getAlteredTables().add(table);
alterSchemaMetaDataPOJO.getAlteredTables().add(newTable);
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
}

private ShardingSphereTable newShardingSphereTable(final ShardingSphereTable table) {
ShardingSphereTable result = new ShardingSphereTable(table.getName(), table.getAllColumns(), table.getAllIndexes(), table.getAllConstraints(), table.getType());
result.getColumnNames().addAll(table.getColumnNames());
result.getVisibleColumns().addAll(table.getVisibleColumns());
result.getPrimaryKeyColumns().addAll(table.getPrimaryKeyColumns());
return result;
}

@Override
public Class<CreateIndexStatement> getType() {
return CreateIndexStatement.class;
Original file line number Diff line number Diff line change
@@ -19,8 +19,12 @@

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO;
import org.apache.shardingsphere.infra.metadata.database.schema.util.IndexMetaDataUtils;
@@ -49,9 +53,13 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
continue;
}
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), actualSchemaName);
ShardingSphereTable table = newShardingSphereTable(database.getSchema(actualSchemaName).getTable(logicTableName.get()));
table.removeIndex(each.getIndexName().getIdentifier().getValue());
alterSchemaMetaDataPOJO.getAlteredTables().add(table);
ShardingSpherePreconditions.checkState(database.containsSchema(actualSchemaName), () -> new SchemaNotFoundException(schemaName));
ShardingSphereSchema schema = database.getSchema(actualSchemaName);
ShardingSpherePreconditions.checkState(schema.containsTable(logicTableName.get()), () -> new TableNotFoundException(logicTableName.get()));
ShardingSphereTable table = schema.getTable(logicTableName.get());
ShardingSphereTable newTable = new ShardingSphereTable(table.getName(), table.getAllColumns(), table.getAllIndexes(), table.getAllConstraints(), table.getType());
newTable.removeIndex(each.getIndexName().getIdentifier().getValue());
alterSchemaMetaDataPOJO.getAlteredTables().add(newTable);
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
}
}
@@ -65,14 +73,6 @@ private Optional<String> findLogicTableName(final ShardingSphereDatabase databas
return tableNames.isEmpty() ? Optional.empty() : Optional.of(tableNames.iterator().next().getTableName());
}

private ShardingSphereTable newShardingSphereTable(final ShardingSphereTable table) {
ShardingSphereTable result = new ShardingSphereTable(table.getName(), table.getAllColumns(), table.getAllIndexes(), table.getAllConstraints(), table.getType());
result.getColumnNames().addAll(table.getColumnNames());
result.getVisibleColumns().addAll(table.getVisibleColumns());
result.getPrimaryKeyColumns().addAll(table.getPrimaryKeyColumns());
return result;
}

@Override
public Class<DropIndexStatement> getType() {
return DropIndexStatement.class;

0 comments on commit fb69b11

Please sign in to comment.