Search in sources :

Example 46 with RouteResultset

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

the class DruidMysqlRouteStrategyTest method testERroute.

public void testERroute() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "insert into orders (id,name,customer_id) values(1,'testonly',1)";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    sql = "insert into orders (id,name,customer_id) values(1,'testonly',2000001)";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // can't update join key
    sql = "update orders set id=1 ,name='aaa' , customer_id=2000001";
    String err = null;
    try {
        rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    } catch (SQLNonTransientException e) {
        err = e.getMessage();
    }
    Assert.assertEquals(true, err.startsWith("Parent relevant column can't be updated ORDERS->CUSTOMER_ID"));
    // route by parent rule ,update sql
    sql = "update orders set id=1 ,name='aaa' where customer_id=2000001";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // route by parent rule but can't find datanode
    sql = "update orders set id=1 ,name='aaa' where customer_id=-1";
    try {
        rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    } catch (Exception e) {
        err = e.getMessage();
    }
    Assert.assertEquals(true, err.startsWith("can't find datanode for sharding column:"));
    // route by parent rule ,select sql
    sql = "select * from orders  where customer_id=2000001";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // route by parent rule ,delete sql
    sql = "delete from orders  where customer_id=2000001";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    //test alias in column
    sql = "select name as order_name from  orders order by order_name limit 10,5";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    MySqlStatementParser parser = new MySqlStatementParser("SELECT name AS order_name FROM orders ORDER BY order_name LIMIT 0,15");
    SQLStatement statement = parser.parseStatement();
//		Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(io.mycat.config.model.SchemaConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLNonTransientException(java.sql.SQLNonTransientException) NoSuchElementException(java.util.NoSuchElementException) RouteResultset(io.mycat.route.RouteResultset)

Example 47 with RouteResultset

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

the class DruidMysqlRouteStrategyTest method testGlobalTableroute.

public void testGlobalTableroute() throws Exception {
    String sql = null;
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = null;
    // select of global table route to only one datanode defined
    sql = "select * from company where company.name like 'aaa'";
    schema = schemaMap.get("TESTDB");
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    // query of global table only route to one datanode
    sql = "insert into company (id,name,level) values(111,'company1',3)";
    schema = schemaMap.get("TESTDB");
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    // update of global table route to every datanode defined
    sql = "update company set name=name+aaa";
    schema = schemaMap.get("TESTDB");
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    // delete of global table route to every datanode defined
    sql = "delete from company where id = 1";
    schema = schemaMap.get("TESTDB");
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    // company is global table ,will route to differnt tables
    schema = schemaMap.get("TESTDB");
    sql = "select * from  company A where a.sharding_id=10001 union select * from  company B where B.sharding_id =10010";
    Set<String> nodeSet = new HashSet<String>();
    for (int i = 0; i < 10; i++) {
        rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
        Assert.assertEquals(false, rrs.isCacheAble());
        Assert.assertEquals(1, rrs.getNodes().length);
        nodeSet.add(rrs.getNodes()[0].getName());
    }
    Assert.assertEquals(true, nodeSet.size() > 1);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset) HashSet(java.util.HashSet)

Example 48 with RouteResultset

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

the class HintTest method testHint.

/**
     * 测试注解
     *
     * @throws Exception
     */
@Test
public void testHint() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    //使用注解(新注解,/*后面没有空格),路由到1个节点
    String sql = "/*!mycat: sql = select * from employee where sharding_id = 10010 */select * from employee";
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
    Assert.assertTrue(rrs.getNodes().length == 1);
    //使用注解(新注解,/*后面有空格),路由到1个节点
    sql = "/*#mycat: sql = select * from employee where sharding_id = 10000 */select * from employee";
    rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
    Assert.assertTrue(rrs.getNodes().length == 1);
    //不用注解,路由到2个节点
    sql = "select * from employee";
    rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
    Assert.assertTrue(rrs.getNodes().length == 2);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 49 with RouteResultset

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

the class PartitionByRangeModTest method testRange.

@Test
public void testRange() throws SQLNonTransientException {
    String sql = "select * from offer  where id between 2000000  and 4000001     order by id desc limit 100";
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(10, rrs.getNodes().length);
    sql = "select * from offer  where id between 9  and 2000     order by id desc limit 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(5, rrs.getNodes().length);
    sql = "select * from offer  where id between 4000001  and 6005001     order by id desc limit 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(8, rrs.getNodes().length);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 50 with RouteResultset

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

the class DruidMysqlRouteStrategyTest method testOr.

/**
     * 测试or语句的路由
     *
     * @throws Exception
     */
@Test
public void testOr() throws Exception {
    //    	0-200M=0
    //    	200M1-400M=1
    //    	400M1-600M=2
    //    	600M1-800M=3
    //    	800M1-1000M=4
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select * from customer where sharding_id=10000 or 1=1;";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 2);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
    Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
    sql = "select * from customer where sharding_id = 10000 or sharding_id = 10010";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
    Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
    sql = "select * from customer where sharding_id = 10000 or user_id = 'wangwu'";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
    Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

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