Search in sources :

Example 6 with ValuePredicateAnalyzer

use of io.mycat.calcite.rewriter.ValuePredicateAnalyzer in project Mycat2 by MyCATApache.

the class ValuePredicateAnalyzerTest method testMultiOrEquals4.

@Test
public void testMultiOrEquals4() {
    DrdsTest.getDrds();
    int count = 4;
    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])]}", 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(count, 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)

Example 7 with ValuePredicateAnalyzer

use of io.mycat.calcite.rewriter.ValuePredicateAnalyzer in project Mycat2 by MyCATApache.

the class ValuePredicateAnalyzerTest method testEqual.

@Test
public void testEqual() {
    DrdsSqlCompiler drds = DrdsTest.getDrds();
    MetadataManager metadataManager = DrdsTest.getMetadataManager();
    ShardingTable table = (ShardingTable) metadataManager.getTable("db1", "sharding");
    RexNode rexNode = rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), 0), rexBuilder.makeLiteral(1, MycatCalciteSupport.TypeFactory.createSqlType(SqlTypeName.INTEGER), true));
    List<String> columnList = Arrays.asList("id");
    ValuePredicateAnalyzer valuePredicateAnalyzer2 = new ValuePredicateAnalyzer(Collections.singletonList(KeyMeta.of("default", "id")), columnList);
    List<Partition> dataNodes = ValueIndexCondition.getPartitions(table.getShardingFuntion(), valuePredicateAnalyzer2.translateMatch(rexNode), Arrays.asList(1));
    Assert.assertEquals(("[{targetName='c1', schemaName='db1_1', tableName='sharding_1', index=3, dbIndex=1, tableIndex=1}]").toString(), dataNodes.toString());
}
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 8 with ValuePredicateAnalyzer

use of io.mycat.calcite.rewriter.ValuePredicateAnalyzer in project Mycat2 by MyCATApache.

the class ValuePredicateAnalyzerTest method testTrue.

@Test
public void testTrue() {
    RexLiteral rexLiteral = rexBuilder.makeLiteral(true);
    List<String> columnList = Arrays.asList("id");
    ValuePredicateAnalyzer valuePredicateAnalyzer2 = new ValuePredicateAnalyzer(Arrays.asList(KeyMeta.of("default", Arrays.asList("id"))), columnList);
    Map<QueryType, List<ValueIndexCondition>> indexCondition = valuePredicateAnalyzer2.translateMatch(rexLiteral);
    Assert.assertEquals("{}", indexCondition.toString());
}
Also used : ValuePredicateAnalyzer(io.mycat.calcite.rewriter.ValuePredicateAnalyzer) RexLiteral(org.apache.calcite.rex.RexLiteral) QueryType(io.mycat.querycondition.QueryType) Test(org.junit.Test)

Example 9 with ValuePredicateAnalyzer

use of io.mycat.calcite.rewriter.ValuePredicateAnalyzer 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 10 with ValuePredicateAnalyzer

use of io.mycat.calcite.rewriter.ValuePredicateAnalyzer 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)

Aggregations

ValuePredicateAnalyzer (io.mycat.calcite.rewriter.ValuePredicateAnalyzer)13 RexNode (org.apache.calcite.rex.RexNode)12 Test (org.junit.Test)12 ShardingTable (io.mycat.calcite.table.ShardingTable)11 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)10 MetadataManager (io.mycat.MetadataManager)10 QueryType (io.mycat.querycondition.QueryType)9 Partition (io.mycat.Partition)7 RexBuilder (org.apache.calcite.rex.RexBuilder)6 RexLiteral (org.apache.calcite.rex.RexLiteral)6 MycatCalciteSupport (io.mycat.calcite.MycatCalciteSupport)5 ValueIndexCondition (io.mycat.calcite.rewriter.ValueIndexCondition)5 KeyMeta (io.mycat.querycondition.KeyMeta)5 java.util (java.util)5 NotThreadSafe (javax.annotation.concurrent.NotThreadSafe)5 RexUtil (org.apache.calcite.rex.RexUtil)5 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)5 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)5 Assert (org.junit.Assert)5 ImmutableList (com.google.common.collect.ImmutableList)1