Search in sources :

Example 11 with RouteResultsetNode

use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.

the class DruidInsertParser method parserBatchInsert.

/**
 * insert into .... select .... or insert into table() values (),(),....
 *
 * @param schemaInfo SchemaInfo
 * @param rrs RouteResultset
 * @param partitionColumn partitionColumn
 * @param insertStmt insertStmt
 * @throws SQLNonTransientException if the column size of values is not correct
 */
private void parserBatchInsert(SchemaInfo schemaInfo, RouteResultset rrs, String partitionColumn, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    // insert into table() values (),(),....
    SchemaConfig schema = schemaInfo.getSchemaConfig();
    String tableName = schemaInfo.getTable();
    // the size of columns
    int columnNum = getTableColumns(schemaInfo, insertStmt.getColumns());
    int shardingColIndex = tryGetShardingColIndex(schemaInfo, insertStmt, partitionColumn);
    List<ValuesClause> valueClauseList = insertStmt.getValuesList();
    Map<Integer, List<ValuesClause>> nodeValuesMap = 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.info(msg);
            throw new SQLNonTransientException(msg);
        }
        SQLExpr expr = valueClause.getValues().get(shardingColIndex);
        String shardingValue = shardingValueToSting(expr);
        Integer nodeIndex = algorithm.calculate(shardingValue);
        // null means can't find any valid index
        if (nodeIndex == null) {
            String msg = "can't find any valid datanode :" + tableName + " -> " + partitionColumn + " -> " + shardingValue;
            LOGGER.info(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(), RouterUtil.removeSchema(insertStmt.toString(), schemaInfo.getSchema()));
        count++;
    }
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) TableConfig(com.actiontech.dble.config.model.TableConfig) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 12 with RouteResultsetNode

use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.

the class DruidInsertParser method parserSingleInsert.

/**
 * @param schemaInfo SchemaInfo
 * @param rrs RouteResultset
 * @param partitionColumn partitionColumn
 * @param insertStmt insertStmt
 * @throws SQLNonTransientException if not find an valid data node
 */
private void parserSingleInsert(SchemaInfo schemaInfo, RouteResultset rrs, String partitionColumn, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    int shardingColIndex = tryGetShardingColIndex(schemaInfo, insertStmt, partitionColumn);
    SQLExpr valueExpr = insertStmt.getValues().getValues().get(shardingColIndex);
    String shardingValue = shardingValueToSting(valueExpr);
    TableConfig tableConfig = schemaInfo.getSchemaConfig().getTables().get(schemaInfo.getTable());
    AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
    Integer nodeIndex = algorithm.calculate(shardingValue);
    if (nodeIndex == null || nodeIndex >= tableConfig.getDataNodes().size()) {
        String msg = "can't find any valid data node :" + schemaInfo.getTable() + " -> " + partitionColumn + " -> " + shardingValue;
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    RouteResultsetNode[] nodes = new RouteResultsetNode[1];
    nodes[0] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), RouterUtil.removeSchema(insertStmt.toString(), schemaInfo.getSchema()));
    // INSERT INTO TABLEName (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;
    if (insertStmt.getDuplicateKeyUpdate() != null) {
        List<SQLExpr> updateList = insertStmt.getDuplicateKeyUpdate();
        for (SQLExpr expr : updateList) {
            SQLBinaryOpExpr opExpr = (SQLBinaryOpExpr) expr;
            String column = StringUtil.removeBackQuote(opExpr.getLeft().toString().toUpperCase());
            if (column.equals(partitionColumn)) {
                String msg = "Sharding column can't be updated: " + schemaInfo.getTable() + " -> " + partitionColumn;
                LOGGER.info(msg);
                throw new SQLNonTransientException(msg);
            }
        }
    }
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) TableConfig(com.actiontech.dble.config.model.TableConfig) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 13 with RouteResultsetNode

use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.

the class DruidLockTableParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLNonTransientException {
    // for lock tables table1 write, table2
    // DruidParser can only parser table1,
    // use "," to judge
    String sql = rrs.getStatement();
    sql = sql.replaceAll("\n", " ").replaceAll("\t", " ");
    String[] stmts = SplitUtil.split(sql, ',', true);
    // contains ","
    if (stmts.length > 1) {
        String tmpStmt = null;
        String[] tmpWords = null;
        for (int i = 1; i < stmts.length; i++) {
            tmpStmt = stmts[i];
            tmpWords = SplitUtil.split(tmpStmt, ' ', true);
            if (tmpWords.length == 2 && ("READ".equalsIgnoreCase(tmpWords[1]) || "WRITE".equalsIgnoreCase(tmpWords[1]))) {
                // unsupport lock multi-table
                continue;
            } else {
                // unsupport lock multi-table
                throw new SQLNonTransientException("You have an error in your SQL syntax, don't support lock multi tables!");
            }
        }
        LOGGER.info("can't lock multi-table");
        throw new SQLNonTransientException("can't lock multi-table");
    }
    MySqlLockTableStatement lockTableStat = (MySqlLockTableStatement) stmt;
    String table = lockTableStat.getTableSource().toString();
    if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
        table = table.toLowerCase();
    }
    TableConfig tableConfig = schema.getTables().get(table);
    if (tableConfig == null) {
        String msg = "can't find table define of " + table + " in schema:" + schema.getName();
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    LockType lockType = lockTableStat.getLockType();
    if (LockType.WRITE != lockType && LockType.READ != lockType) {
        String msg = "lock type must be write or read";
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    List<String> dataNodes = tableConfig.getDataNodes();
    RouteResultsetNode[] nodes = new RouteResultsetNode[dataNodes.size()];
    for (int i = 0; i < dataNodes.size(); i++) {
        nodes[i] = new RouteResultsetNode(dataNodes.get(i), ServerParse.LOCK, stmt.toString());
    }
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
    return schema;
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) MySqlLockTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) TableConfig(com.actiontech.dble.config.model.TableConfig) LockType(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement.LockType)

Example 14 with RouteResultsetNode

use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.

the class DruidReplaceParser method parserBatchInsert.

/**
 * insert into .... select .... OR insert into table() values (),(),....
 *
 * @param schemaInfo SchemaInfo
 * @param rrs RouteResultset
 * @param partitionColumn partitionColumn
 * @param replace MySqlReplaceStatement
 * @throws SQLNonTransientException  if the column size of values is not correct
 */
private void parserBatchInsert(SchemaInfo schemaInfo, RouteResultset rrs, String partitionColumn, MySqlReplaceStatement replace) throws SQLNonTransientException {
    // insert into table() values (),(),....
    SchemaConfig schema = schemaInfo.getSchemaConfig();
    String tableName = schemaInfo.getTable();
    // column number
    int columnNum = getTableColumns(schemaInfo, replace.getColumns());
    int shardingColIndex = tryGetShardingColIndex(schemaInfo, replace, partitionColumn);
    List<SQLInsertStatement.ValuesClause> valueClauseList = replace.getValuesList();
    Map<Integer, List<SQLInsertStatement.ValuesClause>> nodeValuesMap = new HashMap<>();
    TableConfig tableConfig = schema.getTables().get(tableName);
    AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
    for (SQLInsertStatement.ValuesClause valueClause : valueClauseList) {
        if (valueClause.getValues().size() != columnNum) {
            String msg = "bad insert sql columnSize != valueSize:" + columnNum + " != " + valueClause.getValues().size() + "values:" + valueClause;
            LOGGER.info(msg);
            throw new SQLNonTransientException(msg);
        }
        SQLExpr expr = valueClause.getValues().get(shardingColIndex);
        String shardingValue = shardingValueToSting(expr);
        Integer nodeIndex = algorithm.calculate(shardingValue);
        // no part find for this record
        if (nodeIndex == null) {
            String msg = "can't find any valid datanode :" + tableName + " -> " + partitionColumn + " -> " + shardingValue;
            LOGGER.info(msg);
            throw new SQLNonTransientException(msg);
        }
        if (nodeValuesMap.get(nodeIndex) == null) {
            nodeValuesMap.put(nodeIndex, new ArrayList<SQLInsertStatement.ValuesClause>());
        }
        nodeValuesMap.get(nodeIndex).add(valueClause);
    }
    RouteResultsetNode[] nodes = new RouteResultsetNode[nodeValuesMap.size()];
    int count = 0;
    for (Map.Entry<Integer, List<SQLInsertStatement.ValuesClause>> node : nodeValuesMap.entrySet()) {
        Integer nodeIndex = node.getKey();
        List<SQLInsertStatement.ValuesClause> valuesList = node.getValue();
        ReplaceTemp temp = new ReplaceTemp(replace);
        temp.setValuesList(valuesList);
        nodes[count] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), RouterUtil.removeSchema(temp.toString(), schemaInfo.getSchema()));
        count++;
    }
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) ReplaceTemp(com.actiontech.dble.route.parser.druid.ReplaceTemp) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) TableConfig(com.actiontech.dble.config.model.TableConfig)

Example 15 with RouteResultsetNode

use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.

the class DruidReplaceParser method parserSingleInsert.

/**
 * insert single record
 *
 * @param schemaInfo SchemaInfo
 * @param rrs RouteResultset
 * @param partitionColumn partitionColumn
 * @param replaceStatement MySqlReplaceStatement
 * @throws SQLNonTransientException if not find a valid data node
 */
private void parserSingleInsert(SchemaInfo schemaInfo, RouteResultset rrs, String partitionColumn, MySqlReplaceStatement replaceStatement) throws SQLNonTransientException {
    int shardingColIndex = tryGetShardingColIndex(schemaInfo, replaceStatement, partitionColumn);
    SQLExpr valueExpr = replaceStatement.getValuesList().get(0).getValues().get(shardingColIndex);
    String shardingValue = shardingValueToSting(valueExpr);
    TableConfig tableConfig = schemaInfo.getSchemaConfig().getTables().get(schemaInfo.getTable());
    AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
    Integer nodeIndex = algorithm.calculate(shardingValue);
    if (nodeIndex == null) {
        String msg = "can't find any valid data node :" + schemaInfo.getTable() + " -> " + partitionColumn + " -> " + shardingValue;
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    RouteResultsetNode[] nodes = new RouteResultsetNode[1];
    nodes[0] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), RouterUtil.removeSchema(replaceStatement.toString(), schemaInfo.getSchema()));
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) TableConfig(com.actiontech.dble.config.model.TableConfig) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)48 BackendConnection (com.actiontech.dble.backend.BackendConnection)12 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)9 RouteResultset (com.actiontech.dble.route.RouteResultset)8 TableConfig (com.actiontech.dble.config.model.TableConfig)6 SQLNonTransientException (java.sql.SQLNonTransientException)6 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)5 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)5 PushDownVisitor (com.actiontech.dble.backend.mysql.nio.handler.builder.sqlvisitor.PushDownVisitor)4 AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 ServerConfig (com.actiontech.dble.config.ServerConfig)3 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)3 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)3 LoadData (com.actiontech.dble.sqlengine.mpp.LoadData)3 LayerCachePool (com.actiontech.dble.cache.LayerCachePool)2 DruidShardingParseInfo (com.actiontech.dble.route.parser.druid.DruidShardingParseInfo)2 SQLException (java.sql.SQLException)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2