Search in sources :

Example 21 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project calcite by apache.

the class MaterializedViewFilterScanRule method apply.

protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) {
    RelOptPlanner planner = call.getPlanner();
    List<RelOptMaterialization> materializations = (planner instanceof VolcanoPlanner) ? ((VolcanoPlanner) planner).getMaterializations() : ImmutableList.<RelOptMaterialization>of();
    if (!materializations.isEmpty()) {
        RelNode root = filter.copy(filter.getTraitSet(), Collections.singletonList((RelNode) scan));
        List<RelOptMaterialization> applicableMaterializations = RelOptMaterializations.getApplicableMaterializations(root, materializations);
        for (RelOptMaterialization materialization : applicableMaterializations) {
            if (RelOptUtil.areRowTypesEqual(scan.getRowType(), materialization.queryRel.getRowType(), false)) {
                RelNode target = materialization.queryRel;
                final HepPlanner hepPlanner = new HepPlanner(program, planner.getContext());
                hepPlanner.setRoot(target);
                target = hepPlanner.findBestExp();
                List<RelNode> subs = new MaterializedViewSubstitutionVisitor(target, root).go(materialization.tableRel);
                for (RelNode s : subs) {
                    call.transformTo(s);
                }
            }
        }
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) VolcanoPlanner(org.apache.calcite.plan.volcano.VolcanoPlanner) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) MaterializedViewSubstitutionVisitor(org.apache.calcite.plan.MaterializedViewSubstitutionVisitor)

Example 22 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project calcite by apache.

the class PigRelBuilderStyleTest method getVolcanoPlanner.

private RelOptPlanner getVolcanoPlanner(RelNode root) {
    final RelBuilderFactory builderFactory = RelBuilder.proto(PigRelFactories.ALL_PIG_REL_FACTORIES);
    // VolcanoPlanner
    final RelOptPlanner planner = root.getCluster().getPlanner();
    for (RelOptRule r : PigRules.ALL_PIG_OPT_RULES) {
        planner.addRule(r);
    }
    planner.removeRule(FilterAggregateTransposeRule.INSTANCE);
    planner.removeRule(FilterJoinRule.FILTER_ON_JOIN);
    planner.addRule(new FilterAggregateTransposeRule(PigFilter.class, builderFactory, PigAggregate.class));
    planner.addRule(new FilterIntoJoinRule(true, builderFactory, TRUE_PREDICATE));
    planner.setRoot(root);
    return planner;
}
Also used : RelBuilderFactory(org.apache.calcite.tools.RelBuilderFactory) PigAggregate(org.apache.calcite.adapter.pig.PigAggregate) FilterIntoJoinRule(org.apache.calcite.rel.rules.FilterJoinRule.FilterIntoJoinRule) FilterAggregateTransposeRule(org.apache.calcite.rel.rules.FilterAggregateTransposeRule) PigFilter(org.apache.calcite.adapter.pig.PigFilter) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule)

Example 23 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project calcite by apache.

the class Programs method of.

/**
 * Creates a program that executes a {@link HepProgram}.
 */
public static Program of(final HepProgram hepProgram, final boolean noDag, final RelMetadataProvider metadataProvider) {
    return new Program() {

        public RelNode run(RelOptPlanner planner, RelNode rel, RelTraitSet requiredOutputTraits, List<RelOptMaterialization> materializations, List<RelOptLattice> lattices) {
            final HepPlanner hepPlanner = new HepPlanner(hepProgram, null, noDag, null, RelOptCostImpl.FACTORY);
            List<RelMetadataProvider> list = Lists.newArrayList();
            if (metadataProvider != null) {
                list.add(metadataProvider);
            }
            hepPlanner.registerMetadataProviders(list);
            RelMetadataProvider plannerChain = ChainedRelMetadataProvider.of(list);
            rel.getCluster().setMetadataProvider(plannerChain);
            hepPlanner.setRoot(rel);
            return hepPlanner.findBestExp();
        }
    };
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) RelNode(org.apache.calcite.rel.RelNode) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner)

Example 24 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project calcite by apache.

the class Programs method standard.

/**
 * Returns the standard program with user metadata provider.
 */
public static Program standard(RelMetadataProvider metadataProvider) {
    final Program program1 = new Program() {

        public RelNode run(RelOptPlanner planner, RelNode rel, RelTraitSet requiredOutputTraits, List<RelOptMaterialization> materializations, List<RelOptLattice> lattices) {
            planner.setRoot(rel);
            for (RelOptMaterialization materialization : materializations) {
                planner.addMaterialization(materialization);
            }
            for (RelOptLattice lattice : lattices) {
                planner.addLattice(lattice);
            }
            final RelNode rootRel2 = rel.getTraitSet().equals(requiredOutputTraits) ? rel : planner.changeTraits(rel, requiredOutputTraits);
            assert rootRel2 != null;
            planner.setRoot(rootRel2);
            final RelOptPlanner planner2 = planner.chooseDelegate();
            final RelNode rootRel3 = planner2.findBestExp();
            assert rootRel3 != null : "could not implement exp";
            return rootRel3;
        }
    };
    return sequence(subQuery(metadataProvider), new DecorrelateProgram(), new TrimFieldsProgram(), program1, // EnumerableCalcRel is introduced.
    calc(metadataProvider));
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) RelNode(org.apache.calcite.rel.RelNode) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) RelOptLattice(org.apache.calcite.plan.RelOptLattice) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner)

Example 25 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project drill by apache.

the class IndexSelector method getCandidateIndexes.

/**
 * Run the index selection algorithm and return the top N indexes
 */
public void getCandidateIndexes(IndexConditionInfo.Builder infoBuilder, List<IndexGroup> coveringIndexes, List<IndexGroup> nonCoveringIndexes, List<IndexGroup> intersectIndexes) {
    RelOptPlanner planner = indexContext.getCall().getPlanner();
    PlannerSettings settings = PrelUtil.getPlannerSettings(planner);
    List<IndexGroup> candidateIndexes = Lists.newArrayList();
    logger.info("index_plan_info: Analyzing {} indexes for prefix matches: {}", indexPropList.size(), indexPropList);
    // analysis phase
    for (IndexProperties p : indexPropList) {
        analyzePrefixMatches(p);
        // can satisfy required collation
        if (p.numLeadingFilters() > 0 || p.satisfiesCollation()) {
            double selThreshold = p.isCovering() ? settings.getIndexCoveringSelThreshold() : settings.getIndexNonCoveringSelThreshold();
            // all when full table scan is disable to avoid a CannotPlanException
            if (settings.isDisableFullTableScan() || p.getLeadingSelectivity() <= selThreshold) {
                IndexGroup index = new IndexGroup();
                index.addIndexProp(p);
                candidateIndexes.add(index);
            } else {
                if (p.getLeadingSelectivity() > selThreshold) {
                    logger.debug("Skipping index {}. The leading selectivity {} is larger than threshold {}", p.getIndexDesc().getIndexName(), p.getLeadingSelectivity(), selThreshold);
                }
            }
        }
    }
    if (candidateIndexes.size() == 0) {
        logger.info("index_plan_info: No suitable indexes found !");
        return;
    }
    int max_candidate_indexes = (int) PrelUtil.getPlannerSettings(planner).getIndexMaxChosenIndexesPerTable();
    // to be exercised even for few indexes
    if (candidateIndexes.size() > 1) {
        Collections.sort(candidateIndexes, new IndexComparator(planner, builder));
    }
    // Generate index intersections for ranking
    addIndexIntersections(candidateIndexes, infoBuilder, settings.getMaxIndexesToIntersect());
    // Sort again after intersect plan is added to the list
    if (candidateIndexes.size() > 1) {
        Collections.sort(candidateIndexes, new IndexComparator(planner, builder));
    }
    logger.info("index_plan_info: The top ranked indexes are: ");
    int count = 0;
    boolean foundCovering = false;
    boolean foundCoveringCollation = false;
    boolean foundNonCoveringCollation = false;
    // pick the best N indexes
    for (int i = 0; i < candidateIndexes.size(); i++) {
        IndexGroup index = candidateIndexes.get(i);
        if (index.numIndexes() == 1 && index.getIndexProps().get(0).isCovering()) {
            IndexProperties indexProps = index.getIndexProps().get(0);
            if (foundCoveringCollation) {
                // if previously we already found a higher ranked covering index that satisfies collation,
                // then skip this one (note that selectivity and cost considerations were already handled
                // by the ranking phase)
                logger.debug("index_plan_info: Skipping covering index {} because a higher ranked covering index with collation already exists.", indexProps.getIndexDesc().getIndexName());
                continue;
            }
            coveringIndexes.add(index);
            logger.info("index_plan_info: name: {}, covering, collation: {}, leadingSelectivity: {}, cost: {}", indexProps.getIndexDesc().getIndexName(), indexProps.satisfiesCollation(), indexProps.getLeadingSelectivity(), indexProps.getSelfCost(planner));
            count++;
            foundCovering = true;
            if (indexProps.satisfiesCollation()) {
                foundCoveringCollation = true;
            }
        } else if (index.numIndexes() == 1) {
            // non-covering
            IndexProperties indexProps = index.getIndexProps().get(0);
            // non-covering index does not have collation
            if (foundCoveringCollation || (foundCovering && !indexProps.satisfiesCollation())) {
                logger.debug("index_plan_info: Skipping non-covering index {} because it does not have collation and a higher ranked covering index already exists.", indexProps.getIndexDesc().getIndexName());
                continue;
            }
            if (indexProps.satisfiesCollation()) {
                foundNonCoveringCollation = true;
            }
            // all other non-covering indexes can be added to the list because 2 or more non-covering index could
            // be considered for intersection later; currently the index selector is not costing the index intersection
            nonCoveringIndexes.add(index);
            logger.info("index_plan_info: name: {}, non-covering, collation: {}, leadingSelectivity: {}, cost: {}", indexProps.getIndexDesc().getIndexName(), indexProps.satisfiesCollation(), indexProps.getLeadingSelectivity(), indexProps.getSelfCost(planner));
            count++;
        } else {
            // intersect indexes
            if (foundCoveringCollation || (foundCovering && !index.getIndexProps().get(index.numIndexes() - 1).satisfiesCollation()) || foundNonCoveringCollation) {
                continue;
            }
            IndexGroup intersectIndex = new IndexGroup();
            double isectLeadingSel = 1.0;
            String isectName = "Intersect-" + count;
            for (IndexProperties indexProps : index.getIndexProps()) {
                intersectIndex.addIndexProp(indexProps);
                isectLeadingSel *= indexProps.getLeadingSelectivity();
                logger.info("name: {}, {}, collation: {}, leadingSelectivity: {}, cost: {}", indexProps.getIndexDesc().getIndexName(), isectName, indexProps.satisfiesCollation(), indexProps.getLeadingSelectivity(), indexProps.getSelfCost(planner));
            }
            logger.info("name: {}, intersect-idx, collation: {}, leadingSelectivity: {}, cost: {}", isectName, index.getIndexProps().get(index.numIndexes() - 1).satisfiesCollation(), isectLeadingSel, index.getIndexProps().get(0).getIntersectCost(index, builder, planner));
            intersectIndexes.add(intersectIndex);
        }
        if (count == max_candidate_indexes) {
            break;
        }
    }
}
Also used : PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner)

Aggregations

RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)26 RelNode (org.apache.calcite.rel.RelNode)16 RelOptCluster (org.apache.calcite.plan.RelOptCluster)9 RelTraitSet (org.apache.calcite.plan.RelTraitSet)9 RexBuilder (org.apache.calcite.rex.RexBuilder)7 RelOptMaterialization (org.apache.calcite.plan.RelOptMaterialization)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 ImmutableList (com.google.common.collect.ImmutableList)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 RelOptRule (org.apache.calcite.plan.RelOptRule)4 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)4 VolcanoPlanner (org.apache.calcite.plan.volcano.VolcanoPlanner)4 JaninoRelMetadataProvider (org.apache.calcite.rel.metadata.JaninoRelMetadataProvider)4 Properties (java.util.Properties)3 DataContext (org.apache.calcite.DataContext)3 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)3 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 HepProgram (org.apache.calcite.plan.hep.HepProgram)3 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)3