Search in sources :

Example 1 with TiIndexColumn

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

the class IndexKey method encodeIndexDataValues.

public static EncodeIndexDataResult encodeIndexDataValues(Row row, List<TiIndexColumn> indexColumns, Handle handle, boolean appendHandleIfContainsNull, TiTableInfo tableInfo) {
    // when appendHandleIfContainsNull is true, append handle column if any of the index column is
    // NULL
    boolean appendHandle = false;
    if (handle.isInt()) {
        if (appendHandleIfContainsNull) {
            for (TiIndexColumn col : indexColumns) {
                DataType colTp = tableInfo.getColumn(col.getOffset()).getType();
                if (row.get(col.getOffset(), colTp) == null) {
                    appendHandle = true;
                    break;
                }
            }
        }
    }
    Key[] keys = new Key[indexColumns.size() + (appendHandle ? 1 : 0)];
    for (int i = 0; i < indexColumns.size(); i++) {
        TiIndexColumn col = indexColumns.get(i);
        DataType colTp = tableInfo.getColumn(col.getOffset()).getType();
        // truncate index's if necessary
        Key key = TypedKey.toTypedKey(row.get(col.getOffset(), colTp), colTp, (int) col.getLength());
        keys[i] = key;
    }
    if (appendHandle) {
        Key key = TypedKey.toTypedKey(handle, IntegerType.BIGINT);
        keys[keys.length - 1] = key;
    }
    return new EncodeIndexDataResult(keys, appendHandle);
}
Also used : TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) DataType(com.pingcap.tikv.types.DataType)

Example 2 with TiIndexColumn

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

the class TableCodecV2 method decodeRow.

protected static Row decodeRow(byte[] value, Handle handle, TiTableInfo tableInfo) {
    if (handle == null && tableInfo.isPkHandle()) {
        throw new IllegalArgumentException("when pk is handle, handle cannot be null");
    }
    int colSize = tableInfo.getColumns().size();
    // decode bytes to Map<ColumnID, Data>
    HashMap<Long, Object> decodedDataMap = new HashMap<>(colSize);
    RowV2 rowV2 = RowV2.createNew(value);
    TiIndexInfo pk = tableInfo.getPrimaryKey();
    if (pk != null) {
        List<TiColumnInfo> cols = new ArrayList<>();
        for (TiIndexColumn indexColumn : pk.getIndexColumns()) {
            TiColumnInfo col = tableInfo.getColumn(indexColumn.getOffset());
            cols.add(col);
        }
        if (tableInfo.isPkHandle()) {
            assert cols.size() == 1;
            decodedDataMap.put(cols.get(0).getId(), handle.data()[0]);
        }
        if (tableInfo.isCommonHandle()) {
            for (int i = 0; i < cols.size(); i++) {
                decodedDataMap.put(cols.get(i).getId(), handle.data()[i]);
            }
        }
    }
    for (TiColumnInfo col : tableInfo.getColumns()) {
        if (decodedDataMap.containsKey(col.getId())) {
            continue;
        } else if (col.isPrimaryKey() && tableInfo.isPkHandle()) {
            decodedDataMap.put(col.getId(), handle);
            continue;
        }
        RowV2.ColIDSearchResult searchResult = rowV2.findColID(col.getId());
        if (searchResult.isNull) {
            // current col is null, nothing should be added to decodedMap
            continue;
        }
        if (!searchResult.notFound) {
            // corresponding column should be found
            assert (searchResult.idx != -1);
            byte[] colData = rowV2.getData(searchResult.idx);
            Object d = RowDecoderV2.decodeCol(colData, col.getType());
            decodedDataMap.put(col.getId(), d);
        }
    }
    Object[] res = new Object[colSize];
    // construct Row with Map<ColumnID, Data> & handle
    for (int i = 0; i < colSize; i++) {
        // skip pk is handle case
        TiColumnInfo col = tableInfo.getColumn(i);
        res[i] = decodedDataMap.get(col.getId());
    }
    return ObjectRowImpl.create(res);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo)

Example 3 with TiIndexColumn

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

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

the class IndexMatcherTest method matchOnlyEq.

@Test
public void matchOnlyEq() {
    TiTableInfo table = createTable();
    TiIndexInfo index = table.getIndices().get(0);
    TiIndexColumn col = index.getIndexColumns().get(0);
    IndexMatcher matcher = IndexMatcher.equalOnlyMatcher(col);
    Constant c0 = Constant.create(0, IntegerType.INT);
    Constant c1 = Constant.create(1, IntegerType.INT);
    Constant c2 = Constant.create(2, IntegerType.INT);
    ColumnRef col1 = ColumnRef.create("c1", table);
    ColumnRef col2 = ColumnRef.create("c2", table);
    // index col = c1, long
    Expression cond = equal(col1, c1);
    assertTrue(matcher.match(cond));
    cond = equal(c1, col1);
    assertTrue(matcher.match(cond));
    cond = equal(col2, col1);
    assertFalse(matcher.match(cond));
    cond = equal(c1, c1);
    assertFalse(matcher.match(cond));
    cond = and(equal(c1, col1), equal(col1, c2));
    assertFalse(matcher.match(cond));
    cond = or(equal(c1, col1), equal(col1, c2));
    assertTrue(matcher.match(cond));
    cond = lessEqual(c0, col1);
    assertFalse(matcher.match(cond));
}
Also used : TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) IndexMatcher(com.pingcap.tikv.expression.visitor.IndexMatcher) Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ColumnRef(com.pingcap.tikv.expression.ColumnRef) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 5 with TiIndexColumn

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

the class IndexMatcherTest method matchAll.

@Test
public void matchAll() {
    TiTableInfo table = createTable();
    TiIndexInfo index = table.getIndices().get(0);
    TiIndexColumn col = index.getIndexColumns().get(0);
    IndexMatcher matcher = IndexMatcher.matcher(col);
    Constant c1 = Constant.create(1, IntegerType.INT);
    Constant c2 = Constant.create(2, IntegerType.INT);
    ColumnRef col1 = ColumnRef.create("c1", table);
    // index col = c1, long
    Expression cond = lessEqual(col1, c1);
    assertTrue(matcher.match(cond));
    cond = greaterEqual(c1, col1);
    assertTrue(matcher.match(cond));
    cond = lessThan(ColumnRef.create("c2", table), ColumnRef.create("c1", table));
    assertFalse(matcher.match(cond));
    cond = lessThan(c1, c1);
    assertFalse(matcher.match(cond));
    cond = and(lessThan(c1, col1), lessThan(col1, c2));
    assertTrue(matcher.match(cond));
    cond = or(lessThan(c1, col1), lessThan(col1, c2));
    assertTrue(matcher.match(cond));
}
Also used : TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) IndexMatcher(com.pingcap.tikv.expression.visitor.IndexMatcher) Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ColumnRef(com.pingcap.tikv.expression.ColumnRef) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Aggregations

TiIndexColumn (com.pingcap.tikv.meta.TiIndexColumn)6 TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)5 Expression (com.pingcap.tikv.expression.Expression)3 IndexMatcher (com.pingcap.tikv.expression.visitor.IndexMatcher)3 TiColumnInfo (com.pingcap.tikv.meta.TiColumnInfo)3 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ColumnRef (com.pingcap.tikv.expression.ColumnRef)2 Constant (com.pingcap.tikv.expression.Constant)2 DataType (com.pingcap.tikv.types.DataType)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