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