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;
}
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);
}
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;
}
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);
}
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;
}
Aggregations