Search in sources :

Example 1 with RelTrait

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

the class VolcanoPlanner method changeTraitsUsingConverters.

private RelNode changeTraitsUsingConverters(RelNode rel, RelTraitSet toTraits, boolean allowAbstractConverters) {
    final RelTraitSet fromTraits = rel.getTraitSet();
    assert fromTraits.size() >= toTraits.size();
    final boolean allowInfiniteCostConverters = SaffronProperties.INSTANCE.allowInfiniteCostConverters().get();
    // Traits may build on top of another...for example a collation trait
    // would typically come after a distribution trait since distribution
    // destroys collation; so when doing the conversion below we use
    // fromTraits as the trait of the just previously converted RelNode.
    // Also, toTraits may have fewer traits than fromTraits, excess traits
    // will be left as is.  Finally, any null entries in toTraits are
    // ignored.
    RelNode converted = rel;
    for (int i = 0; (converted != null) && (i < toTraits.size()); i++) {
        RelTrait fromTrait = converted.getTraitSet().getTrait(i);
        final RelTraitDef traitDef = fromTrait.getTraitDef();
        RelTrait toTrait = toTraits.getTrait(i);
        if (toTrait == null) {
            continue;
        }
        assert traitDef == toTrait.getTraitDef();
        // if (fromTrait.subsumes(toTrait)) {
        if (fromTrait.equals(toTrait)) {
            // No need to convert; it's already correct.
            continue;
        }
        rel = traitDef.convert(this, converted, toTrait, allowInfiniteCostConverters);
        if (rel != null) {
            assert rel.getTraitSet().getTrait(traitDef).satisfies(toTrait);
            rel = completeConversion(rel, allowInfiniteCostConverters, toTraits, Expressions.list(traitDef));
            if (rel != null) {
                register(rel, converted);
            }
        }
        if ((rel == null) && allowAbstractConverters) {
            RelTraitSet stepTraits = converted.getTraitSet().replace(toTrait);
            rel = getSubset(converted, stepTraits);
        }
        converted = rel;
    }
    // make sure final converted traitset subsumes what was required
    if (converted != null) {
        assert converted.getTraitSet().satisfies(toTraits);
    }
    return converted;
}
Also used : RelTrait(org.apache.calcite.plan.RelTrait) RelNode(org.apache.calcite.rel.RelNode) RelTraitDef(org.apache.calcite.plan.RelTraitDef) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 2 with RelTrait

use of org.apache.calcite.plan.RelTrait 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 3 with RelTrait

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

the class RelSubset method computeDigest.

@Override
protected String computeDigest() {
    StringBuilder digest = new StringBuilder("Subset#");
    digest.append(set.id);
    for (RelTrait trait : traitSet) {
        digest.append('.').append(trait);
    }
    return digest.toString();
}
Also used : RelTrait(org.apache.calcite.plan.RelTrait)

Example 4 with RelTrait

use of org.apache.calcite.plan.RelTrait in project druid by druid-io.

the class DruidQueryBuilder method getRelTraits.

public RelTrait[] getRelTraits() {
    final List<RelFieldCollation> collations = Lists.newArrayList();
    if (limitSpec != null) {
        for (OrderByColumnSpec orderBy : limitSpec.getColumns()) {
            final int i = outputRowSignature.getRowOrder().indexOf(orderBy.getDimension());
            final RelFieldCollation.Direction direction = orderBy.getDirection() == OrderByColumnSpec.Direction.ASCENDING ? RelFieldCollation.Direction.ASCENDING : RelFieldCollation.Direction.DESCENDING;
            collations.add(new RelFieldCollation(i, direction));
        }
    }
    if (!collations.isEmpty()) {
        return new RelTrait[] { RelCollations.of(collations) };
    } else {
        return new RelTrait[] {};
    }
}
Also used : OrderByColumnSpec(io.druid.query.groupby.orderby.OrderByColumnSpec) RelTrait(org.apache.calcite.plan.RelTrait) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 5 with RelTrait

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

the class RelSet method addInternal.

/**
 * Adds an expression <code>rel</code> to this set, without creating a
 * {@link org.apache.calcite.plan.volcano.RelSubset}. (Called only from
 * {@link org.apache.calcite.plan.volcano.RelSubset#add}.
 *
 * @param rel Relational expression
 */
void addInternal(RelNode rel) {
    if (!rels.contains(rel)) {
        rels.add(rel);
        for (RelTrait trait : rel.getTraitSet()) {
            assert trait == trait.getTraitDef().canonize(trait);
        }
        VolcanoPlanner planner = (VolcanoPlanner) rel.getCluster().getPlanner();
        if (planner.listener != null) {
            postEquivalenceEvent(planner, rel);
        }
    }
    if (this.rel == null) {
        this.rel = rel;
    } else {
        // Row types must be the same, except for field names.
        RelOptUtil.verifyTypeEquivalence(this.rel, rel, this);
    }
}
Also used : RelTrait(org.apache.calcite.plan.RelTrait)

Aggregations

RelTrait (org.apache.calcite.plan.RelTrait)13 RelTraitDef (org.apache.calcite.plan.RelTraitDef)5 RelNode (org.apache.calcite.rel.RelNode)4 RelTraitSet (org.apache.calcite.plan.RelTraitSet)3 ConverterRule (org.apache.calcite.rel.convert.ConverterRule)3 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RelOptRuleOperand (org.apache.calcite.plan.RelOptRuleOperand)2 OrderByColumnSpec (io.druid.query.groupby.orderby.OrderByColumnSpec)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 CommonRelSubExprRule (org.apache.calcite.plan.CommonRelSubExprRule)1 RelCollation (org.apache.calcite.rel.RelCollation)1 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)1 Converter (org.apache.calcite.rel.convert.Converter)1 RelWriterImpl (org.apache.calcite.rel.externalize.RelWriterImpl)1 RexNode (org.apache.calcite.rex.RexNode)1 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)1