Search in sources :

Example 1 with TiPartitionDef

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

the class TiKVScanAnalyzer method buildTableScanKeyRange.

@VisibleForTesting
Map<Long, List<KeyRange>> buildTableScanKeyRange(TiTableInfo table, List<IndexRange> indexRanges, List<TiPartitionDef> prunedParts) {
    requireNonNull(table, "Table is null");
    requireNonNull(indexRanges, "indexRanges is null");
    if (table.isPartitionEnabled()) {
        List<Long> ids = new ArrayList<>();
        for (TiPartitionDef pDef : prunedParts) {
            ids.add(pDef.getId());
        }
        return buildTableScanKeyRangeWithIds(ids, indexRanges);
    } else {
        return buildTableScanKeyRangeWithIds(ImmutableList.of(table.getId()), indexRanges);
    }
}
Also used : TiPartitionDef(com.pingcap.tikv.meta.TiPartitionDef) ArrayList(java.util.ArrayList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with TiPartitionDef

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

the class TiKVScanAnalyzer method buildIndexScanKeyRange.

@VisibleForTesting
Map<Long, List<KeyRange>> buildIndexScanKeyRange(TiTableInfo table, TiIndexInfo index, List<IndexRange> indexRanges, List<TiPartitionDef> prunedParts) {
    requireNonNull(table, "Table cannot be null to encoding keyRange");
    requireNonNull(index, "Index cannot be null to encoding keyRange");
    requireNonNull(indexRanges, "indexRanges cannot be null to encoding keyRange");
    if (table.isPartitionEnabled()) {
        List<Long> ids = new ArrayList<>();
        for (TiPartitionDef pDef : prunedParts) {
            ids.add(pDef.getId());
        }
        return buildIndexScanKeyRangeWithIds(ids, index, indexRanges);
    } else {
        return buildIndexScanKeyRangeWithIds(ImmutableList.of(table.getId()), index, indexRanges);
    }
}
Also used : TiPartitionDef(com.pingcap.tikv.meta.TiPartitionDef) ArrayList(java.util.ArrayList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with TiPartitionDef

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

the class PartitionPruner method generateRangeExprs.

static void generateRangeExprs(TiPartitionInfo partInfo, List<Expression> partExprs, TiParser parser, String partExprStr, int lessThanIdx) {
    // partExprColRefs.addAll(PredicateUtils.extractColumnRefFromExpression(partExpr));
    for (int i = 0; i < partInfo.getDefs().size(); i++) {
        TiPartitionDef pDef = partInfo.getDefs().get(i);
        String current = pDef.getLessThan().get(lessThanIdx);
        String leftHand;
        if (current.equals("MAXVALUE")) {
            leftHand = "true";
        } else {
            leftHand = String.format("%s < %s", wrapColumnName(partExprStr), current);
        }
        if (i == 0) {
            partExprs.add(parser.parseExpression(leftHand));
        } else {
            String previous = partInfo.getDefs().get(i - 1).getLessThan().get(lessThanIdx);
            String and = String.format("%s >= %s and %s", wrapColumnName(partExprStr), previous, leftHand);
            partExprs.add(parser.parseExpression(and));
        }
    }
}
Also used : TiPartitionDef(com.pingcap.tikv.meta.TiPartitionDef)

Example 4 with TiPartitionDef

use of com.pingcap.tikv.meta.TiPartitionDef 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)

Example 5 with TiPartitionDef

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

the class TiParserTest method createTaleInfoWithParts.

private TiTableInfo createTaleInfoWithParts() {
    List<TiPartitionDef> partDefs = new ArrayList<>();
    partDefs.add(new TiPartitionDef(1L, CIStr.newCIStr("p0"), ImmutableList.of("5"), ""));
    partDefs.add(new TiPartitionDef(2L, CIStr.newCIStr("p1"), ImmutableList.of("10"), ""));
    partDefs.add(new TiPartitionDef(3L, CIStr.newCIStr("p2"), ImmutableList.of("15"), ""));
    partDefs.add(new TiPartitionDef(4L, CIStr.newCIStr("p3"), ImmutableList.of("MAXVALUE"), ""));
    return new MetaUtils.TableBuilder().name("rcx").addColumn("a", IntegerType.INT, true).addColumn("b", IntegerType.INT).addColumn("c", StringType.CHAR).addColumn("d", IntegerType.INT).addPartition("a", PartitionType.RangePartition, partDefs, null).build();
}
Also used : TiPartitionDef(com.pingcap.tikv.meta.TiPartitionDef) MetaUtils(com.pingcap.tikv.meta.MetaUtils) ArrayList(java.util.ArrayList)

Aggregations

TiPartitionDef (com.pingcap.tikv.meta.TiPartitionDef)5 ArrayList (java.util.ArrayList)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 PartAndFilterExprRewriter (com.pingcap.tikv.expression.visitor.PartAndFilterExprRewriter)1 TypedKey (com.pingcap.tikv.key.TypedKey)1 MetaUtils (com.pingcap.tikv.meta.MetaUtils)1