Search in sources :

Example 11 with RangeVariable

use of io.mycat.RangeVariable 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 12 with RangeVariable

use of io.mycat.RangeVariable 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 13 with RangeVariable

use of io.mycat.RangeVariable 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 14 with RangeVariable

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

the class ValueIndexCondition method getPartitions.

@NotNull
public static List<Partition> getPartitions(CustomRuleFunction customRuleFunction, Map<QueryType, List<ValueIndexCondition>> conditions, List<Object> params) {
    if (conditions == null || conditions.isEmpty()) {
        return customRuleFunction.calculate(Collections.emptyMap());
    }
    Objects.requireNonNull(conditions);
    List<Partition> partitions = customRuleFunction.calculate(Collections.emptyMap());
    for (Map.Entry<QueryType, List<ValueIndexCondition>> entry : conditions.entrySet()) {
        for (ValueIndexCondition condition : entry.getValue()) {
            List<Object> pointQueryKey = resolveParam(params, condition.getPointQueryKey());
            ComparisonOperator rangeQueryLowerOp = condition.getRangeQueryLowerOp();
            List<Object> rangeQueryLowerKey = resolveParam(params, condition.getRangeQueryLowerKey());
            ComparisonOperator rangeQueryUpperOp = condition.getRangeQueryUpperOp();
            List<Object> rangeQueryUpperKey = resolveParam(params, condition.getRangeQueryUpperKey());
            switch(condition.getQueryType()) {
                case PK_POINT_QUERY:
                    {
                        // queryByPrimaryKey
                        Map<String, RangeVariable> map = new HashMap<>();
                        if (pointQueryKey.size() > 1) {
                            List<Partition> curPartitions = new LinkedList<>();
                            for (Object o1 : pointQueryKey) {
                                String indexColumnName = condition.getIndexColumnNames().get(0);
                                RangeVariable rangeVariable = new RangeVariable(indexColumnName, RangeVariableType.EQUAL, o1);
                                map.put(indexColumnName, rangeVariable);
                                List<Partition> calculate = customRuleFunction.calculate(map);
                                curPartitions.addAll(calculate);
                            }
                            partitions = calculatePartitions(curPartitions, partitions);
                        } else {
                            Object o = pointQueryKey.get(0);
                            String indexColumnName = condition.getIndexColumnNames().get(0);
                            RangeVariable rangeVariable = new RangeVariable(indexColumnName, RangeVariableType.EQUAL, o);
                            map.put(indexColumnName, rangeVariable);
                            partitions = calculatePartitions(customRuleFunction, map, partitions);
                        }
                        break;
                    }
                case PK_RANGE_QUERY:
                    {
                        ArrayList<Object> leftValues = new ArrayList<>(2);
                        ArrayList<Object> rightValues = new ArrayList<>(2);
                        if (rangeQueryUpperOp == ComparisonOperator.LT || rangeQueryUpperOp == ComparisonOperator.LTE) {
                            for (Object o1 : rangeQueryLowerKey) {
                                if (o1 instanceof RexNode) {
                                    o1 = MycatRexUtil.resolveParam((RexNode) o1, params);
                                }
                                leftValues.add(o1);
                            }
                            Collections.sort((List) rangeQueryLowerKey);
                        }
                        if (rangeQueryLowerOp == ComparisonOperator.GT || rangeQueryLowerOp == ComparisonOperator.GTE) {
                            for (Object o1 : rangeQueryUpperKey) {
                                if (o1 instanceof RexNode) {
                                    o1 = MycatRexUtil.resolveParam((RexNode) o1, params);
                                }
                                rightValues.add(o1);
                            }
                            Collections.sort((List) rangeQueryUpperKey);
                        }
                        Object smallOne = null;
                        Object bigOne = null;
                        if (!rangeQueryLowerKey.isEmpty()) {
                            smallOne = rangeQueryLowerKey.get(0);
                        }
                        if (!rangeQueryUpperKey.isEmpty()) {
                            bigOne = rangeQueryUpperKey.get(rangeQueryUpperKey.size() - 1);
                        }
                        Map<String, RangeVariable> map = new HashMap<>();
                        if (smallOne != null && bigOne != null && rangeQueryUpperOp == ComparisonOperator.LTE && rangeQueryLowerOp == ComparisonOperator.GTE) {
                            for (String indexColumnName : condition.getIndexColumnNames()) {
                                RangeVariable rangeVariable = new RangeVariable(indexColumnName, RangeVariableType.RANGE, smallOne, bigOne);
                                map.put(indexColumnName, rangeVariable);
                            }
                        } else if (smallOne != null) {
                            RangeVariableType type = null;
                            if (rangeQueryUpperOp == ComparisonOperator.LT) {
                                type = RangeVariableType.LT;
                            } else if (rangeQueryUpperOp == ComparisonOperator.LTE) {
                                type = RangeVariableType.LTE;
                            }
                            if (type != null) {
                                for (String indexColumnName : condition.getIndexColumnNames()) {
                                    RangeVariable rangeVariable = new RangeVariable(indexColumnName, type, smallOne);
                                    map.put(indexColumnName, rangeVariable);
                                }
                            }
                        } else if (bigOne != null) {
                            RangeVariableType type = null;
                            if (rangeQueryUpperOp == ComparisonOperator.GT) {
                                type = RangeVariableType.GT;
                            } else if (rangeQueryUpperOp == ComparisonOperator.GTE) {
                                type = RangeVariableType.GTE;
                            }
                            if (type != null) {
                                for (String indexColumnName : condition.getIndexColumnNames()) {
                                    RangeVariable rangeVariable = new RangeVariable(indexColumnName, type, bigOne);
                                    map.put(indexColumnName, rangeVariable);
                                }
                            }
                        }
                        partitions = calculatePartitions(customRuleFunction, map, partitions);
                        break;
                    }
                case PK_FULL_SCAN:
                    break;
                default:
            }
        }
    }
    return partitions;
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) ComparisonOperator(io.mycat.querycondition.ComparisonOperator) RangeVariableType(io.mycat.RangeVariableType) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimestampString(org.apache.calcite.util.TimestampString) TimeString(org.apache.calcite.util.TimeString) ToString(lombok.ToString) QueryType(io.mycat.querycondition.QueryType) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with RangeVariable

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

the class AutoFunction method calculate.

@Override
public List<Partition> calculate(Map<String, RangeVariable> values) {
    boolean getDbIndex = false;
    int dIndex = 0;
    boolean getTIndex = false;
    int tIndex = 0;
    Optional<Set<Object>> dbRange = Optional.empty();
    Optional<Set<Object>> tableRange = Optional.empty();
    Set<Map.Entry<String, RangeVariable>> entries = values.entrySet();
    for (Map.Entry<String, RangeVariable> e : entries) {
        RangeVariable rangeVariable = e.getValue();
        for (String dbShardingKey : dbKeys) {
            if (SQLUtils.nameEquals(dbShardingKey, e.getKey())) {
                switch(rangeVariable.getOperator()) {
                    case EQUAL:
                        Object value = rangeVariable.getValue();
                        dIndex = finalDbFunction.applyAsInt(value);
                        getDbIndex = true;
                        if (dIndex < 0) {
                            finalDbFunction.applyAsInt(value);
                            throw new IllegalArgumentException();
                        }
                        break;
                    case RANGE:
                        if (isShardingDbEnum()) {
                            dbRange = getRange(rangeVariable, this.partitionSize, dbMethod.getMethodName(), finalDbFunction);
                        }
                        break;
                    default:
                        continue;
                }
            }
        }
        for (String tableShardingKey : tableKeys) {
            if (SQLUtils.nameEquals(tableShardingKey, e.getKey())) {
                switch(rangeVariable.getOperator()) {
                    case EQUAL:
                        Object value = rangeVariable.getValue();
                        tIndex = finalTableFunction.applyAsInt(value);
                        getTIndex = true;
                        break;
                    case RANGE:
                        if (isShardingTableEnum()) {
                            tableRange = getRange(rangeVariable, this.partitionSize, tableMethod.getMethodName(), finalTableFunction);
                        }
                        break;
                    default:
                        continue;
                }
            }
        }
    }
    if (getDbIndex && getTIndex) {
        return (List) scanOnlyDbTableIndex(dIndex, tIndex);
    }
    if (getTIndex) {
        return (List) scanOnlyTableIndex(tIndex);
    }
    if (getDbIndex) {
        return (List) scanOnlyDbIndex(dIndex);
    }
    if (dbRange.isPresent() || tableRange.isPresent()) {
        List<Partition> res = new ArrayList<>();
        if (dbRange.isPresent() && tableRange.isPresent()) {
            Set<Object> dbSet = dbRange.get();
            Set<Object> tableSet = tableRange.get();
            if (dbKeys.equals(tableKeys)) {
                for (Object localDate : dbSet) {
                    List<Partition> partitions = scanOnlyDbTableIndex(finalDbFunction.applyAsInt(localDate), finalTableFunction.applyAsInt(localDate));
                    res.addAll(partitions);
                }
            } else {
                for (Object outer : dbSet) {
                    List<Partition> partitions = scanOnlyDbIndex(finalDbFunction.applyAsInt(outer));
                    for (Object inner : tableSet) {
                        int i = finalTableFunction.applyAsInt(inner);
                        res.addAll(partitions.stream().filter(p -> p.getTableIndex() == i).collect(Collectors.toList()));
                    }
                }
            }
            return res;
        } else if (dbRange.isPresent()) {
            Set<Object> set = dbRange.get();
            for (Object o : set) {
                res.addAll(scanOnlyDbIndex(finalDbFunction.applyAsInt(o)));
            }
            return res;
        } else if (tableRange.isPresent()) {
            Set<Object> set = tableRange.get();
            for (Object o : set) {
                res.addAll(scanOnlyTableIndex(finalTableFunction.applyAsInt(o)));
            }
            return res;
        }
    }
    return scanAll();
}
Also used : RangeVariable(io.mycat.RangeVariable) Partition(io.mycat.Partition)

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