Java Code Examples for org.apache.calcite.rel.convert.ConverterRule#getOutTrait()

The following examples show how to use org.apache.calcite.rel.convert.ConverterRule#getOutTrait() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConventionTraitDef.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void registerConverterRule(
    RelOptPlanner planner,
    ConverterRule converterRule) {
  if (converterRule.isGuaranteed()) {
    ConversionData conversionData = getConversionData(planner);

    final Convention inConvention =
        (Convention) converterRule.getInTrait();
    final Convention outConvention =
        (Convention) converterRule.getOutTrait();
    conversionData.conversionGraph.addVertex(inConvention);
    conversionData.conversionGraph.addVertex(outConvention);
    conversionData.conversionGraph.addEdge(inConvention, outConvention);

    conversionData.mapArcToConverterRule.put(
        Pair.of(inConvention, outConvention), converterRule);
  }
}
 
Example 2
Source File: HepPlanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
private boolean doesConverterApply(
    ConverterRule converterRule,
    HepRelVertex vertex) {
  RelTrait outTrait = converterRule.getOutTrait();
  List<HepRelVertex> parents = Graphs.predecessorListOf(graph, vertex);
  for (HepRelVertex parent : parents) {
    RelNode parentRel = parent.getCurrentRel();
    if (parentRel instanceof Converter) {
      // We don't support converter chains.
      continue;
    }
    if (parentRel.getTraitSet().contains(outTrait)) {
      // This parent wants the traits produced by the converter.
      return true;
    }
  }
  return (vertex == root)
      && (requestedRootTraits != null)
      && requestedRootTraits.contains(outTrait);
}
 
Example 3
Source File: ConventionTraitDef.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void registerConverterRule(
    RelOptPlanner planner,
    ConverterRule converterRule) {
  if (converterRule.isGuaranteed()) {
    ConversionData conversionData = getConversionData(planner);

    final Convention inConvention =
        (Convention) converterRule.getInTrait();
    final Convention outConvention =
        (Convention) converterRule.getOutTrait();
    conversionData.conversionGraph.addVertex(inConvention);
    conversionData.conversionGraph.addVertex(outConvention);
    conversionData.conversionGraph.addEdge(inConvention, outConvention);

    conversionData.mapArcToConverterRule.put(
        Pair.of(inConvention, outConvention), converterRule);
  }
}
 
Example 4
Source File: HepPlanner.java    From calcite with Apache License 2.0 6 votes vote down vote up
private boolean doesConverterApply(
    ConverterRule converterRule,
    HepRelVertex vertex) {
  RelTrait outTrait = converterRule.getOutTrait();
  List<HepRelVertex> parents = Graphs.predecessorListOf(graph, vertex);
  for (HepRelVertex parent : parents) {
    RelNode parentRel = parent.getCurrentRel();
    if (parentRel instanceof Converter) {
      // We don't support converter chains.
      continue;
    }
    if (parentRel.getTraitSet().contains(outTrait)) {
      // This parent wants the traits produced by the converter.
      return true;
    }
  }
  return (vertex == root)
      && (requestedRootTraits != null)
      && requestedRootTraits.contains(outTrait);
}
 
Example 5
Source File: VolcanoPlannerTraitTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void registerConverterRule(
    RelOptPlanner planner,
    ConverterRule converterRule) {
  if (!converterRule.isGuaranteed()) {
    return;
  }

  RelTrait fromTrait = converterRule.getInTrait();
  RelTrait toTrait = converterRule.getOutTrait();

  conversionMap.put(fromTrait, Pair.of(toTrait, converterRule));
}
 
Example 6
Source File: HepPlanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
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[0]),
          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;
}
 
Example 7
Source File: HepPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
private HepRelVertex applyRule(
    RelOptRule rule,
    HepRelVertex vertex,
    boolean forceConversions) {
  if (!graph.vertexSet().contains(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[0]),
          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;
}