Search in sources :

Example 96 with SQLNonTransientException

use of java.sql.SQLNonTransientException 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 97 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.

the class DruidInsertParser method parserSingleInsert.

/**
 * 单条insert(非批量)
 * @param schema
 * @param rrs
 * @param partitionColumn
 * @param tableName
 * @param insertStmt
 * @throws SQLNonTransientException
 */
private void parserSingleInsert(SchemaConfig schema, RouteResultset rrs, String partitionColumn, String tableName, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    boolean isFound = false;
    for (int i = 0; i < insertStmt.getColumns().size(); i++) {
        if (partitionColumn.equalsIgnoreCase(StringUtil.removeBackquote(insertStmt.getColumns().get(i).toString()))) {
            // 找到分片字段
            isFound = true;
            String column = StringUtil.removeBackquote(insertStmt.getColumns().get(i).toString());
            String value = StringUtil.removeBackquote(insertStmt.getValues().getValues().get(i).toString());
            RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
            routeCalculateUnit.addShardingExpr(tableName, column, value);
            ctx.addRouteCalculateUnit(routeCalculateUnit);
            // mycat是单分片键,找到了就返回
            break;
        }
    }
    if (!isFound) {
        // 分片表的
        String msg = "bad insert sql (sharding column:" + partitionColumn + " not provided," + insertStmt;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    // 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: " + tableName + " -> " + partitionColumn;
                LOGGER.warn(msg);
                throw new SQLNonTransientException(msg);
            }
        }
    }
}
Also used : RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) SQLNonTransientException(java.sql.SQLNonTransientException) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 98 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.

the class DruidSelectParser method tryRoute.

private void tryRoute(SchemaConfig schema, RouteResultset rrs, LayerCachePool cachePool) throws SQLNonTransientException {
    if (rrs.isFinishedRoute()) {
        // 避免重复路由
        return;
    }
    // 无表的select语句直接路由带任一节点
    if ((ctx.getTables() == null || ctx.getTables().size() == 0) && (ctx.getTableAliasMap() == null || ctx.getTableAliasMap().isEmpty())) {
        rrs = RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode(), ctx.getSql());
        rrs.setFinishedRoute(true);
        return;
    }
    // RouterUtil.tryRouteForTables(schema, ctx, rrs, true, cachePool);
    SortedSet<RouteResultsetNode> nodeSet = new TreeSet<RouteResultsetNode>();
    boolean isAllGlobalTable = RouterUtil.isAllGlobalTable(ctx, schema);
    for (RouteCalculateUnit unit : ctx.getRouteCalculateUnits()) {
        RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, ctx, unit, rrs, true, cachePool);
        if (rrsTmp != null && rrsTmp.getNodes() != null) {
            for (RouteResultsetNode node : rrsTmp.getNodes()) {
                nodeSet.add(node);
            }
        }
        if (isAllGlobalTable) {
            // 都是全局表时只计算一遍路由
            break;
        }
    }
    if (nodeSet.size() == 0) {
        Collection<String> stringCollection = ctx.getTableAliasMap().values();
        for (String table : stringCollection) {
            if (table != null && table.toLowerCase().contains("information_schema.")) {
                rrs = RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode(), ctx.getSql());
                rrs.setFinishedRoute(true);
                return;
            }
        }
        String msg = " find no Route:" + ctx.getSql();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
    int i = 0;
    for (Iterator<RouteResultsetNode> iterator = nodeSet.iterator(); iterator.hasNext(); ) {
        nodes[i] = (RouteResultsetNode) iterator.next();
        i++;
    }
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
}
Also used : RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) SQLNonTransientException(java.sql.SQLNonTransientException) TreeSet(java.util.TreeSet) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 99 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.

the class DruidSelectParser method changeSql.

/**
 * 改写sql:需要加limit的加上
 */
@Override
public void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, LayerCachePool cachePool) throws SQLNonTransientException {
    tryRoute(schema, rrs, cachePool);
    rrs.copyLimitToNodes();
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        int limitStart = 0;
        int limitSize = schema.getDefaultMaxLimit();
        // clear group having
        SQLSelectGroupByClause groupByClause = mysqlSelectQuery.getGroupBy();
        // Modified by winbill, 20160614, do NOT include having clause when routing to multiple nodes
        if (groupByClause != null && groupByClause.getHaving() != null && isRoutMultiNode(schema, rrs)) {
            groupByClause.setHaving(null);
        }
        Map<String, Map<String, Set<ColumnRoutePair>>> allConditions = getAllConditions();
        boolean isNeedAddLimit = isNeedAddLimit(schema, rrs, mysqlSelectQuery, allConditions);
        if (isNeedAddLimit) {
            Limit limit = new Limit();
            limit.setRowCount(new SQLIntegerExpr(limitSize));
            mysqlSelectQuery.setLimit(limit);
            rrs.setLimitSize(limitSize);
            String sql = getSql(rrs, stmt, isNeedAddLimit);
            rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitSize, true);
        }
        Limit limit = mysqlSelectQuery.getLimit();
        if (limit != null && !isNeedAddLimit) {
            SQLIntegerExpr offset = (SQLIntegerExpr) limit.getOffset();
            SQLIntegerExpr count = (SQLIntegerExpr) limit.getRowCount();
            if (offset != null) {
                limitStart = offset.getNumber().intValue();
                rrs.setLimitStart(limitStart);
            }
            if (count != null) {
                limitSize = count.getNumber().intValue();
                rrs.setLimitSize(limitSize);
            }
            if (isNeedChangeLimit(rrs)) {
                Limit changedLimit = new Limit();
                changedLimit.setRowCount(new SQLIntegerExpr(limitStart + limitSize));
                if (offset != null) {
                    if (limitStart < 0) {
                        String msg = "You have an error in your SQL syntax; check the manual that " + "corresponds to your MySQL server version for the right syntax to use near '" + limitStart + "'";
                        throw new SQLNonTransientException(ErrorCode.ER_PARSE_ERROR + " - " + msg);
                    } else {
                        changedLimit.setOffset(new SQLIntegerExpr(0));
                    }
                }
                mysqlSelectQuery.setLimit(changedLimit);
                String sql = getSql(rrs, stmt, isNeedAddLimit);
                rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitStart + limitSize, true);
                // 设置改写后的sql
                ctx.setSql(sql);
            } else {
                rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), getCtx().getSql(), rrs.getLimitStart(), rrs.getLimitSize(), true);
            // ctx.setSql(nativeSql);
            }
        }
        if (rrs.isDistTable()) {
            SQLTableSource from = mysqlSelectQuery.getFrom();
            for (RouteResultsetNode node : rrs.getNodes()) {
                SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
                sqlIdentifierExpr.setParent(from);
                sqlIdentifierExpr.setName(node.getSubTableName());
                SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
                from2.setAlias(from.getAlias());
                mysqlSelectQuery.setFrom(from2);
                node.setStatement(stmt.toString());
            }
        }
        rrs.setCacheAble(isNeedCache(schema, rrs, mysqlSelectQuery, allConditions));
    }
}
Also used : SQLSelectGroupByClause(com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(io.mycat.route.RouteResultsetNode) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) Limit(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 100 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.

the class DruidUpdateParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    // 这里限制了update分片表的个数只能有一个
    if (ctx.getTables() != null && getUpdateTableCount() > 1 && !schema.isNoSharding()) {
        String msg = "multi table related update not supported,tables:" + ctx.getTables();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    MySqlUpdateStatement update = (MySqlUpdateStatement) stmt;
    String tableName = StringUtil.removeBackquote(update.getTableName().getSimpleName().toUpperCase());
    TableConfig tc = schema.getTables().get(tableName);
    if (RouterUtil.isNoSharding(schema, tableName)) {
        // 整个schema都不分库或者该表不拆分
        RouterUtil.routeForTableMeta(rrs, schema, tableName, rrs.getStatement());
        rrs.setFinishedRoute(true);
        return;
    }
    String partitionColumn = tc.getPartitionColumn();
    String joinKey = tc.getJoinKey();
    if (tc.isGlobalTable() || (partitionColumn == null && joinKey == null)) {
        // 修改全局表 update 受影响的行数
        RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), rrs.getStatement(), tc.isGlobalTable());
        rrs.setFinishedRoute(true);
        return;
    }
    confirmShardColumnNotUpdated(update, schema, tableName, partitionColumn, joinKey, rrs);
    if (schema.getTables().get(tableName).isGlobalTable() && ctx.getRouteCalculateUnit().getTablesAndConditions().size() > 1) {
        throw new SQLNonTransientException("global table is not supported in multi table related update " + tableName);
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) TableConfig(io.mycat.config.model.TableConfig) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement)

Aggregations

SQLNonTransientException (java.sql.SQLNonTransientException)125 TableConfig (io.mycat.config.model.TableConfig)26 SQLException (java.sql.SQLException)23 Test (org.testng.annotations.Test)19 BaseTest (util.BaseTest)19 RouteResultset (io.mycat.route.RouteResultset)17 TableConfig (com.actiontech.dble.config.model.TableConfig)15 HashMap (java.util.HashMap)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)13 RouteResultsetNode (io.mycat.route.RouteResultsetNode)13 HashSet (java.util.HashSet)13 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)12 ColumnRoutePair (io.mycat.sqlengine.mpp.ColumnRoutePair)12 LinkedHashSet (java.util.LinkedHashSet)12 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)11 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)11 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)10 Map (java.util.Map)10 SlotFunction (io.mycat.route.function.SlotFunction)9 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)8