Search in sources :

Example 1 with RelOptRuleOperand

use of org.apache.calcite.plan.RelOptRuleOperand 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 RelOptRuleOperand

use of org.apache.calcite.plan.RelOptRuleOperand in project calcite by apache.

the class VolcanoPlanner method fireRules.

/**
 * Fires all rules matched by a relational expression.
 *
 * @param rel      Relational expression which has just been created (or maybe
 *                 from the queue)
 * @param deferred If true, each time a rule matches, just add an entry to
 *                 the queue.
 */
void fireRules(RelNode rel, boolean deferred) {
    for (RelOptRuleOperand operand : classOperands.get(rel.getClass())) {
        if (operand.matches(rel)) {
            final VolcanoRuleCall ruleCall;
            if (deferred) {
                ruleCall = new DeferringRuleCall(this, operand);
            } else {
                ruleCall = new VolcanoRuleCall(this, operand);
            }
            ruleCall.match(rel);
        }
    }
}
Also used : RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand)

Example 3 with RelOptRuleOperand

use of org.apache.calcite.plan.RelOptRuleOperand in project calcite by apache.

the class HepPlanner method matchOperands.

private boolean matchOperands(RelOptRuleOperand operand, RelNode rel, List<RelNode> bindings, Map<RelNode, List<RelNode>> nodeChildren) {
    if (!operand.matches(rel)) {
        return false;
    }
    bindings.add(rel);
    @SuppressWarnings("unchecked") List<HepRelVertex> childRels = (List) rel.getInputs();
    switch(operand.childPolicy) {
        case ANY:
            return true;
        case UNORDERED:
            // matchAnyChildren, usually there's just one operand.
            for (RelOptRuleOperand childOperand : operand.getChildOperands()) {
                boolean match = false;
                for (HepRelVertex childRel : childRels) {
                    match = matchOperands(childOperand, childRel.getCurrentRel(), bindings, nodeChildren);
                    if (match) {
                        break;
                    }
                }
                if (!match) {
                    return false;
                }
            }
            final List<RelNode> children = new ArrayList<>(childRels.size());
            for (HepRelVertex childRel : childRels) {
                children.add(childRel.getCurrentRel());
            }
            nodeChildren.put(rel, children);
            return true;
        default:
            int n = operand.getChildOperands().size();
            if (childRels.size() < n) {
                return false;
            }
            for (Pair<HepRelVertex, RelOptRuleOperand> pair : Pair.zip(childRels, operand.getChildOperands())) {
                boolean match = matchOperands(pair.right, pair.left.getCurrentRel(), bindings, nodeChildren);
                if (!match) {
                    return false;
                }
            }
            return true;
    }
}
Also used : RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 4 with RelOptRuleOperand

use of org.apache.calcite.plan.RelOptRuleOperand in project calcite by apache.

the class RuleQueue method checkDuplicateSubsets.

/**
 * Recursively checks whether there are any duplicate subsets along any path
 * from root of the operand tree to one of the leaves.
 *
 * <p>It is OK for a match to have duplicate subsets if they are not on the
 * same path. For example,
 *
 * <blockquote><pre>
 *   Join
 *  /   \
 * X     X
 * </pre></blockquote>
 *
 * <p>is a valid match.
 *
 * @throws org.apache.calcite.util.Util.FoundOne on match
 */
private void checkDuplicateSubsets(Deque<RelSubset> subsets, RelOptRuleOperand operand, RelNode[] rels) {
    final RelSubset subset = planner.getSubset(rels[operand.ordinalInRule]);
    if (subsets.contains(subset)) {
        throw Util.FoundOne.NULL;
    }
    if (!operand.getChildOperands().isEmpty()) {
        subsets.push(subset);
        for (RelOptRuleOperand childOperand : operand.getChildOperands()) {
            checkDuplicateSubsets(subsets, childOperand, rels);
        }
        final RelSubset x = subsets.pop();
        assert x == subset;
    }
}
Also used : RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand)

Example 5 with RelOptRuleOperand

use of org.apache.calcite.plan.RelOptRuleOperand in project calcite by apache.

the class VolcanoPlanner method onNewClass.

@Override
protected void onNewClass(RelNode node) {
    super.onNewClass(node);
    // Create mappings so that instances of this class will match existing
    // operands.
    final Class<? extends RelNode> clazz = node.getClass();
    for (RelOptRule rule : ruleSet) {
        for (RelOptRuleOperand operand : rule.getOperands()) {
            if (operand.getMatchedClass().isAssignableFrom(clazz)) {
                classOperands.put(clazz, operand);
            }
        }
    }
}
Also used : RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand) RelOptRule(org.apache.calcite.plan.RelOptRule)

Aggregations

RelOptRuleOperand (org.apache.calcite.plan.RelOptRuleOperand)7 RelTrait (org.apache.calcite.plan.RelTrait)2 RelTraitDef (org.apache.calcite.plan.RelTraitDef)2 RelNode (org.apache.calcite.rel.RelNode)2 ConverterRule (org.apache.calcite.rel.convert.ConverterRule)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RelOptRule (org.apache.calcite.plan.RelOptRule)1