Search in sources :

Example 61 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class RuleFunctionSuitTableTest method testPartitionByHashMod.

@Test
public void testPartitionByHashMod() {
    PartitionByHashMod partition = new PartitionByHashMod();
    // partition = 3;
    partition.setCount(3);
    Assert.assertEquals(3, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "partition-hash-mod");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2,dn3", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit1);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(1, suit2);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2"));
    int suit3 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit3);
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Example 62 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class DruidCreateTableParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement) stmt;
    if (createStmt.getQuery() != null) {
        String msg = "create table from other table not supported :" + stmt;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String tableName = StringUtil.removeBackquote(createStmt.getTableSource().toString().toUpperCase());
    if (schema.getTables().containsKey(tableName)) {
        TableConfig tableConfig = schema.getTables().get(tableName);
        AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
        if (algorithm instanceof SlotFunction) {
            SQLColumnDefinition column = new SQLColumnDefinition();
            column.setDataType(new SQLCharacterDataType("int"));
            column.setName(new SQLIdentifierExpr("_slot"));
            column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
            ((SQLCreateTableStatement) stmt).getTableElementList().add(column);
            String sql = createStmt.toString();
            rrs.setStatement(sql);
            ctx.setSql(sql);
        }
    }
    ctx.addTable(tableName);
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLNonTransientException(java.sql.SQLNonTransientException) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) TableConfig(io.mycat.config.model.TableConfig) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SlotFunction(io.mycat.route.function.SlotFunction)

Example 63 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class DruidInsertParser method parserBatchInsert.

/**
 * insert into .... select .... 或insert into table() values (),(),....
 * @param schema
 * @param rrs
 * @param insertStmt
 * @throws SQLNonTransientException
 */
private void parserBatchInsert(SchemaConfig schema, RouteResultset rrs, String partitionColumn, String tableName, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    // insert into table() values (),(),....
    if (insertStmt.getValuesList().size() > 1) {
        // 字段列数
        int columnNum = insertStmt.getColumns().size();
        int shardingColIndex = getShardingColIndex(insertStmt, partitionColumn);
        if (shardingColIndex == -1) {
            String msg = "bad insert sql (sharding column:" + partitionColumn + " not provided," + insertStmt;
            LOGGER.warn(msg);
            throw new SQLNonTransientException(msg);
        } else {
            List<ValuesClause> valueClauseList = insertStmt.getValuesList();
            Map<Integer, List<ValuesClause>> nodeValuesMap = new HashMap<Integer, List<ValuesClause>>();
            Map<Integer, Integer> slotsMap = new HashMap<>();
            TableConfig tableConfig = schema.getTables().get(tableName);
            AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
            for (ValuesClause valueClause : valueClauseList) {
                if (valueClause.getValues().size() != columnNum) {
                    String msg = "bad insert sql columnSize != valueSize:" + columnNum + " != " + valueClause.getValues().size() + "values:" + valueClause;
                    LOGGER.warn(msg);
                    throw new SQLNonTransientException(msg);
                }
                SQLExpr expr = valueClause.getValues().get(shardingColIndex);
                String shardingValue = null;
                if (expr instanceof SQLIntegerExpr) {
                    SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
                    shardingValue = intExpr.getNumber() + "";
                } else if (expr instanceof SQLCharExpr) {
                    SQLCharExpr charExpr = (SQLCharExpr) expr;
                    shardingValue = charExpr.getText();
                }
                Integer nodeIndex = algorithm.calculate(shardingValue);
                if (algorithm instanceof SlotFunction) {
                    slotsMap.put(nodeIndex, ((SlotFunction) algorithm).slotValue());
                }
                // 没找到插入的分片
                if (nodeIndex == null) {
                    String msg = "can't find any valid datanode :" + tableName + " -> " + partitionColumn + " -> " + shardingValue;
                    LOGGER.warn(msg);
                    throw new SQLNonTransientException(msg);
                }
                if (nodeValuesMap.get(nodeIndex) == null) {
                    nodeValuesMap.put(nodeIndex, new ArrayList<ValuesClause>());
                }
                nodeValuesMap.get(nodeIndex).add(valueClause);
            }
            RouteResultsetNode[] nodes = new RouteResultsetNode[nodeValuesMap.size()];
            int count = 0;
            for (Map.Entry<Integer, List<ValuesClause>> node : nodeValuesMap.entrySet()) {
                Integer nodeIndex = node.getKey();
                List<ValuesClause> valuesList = node.getValue();
                insertStmt.setValuesList(valuesList);
                nodes[count] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), insertStmt.toString());
                if (algorithm instanceof SlotFunction) {
                    nodes[count].setSlot(slotsMap.get(nodeIndex));
                    nodes[count].setStatement(ParseUtil.changeInsertAddSlot(nodes[count].getStatement(), nodes[count].getSlot()));
                }
                nodes[count++].setSource(rrs);
            }
            rrs.setNodes(nodes);
            rrs.setFinishedRoute(true);
        }
    } else if (insertStmt.getQuery() != null) {
        // insert into .... select ....
        String msg = "TODO:insert into .... select .... not supported!";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) HashMap(java.util.HashMap) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SlotFunction(io.mycat.route.function.SlotFunction) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(io.mycat.route.RouteResultsetNode) TableConfig(io.mycat.config.model.TableConfig) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 64 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class DruidSelectParser method isNeedCache.

private boolean isNeedCache(SchemaConfig schema, RouteResultset rrs, MySqlSelectQueryBlock mysqlSelectQuery, Map<String, Map<String, Set<ColumnRoutePair>>> allConditions) {
    if (ctx.getTables() == null || ctx.getTables().size() == 0) {
        return false;
    }
    TableConfig tc = schema.getTables().get(ctx.getTables().get(0));
    if (tc == null || (ctx.getTables().size() == 1 && tc.isGlobalTable())) {
        // || (ctx.getTables().size() == 1) && tc.getRule() == null && tc.getDataNodes().size() == 1
        return false;
    } else {
        // 单表主键查询
        if (ctx.getTables().size() == 1) {
            String tableName = ctx.getTables().get(0);
            String primaryKey = schema.getTables().get(tableName).getPrimaryKey();
            // schema.getTables().get(ctx.getTables().get(0)).getParentKey() != null;
            if (ctx.getRouteCalculateUnit().getTablesAndConditions().get(tableName) != null && ctx.getRouteCalculateUnit().getTablesAndConditions().get(tableName).get(primaryKey) != null && tc.getDataNodes().size() > 1) {
                // 有主键条件
                return false;
            }
        // 全局表不缓存
        } else if (RouterUtil.isAllGlobalTable(ctx, schema)) {
            return false;
        }
        return true;
    }
}
Also used : TableConfig(io.mycat.config.model.TableConfig)

Example 65 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class BatchInsertSequence method route.

@Override
public void route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool) {
    int rs = ServerParse.parse(realSQL);
    this.sqltype = rs & 0xff;
    this.sysConfig = sysConfig;
    this.schema = schema;
    this.charset = charset;
    this.sc = sc;
    this.cachePool = cachePool;
    try {
        MySqlStatementParser parser = new MySqlStatementParser(realSQL);
        SQLStatement statement = parser.parseStatement();
        MySqlInsertStatement insert = (MySqlInsertStatement) statement;
        if (insert.getValuesList() != null) {
            String tableName = StringUtil.getTableName(realSQL).toUpperCase();
            TableConfig tableConfig = schema.getTables().get(tableName);
            // 获得表的主键字段
            String primaryKey = tableConfig.getPrimaryKey();
            SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
            sqlIdentifierExpr.setName(primaryKey);
            insert.getColumns().add(sqlIdentifierExpr);
            if (sequenceHandler == null) {
                int seqHandlerType = MycatServer.getInstance().getConfig().getSystem().getSequnceHandlerType();
                switch(seqHandlerType) {
                    case SystemConfig.SEQUENCEHANDLER_MYSQLDB:
                        sequenceHandler = IncrSequenceMySQLHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_LOCALFILE:
                        sequenceHandler = IncrSequencePropHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_LOCAL_TIME:
                        sequenceHandler = IncrSequenceTimeHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED:
                        sequenceHandler = DistributedSequenceHandler.getInstance(MycatServer.getInstance().getConfig().getSystem());
                        break;
                    case SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT:
                        sequenceHandler = IncrSequenceZKHandler.getInstance();
                        break;
                    default:
                        throw new java.lang.IllegalArgumentException("Invalid sequnce handler type " + seqHandlerType);
                }
            }
            for (ValuesClause vc : insert.getValuesList()) {
                SQLIntegerExpr sqlIntegerExpr = new SQLIntegerExpr();
                long value = sequenceHandler.nextId(tableName.toUpperCase());
                // 插入生成的sequence值
                sqlIntegerExpr.setNumber(value);
                vc.addValue(sqlIntegerExpr);
            }
            String insertSql = insert.toString();
            this.executeSql = insertSql;
        }
    } catch (Exception e) {
        LOGGER.error("BatchInsertSequence.route(......)", e);
    }
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) TableConfig(io.mycat.config.model.TableConfig) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Aggregations

TableConfig (io.mycat.config.model.TableConfig)84 SchemaConfig (io.mycat.config.model.SchemaConfig)26 SQLNonTransientException (java.sql.SQLNonTransientException)26 Test (org.junit.Test)18 RuleConfig (io.mycat.config.model.rule.RuleConfig)16 RouteResultset (io.mycat.route.RouteResultset)14 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)14 HashMap (java.util.HashMap)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SlotFunction (io.mycat.route.function.SlotFunction)12 ArrayList (java.util.ArrayList)12 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)8 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)8 ConfigException (io.mycat.config.util.ConfigException)8 RouteResultsetNode (io.mycat.route.RouteResultsetNode)8 HashSet (java.util.HashSet)8 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)6 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)6 CacheService (io.mycat.cache.CacheService)6