Search in sources :

Example 1 with ConverterRule

use of org.apache.calcite.rel.convert.ConverterRule in project calcite by apache.

the class VolcanoPlanner method removeRule.

public boolean removeRule(RelOptRule rule) {
    if (!ruleSet.remove(rule)) {
        // Rule was not present.
        return false;
    }
    // Remove description.
    unmapRuleDescription(rule);
    // Remove operands.
    for (Iterator<RelOptRuleOperand> iter = classOperands.values().iterator(); iter.hasNext(); ) {
        RelOptRuleOperand entry = iter.next();
        if (entry.getRule().equals(rule)) {
            iter.remove();
        }
    }
    // graph.)
    if (rule instanceof ConverterRule) {
        ConverterRule converterRule = (ConverterRule) rule;
        final RelTrait ruleTrait = converterRule.getInTrait();
        final RelTraitDef ruleTraitDef = ruleTrait.getTraitDef();
        if (traitDefs.contains(ruleTraitDef)) {
            ruleTraitDef.deregisterConverterRule(this, converterRule);
        }
    }
    return true;
}
Also used : RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand) ConverterRule(org.apache.calcite.rel.convert.ConverterRule) RelTrait(org.apache.calcite.plan.RelTrait) RelTraitDef(org.apache.calcite.plan.RelTraitDef)

Example 2 with ConverterRule

use of org.apache.calcite.rel.convert.ConverterRule in project calcite by apache.

the class VolcanoPlannerTest method removeTrivialProject.

private void removeTrivialProject(boolean useRule) {
    VolcanoPlanner planner = new VolcanoPlanner();
    planner.ambitious = true;
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    if (useRule) {
        planner.addRule(ProjectRemoveRule.INSTANCE);
    }
    planner.addRule(new PhysLeafRule());
    planner.addRule(new GoodSingleRule());
    planner.addRule(new PhysProjectRule());
    planner.addRule(new ConverterRule(RelNode.class, PHYS_CALLING_CONVENTION, EnumerableConvention.INSTANCE, "PhysToIteratorRule") {

        public RelNode convert(RelNode rel) {
            return new PhysToIteratorConverter(rel.getCluster(), rel);
        }
    });
    RelOptCluster cluster = newCluster(planner);
    PhysLeafRel leafRel = new PhysLeafRel(cluster, "a");
    final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(leafRel.getCluster(), null);
    RelNode projectRel = relBuilder.push(leafRel).project(relBuilder.alias(relBuilder.field(0), "this")).build();
    NoneSingleRel singleRel = new NoneSingleRel(cluster, projectRel);
    RelNode convertedRel = planner.changeTraits(singleRel, cluster.traitSetOf(EnumerableConvention.INSTANCE));
    planner.setRoot(convertedRel);
    RelNode result = planner.chooseDelegate().findBestExp();
    assertTrue(result instanceof PhysToIteratorConverter);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) ConverterRule(org.apache.calcite.rel.convert.ConverterRule) RelBuilder(org.apache.calcite.tools.RelBuilder) GoodSingleRule(org.apache.calcite.plan.volcano.PlannerTests.GoodSingleRule) RelNode(org.apache.calcite.rel.RelNode) PhysLeafRule(org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRule) NoneSingleRel(org.apache.calcite.plan.volcano.PlannerTests.NoneSingleRel) PhysLeafRel(org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRel)

Example 3 with ConverterRule

use of org.apache.calcite.rel.convert.ConverterRule in project calcite by apache.

the class VolcanoPlanner method addRule.

public boolean addRule(RelOptRule rule) {
    if (locked) {
        return false;
    }
    if (ruleSet.contains(rule)) {
        // Rule already exists.
        return false;
    }
    final boolean added = ruleSet.add(rule);
    assert added;
    final String ruleName = rule.toString();
    if (ruleNames.put(ruleName, rule.getClass())) {
        Set<Class> x = ruleNames.get(ruleName);
        if (x.size() > 1) {
            throw new RuntimeException("Rule description '" + ruleName + "' is not unique; classes: " + x);
        }
    }
    mapRuleDescription(rule);
    // it.
    for (RelOptRuleOperand operand : rule.getOperands()) {
        for (Class<? extends RelNode> subClass : subClasses(operand.getMatchedClass())) {
            classOperands.put(subClass, operand);
        }
    }
    // with the trait.
    if (rule instanceof ConverterRule) {
        ConverterRule converterRule = (ConverterRule) rule;
        final RelTrait ruleTrait = converterRule.getInTrait();
        final RelTraitDef ruleTraitDef = ruleTrait.getTraitDef();
        if (traitDefs.contains(ruleTraitDef)) {
            ruleTraitDef.registerConverterRule(this, converterRule);
        }
    }
    return true;
}
Also used : RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand) ConverterRule(org.apache.calcite.rel.convert.ConverterRule) RelTrait(org.apache.calcite.plan.RelTrait) RelTraitDef(org.apache.calcite.plan.RelTraitDef)

Example 4 with ConverterRule

use of org.apache.calcite.rel.convert.ConverterRule in project calcite by apache.

the class HepPlanner method executeInstruction.

void executeInstruction(HepInstruction.ConverterRules instruction) {
    assert currentProgram.group == null;
    if (instruction.ruleSet == null) {
        instruction.ruleSet = new LinkedHashSet<>();
        for (RelOptRule rule : allRules) {
            if (!(rule instanceof ConverterRule)) {
                continue;
            }
            ConverterRule converter = (ConverterRule) rule;
            if (converter.isGuaranteed() != instruction.guaranteed) {
                continue;
            }
            // Add the rule itself to work top-down
            instruction.ruleSet.add(converter);
            if (!instruction.guaranteed) {
                // Add a TraitMatchingRule to work bottom-up
                instruction.ruleSet.add(new TraitMatchingRule(converter, RelFactories.LOGICAL_BUILDER));
            }
        }
    }
    applyRules(instruction.ruleSet, instruction.guaranteed);
}
Also used : ConverterRule(org.apache.calcite.rel.convert.ConverterRule) TraitMatchingRule(org.apache.calcite.rel.convert.TraitMatchingRule) RelOptRule(org.apache.calcite.plan.RelOptRule)

Example 5 with ConverterRule

use of org.apache.calcite.rel.convert.ConverterRule in project calcite by apache.

the class HepPlanner method applyRule.

private HepRelVertex applyRule(RelOptRule rule, HepRelVertex vertex, boolean forceConversions) {
    if (!belongsToDag(vertex)) {
        return null;
    }
    RelTrait parentTrait = null;
    List<RelNode> parents = null;
    if (rule instanceof ConverterRule) {
        // Guaranteed converter rules require special casing to make sure
        // they only fire where actually needed, otherwise they tend to
        // fire to infinity and beyond.
        ConverterRule converterRule = (ConverterRule) rule;
        if (converterRule.isGuaranteed() || !forceConversions) {
            if (!doesConverterApply(converterRule, vertex)) {
                return null;
            }
            parentTrait = converterRule.getOutTrait();
        }
    } else if (rule instanceof CommonRelSubExprRule) {
        // Only fire CommonRelSubExprRules if the vertex is a common
        // subexpression.
        List<HepRelVertex> parentVertices = getVertexParents(vertex);
        if (parentVertices.size() < 2) {
            return null;
        }
        parents = new ArrayList<>();
        for (HepRelVertex pVertex : parentVertices) {
            parents.add(pVertex.getCurrentRel());
        }
    }
    final List<RelNode> bindings = new ArrayList<>();
    final Map<RelNode, List<RelNode>> nodeChildren = new HashMap<>();
    boolean match = matchOperands(rule.getOperand(), vertex.getCurrentRel(), bindings, nodeChildren);
    if (!match) {
        return null;
    }
    HepRuleCall call = new HepRuleCall(this, rule.getOperand(), bindings.toArray(new RelNode[bindings.size()]), nodeChildren, parents);
    // Allow the rule to apply its own side-conditions.
    if (!rule.matches(call)) {
        return null;
    }
    fireRule(call);
    if (!call.getResults().isEmpty()) {
        return applyTransformationResults(vertex, call, parentTrait);
    }
    return null;
}
Also used : CommonRelSubExprRule(org.apache.calcite.plan.CommonRelSubExprRule) RelTrait(org.apache.calcite.plan.RelTrait) ConverterRule(org.apache.calcite.rel.convert.ConverterRule) RelNode(org.apache.calcite.rel.RelNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Aggregations

ConverterRule (org.apache.calcite.rel.convert.ConverterRule)5 RelTrait (org.apache.calcite.plan.RelTrait)3 RelOptRuleOperand (org.apache.calcite.plan.RelOptRuleOperand)2 RelTraitDef (org.apache.calcite.plan.RelTraitDef)2 RelNode (org.apache.calcite.rel.RelNode)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CommonRelSubExprRule (org.apache.calcite.plan.CommonRelSubExprRule)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelOptRule (org.apache.calcite.plan.RelOptRule)1 GoodSingleRule (org.apache.calcite.plan.volcano.PlannerTests.GoodSingleRule)1 NoneSingleRel (org.apache.calcite.plan.volcano.PlannerTests.NoneSingleRel)1 PhysLeafRel (org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRel)1 PhysLeafRule (org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRule)1 TraitMatchingRule (org.apache.calcite.rel.convert.TraitMatchingRule)1 RelBuilder (org.apache.calcite.tools.RelBuilder)1