Search in sources :

Example 11 with RelTrait

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

Example 12 with RelTrait

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

the class AbstractRelNode method computeDigest.

/**
 * Computes the digest. Does not modify this object.
 *
 * @return Digest
 */
protected String computeDigest() {
    StringWriter sw = new StringWriter();
    RelWriter pw = new RelWriterImpl(new PrintWriter(sw), SqlExplainLevel.DIGEST_ATTRIBUTES, false) {

        protected void explain_(RelNode rel, List<Pair<String, Object>> values) {
            pw.write(getRelTypeName());
            for (RelTrait trait : traitSet) {
                pw.write(".");
                pw.write(trait.toString());
            }
            pw.write("(");
            int j = 0;
            for (Pair<String, Object> value : values) {
                if (j++ > 0) {
                    pw.write(",");
                }
                pw.write(value.left + "=" + value.right);
            }
            pw.write(")");
        }
    };
    explain(pw);
    return sw.toString();
}
Also used : RelTrait(org.apache.calcite.plan.RelTrait) StringWriter(java.io.StringWriter) RelWriterImpl(org.apache.calcite.rel.externalize.RelWriterImpl) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) PrintWriter(java.io.PrintWriter)

Example 13 with RelTrait

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

the class MapRDBPushProjectIntoScan method doPushProjectIntoGroupScan.

protected void doPushProjectIntoGroupScan(RelOptRuleCall call, ProjectPrel project, ScanPrel scan, JsonTableGroupScan groupScan) {
    try {
        DrillRelOptUtil.ProjectPushInfo columnInfo = DrillRelOptUtil.getFieldsInformation(scan.getRowType(), project.getProjects());
        if (columnInfo == null || Utilities.isStarQuery(columnInfo.getFields()) || !groupScan.canPushdownProjects(columnInfo.getFields())) {
            return;
        }
        RelTraitSet newTraits = call.getPlanner().emptyTraitSet();
        // Clear out collation trait
        for (RelTrait trait : scan.getTraitSet()) {
            if (!(trait instanceof RelCollation)) {
                newTraits.plus(trait);
            }
        }
        final ScanPrel newScan = new ScanPrel(scan.getCluster(), newTraits.plus(Prel.DRILL_PHYSICAL), groupScan.clone(columnInfo.getFields()), columnInfo.createNewRowType(project.getInput().getCluster().getTypeFactory()), scan.getTable());
        List<RexNode> newProjects = Lists.newArrayList();
        for (RexNode n : project.getChildExps()) {
            newProjects.add(n.accept(columnInfo.getInputReWriter()));
        }
        final ProjectPrel newProj = new ProjectPrel(project.getCluster(), project.getTraitSet().plus(Prel.DRILL_PHYSICAL), newScan, newProjects, project.getRowType());
        if (ProjectRemoveRule.isTrivial(newProj) && // the old project did not involve any column renaming
        sameRowTypeProjectionsFields(project.getRowType(), newScan.getRowType())) {
            call.transformTo(newScan);
        } else {
            call.transformTo(newProj);
        }
    } catch (Exception e) {
        throw new DrillRuntimeException(e);
    }
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) RelCollation(org.apache.calcite.rel.RelCollation) ScanPrel(org.apache.drill.exec.planner.physical.ScanPrel) RelTrait(org.apache.calcite.plan.RelTrait) DrillRelOptUtil(org.apache.drill.exec.planner.common.DrillRelOptUtil) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) RexNode(org.apache.calcite.rex.RexNode)

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