Search in sources :

Example 6 with TiColumnInfo

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

the class ColumnRef method create.

public static ColumnRef create(String name, TiTableInfo table) {
    name = name.replaceAll("`", "");
    TiColumnInfo col = table.getColumn(name);
    if (col != null) {
        return new ColumnRef(name, col.getType());
    }
    throw new TiExpressionException(String.format("Column name %s not found in table %s", name, table));
}
Also used : TiExpressionException(com.pingcap.tikv.exception.TiExpressionException) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo)

Example 7 with TiColumnInfo

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

the class TiKVScanAnalyzer method isCoveringIndex.

// If all the columns requested in the select list of query, are available in the index, then the
// query engine doesn't have to lookup the table again compared with double read.
boolean isCoveringIndex(List<TiColumnInfo> columns, TiIndexInfo indexColumns, boolean pkIsHandle) {
    if (columns.isEmpty()) {
        return false;
    }
    Map<String, TiIndexColumn> colInIndex = indexColumns.getIndexColumns().stream().collect(Collectors.toMap(TiIndexColumn::getName, col -> col));
    for (TiColumnInfo colInfo : columns) {
        if (pkIsHandle && colInfo.isPrimaryKey()) {
            continue;
        }
        if (colInfo.getId() == -1) {
            continue;
        }
        boolean colNotInIndex = false;
        if (colInIndex.containsKey(colInfo.getName())) {
            TiIndexColumn indexCol = colInIndex.get(colInfo.getName());
            boolean isFullLength = indexCol.isLengthUnspecified() || indexCol.getLength() == colInfo.getType().getLength();
            if (!colInfo.getName().equalsIgnoreCase(indexCol.getName()) || !isFullLength) {
                colNotInIndex = true;
            }
        } else {
            colNotInIndex = true;
        }
        if (colNotInIndex) {
            return false;
        }
    }
    return true;
}
Also used : TiDAGRequest(com.pingcap.tikv.meta.TiDAGRequest) TableStatistics(com.pingcap.tikv.statistics.TableStatistics) EncodeType(com.pingcap.tidb.tipb.EncodeType) Expression(com.pingcap.tikv.expression.Expression) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo) Key(com.pingcap.tikv.key.Key) IndexStatistics(com.pingcap.tikv.statistics.IndexStatistics) Pair(com.pingcap.tikv.util.Pair) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) TiPartitionDef(com.pingcap.tikv.meta.TiPartitionDef) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) PartitionPruner(com.pingcap.tikv.expression.PartitionPruner) TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) Logger(org.slf4j.Logger) RowKey(com.pingcap.tikv.key.RowKey) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Range(com.google.common.collect.Range) Set(java.util.Set) TiTimestamp(com.pingcap.tikv.meta.TiTimestamp) IndexMatcher(com.pingcap.tikv.expression.visitor.IndexMatcher) Collectors(java.util.stream.Collectors) KeyRangeUtils.makeCoprocRange(com.pingcap.tikv.util.KeyRangeUtils.makeCoprocRange) IndexScanKeyRangeBuilder(com.pingcap.tikv.key.IndexScanKeyRangeBuilder) TiStoreType(com.pingcap.tikv.region.TiStoreType) List(java.util.List) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) MySQLType(com.pingcap.tikv.types.MySQLType) PredicateUtils.expressionToIndexRanges(com.pingcap.tikv.predicates.PredicateUtils.expressionToIndexRanges) BoundType(com.google.common.collect.BoundType) IndexScanType(com.pingcap.tikv.meta.TiDAGRequest.IndexScanType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TypedKey(com.pingcap.tikv.key.TypedKey) TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo)

Example 8 with TiColumnInfo

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

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

the class TableCodecTest method testRowCodecThrowException.

@Test
public void testRowCodecThrowException() {
    try {
        TiColumnInfo col1 = new TiColumnInfo(1, "pk", 0, IntegerType.BIGINT, true);
        TiColumnInfo col2 = new TiColumnInfo(2, "c1", 1, IntegerType.BIGINT, false);
        TiColumnInfo colIgnored = new TiColumnInfo(-1, "cIgnored", -1, IntegerType.BIGINT, false);
        TableCodec.encodeRow(ImmutableList.of(col1, col2, colIgnored, colIgnored), new Object[] { 1L, 2L }, true, false);
        expectedEx.expect(IllegalAccessException.class);
        expectedEx.expectMessage("encodeRow error: data and columnID count not match 4 vs 2");
    } catch (IllegalAccessException ignored) {
    }
}
Also used : TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo) Test(org.junit.Test)

Aggregations

TiColumnInfo (com.pingcap.tikv.meta.TiColumnInfo)9 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 TiIndexColumn (com.pingcap.tikv.meta.TiIndexColumn)3 TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)3 TiExpressionException (com.pingcap.tikv.exception.TiExpressionException)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 BoundType (com.google.common.collect.BoundType)1 ImmutableList (com.google.common.collect.ImmutableList)1 Range (com.google.common.collect.Range)1 ByteString (com.google.protobuf.ByteString)1 EncodeType (com.pingcap.tidb.tipb.EncodeType)1 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)1 Expression (com.pingcap.tikv.expression.Expression)1 PartitionPruner (com.pingcap.tikv.expression.PartitionPruner)1 IndexMatcher (com.pingcap.tikv.expression.visitor.IndexMatcher)1 IndexScanKeyRangeBuilder (com.pingcap.tikv.key.IndexScanKeyRangeBuilder)1 Key (com.pingcap.tikv.key.Key)1