Search in sources :

Example 26 with Expression

use of com.pingcap.tikv.expression.Expression in project tispark by pingcap.

the class TiKVScanAnalyzerTest method extractConditionsTest.

@Test
public void extractConditionsTest() {
    TiTableInfo table = createTable();
    TiIndexInfo index = table.getIndices().get(0);
    Expression eq1 = equal(ColumnRef.create("c1", table), Constant.create(0, IntegerType.INT));
    Expression eq2 = equal(ColumnRef.create("c2", table), Constant.create("test", StringType.VARCHAR));
    Expression le1 = lessEqual(ColumnRef.create("c3", table), Constant.create("fxxx", StringType.VARCHAR));
    // Last one should be pushed back
    Expression eq3 = equal(ColumnRef.create("c4", table), Constant.create("fxxx", StringType.VARCHAR));
    List<Expression> exprs = ImmutableList.of(eq1, eq2, le1, eq3);
    ScanSpec result = TiKVScanAnalyzer.extractConditions(exprs, table, index);
    assertEquals(1, result.getResidualPredicates().size());
    assertEquals(eq3, result.getResidualPredicates().toArray()[0]);
    assertEquals(2, result.getPointPredicates().size());
    assertEquals(eq1, result.getPointPredicates().get(0));
    assertEquals(eq2, result.getPointPredicates().get(1));
    assertTrue(result.getRangePredicate().isPresent());
    assertEquals(le1, result.getRangePredicate().get());
}
Also used : Expression(com.pingcap.tikv.expression.Expression) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 27 with Expression

use of com.pingcap.tikv.expression.Expression in project tispark by pingcap.

the class TiKVScanAnalyzerTest method buildTableScanKeyRangeTest.

@Test
public void buildTableScanKeyRangeTest() {
    // This test also covers partitioned table. When it comes to partitioned table
    // we need to build key range from table ids(collect from partition definitions)
    TiTableInfo table = createTableWithIndex(6, 5);
    TiIndexInfo pkIndex = TiIndexInfo.generateFakePrimaryKeyIndex(table);
    Expression eq1 = lessThan(ColumnRef.create("c1", table), Constant.create(3, IntegerType.INT));
    List<Expression> exprs = ImmutableList.of(eq1);
    ScanSpec result = TiKVScanAnalyzer.extractConditions(exprs, table, pkIndex);
    List<IndexRange> irs = expressionToIndexRanges(result.getPointPredicates(), result.getRangePredicate(), table, pkIndex);
    TiKVScanAnalyzer scanAnalyzer = new TiKVScanAnalyzer();
    Map<Long, List<Coprocessor.KeyRange>> keyRanges = scanAnalyzer.buildTableScanKeyRange(table, irs, null);
    assertEquals(keyRanges.size(), 1);
    Coprocessor.KeyRange keyRange = keyRanges.get(table.getId()).get(0);
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 114, 0, 0, 0, 0, 0, 0, 0, 0 }), keyRange.getStart());
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 115, 0, 0, 0, 0, 0, 0, 0, 0 }), keyRange.getEnd());
}
Also used : Coprocessor(org.tikv.kvproto.Coprocessor) Expression(com.pingcap.tikv.expression.Expression) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 28 with Expression

use of com.pingcap.tikv.expression.Expression in project tispark by pingcap.

the class TiDAGRequestTest method selectRequestEquals.

private static boolean selectRequestEquals(TiDAGRequest lhs, TiDAGRequest rhs) {
    assertEquals(lhs.getFields().size(), rhs.getFields().size());
    Map<String, Integer> lhsMap = new HashMap<>();
    Map<String, Integer> rhsMap = new HashMap<>();
    for (int i = 0; i < lhs.getFields().size(); i++) {
        ColumnRef lCol = lhs.getFields().get(i);
        ColumnRef rCol = rhs.getFields().get(i);
        lhsMap.put(lCol.getName(), i);
        rhsMap.put(rCol.getName(), i);
    }
    for (int i = 0; i < lhs.getFields().size(); i++) {
        Expression lhsExpr = lhs.getFields().get(i);
        Expression rhsExpr = rhs.getFields().get(i);
        Expr lhsExprProto = ProtoConverter.toProto(lhsExpr, lhsMap);
        Expr rhsExprProto = ProtoConverter.toProto(rhsExpr, rhsMap);
        if (!lhsExprProto.equals(rhsExprProto))
            return false;
    }
    assertEquals(lhs.getAggregates().size(), rhs.getAggregates().size());
    for (int i = 0; i < lhs.getAggregates().size(); i++) {
        Expression lhsExpr = lhs.getAggregates().get(i);
        Expression rhsExpr = rhs.getAggregates().get(i);
        Expr lhsExprProto = ProtoConverter.toProto(lhsExpr, lhsMap);
        Expr rhsExprProto = ProtoConverter.toProto(rhsExpr, rhsMap);
        if (!lhsExprProto.equals(rhsExprProto))
            return false;
    }
    assertEquals(lhs.getGroupByItems().size(), rhs.getGroupByItems().size());
    for (int i = 0; i < lhs.getGroupByItems().size(); i++) {
        ByItem lhsItem = lhs.getGroupByItems().get(i);
        ByItem rhsItem = rhs.getGroupByItems().get(i);
        if (!lhsItem.toProto(lhsMap).equals(rhsItem.toProto(rhsMap)))
            return false;
    }
    assertEquals(lhs.getOrderByItems().size(), rhs.getOrderByItems().size());
    for (int i = 0; i < lhs.getOrderByItems().size(); i++) {
        ByItem lhsItem = lhs.getOrderByItems().get(i);
        ByItem rhsItem = rhs.getOrderByItems().get(i);
        if (!lhsItem.toProto(lhsMap).equals(rhsItem.toProto(rhsMap)))
            return false;
    }
    assertEquals(lhs.getRangesMaps().size(), rhs.getRangesMaps().size());
    assertEquals(lhs.getRangesMaps(), rhs.getRangesMaps());
    assertEquals(lhs.getFilters().size(), rhs.getFilters().size());
    for (int i = 0; i < lhs.getFilters().size(); i++) {
        Expression lhsItem = lhs.getFilters().get(i);
        Expression rhsItem = rhs.getFilters().get(i);
        Expr lhsExprProto = ProtoConverter.toProto(lhsItem);
        Expr rhsExprProto = ProtoConverter.toProto(rhsItem);
        if (!lhsExprProto.equals(rhsExprProto))
            return false;
    }
    assertEquals(lhs.getTableInfo().toProto(), rhs.getTableInfo().toProto());
    assertEquals(lhs.getLimit(), rhs.getLimit());
    assertEquals(lhs.isDistinct(), rhs.isDistinct());
    assertEquals(lhs.getIndexInfo().toProto(lhs.getTableInfo()), rhs.getIndexInfo().toProto(rhs.getTableInfo()));
    assertEquals(lhs.getStartTs(), rhs.getStartTs());
    assertEquals(lhs.getTimeZoneOffset(), rhs.getTimeZoneOffset());
    assertEquals(lhs.getFlags(), rhs.getFlags());
    return true;
}
Also used : ByItem(com.pingcap.tikv.expression.ByItem) Expr(com.pingcap.tidb.tipb.Expr) HashMap(java.util.HashMap) Expression(com.pingcap.tikv.expression.Expression) ByteString(com.google.protobuf.ByteString) ColumnRef(com.pingcap.tikv.expression.ColumnRef)

Aggregations

Expression (com.pingcap.tikv.expression.Expression)28 Test (org.junit.Test)16 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)11 ArithmeticBinaryExpression (com.pingcap.tikv.expression.ArithmeticBinaryExpression)10 ComparisonBinaryExpression (com.pingcap.tikv.expression.ComparisonBinaryExpression)9 LogicalBinaryExpression (com.pingcap.tikv.expression.LogicalBinaryExpression)9 TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)8 ColumnRef (com.pingcap.tikv.expression.ColumnRef)7 Constant (com.pingcap.tikv.expression.Constant)6 FuncCallExpr (com.pingcap.tikv.expression.FuncCallExpr)6 Expr (com.pingcap.tidb.tipb.Expr)5 StringRegExpression (com.pingcap.tikv.expression.StringRegExpression)4 ArrayList (java.util.ArrayList)4 ImmutableList (com.google.common.collect.ImmutableList)3 Range (com.google.common.collect.Range)3 TiExpressionException (com.pingcap.tikv.exception.TiExpressionException)3 TypedKey (com.pingcap.tikv.key.TypedKey)3 List (java.util.List)3 ScalarFuncSig (com.pingcap.tidb.tipb.ScalarFuncSig)2 UnsupportedSyntaxException (com.pingcap.tikv.exception.UnsupportedSyntaxException)2