Search in sources :

Example 1 with PartAndFilterExprRewriter

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

the class RangePartitionPruner method pruneRangeNormalPart.

private List<TiPartitionDef> pruneRangeNormalPart(Expression cnfExpr) {
    Objects.requireNonNull(cnfExpr, "cnf expression cannot be null at pruning stage");
    // we need rewrite filter expression if partition expression is a Year expression.
    // This step is designed to deal with y < '1995-10-10'(in filter condition and also a part of
    // partition expression) where y is a date type.
    // Rewriting only applies partition expression on the constant part, resulting year(y) < 1995.
    PartAndFilterExprRewriter expressionRewriter = new PartAndFilterExprRewriter(partExpr);
    cnfExpr = expressionRewriter.rewrite(cnfExpr);
    // if we find an unsupported partition function, we downgrade to scan all partitions.
    if (expressionRewriter.isUnsupportedPartFnFound()) {
        return partInfo.getDefs();
    }
    RangeSet<TypedKey> filterRange = rangeBuilder.buildRange(cnfExpr);
    List<TiPartitionDef> pDefs = new ArrayList<>();
    for (int i = 0; i < partExprs.size(); i++) {
        Expression partExpr = partExprs.get(i);
        // when we build range, we still need rewrite partition expression.
        // If we have a year(purchased) < 1995 which cannot be normalized, we need
        // to rewrite it into purchased < 1995 to let RangeSetBuilder be happy.
        RangeSet<TypedKey> partRange = rangeBuilder.buildRange(expressionRewriter.rewrite(partExpr));
        partRange.removeAll(filterRange.complement());
        if (!partRange.isEmpty()) {
            // part range is empty indicates this partition can be pruned.
            pDefs.add(partInfo.getDefs().get(i));
        }
    }
    return pDefs;
}
Also used : TiPartitionDef(com.pingcap.tikv.meta.TiPartitionDef) TypedKey(com.pingcap.tikv.key.TypedKey) ArrayList(java.util.ArrayList) PartAndFilterExprRewriter(com.pingcap.tikv.expression.visitor.PartAndFilterExprRewriter)

Aggregations

PartAndFilterExprRewriter (com.pingcap.tikv.expression.visitor.PartAndFilterExprRewriter)1 TypedKey (com.pingcap.tikv.key.TypedKey)1 TiPartitionDef (com.pingcap.tikv.meta.TiPartitionDef)1 ArrayList (java.util.ArrayList)1