Search in sources :

Example 1 with CoveringPlanNoFilterGenerator

use of org.apache.drill.exec.planner.index.generators.CoveringPlanNoFilterGenerator in project drill by apache.

the class DbScanSortRemovalRule method doOnMatch.

private void doOnMatch(IndexPhysicalPlanCallContext indexContext) {
    Stopwatch indexPlanTimer = Stopwatch.createStarted();
    final PlannerSettings settings = PrelUtil.getPlannerSettings(indexContext.call.getPlanner());
    DbGroupScan groupScan = (DbGroupScan) indexContext.scan.getGroupScan();
    boolean isIndexScan = groupScan.isIndexScan();
    if (!isIndexScan) {
        // This case generates the index scan and removes the sort if possible.
        final IndexCollection indexCollection = groupScan.getSecondaryIndexCollection(indexContext.scan);
        if (indexCollection == null) {
            return;
        }
        if (settings.isStatisticsEnabled()) {
            groupScan.getStatistics().initialize(null, indexContext.scan, indexContext);
        }
        IndexPlanUtils.updateSortExpression(indexContext, indexContext.getSort() != null ? indexContext.getCollation().getFieldCollations() : null);
        IndexSelector selector = new IndexSelector(indexContext);
        for (IndexDescriptor indexDesc : indexCollection) {
            indexDesc.getIndexGroupScan().setStatistics(groupScan.getStatistics());
            FunctionalIndexInfo functionInfo = indexDesc.getFunctionalInfo();
            if (IndexPlanUtils.isCoveringIndex(indexContext, functionInfo)) {
                selector.addIndex(indexDesc, true, indexContext.lowerProject != null ? indexContext.lowerProject.getRowType().getFieldCount() : indexContext.scan.getRowType().getFieldCount());
            }
        }
        IndexProperties idxProp = selector.getBestIndexNoFilter();
        if (idxProp != null) {
            try {
                // generate a covering plan
                CoveringPlanNoFilterGenerator planGen = new CoveringPlanNoFilterGenerator(indexContext, idxProp.getIndexDesc().getFunctionalInfo(), false, settings);
                if (planGen.convertChild() != null) {
                    indexContext.getCall().transformTo(planGen.convertChild());
                } else {
                    logger.debug("Not able to generate index plan in {}", this.getClass().toString());
                }
            } catch (Exception e) {
                logger.warn("Exception while trying to generate indexscan to remove sort", e);
            }
        }
    } else {
        Preconditions.checkNotNull(indexContext.getSort());
        // This case tries to use the already generated index to see if a sort can be removed.
        if (indexContext.scan.getTraitSet().getTrait(RelCollationTraitDef.INSTANCE).getFieldCollations().size() == 0) {
            return;
        }
        try {
            RelNode finalRel = indexContext.scan.copy(indexContext.scan.getTraitSet(), indexContext.scan.getInputs());
            if (indexContext.lowerProject != null) {
                List<RelNode> inputs = Lists.newArrayList();
                inputs.add(finalRel);
                finalRel = indexContext.lowerProject.copy(indexContext.lowerProject.getTraitSet(), inputs);
            }
            finalRel = AbstractIndexPlanGenerator.getSortNode(indexContext, finalRel, true, false, indexContext.exch != null);
            if (finalRel == null) {
                logger.debug("Not able to generate index plan in {}", this.getClass().toString());
                return;
            }
            finalRel = Prule.convert(finalRel, finalRel.getTraitSet().plus(Prel.DRILL_PHYSICAL));
            indexContext.getCall().transformTo(finalRel);
        } catch (Exception e) {
            logger.warn("Exception while trying to use the indexscan to remove the sort", e);
        }
    }
    indexPlanTimer.stop();
    logger.debug("Index Planning took {} ms", indexPlanTimer.elapsed(TimeUnit.MILLISECONDS));
}
Also used : PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) IndexSelector(org.apache.drill.exec.planner.index.IndexSelector) Stopwatch(org.apache.drill.shaded.guava.com.google.common.base.Stopwatch) IndexDescriptor(org.apache.drill.exec.planner.index.IndexDescriptor) IndexCollection(org.apache.drill.exec.planner.index.IndexCollection) IndexProperties(org.apache.drill.exec.planner.index.IndexProperties) CoveringPlanNoFilterGenerator(org.apache.drill.exec.planner.index.generators.CoveringPlanNoFilterGenerator) RelNode(org.apache.calcite.rel.RelNode) DbGroupScan(org.apache.drill.exec.physical.base.DbGroupScan) FunctionalIndexInfo(org.apache.drill.exec.planner.index.FunctionalIndexInfo)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)1 DbGroupScan (org.apache.drill.exec.physical.base.DbGroupScan)1 FunctionalIndexInfo (org.apache.drill.exec.planner.index.FunctionalIndexInfo)1 IndexCollection (org.apache.drill.exec.planner.index.IndexCollection)1 IndexDescriptor (org.apache.drill.exec.planner.index.IndexDescriptor)1 IndexProperties (org.apache.drill.exec.planner.index.IndexProperties)1 IndexSelector (org.apache.drill.exec.planner.index.IndexSelector)1 CoveringPlanNoFilterGenerator (org.apache.drill.exec.planner.index.generators.CoveringPlanNoFilterGenerator)1 PlannerSettings (org.apache.drill.exec.planner.physical.PlannerSettings)1 Stopwatch (org.apache.drill.shaded.guava.com.google.common.base.Stopwatch)1