Search in sources :

Example 1 with TiStoreType

use of com.pingcap.tikv.region.TiStoreType in project tispark by pingcap.

the class TiKVScanAnalyzer method buildTiDAGReq.

// Build scan plan picking access path with lowest cost by estimation
public TiDAGRequest buildTiDAGReq(boolean allowIndexScan, boolean useIndexScanFirst, boolean canUseTiKV, boolean canUseTiFlash, List<TiColumnInfo> columnList, List<Expression> conditions, TiTableInfo table, TableStatistics tableStatistics, TiTimestamp ts, TiDAGRequest dagRequest) {
    TiKVScanPlan minPlan = null;
    if (canUseTiKV) {
        minPlan = buildTableScan(conditions, table, tableStatistics);
    }
    if (canUseTiFlash) {
        // it is possible that only TiFlash plan exists due to isolation read.
        TiKVScanPlan plan = buildTiFlashScan(columnList, conditions, table, tableStatistics);
        if (minPlan == null || plan.getCost() < minPlan.getCost()) {
            minPlan = plan;
        }
    } else if (canUseTiKV && allowIndexScan) {
        minPlan.getFilters().forEach(dagRequest::addDowngradeFilter);
        if (table.isPartitionEnabled()) {
        // disable index scan
        } else {
            TiKVScanPlan minIndexPlan = null;
            double minIndexCost = Double.MAX_VALUE;
            for (TiIndexInfo index : table.getIndices()) {
                if (table.isCommonHandle() && table.getPrimaryKey().equals(index)) {
                    continue;
                }
                if (supportIndexScan(index, table)) {
                    TiKVScanPlan plan = buildIndexScan(columnList, conditions, index, table, tableStatistics, false);
                    if (plan.getCost() < minIndexCost) {
                        minIndexPlan = plan;
                        minIndexCost = plan.getCost();
                    }
                }
            }
            if (minIndexPlan != null && (minIndexCost < minPlan.getCost() || useIndexScanFirst)) {
                minPlan = minIndexPlan;
            }
        }
    }
    if (minPlan == null) {
        throw new TiClientInternalException("No valid plan found for table '" + table.getName() + "'");
    }
    TiStoreType minPlanStoreType = minPlan.getStoreType();
    // TiKV should not use CHBlock as Encode Type.
    if (minPlanStoreType == TiStoreType.TiKV && dagRequest.getEncodeType() == EncodeType.TypeCHBlock) {
        dagRequest.setEncodeType(EncodeType.TypeChunk);
    }
    // Set DAG Request's store type as minPlan's store type.
    dagRequest.setStoreType(minPlanStoreType);
    dagRequest.addRanges(minPlan.getKeyRanges());
    dagRequest.setPrunedParts(minPlan.getPrunedParts());
    dagRequest.addFilters(new ArrayList<>(minPlan.getFilters()));
    if (minPlan.isIndexScan()) {
        dagRequest.setIndexInfo(minPlan.getIndex());
        // need to set isDoubleRead to true for dagRequest in case of double read
        dagRequest.setIsDoubleRead(minPlan.isDoubleRead());
    }
    dagRequest.setTableInfo(table);
    dagRequest.setStartTs(ts);
    dagRequest.setEstimatedCount(minPlan.getEstimatedRowCount());
    return dagRequest;
}
Also used : TiStoreType(com.pingcap.tikv.region.TiStoreType) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo)

Aggregations

TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)1 TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)1 TiStoreType (com.pingcap.tikv.region.TiStoreType)1