Search in sources :

Example 11 with TiIndexInfo

use of com.pingcap.tikv.meta.TiIndexInfo 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 12 with TiIndexInfo

use of com.pingcap.tikv.meta.TiIndexInfo in project tispark by pingcap.

the class TiKVScanAnalyzerTest method TestCoveringIndex.

@Test
public void TestCoveringIndex() {
    InternalTypeHolder holder = new InternalTypeHolder(MySQLType.TypeVarchar.getTypeCode(), 0, // indicating a prefix type
    3, 0, "", "", ImmutableList.of());
    Map<String, DataType> dataTypeMap = new HashMap<>();
    dataTypeMap.put("id", IntegerType.INT);
    dataTypeMap.put("a", IntegerType.INT);
    dataTypeMap.put("b", IntegerType.INT);
    dataTypeMap.put("c", IntegerType.INT);
    dataTypeMap.put("holder", DataTypeFactory.of(holder));
    Map<String, Integer> offsetMap = new HashMap<>();
    offsetMap.put("id", 0);
    offsetMap.put("a", 1);
    offsetMap.put("b", 2);
    offsetMap.put("c", 3);
    offsetMap.put("holder", 4);
    class test {

        private final String[] columnNames;

        private final String[] indexNames;

        private final int[] indexLens;

        private final boolean isCovering;

        private test(String[] col, String[] idx, int[] idxLen, boolean result) {
            columnNames = col;
            indexNames = idx;
            indexLens = idxLen;
            isCovering = result;
        }

        private String[] getColumnNames() {
            return columnNames;
        }

        private String[] getIndexNames() {
            return indexNames;
        }

        private int[] getIndexLens() {
            return indexLens;
        }
    }
    final test[] tests = { new test(new String[] { "a" }, new String[] { "a" }, new int[] { -1 }, true), new test(new String[] { "a" }, new String[] { "a", "b" }, new int[] { -1, -1 }, true), new test(new String[] { "a", "b" }, new String[] { "b", "a" }, new int[] { -1, -1 }, true), new test(new String[] { "a", "b" }, new String[] { "b", "c" }, new int[] { -1, -1 }, false), new test(new String[] { "holder", "b" }, new String[] { "holder", "b" }, new int[] { 50, -1 }, false), new test(new String[] { "a", "b" }, new String[] { "a", "c" }, new int[] { -1, -1 }, false), new test(new String[] { "id", "a" }, new String[] { "a", "b" }, new int[] { -1, -1 }, true) };
    TiKVScanAnalyzer scanBuilder = new TiKVScanAnalyzer();
    for (test t : tests) {
        List<TiColumnInfo> columns = new ArrayList<>();
        List<TiIndexColumn> indexCols = new ArrayList<>();
        boolean pkIsHandle = false;
        for (int i = 0; i < t.getColumnNames().length; i++) {
            String colName = t.getColumnNames()[i];
            if (colName.equals("id")) {
                pkIsHandle = true;
            }
            columns.add(new TiColumnInfo(offsetMap.get(colName), colName, i, dataTypeMap.get(colName), colName.equals("id")));
        }
        for (int i = 0; i < t.getIndexNames().length; i++) {
            String idxName = t.getIndexNames()[i];
            int idxLen = t.getIndexLens()[i];
            indexCols.add(new TiIndexColumn(CIStr.newCIStr(idxName), offsetMap.get(idxName), idxLen));
        }
        TiIndexInfo indexInfo = new TiIndexInfo(1, CIStr.newCIStr("test_idx"), CIStr.newCIStr("testTable"), ImmutableList.copyOf(indexCols), false, false, SchemaState.StatePublic.getStateCode(), "Test Index", IndexType.IndexTypeBtree.getTypeCode(), false, false);
        boolean isCovering = scanBuilder.isCoveringIndex(columns, indexInfo, pkIsHandle);
        assertEquals(t.isCovering, isCovering);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) InternalTypeHolder(com.pingcap.tikv.meta.TiColumnInfo.InternalTypeHolder) TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) DataType(com.pingcap.tikv.types.DataType) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 13 with TiIndexInfo

use of com.pingcap.tikv.meta.TiIndexInfo 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)

Aggregations

TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)13 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)10 Test (org.junit.Test)10 Expression (com.pingcap.tikv.expression.Expression)9 ArrayList (java.util.ArrayList)6 TiIndexColumn (com.pingcap.tikv.meta.TiIndexColumn)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ColumnRef (com.pingcap.tikv.expression.ColumnRef)3 IndexMatcher (com.pingcap.tikv.expression.visitor.IndexMatcher)3 TiColumnInfo (com.pingcap.tikv.meta.TiColumnInfo)3 HashMap (java.util.HashMap)3 Range (com.google.common.collect.Range)2 ByteString (com.google.protobuf.ByteString)2 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)2 Constant (com.pingcap.tikv.expression.Constant)2 TypedKey (com.pingcap.tikv.key.TypedKey)2 TiStoreType (com.pingcap.tikv.region.TiStoreType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1