Search in sources :

Example 31 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class ServerLoadDataInfileHandler method parseOneLine.

private void parseOneLine(List<SQLExpr> columns, String tableName, String[] line, boolean toFile, String lineEnd) {
    RouteResultset rrs = tryDirectRoute(sql, line);
    if (rrs == null || rrs.getNodes() == null || rrs.getNodes().length == 0) {
        String insertSql = makeSimpleInsert(columns, line, tableName, true);
        rrs = serverConnection.routeSQL(insertSql, ServerParse.INSERT);
    }
    if (rrs == null || rrs.getNodes() == null || rrs.getNodes().length == 0) {
    //无路由处理
    } else {
        for (RouteResultsetNode routeResultsetNode : rrs.getNodes()) {
            String name = routeResultsetNode.getName();
            LoadData data = routeResultMap.get(name);
            if (data == null) {
                data = new LoadData();
                data.setCharset(loadData.getCharset());
                data.setEnclose(loadData.getEnclose());
                data.setFieldTerminatedBy(loadData.getFieldTerminatedBy());
                data.setLineTerminatedBy(loadData.getLineTerminatedBy());
                data.setEscape(loadData.getEscape());
                routeResultMap.put(name, data);
            }
            String jLine = joinField(line, data);
            if (shoudAddSlot) {
                jLine = jLine + loadData.getFieldTerminatedBy() + routeResultsetNode.getSlot();
            }
            if (data.getData() == null) {
                data.setData(Lists.newArrayList(jLine));
            } else {
                data.getData().add(jLine);
            }
            if (toFile && //避免当导入数据跨多分片时内存溢出的情况
            data.getData().size() > 10000) {
                saveDataToFile(data, name);
            }
        }
    }
}
Also used : LoadData(io.mycat.sqlengine.mpp.LoadData) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 32 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class Explain2Handler method handle.

public static void handle(String stmt, ServerConnection c, int offset) {
    try {
        stmt = stmt.substring(offset);
        if (!stmt.toLowerCase().contains("datanode=") || !stmt.toLowerCase().contains("sql=")) {
            showerror(stmt, c, "explain2 datanode=? sql=?");
            return;
        }
        String dataNode = stmt.substring(stmt.indexOf("=") + 1, stmt.indexOf("sql=")).trim();
        String sql = "explain " + stmt.substring(stmt.indexOf("sql=") + 4, stmt.length()).trim();
        if (dataNode == null || dataNode.isEmpty() || sql == null || sql.isEmpty()) {
            showerror(stmt, c, "dataNode or sql is null or empty");
            return;
        }
        RouteResultsetNode node = new RouteResultsetNode(dataNode, ServerParse.SELECT, sql);
        RouteResultset rrs = new RouteResultset(sql, ServerParse.SELECT);
        node.setSource(rrs);
        EMPTY_ARRAY[0] = node;
        rrs.setNodes(EMPTY_ARRAY);
        SingleNodeHandler singleNodeHandler = new SingleNodeHandler(rrs, c.getSession2());
        singleNodeHandler.execute();
    } catch (Exception e) {
        logger.error(e.getMessage(), e.getCause());
        showerror(stmt, c, e.getMessage());
    }
}
Also used : SingleNodeHandler(io.mycat.backend.mysql.nio.handler.SingleNodeHandler) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 33 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class ExplainHandler method handle.

public static void handle(String stmt, ServerConnection c, int offset) {
    stmt = stmt.substring(offset).trim();
    RouteResultset rrs = getRouteResultset(c, stmt);
    if (rrs == null) {
        return;
    }
    ByteBuffer buffer = c.allocate();
    // write header
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    byte packetId = header.packetId;
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        field.packetId = ++packetId;
        buffer = field.write(buffer, c, true);
    }
    // write eof
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    buffer = eof.write(buffer, c, true);
    // write rows
    RouteResultsetNode[] rrsn = rrs.getNodes();
    for (RouteResultsetNode node : rrsn) {
        RowDataPacket row = getRow(node, c.getCharset());
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket) RouteResultset(io.mycat.route.RouteResultset)

Example 34 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testDuplicatePartitionKey.

public void testDuplicatePartitionKey() throws Exception {
    String sql = null;
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = null;
    sql = "select * from cndb.offer where (offer_id, group_id ) In (123,234)";
    schema = schemaMap.get("cndb");
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals(128, rrs.getNodes().length);
    for (int i = 0; i < 128; i++) {
        //            Assert.assertEquals("offer_dn" + i ,
        //                    rrs.getNodes()[i].getName());//node的排序有变化,所以此处不强求
        Assert.assertEquals("select * from offer where (offer_id, group_id ) In (123,234)", rrs.getNodes()[i].getStatement());
    }
    sql = "SELECT * FROM offer WHERE FALSE OR offer_id = 123 AND member_id = 123 OR member_id = 123 AND member_id = 234 OR member_id = 123 AND member_id = 345 OR member_id = 123 AND member_id = 456 OR offer_id = 234 AND group_id = 123 OR offer_id = 234 AND group_id = 234 OR offer_id = 234 AND group_id = 345 OR offer_id = 234 AND group_id = 456 OR offer_id = 345 AND group_id = 123 OR offer_id = 345 AND group_id = 234 OR offer_id = 345 AND group_id = 345 OR offer_id = 345 AND group_id = 456 OR offer_id = 456 AND group_id = 123 OR offer_id = 456 AND group_id = 234 OR offer_id = 456 AND group_id = 345 OR offer_id = 456 AND group_id = 456";
    schema = schemaMap.get("cndb");
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    getNodeMap(rrs, 128);
    sql = "select * from  offer where false" + " or offer_id=123 and group_id=123" + " or group_id=123 and offer_id=234" + " or offer_id=123 and group_id=345" + " or offer_id=123 and group_id=456  ";
    schema = schemaMap.get("cndb");
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Assert.assertEquals(-1l, rrs.getLimitSize());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset)

Example 35 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testIgnoreSchema.

public void testIgnoreSchema() throws Exception {
    SchemaConfig schema = schemaMap.get("ignoreSchemaTest");
    String sql = "select * from offer where offer_id=1";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
    sql = "select * from ignoreSchemaTest.offer1 where ignoreSchemaTest.offer1.offer_id=1";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("select * from offer1 where offer1.offer_id=1", rrs.getNodes()[0].getStatement());
    sql = "select * from ignoreSchemaTest2.offer where ignoreSchemaTest2.offer.offer_id=1";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(sql, rrs.getNodes()[0].getStatement(), sql);
    sql = "select * from ignoreSchemaTest2.offer a,offer b  where ignoreSchemaTest2.offer.offer_id=1";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("select * from ignoreSchemaTest2.offer a,offer b  where ignoreSchemaTest2.offer.offer_id=1", rrs.getNodes()[0].getStatement());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

RouteResultset (io.mycat.route.RouteResultset)52 SchemaConfig (io.mycat.config.model.SchemaConfig)34 SystemConfig (io.mycat.config.model.SystemConfig)32 RouteResultsetNode (io.mycat.route.RouteResultsetNode)15 Test (org.junit.Test)13 SQLNonTransientException (java.sql.SQLNonTransientException)11 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 TableConfig (io.mycat.config.model.TableConfig)6 CacheService (io.mycat.cache.CacheService)4 RouteService (io.mycat.route.RouteService)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 NoSuchElementException (java.util.NoSuchElementException)4 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 DruidShardingParseInfo (io.mycat.route.parser.druid.DruidShardingParseInfo)3 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)3 TreeSet (java.util.TreeSet)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)2