Java Code Examples for org.apache.calcite.plan.RelOptRule#matches()

The following examples show how to use org.apache.calcite.plan.RelOptRule#matches() . 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: 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 2
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;
}