Search in sources :

Example 1 with IndexRangeSetBuilder

use of com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder 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 2 with IndexRangeSetBuilder

use of com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder in project tispark by pingcap.

the class PredicateUtils method expressionToIndexRanges.

/**
 * Build index ranges from access points and access conditions
 *
 * @param pointPredicates conditions converting to a single point access
 * @param rangePredicate conditions converting to a range
 * @return Index Range for scan
 */
public static List<IndexRange> expressionToIndexRanges(List<Expression> pointPredicates, Optional<Expression> rangePredicate, TiTableInfo table, TiIndexInfo index) {
    requireNonNull(pointPredicates, "pointPredicates is null");
    requireNonNull(rangePredicate, "rangePredicate is null");
    ImmutableList.Builder<IndexRange> builder = ImmutableList.builder();
    IndexRangeSetBuilder indexRangeBuilder = new IndexRangeSetBuilder(table, index);
    if (pointPredicates.size() != 0) {
        List<Key> pointKeys = expressionToPoints(pointPredicates, table, index);
        for (Key key : pointKeys) {
            if (rangePredicate.isPresent()) {
                Set<Range<TypedKey>> ranges = indexRangeBuilder.buildRange(rangePredicate.get()).asRanges();
                for (Range<TypedKey> range : ranges) {
                    builder.add(new IndexRange(key, range));
                }
            } else {
                // no predicates with point keys leads to empty range encoding
                builder.add(new IndexRange(key, null));
            }
        }
    } else {
        if (rangePredicate.isPresent()) {
            Set<Range<TypedKey>> ranges = indexRangeBuilder.buildRange(rangePredicate.get()).asRanges();
            for (Range<TypedKey> range : ranges) {
                builder.add(new IndexRange(null, range));
            }
        } else {
            // no filter at all means full range
            builder.add(new IndexRange(null, Range.all()));
        }
    }
    return builder.build();
}
Also used : TypedKey(com.pingcap.tikv.key.TypedKey) ImmutableList(com.google.common.collect.ImmutableList) IndexRangeSetBuilder(com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder) Range(com.google.common.collect.Range) CompoundKey(com.pingcap.tikv.key.CompoundKey) Key(com.pingcap.tikv.key.Key) TypedKey(com.pingcap.tikv.key.TypedKey)

Aggregations

Range (com.google.common.collect.Range)2 IndexRangeSetBuilder (com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder)2 CompoundKey (com.pingcap.tikv.key.CompoundKey)2 Key (com.pingcap.tikv.key.Key)2 TypedKey (com.pingcap.tikv.key.TypedKey)2 ImmutableList (com.google.common.collect.ImmutableList)1 TiExpressionException (com.pingcap.tikv.exception.TiExpressionException)1 Expression (com.pingcap.tikv.expression.Expression)1 ArrayList (java.util.ArrayList)1