Search in sources :

Example 26 with ShardingTable

use of io.mycat.calcite.table.ShardingTable in project Mycat2 by MyCATApache.

the class YYYYDDFunctionTest method testYYYYDD.

/**
 *     https://help.aliyun.com/document_detail/71337.html
 */
@Test
public void testYYYYDD() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by YYYYDD(traveldate) tbpartition by YYYYDD(traveldate) tbpartitions 92 dbpartitions 8;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"8\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${index}\",\n" + "\t\t\t\t\t\"tableNum\":\"92\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYDD(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYDD(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
    String s = calculate.toString();
    Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_5', tableName='sharding_533', index=533, dbIndex=5, tableIndex=73}]"));
    Assert.assertEquals(736, shardingFuntion.calculate(Collections.emptyMap()).size());
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 27 with ShardingTable

use of io.mycat.calcite.table.ShardingTable in project Mycat2 by MyCATApache.

the class YYYYDDFunctionTest method testYYYYDDHashId.

@Test
public void testYYYYDDHashId() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by YYYYDD(traveldate) tbpartition by mod_hash(id) tbpartitions 8 dbpartitions 366;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"366\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"2\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYDD(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
    String s = calculate.toString();
    Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_157', tableName='sharding_0', index=314, dbIndex=157, tableIndex=0}, {targetName='c0', schemaName='db1_157', tableName='sharding_1', index=315, dbIndex=157, tableIndex=1}]"));
    Assert.assertEquals(732, shardingFuntion.calculate(Collections.emptyMap()).size());
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 28 with ShardingTable

use of io.mycat.calcite.table.ShardingTable in project Mycat2 by MyCATApache.

the class HashFunctionTest method testHashTableId.

@Test
public void testHashTableId() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " tbpartition by mod_hash(id) tbpartitions 2;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"2\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("id", (new RangeVariable("id", RangeVariableType.EQUAL, 15))));
    Assert.assertEquals(1, calculate.size());
    Assert.assertEquals(2, shardingFuntion.calculate(Collections.emptyMap()).size());
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 29 with ShardingTable

use of io.mycat.calcite.table.ShardingTable in project Mycat2 by MyCATApache.

the class HashFunctionTest method testHashDbId.

@Test
public void testHashDbId() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by mod_hash(id);");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"2\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"hash(id)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    String s1 = shardingFuntion.calculate(Collections.emptyMap()).toString();
    Assert.assertEquals("[{targetName='c0', schemaName='db1_0', tableName='sharding_0', index=0, dbIndex=0, tableIndex=0}, {targetName='c0', schemaName='db1_1', tableName='sharding_0', index=1, dbIndex=1, tableIndex=0}]", s1);
    System.out.println();
}
Also used : MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 30 with ShardingTable

use of io.mycat.calcite.table.ShardingTable in project Mycat2 by MyCATApache.

the class StatisticCenterTest method testSharding.

@Test
@Disabled
@Ignore
public void testSharding() throws Exception {
    DrdsSqlCompiler drds = getDrds();
    String schemaName = "db1";
    String tableName = "sharding";
    MetadataManager metadataManager = getMetadataManager();
    TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
    if (tableHandler == null) {
        return;
    }
    tableHandler.createPhysicalTables();
    ShardingTable table = (ShardingTable) tableHandler;
    List<Partition> backends = table.getBackends();
    Partition c0 = backends.get(0);
    Partition c1 = backends.get(1);
    try (DefaultConnection connection = jdbcManager.getConnection("prototype")) {
        deleteData(connection.getRawConnection(), schemaName, tableName);
        JdbcUtils.execute(connection.getRawConnection(), "insert db1.sharding(id) values(?)", Arrays.asList(1));
    }
    Double count = statisticCenter.computeTableRowCount(tableHandler);
    Assert.assertTrue(count.equals(count));
    statisticCenter.fetchTableRowCount(tableHandler);
    try (DefaultConnection connection = jdbcManager.getConnection("prototype")) {
        List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection.getRawConnection(), "select * from mycat.analyze_table ", Arrays.asList());
        Assert.assertTrue(maps.toString().contains(tableName));
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ShardingTable(io.mycat.calcite.table.ShardingTable) NameMap(io.mycat.util.NameMap) DrdsTest(io.mycat.drdsrunner.DrdsTest) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

ShardingTable (io.mycat.calcite.table.ShardingTable)36 Test (org.junit.Test)26 MetadataManager (io.mycat.MetadataManager)22 Partition (io.mycat.Partition)17 RexNode (org.apache.calcite.rex.RexNode)15 CustomRuleFunction (io.mycat.router.CustomRuleFunction)14 ShardingTableConfig (io.mycat.config.ShardingTableConfig)12 RangeVariable (io.mycat.RangeVariable)11 ValuePredicateAnalyzer (io.mycat.calcite.rewriter.ValuePredicateAnalyzer)11 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)10 QueryType (io.mycat.querycondition.QueryType)10 Map (java.util.Map)10 java.util (java.util)8 ImmutableList (com.google.common.collect.ImmutableList)6 GlobalTable (io.mycat.calcite.table.GlobalTable)6 NormalTable (io.mycat.calcite.table.NormalTable)6 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 MycatCalciteSupport (io.mycat.calcite.MycatCalciteSupport)5 ValueIndexCondition (io.mycat.calcite.rewriter.ValueIndexCondition)5