Search in sources :

Example 16 with SchemaConfig

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

the class DruidMysqlHavingTest method testHaving.

@Test
public void testHaving() throws SQLNonTransientException {
    String sql = "select avg(offer_id) avgofferid, member_id from offer_detail group by member_id having avgofferid > 100";
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getSqlMerge().getHavingColsName().length);
    sql = "select avg(offer_id) avgofferid, member_id from offer_detail group by member_id having avg(offer_id) > 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getSqlMerge().getHavingColsName().length);
    sql = "select count(offer_id) countofferid, member_id from offer_detail group by member_id having countofferid > 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getSqlMerge().getHavingColsName().length);
    sql = "select count(offer_id) countofferid, member_id from offer_detail group by member_id having count(offer_id) > 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getSqlMerge().getHavingColsName().length);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) Test(org.junit.Test)

Example 17 with SchemaConfig

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

the class DruidMysqlRouteStrategyTest method testGroupLimit.

public void testGroupLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = null;
    RouteResultset rrs = null;
    sql = "select count(*) from (select * from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select count(*) from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select * from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select * from (select count(*) from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 18 with SchemaConfig

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

the class DDLRouteTest method testSpecialCharDDL.

@Test
public void testSpecialCharDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // alter table test
    String sql = " ALTER TABLE COMPANY\r\nADD COLUMN TEST  VARCHAR(255) NULL AFTER CREATE_DATE,\r\n CHARACTER SET = UTF8";
    sql = RouterUtil.getFixedSql(sql);
    List<String> dataNodes = new ArrayList<>();
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(sql, 0));
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 19 with SchemaConfig

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

the class DDLRouteTest method testTableMetaRead.

@Test
public void testTableMetaRead() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = "desc offer";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer", rrs.getNodes()[0].getStatement());
    sql = " desc cndb.offer";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer", rrs.getNodes()[0].getStatement());
    sql = " desc cndb.offer col1";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer col1", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL COLUMNS FROM  offer  IN db_name WHERE true";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SHOW, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW FULL COLUMNS FROM offer WHERE true", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL COLUMNS FROM  db.offer  IN db_name WHERE true";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SHOW, sql, null, null, cachePool);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW FULL COLUMNS FROM offer WHERE true", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL TABLES FROM `TESTDB` WHERE Table_type != 'VIEW'";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SHOW, sql, null, null, cachePool);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("SHOW FULL TABLES WHERE Table_type != 'VIEW'", rrs.getNodes()[0].getStatement());
    sql = "SHOW INDEX  IN offer FROM  db_name";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SHOW, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW INDEX  FROM offer", rrs.getNodes()[0].getStatement());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 20 with SchemaConfig

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

the class PartitionByRangeDateHashTest method testRange.

@Test
public void testRange() throws SQLNonTransientException {
    String sql = "select * from offer1  where col_date between '2014-01-01 00:00:00'  and '2014-01-03 23:59:59'     order by id desc limit 100";
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    junit.framework.Assert.assertEquals(6, rrs.getNodes().length);
    sql = "select * from offer1  where col_date between '2014-01-01 00:00:00'  and '2014-01-04 00:00:59'      order by id desc limit 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    junit.framework.Assert.assertEquals(12, rrs.getNodes().length);
    sql = "select * from offer1  where col_date between '2014-01-04 00:00:00'  and '2014-01-06 23:59:59'      order by id desc limit 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    junit.framework.Assert.assertEquals(6, 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)

Aggregations

SchemaConfig (io.mycat.config.model.SchemaConfig)79 SystemConfig (io.mycat.config.model.SystemConfig)54 RouteResultset (io.mycat.route.RouteResultset)34 Test (org.junit.Test)31 TableConfig (io.mycat.config.model.TableConfig)12 MycatConfig (io.mycat.config.MycatConfig)9 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 SQLNonTransientException (java.sql.SQLNonTransientException)7 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)6 RowDataPacket (io.mycat.net.mysql.RowDataPacket)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (io.mycat.cache.CacheService)5 MycatCluster (io.mycat.config.MycatCluster)5 UserConfig (io.mycat.config.model.UserConfig)5 ArrayList (java.util.ArrayList)5 NoSuchElementException (java.util.NoSuchElementException)5 EOFPacket (io.mycat.net.mysql.EOFPacket)4 FieldPacket (io.mycat.net.mysql.FieldPacket)4 RouteService (io.mycat.route.RouteService)4