Search in sources :

Example 1 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class PredicateUtils method joinKeys.

private static List<Key> joinKeys(List<Key> lhsKeys, List<Key> rhsKeys) {
    requireNonNull(lhsKeys, "lhsKeys is null");
    requireNonNull(rhsKeys, "rhsKeys is null");
    if (lhsKeys.isEmpty()) {
        return rhsKeys;
    }
    if (rhsKeys.isEmpty()) {
        return lhsKeys;
    }
    ImmutableList.Builder<Key> builder = ImmutableList.builder();
    for (Key lKey : lhsKeys) {
        for (Key rKey : rhsKeys) {
            builder.add(CompoundKey.concat(lKey, rKey));
        }
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) CompoundKey(com.pingcap.tikv.key.CompoundKey) Key(com.pingcap.tikv.key.Key) TypedKey(com.pingcap.tikv.key.TypedKey)

Example 2 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class PredicateUtils method expressionToPoints.

/**
 * Turn access conditions into list of points Each condition is bound to single key We pick up
 * single condition for each index key and disregard if multiple EQ conditions in DNF
 *
 * @param pointPredicates expressions that convertible to access points
 * @return access points for each index
 */
private static List<Key> expressionToPoints(List<Expression> pointPredicates, TiTableInfo table, TiIndexInfo index) {
    requireNonNull(pointPredicates, "pointPredicates cannot be null");
    List<Key> resultKeys = new ArrayList<>();
    IndexRangeSetBuilder indexRangeBuilder = new IndexRangeSetBuilder(table, index);
    for (int i = 0; i < pointPredicates.size(); i++) {
        Expression predicate = pointPredicates.get(i);
        try {
            // each expr will be expand to one or more points
            Set<Range<TypedKey>> ranges = indexRangeBuilder.buildRange(predicate).asRanges();
            List<Key> points = rangesToPoint(ranges);
            resultKeys = joinKeys(resultKeys, points);
        } catch (Exception e) {
            throw new TiExpressionException(String.format("Error converting access points %s", predicate), e);
        }
    }
    return resultKeys;
}
Also used : Expression(com.pingcap.tikv.expression.Expression) IndexRangeSetBuilder(com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder) ArrayList(java.util.ArrayList) TiExpressionException(com.pingcap.tikv.exception.TiExpressionException) Range(com.google.common.collect.Range) CompoundKey(com.pingcap.tikv.key.CompoundKey) Key(com.pingcap.tikv.key.Key) TypedKey(com.pingcap.tikv.key.TypedKey) TiExpressionException(com.pingcap.tikv.exception.TiExpressionException)

Example 3 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class TiKVScanAnalyzer method buildTableScanKeyRangePerId.

private Pair<Key, Key> buildTableScanKeyRangePerId(long id, IndexRange ir) {
    Key startKey;
    Key endKey;
    if (ir.hasAccessKey()) {
        checkArgument(!ir.hasRange(), "Table scan must have one and only one access condition / point");
        Key key = ir.getAccessKey();
        checkArgument(key instanceof TypedKey, "Table scan key range must be typed key");
        TypedKey typedKey = (TypedKey) key;
        startKey = RowKey.toRowKey(id, typedKey);
        endKey = startKey.next();
    } else if (ir.hasRange()) {
        checkArgument(!ir.hasAccessKey(), "Table scan must have one and only one access condition / point");
        Range<TypedKey> r = ir.getRange();
        if (!r.hasLowerBound()) {
            // -INF
            startKey = RowKey.createMin(id);
        } else {
            // Comparison with null should be filtered since it yields unknown always
            startKey = RowKey.toRowKey(id, r.lowerEndpoint());
            if (r.lowerBoundType().equals(BoundType.OPEN)) {
                startKey = startKey.next();
            }
        }
        if (!r.hasUpperBound()) {
            // INF
            endKey = RowKey.createBeyondMax(id);
        } else {
            endKey = RowKey.toRowKey(id, r.upperEndpoint());
            if (r.upperBoundType().equals(BoundType.CLOSED)) {
                endKey = endKey.next();
            }
        }
    } else {
        throw new TiClientInternalException("Empty access conditions");
    }
    return new Pair<>(startKey, endKey);
}
Also used : TypedKey(com.pingcap.tikv.key.TypedKey) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) Range(com.google.common.collect.Range) KeyRangeUtils.makeCoprocRange(com.pingcap.tikv.util.KeyRangeUtils.makeCoprocRange) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) Key(com.pingcap.tikv.key.Key) RowKey(com.pingcap.tikv.key.RowKey) TypedKey(com.pingcap.tikv.key.TypedKey) Pair(com.pingcap.tikv.util.Pair)

Example 4 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class Histogram method lessRowCount.

/**
 * lessRowCount estimates the row count where the column less than values.
 */
private double lessRowCount(Key values) {
    if (values.compareTo(Key.NULL) <= 0) {
        return 0;
    }
    int index = lowerBound(values);
    // index not in range
    if (index == -buckets.size() - 1) {
        return totalRowCount();
    }
    if (index < 0) {
        index = -index - 1;
    } else {
        return buckets.get(index).count - buckets.get(index).getRepeats();
    }
    double curCount = buckets.get(index).count;
    double preCount = 0;
    if (index > 0) {
        preCount = buckets.get(index - 1).count;
    }
    double lessThanBucketValueCount = curCount + nullCount - buckets.get(index).getRepeats();
    Key lowerBound = buckets.get(index).getLowerBound();
    int c;
    if (lowerBound != null) {
        c = values.compareTo(lowerBound);
    } else {
        c = 1;
    }
    if (c < 0) {
        return preCount;
    }
    return (preCount + lessThanBucketValueCount) / 2;
}
Also used : Key(com.pingcap.tikv.key.Key)

Example 5 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class PredicateUtilsTest method expressionToIndexRangesTest.

@Test
public void expressionToIndexRangesTest() {
    TiTableInfo table = createTable();
    ColumnRef col1 = ColumnRef.create("c1", table);
    ColumnRef col4 = ColumnRef.create("c4", table);
    ColumnRef col5 = ColumnRef.create("c5", table);
    Constant c1 = Constant.create(1, IntegerType.INT);
    Constant c2 = Constant.create(2, IntegerType.INT);
    Constant c3 = Constant.create(3, IntegerType.INT);
    Constant c4 = Constant.create(4, IntegerType.INT);
    TypedKey key1 = TypedKey.toTypedKey(1, IntegerType.INT);
    TypedKey key2 = TypedKey.toTypedKey(2, IntegerType.INT);
    TypedKey key3 = TypedKey.toTypedKey(3, IntegerType.INT);
    TypedKey key4 = TypedKey.toTypedKey(4, IntegerType.INT);
    Expression predicate1 = or(or(equal(c1, col1), equal(col1, c2)), equal(col1, c1));
    Expression predicate2 = or(equal(c3, col4), equal(c4, col4));
    Expression rangePredicate = notEqual(col5, c1);
    List<IndexRange> indexRanges = PredicateUtils.expressionToIndexRanges(ImmutableList.of(predicate1, predicate2), Optional.of(rangePredicate), table, null);
    assertEquals(8, indexRanges.size());
    Key indexKey1 = CompoundKey.concat(key1, key3);
    Key indexKey2 = CompoundKey.concat(key1, key4);
    Key indexKey3 = CompoundKey.concat(key2, key3);
    Key indexKey4 = CompoundKey.concat(key2, key4);
    Range<TypedKey> baselineRange1 = Range.lessThan(key1);
    Range<TypedKey> baselineRange2 = Range.greaterThan(key1);
    Set<Key> baselineKeys = ImmutableSet.of(indexKey1, indexKey2, indexKey3, indexKey4);
    Set<Range<TypedKey>> baselineRanges = ImmutableSet.of(baselineRange1, baselineRange2);
    for (IndexRange range : indexRanges) {
        assertTrue(baselineKeys.contains(range.getAccessKey()));
        assertTrue(baselineRanges.contains(range.getRange()));
    }
}
Also used : TypedKey(com.pingcap.tikv.key.TypedKey) Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ColumnRef(com.pingcap.tikv.expression.ColumnRef) Range(com.google.common.collect.Range) Key(com.pingcap.tikv.key.Key) CompoundKey(com.pingcap.tikv.key.CompoundKey) TypedKey(com.pingcap.tikv.key.TypedKey) Test(org.junit.Test)

Aggregations

Key (com.pingcap.tikv.key.Key)15 TypedKey (com.pingcap.tikv.key.TypedKey)6 ImmutableList (com.google.common.collect.ImmutableList)5 TiRegion (com.pingcap.tikv.region.TiRegion)5 ArrayList (java.util.ArrayList)5 Range (com.google.common.collect.Range)4 ByteString (com.google.protobuf.ByteString)4 CompoundKey (com.pingcap.tikv.key.CompoundKey)4 RowKey (com.pingcap.tikv.key.RowKey)4 Key.toRawKey (com.pingcap.tikv.key.Key.toRawKey)3 HashMap (java.util.HashMap)3 List (java.util.List)3 KeyRange (org.tikv.kvproto.Coprocessor.KeyRange)3 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)2 Expression (com.pingcap.tikv.expression.Expression)2 IndexRangeSetBuilder (com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder)2 IndexKey (com.pingcap.tikv.key.IndexKey)2 Pair (com.pingcap.tikv.util.Pair)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2