Skip to content

Commit

Permalink
fix sharding table can not alter column on file config
Browse files Browse the repository at this point in the history
  • Loading branch information
junwen12221 committed Dec 15, 2021
1 parent 2ca52b8 commit 3eeec62
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion hbt/src/main/java/io/mycat/MetadataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ private List<SimpleColumnInfo> getColumnInfoBySelectSQLOnJdbc(List<Partition> ba
return null;
}

private String getCreateTableSQLByJDBC(String schemaName, String tableName, List<Partition> backends) {
public String getCreateTableSQLByJDBC(String schemaName, String tableName, List<Partition> backends) {
backends = new ArrayList<>(backends);
backends.add(new BackendTableInfo(prototype, schemaName, tableName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package io.mycat.sqlhandler.ddl;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import io.mycat.*;
import io.mycat.config.PrototypeServer;
import io.mycat.datasource.jdbc.datasource.JdbcConnectionManager;
import io.mycat.sqlhandler.AbstractSQLHandler;
import io.mycat.sqlhandler.SQLRequest;
Expand All @@ -27,9 +31,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.*;


public class AlterTableSQLHandler extends AbstractSQLHandler<SQLAlterTableStatement> {
Expand All @@ -48,12 +50,34 @@ protected Future<Void> onExecute(SQLRequest<SQLAlterTableStatement> request, Myc
String tableName = SQLUtils.normalize(sqlAlterTableStatement.getTableName());
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
TableHandler tableHandler = metadataManager.getTable(schema, tableName);
MySqlCreateTableStatement createTableStatement = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(tableHandler.getCreateTableSQL());
JdbcConnectionManager connectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
Set<Partition> partitions = getDataNodes(tableHandler);
partitions.add(new BackendTableInfo(metadataManager.getPrototype(), schema, tableName));//add Prototype
executeOnDataNodes(sqlAlterTableStatement, connectionManager, partitions);
CreateTableSQLHandler.INSTANCE.createTable(Collections.emptyMap(), schema, tableName, createTableStatement);


MetadataManager manager = MetaClusterCurrent.wrapper(MetadataManager.class);
String createTableSQLByJDBC = manager.getCreateTableSQLByJDBC(schema, tableName, new ArrayList<>(partitions));
if (createTableSQLByJDBC!=null){
MySqlCreateTableStatement oldCreateTableStatement = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(tableHandler.getCreateTableSQL());
MySqlCreateTableStatement newMySqlCreateTableStatement = (MySqlCreateTableStatement)SQLUtils.parseSingleMysqlStatement(createTableSQLByJDBC);
boolean broadCast = oldCreateTableStatement.isBroadCast();
SQLExpr dbPartitionBy = oldCreateTableStatement.getDbPartitionBy();
SQLExpr dbPartitions = oldCreateTableStatement.getDbPartitions();
SQLExpr tablePartitionBy = oldCreateTableStatement.getTablePartitionBy();
SQLExpr tbpartitions = oldCreateTableStatement.getTbpartitions();

newMySqlCreateTableStatement.setBroadCast(broadCast);
newMySqlCreateTableStatement.setDbPartitionBy(dbPartitionBy);
newMySqlCreateTableStatement.setDbPartitions(dbPartitions);
newMySqlCreateTableStatement.setTablePartitionBy(tablePartitionBy);
newMySqlCreateTableStatement.setTablePartitions(tbpartitions);


CreateTableSQLHandler.INSTANCE.createTable(Collections.emptyMap(), schema, tableName, newMySqlCreateTableStatement);
}else {
return Future.failedFuture(new MycatException("can not generate new create table sql:"+sqlAlterTableStatement));
}
return response.sendOk();
} catch (Throwable throwable) {
return Future.failedFuture(throwable);
Expand Down

0 comments on commit 3eeec62

Please sign in to comment.