Search in sources :

Example 6 with RangeVariable

use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.

the class YYYYWEEKFunctionTest method testYYYYWEEK.

/**
 *     https://help.aliyun.com/document_detail/71334.html
 */
@Test
public void testYYYYWEEK() {
    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 YYYYWEEK(traveldate) tbpartition by YYYYWEEK(traveldate) tbpartitions 3 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\":\"14\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYWEEK(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYWEEK(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_1', tableName='sharding_15', index=15, dbIndex=1, tableIndex=1}]"));
    Assert.assertEquals(112, 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 7 with RangeVariable

use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.

the class MergeSubTablesFunction method calculate.

@Override
public List<Partition> calculate(Map<String, RangeVariable> values) {
    ArrayList<Partition> res = new ArrayList<>();
    for (RangeVariable rangeVariable : values.values()) {
        // 匹配字段名
        if (getColumnName().equalsIgnoreCase(rangeVariable.getColumnName())) {
            // /////////////////////////////////////////////////////////////
            String begin = Objects.toString(rangeVariable.getBegin());
            String end = Objects.toString(rangeVariable.getEnd());
            switch(rangeVariable.getOperator()) {
                case EQUAL:
                    {
                        Partition partition = this.calculate(begin);
                        if (partition != null) {
                            CollectionUtil.setOpAdd(res, partition);
                        } else {
                            return getTable().dataNodes();
                        }
                        break;
                    }
                case RANGE:
                    {
                        List<Partition> partitions = this.calculateRange(begin, end);
                        if (partitions == null || partitions.size() == 0) {
                            return getTable().dataNodes();
                        }
                        CollectionUtil.setOpAdd(res, partitions);
                        break;
                    }
                default:
                    continue;
            }
        }
    }
    return res.isEmpty() ? getTable().dataNodes() : res;
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) ImmutableList(com.google.common.collect.ImmutableList)

Example 8 with RangeVariable

use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.

the class YYYYMMFunctionTest method testYYYYMM.

/**
 *     https://help.aliyun.com/document_detail/71334.html
 */
@Test
public void testYYYYMM() {
    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 YYYYMM(traveldate) tbpartition by YYYYMM(traveldate) tbpartitions 3 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\":\"3\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYMM(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYMM(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_2', tableName='sharding_6', index=6, dbIndex=2, tableIndex=0}]"));
    Assert.assertEquals(24, 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 9 with RangeVariable

use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.

the class YYYYMMFunctionTest method testYYYYMMHash.

@Test
public void testYYYYMMHash() {
    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) tbpartition by YYYYMM(traveldate) tbpartitions 3 dbpartitions 24;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"24\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"3\",\n" + "\t\t\t\t\t\"tableMethod\":\"HASH(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYMM(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_6', tableName='sharding_0', index=18, dbIndex=6, tableIndex=0}, {targetName='c0', schemaName='db1_6', tableName='sharding_1', index=19, dbIndex=6, tableIndex=1}, {targetName='c0', schemaName='db1_6', tableName='sharding_2', index=20, dbIndex=6, tableIndex=2}]"));
    Assert.assertEquals(72, 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 10 with RangeVariable

use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.

the class Mycat1xSingleValueRuleFunction method calculate.

@Override
public List<Partition> calculate(Map<String, RangeVariable> values) {
    ArrayList<Partition> res = new ArrayList<>();
    for (RangeVariable rangeVariable : values.values()) {
        // 匹配字段名
        if (getColumnName().equalsIgnoreCase(rangeVariable.getColumnName())) {
            // /////////////////////////////////////////////////////////////
            String begin = Objects.toString(rangeVariable.getBegin());
            String end = Objects.toString(rangeVariable.getEnd());
            switch(rangeVariable.getOperator()) {
                case EQUAL:
                    {
                        Partition partition = this.calculate(begin);
                        if (partition != null) {
                            CollectionUtil.setOpAdd(res, partition);
                        } else {
                            return getTable().dataNodes();
                        }
                        break;
                    }
                case RANGE:
                    {
                        List<Partition> partitions = this.calculateRange(begin, end);
                        if (partitions == null || partitions.size() == 0) {
                            return getTable().dataNodes();
                        }
                        CollectionUtil.setOpAdd(res, partitions);
                        break;
                    }
                default:
                    continue;
            }
        }
    }
    return res.isEmpty() ? getTable().dataNodes() : res;
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable)

Aggregations

RangeVariable (io.mycat.RangeVariable)15 Partition (io.mycat.Partition)14 MetadataManager (io.mycat.MetadataManager)11 ShardingTable (io.mycat.calcite.table.ShardingTable)11 ShardingTableConfig (io.mycat.config.ShardingTableConfig)11 CustomRuleFunction (io.mycat.router.CustomRuleFunction)11 Test (org.junit.Test)11 Map (java.util.Map)9 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableList (com.google.common.collect.ImmutableList)1 RangeVariableType (io.mycat.RangeVariableType)1 ComparisonOperator (io.mycat.querycondition.ComparisonOperator)1 QueryType (io.mycat.querycondition.QueryType)1 LocalDate (java.time.LocalDate)1 ToString (lombok.ToString)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 DateString (org.apache.calcite.util.DateString)1 NlsString (org.apache.calcite.util.NlsString)1 TimeString (org.apache.calcite.util.TimeString)1 TimestampString (org.apache.calcite.util.TimestampString)1