Search in sources :

Example 21 with ShardingTable

use of io.mycat.calcite.table.ShardingTable 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 22 with ShardingTable

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

the class ValuePredicateAnalyzerTest method testEqualParam.

@Test
public void testEqualParam() {
    DrdsSqlCompiler drds = DrdsTest.getDrds();
    RexNode rexNode = rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(1, MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), true));
    MetadataManager metadataManager = DrdsTest.getMetadataManager();
    ShardingTable table = (ShardingTable) metadataManager.getTable("db1", "sharding");
    List<String> columnList = Arrays.asList("id");
    ValuePredicateAnalyzer valuePredicateAnalyzer2 = new ValuePredicateAnalyzer(Arrays.asList(KeyMeta.of("default", "id")), columnList);
    List<Partition> dataNodes = ValueIndexCondition.getPartitions(table.getShardingFuntion(), valuePredicateAnalyzer2.translateMatch(rexNode), Arrays.asList(0));
    Assert.assertEquals(1, dataNodes.size());
}
Also used : ValuePredicateAnalyzer(io.mycat.calcite.rewriter.ValuePredicateAnalyzer) Partition(io.mycat.Partition) MetadataManager(io.mycat.MetadataManager) DrdsSqlCompiler(io.mycat.DrdsSqlCompiler) ShardingTable(io.mycat.calcite.table.ShardingTable) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 23 with ShardingTable

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

the class ValuePredicateAnalyzerTest method testRange.

@Test
public void testRange() {
    DrdsSqlCompiler drds = DrdsTest.getDrds();
    RexNode leftRexNode = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeInputRef(MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(0, MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), true));
    RexNode rightRexNode = rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN, rexBuilder.makeInputRef(MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(1, MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), true));
    RexNode rexNode = rexBuilder.makeCall(SqlStdOperatorTable.AND, leftRexNode, rightRexNode);
    List<String> columnList = Arrays.asList("id");
    ValuePredicateAnalyzer valuePredicateAnalyzer2 = new ValuePredicateAnalyzer(Arrays.asList(KeyMeta.of("default", "id")), columnList);
    MetadataManager metadataManager = DrdsTest.getMetadataManager();
    ShardingTable table = (ShardingTable) metadataManager.getTable("db1", "sharding");
    Map<QueryType, List<ValueIndexCondition>> condition = valuePredicateAnalyzer2.translateMatch(rexNode);
    Assert.assertEquals("{PK_RANGE_QUERY=[ValueIndexCondition(fieldNames=[id], indexName=default, indexColumnNames=[id], queryType=PK_RANGE_QUERY, rangeQueryLowerOp=GT, rangeQueryLowerKey=[0], rangeQueryUpperOp=LT, rangeQueryUpperKey=[1], pointQueryKey=null)]}", Objects.toString(condition));
}
Also used : ValuePredicateAnalyzer(io.mycat.calcite.rewriter.ValuePredicateAnalyzer) MetadataManager(io.mycat.MetadataManager) DrdsSqlCompiler(io.mycat.DrdsSqlCompiler) ShardingTable(io.mycat.calcite.table.ShardingTable) QueryType(io.mycat.querycondition.QueryType) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 24 with ShardingTable

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

the class ValuePredicateAnalyzerTest method testGte.

@Test
public void testGte() {
    DrdsTest.getDrds();
    int count = 5;
    RexNode rexNode = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeInputRef(MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(5, MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), true));
    List<String> columnList = Arrays.asList("id");
    ValuePredicateAnalyzer valuePredicateAnalyzer2 = new ValuePredicateAnalyzer(Arrays.asList(KeyMeta.of("seqSharding", "id")), columnList);
    Map<QueryType, List<ValueIndexCondition>> indexCondition = valuePredicateAnalyzer2.translateMatch(rexNode);
    Assert.assertEquals("{PK_RANGE_QUERY=[ValueIndexCondition(fieldNames=[id], indexName=seqSharding, indexColumnNames=[id], queryType=PK_RANGE_QUERY, rangeQueryLowerOp=GT, rangeQueryLowerKey=[5], rangeQueryUpperOp=null, rangeQueryUpperKey=null, pointQueryKey=null)]}", Objects.toString(indexCondition));
    MetadataManager metadataManager = DrdsTest.getMetadataManager();
    // 以下代码需要实现对应支持>的分片算法才能继续测试
    ShardingTable table = (ShardingTable) metadataManager.getTable("db1", "seqSharding");
    List o = ValueIndexCondition.getPartitions(table.getShardingFuntion(), valuePredicateAnalyzer2.translateMatch(rexNode), Arrays.asList());
    o.sort(Comparator.comparing(c -> c.toString()));
    Assert.assertEquals(4, o.size());
    Assert.assertEquals("[{targetName='c0', schemaName='db1_0', tableName='sharding_0', index=0, dbIndex=0, tableIndex=0}, {targetName='c0', schemaName='db1_0', tableName='sharding_1', index=1, dbIndex=0, tableIndex=1}, {targetName='c1', schemaName='db1_1', tableName='sharding_2', index=2, dbIndex=1, tableIndex=0}, {targetName='c1', schemaName='db1_1', tableName='sharding_3', index=3, dbIndex=1, tableIndex=1}]", Objects.toString(o));
    System.out.println();
}
Also used : ValuePredicateAnalyzer(io.mycat.calcite.rewriter.ValuePredicateAnalyzer) MetadataManager(io.mycat.MetadataManager) ShardingTable(io.mycat.calcite.table.ShardingTable) ValuePredicateAnalyzer(io.mycat.calcite.rewriter.ValuePredicateAnalyzer) java.util(java.util) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexBuilder(org.apache.calcite.rex.RexBuilder) RexLiteral(org.apache.calcite.rex.RexLiteral) Test(org.junit.Test) MycatCalciteSupport(io.mycat.calcite.MycatCalciteSupport) ValueIndexCondition(io.mycat.calcite.rewriter.ValueIndexCondition) RexUtil(org.apache.calcite.rex.RexUtil) Partition(io.mycat.Partition) QueryType(io.mycat.querycondition.QueryType) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RexNode(org.apache.calcite.rex.RexNode) KeyMeta(io.mycat.querycondition.KeyMeta) Assert(org.junit.Assert) DrdsSqlCompiler(io.mycat.DrdsSqlCompiler) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) MetadataManager(io.mycat.MetadataManager) QueryType(io.mycat.querycondition.QueryType) ShardingTable(io.mycat.calcite.table.ShardingTable) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 25 with ShardingTable

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

the class ValuePredicateAnalyzerTest method testMultiOrEquals5.

@Test
public void testMultiOrEquals5() {
    DrdsTest.getDrds();
    int count = 5;
    List<RexNode> orList = new LinkedList<>();
    for (int i = 0; i < count; i++) {
        RexNode rexNode = rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(i, MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), true));
        orList.add(rexNode);
    }
    RexNode rexNode = rexBuilder.makeCall(SqlStdOperatorTable.OR, orList);
    List<String> columnList = Arrays.asList("id");
    ValuePredicateAnalyzer valuePredicateAnalyzer2 = new ValuePredicateAnalyzer(Arrays.asList(KeyMeta.of("seqSharding", "id")), columnList);
    Map<QueryType, List<ValueIndexCondition>> indexCondition = valuePredicateAnalyzer2.translateMatch(rexNode);
    Assert.assertEquals("{PK_POINT_QUERY=[ValueIndexCondition(fieldNames=[id], indexName=seqSharding, indexColumnNames=[id], queryType=PK_POINT_QUERY, rangeQueryLowerOp=null, rangeQueryLowerKey=null, rangeQueryUpperOp=null, rangeQueryUpperKey=null, pointQueryKey=[0, 1, 2, 3, 4])]}", Objects.toString(indexCondition));
    MetadataManager metadataManager = DrdsTest.getMetadataManager();
    ShardingTable table = (ShardingTable) metadataManager.getTable("db1", "seqSharding");
    List o = ValueIndexCondition.getPartitions(table.getShardingFuntion(), valuePredicateAnalyzer2.translateMatch(rexNode), Arrays.asList());
    o.sort(Comparator.comparing(c -> c.toString()));
    Assert.assertEquals(4, o.size());
    Assert.assertEquals("[{targetName='c0', schemaName='db1_0', tableName='sharding_0', index=0, dbIndex=0, tableIndex=0}, {targetName='c0', schemaName='db1_0', tableName='sharding_1', index=1, dbIndex=0, tableIndex=1}, {targetName='c1', schemaName='db1_1', tableName='sharding_2', index=2, dbIndex=1, tableIndex=0}, {targetName='c1', schemaName='db1_1', tableName='sharding_3', index=3, dbIndex=1, tableIndex=1}]", Objects.toString(o));
    System.out.println();
}
Also used : MetadataManager(io.mycat.MetadataManager) ShardingTable(io.mycat.calcite.table.ShardingTable) ValuePredicateAnalyzer(io.mycat.calcite.rewriter.ValuePredicateAnalyzer) java.util(java.util) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexBuilder(org.apache.calcite.rex.RexBuilder) RexLiteral(org.apache.calcite.rex.RexLiteral) Test(org.junit.Test) MycatCalciteSupport(io.mycat.calcite.MycatCalciteSupport) ValueIndexCondition(io.mycat.calcite.rewriter.ValueIndexCondition) RexUtil(org.apache.calcite.rex.RexUtil) Partition(io.mycat.Partition) QueryType(io.mycat.querycondition.QueryType) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RexNode(org.apache.calcite.rex.RexNode) KeyMeta(io.mycat.querycondition.KeyMeta) Assert(org.junit.Assert) DrdsSqlCompiler(io.mycat.DrdsSqlCompiler) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) ValuePredicateAnalyzer(io.mycat.calcite.rewriter.ValuePredicateAnalyzer) MetadataManager(io.mycat.MetadataManager) QueryType(io.mycat.querycondition.QueryType) ShardingTable(io.mycat.calcite.table.ShardingTable) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

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