Java Code Examples for org.apache.calcite.rel.RelNode#replaceInput()

The following examples show how to use org.apache.calcite.rel.RelNode#replaceInput() . 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: VolcanoPlanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
private boolean fixUpInputs(RelNode rel) {
  List<RelNode> inputs = rel.getInputs();
  int i = -1;
  int changeCount = 0;
  for (RelNode input : inputs) {
    ++i;
    if (input instanceof RelSubset) {
      final RelSubset subset = (RelSubset) input;
      RelSubset newSubset = canonize(subset);
      if (newSubset != subset) {
        rel.replaceInput(i, newSubset);
        if (subset.set != newSubset.set) {
          subset.set.parents.remove(rel);
          newSubset.set.parents.add(rel);
        }
        changeCount++;
      }
    }
  }
  return changeCount > 0;
}
 
Example 2
Source File: HepPlanner.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelNode buildFinalPlan(HepRelVertex vertex) {
  RelNode rel = vertex.getCurrentRel();

  notifyChosen(rel);

  // Recursively process children, replacing this rel's inputs
  // with corresponding child rels.
  List<RelNode> inputs = rel.getInputs();
  for (int i = 0; i < inputs.size(); ++i) {
    RelNode child = inputs.get(i);
    if (!(child instanceof HepRelVertex)) {
      // Already replaced.
      continue;
    }
    child = buildFinalPlan((HepRelVertex) child);
    rel.replaceInput(i, child);
  }
  RelMdUtil.clearCache(rel);
  rel.recomputeDigest();

  return rel;
}
 
Example 3
Source File: HepPlanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
private RelNode buildFinalPlan(HepRelVertex vertex) {
  RelNode rel = vertex.getCurrentRel();

  notifyChosen(rel);

  // Recursively process children, replacing this rel's inputs
  // with corresponding child rels.
  List<RelNode> inputs = rel.getInputs();
  for (int i = 0; i < inputs.size(); ++i) {
    RelNode child = inputs.get(i);
    if (!(child instanceof HepRelVertex)) {
      // Already replaced.
      continue;
    }
    child = buildFinalPlan((HepRelVertex) child);
    rel.replaceInput(i, child);
    rel.recomputeDigest();
  }

  return rel;
}
 
Example 4
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
/** Fallback if none of the other {@code decorrelateRel} methods match. */
public Frame decorrelateRel(RelNode rel) {
	RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs());
	if (rel.getInputs().size() > 0) {
		List<RelNode> oldInputs = rel.getInputs();
		List<RelNode> newInputs = new ArrayList<>();
		for (int i = 0; i < oldInputs.size(); ++i) {
			final Frame frame = getInvoke(oldInputs.get(i));
			if (frame == null || frame.c != null) {
				// if input is not rewritten, or if it produces correlated variables, terminate rewrite
				return null;
			}
			newInputs.add(frame.r);
			newRel.replaceInput(i, frame.r);
		}

		if (!Util.equalShallow(oldInputs, newInputs)) {
			newRel = rel.copy(rel.getTraitSet(), newInputs);
		}
	}
	// the output position should not change since there are no corVars coming from below.
	return new Frame(rel, newRel, null, identityMap(rel.getRowType().getFieldCount()));
}
 
Example 5
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
/** Fallback if none of the other {@code decorrelateRel} methods match. */
public Frame decorrelateRel(RelNode rel) {
	RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs());
	if (rel.getInputs().size() > 0) {
		List<RelNode> oldInputs = rel.getInputs();
		List<RelNode> newInputs = new ArrayList<>();
		for (int i = 0; i < oldInputs.size(); ++i) {
			final Frame frame = getInvoke(oldInputs.get(i));
			if (frame == null || frame.c != null) {
				// if input is not rewritten, or if it produces correlated variables, terminate rewrite
				return null;
			}
			newInputs.add(frame.r);
			newRel.replaceInput(i, frame.r);
		}

		if (!Util.equalShallow(oldInputs, newInputs)) {
			newRel = rel.copy(rel.getTraitSet(), newInputs);
		}
	}
	// the output position should not change since there are no corVars coming from below.
	return new Frame(rel, newRel, null, identityMap(rel.getRowType().getFieldCount()));
}
 
Example 6
Source File: ForcedRulesProgram.java    From calcite-sql-rewriter with Apache License 2.0 5 votes vote down vote up
private RelNode replace(RelNode original, ForcedRule[] rules, JdbcRelBuilderFactory relBuilderFactory) {
	RelNode p = original;
	for (ForcedRule rule : rules) {
		RelNode updated = rule.apply(p, relBuilderFactory);
		if (updated != null) {
			logger.trace("Rule: " + rule.toString() +
					"\nReplacing:\n" + RelOptUtil.toString(p) +
					"\nWith:\n" + RelOptUtil.toString(updated)
			);
			// Must maintain row types so that nothing explodes
			RelOptUtil.equal(
					"RowType of original", p.getRowType(),
					"RowType of replaced", updated.getRowType(),
					Litmus.THROW
			);
			p = updated;
			break;
		}
	}

	List<RelNode> oldInputs = p.getInputs();
	for (int i = 0; i < oldInputs.size(); i++) {
		RelNode originalInput = oldInputs.get(i);
		RelNode replacedInput = replace(originalInput, rules, relBuilderFactory);
		if (replacedInput != originalInput) {
			p.replaceInput(i, replacedInput);
		}
	}
	return p;
}
 
Example 7
Source File: MockRelOptPlanner.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively matches a rule.
 *
 * @param rel             Relational expression
 * @param parent          Parent relational expression
 * @param ordinalInParent Ordinal of relational expression among its
 *                        siblings
 * @return whether match occurred
 */
private boolean matchRecursive(
    RelNode rel,
    RelNode parent,
    int ordinalInParent) {
  List<RelNode> bindings = new ArrayList<RelNode>();
  if (match(
      rule.getOperand(),
      rel,
      bindings)) {
    MockRuleCall call =
        new MockRuleCall(
            this,
            rule.getOperand(),
            bindings.toArray(new RelNode[0]));
    if (rule.matches(call)) {
      rule.onMatch(call);
    }
  }

  if (transformationResult != null) {
    if (parent == null) {
      root = transformationResult;
    } else {
      parent.replaceInput(ordinalInParent, transformationResult);
    }
    return true;
  }

  List<? extends RelNode> children = rel.getInputs();
  for (int i = 0; i < children.size(); ++i) {
    if (matchRecursive(children.get(i), rel, i)) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: HepPlanner.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void contractVertices(
    HepRelVertex preservedVertex,
    HepRelVertex discardedVertex,
    List<HepRelVertex> parents) {
  if (preservedVertex == discardedVertex) {
    // Nop.
    return;
  }

  RelNode rel = preservedVertex.getCurrentRel();
  updateVertex(preservedVertex, rel);

  // Update specified parents of discardedVertex.
  for (HepRelVertex parent : parents) {
    RelNode parentRel = parent.getCurrentRel();
    List<RelNode> inputs = parentRel.getInputs();
    for (int i = 0; i < inputs.size(); ++i) {
      RelNode child = inputs.get(i);
      if (child != discardedVertex) {
        continue;
      }
      parentRel.replaceInput(i, preservedVertex);
    }
    clearCache(parent);
    graph.removeEdge(parent, discardedVertex);
    graph.addEdge(parent, preservedVertex);
    updateVertex(parent, parentRel);
  }

  // NOTE:  we don't actually do graph.removeVertex(discardedVertex),
  // because it might still be reachable from preservedVertex.
  // Leave that job for garbage collection.

  if (discardedVertex == root) {
    root = preservedVertex;
  }
}
 
Example 9
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void rewriteGeneric(RelNode rel) {
  RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs());
  List<RelNode> oldInputs = rel.getInputs();
  for (int i = 0; i < oldInputs.size(); ++i) {
    newRel.replaceInput(
        i,
        getNewForOldRel(oldInputs.get(i)));
  }
  setNewForOldRel(rel, newRel);
}
 
Example 10
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Fallback if none of the other {@code decorrelateRel} methods match. */
public Frame decorrelateRel(RelNode rel) {
  RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs());

  if (rel.getInputs().size() > 0) {
    List<RelNode> oldInputs = rel.getInputs();
    List<RelNode> newInputs = new ArrayList<>();
    for (int i = 0; i < oldInputs.size(); ++i) {
      final Frame frame = getInvoke(oldInputs.get(i), rel);
      if (frame == null || !frame.corDefOutputs.isEmpty()) {
        // if input is not rewritten, or if it produces correlated
        // variables, terminate rewrite
        return null;
      }
      newInputs.add(frame.r);
      newRel.replaceInput(i, frame.r);
    }

    if (!Util.equalShallow(oldInputs, newInputs)) {
      newRel = rel.copy(rel.getTraitSet(), newInputs);
    }
  }

  // the output position should not change since there are no corVars
  // coming from below.
  return register(rel, newRel, identityMap(rel.getRowType().getFieldCount()),
      ImmutableSortedMap.of());
}
 
Example 11
Source File: MoreRelOptUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected RelNode visitChild(RelNode parent, int i, RelNode child) {

  // Ignore the root node.
  if (null == parent) {
    return super.visitChild(parent, i, child);
  }

  // Ignore non-sort child nodes.
  if (!(child instanceof Sort)) {
    return super.visitChild(parent, i, child);
  }

  // Ignore the sort for the top level SELECT. It's valid to use ORDER BY
  // without FETCH / OFFSET here.
  if (child == topLevelSort) {
    return super.visitChild(parent, i, child);
  }

  // If the child Sort has FETCH and LIMIT clauses, do not touch them.
  Sort childAsSort = (Sort) child;
  if (childAsSort.offset == null &&
      childAsSort.fetch == null) {
    parent.replaceInput(i, childAsSort.getInput());
    return super.visitChild(parent, i, childAsSort.getInput());
  }

  return super.visitChild(parent, i, child);
}
 
Example 12
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Fallback if none of the other {@code decorrelateRel} methods match. */
public Frame decorrelateRel(RelNode rel) {
    RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs());

    if (rel.getInputs().size() > 0) {
        List<RelNode> oldInputs = rel.getInputs();
        List<RelNode> newInputs = new ArrayList<>();
        for (int i = 0; i < oldInputs.size(); ++i) {
            final Frame frame = getInvoke(oldInputs.get(i), rel);
            if (frame == null || !frame.corDefOutputs.isEmpty()) {
                // if input is not rewritten, or if it produces correlated
                // variables, terminate rewrite
                return null;
            }
            newInputs.add(frame.r);
            newRel.replaceInput(i, frame.r);
        }

        if (!Util.equalShallow(oldInputs, newInputs)) {
            newRel = rel.copy(rel.getTraitSet(), newInputs);
        }
    }

    // the output position should not change since there are no corVars
    // coming from below.
    return register(rel, newRel, identityMap(rel.getRowType().getFieldCount()), ImmutableSortedMap.of());
}
 
Example 13
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Fallback if none of the other {@code decorrelateRel} methods match. */
public Frame decorrelateRel(RelNode rel) {
  RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs());

  if (rel.getInputs().size() > 0) {
    List<RelNode> oldInputs = rel.getInputs();
    List<RelNode> newInputs = new ArrayList<>();
    for (int i = 0; i < oldInputs.size(); ++i) {
      final Frame frame = getInvoke(oldInputs.get(i), rel);
      if (frame == null || !frame.corDefOutputs.isEmpty()) {
        // if input is not rewritten, or if it produces correlated
        // variables, terminate rewrite
        return null;
      }
      newInputs.add(frame.r);
      newRel.replaceInput(i, frame.r);
    }

    if (!Util.equalShallow(oldInputs, newInputs)) {
      newRel = rel.copy(rel.getTraitSet(), newInputs);
    }
  }

  // the output position should not change since there are no corVars
  // coming from below.
  return register(rel, newRel, identityMap(rel.getRowType().getFieldCount()),
      ImmutableSortedMap.of());
}
 
Example 14
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Fallback if none of the other {@code decorrelateRel} methods match. */
public Frame decorrelateRel(RelNode rel) {
  RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs());

  if (rel.getInputs().size() > 0) {
    List<RelNode> oldInputs = rel.getInputs();
    List<RelNode> newInputs = new ArrayList<>();
    for (int i = 0; i < oldInputs.size(); ++i) {
      final Frame frame = getInvoke(oldInputs.get(i), rel);
      if (frame == null || !frame.corDefOutputs.isEmpty()) {
        // if input is not rewritten, or if it produces correlated
        // variables, terminate rewrite
        return null;
      }
      newInputs.add(frame.r);
      newRel.replaceInput(i, frame.r);
    }

    if (!Util.equalShallow(oldInputs, newInputs)) {
      newRel = rel.copy(rel.getTraitSet(), newInputs);
    }
  }

  // the output position should not change since there are no corVars
  // coming from below.
  return register(rel, newRel, identityMap(rel.getRowType().getFieldCount()),
      ImmutableSortedMap.of());
}
 
Example 15
Source File: HepPlanner.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void contractVertices(
    HepRelVertex preservedVertex,
    HepRelVertex discardedVertex,
    List<HepRelVertex> parents) {
  if (preservedVertex == discardedVertex) {
    // Nop.
    return;
  }

  RelNode rel = preservedVertex.getCurrentRel();
  updateVertex(preservedVertex, rel);

  // Update specified parents of discardedVertex.
  for (HepRelVertex parent : parents) {
    RelNode parentRel = parent.getCurrentRel();
    List<RelNode> inputs = parentRel.getInputs();
    for (int i = 0; i < inputs.size(); ++i) {
      RelNode child = inputs.get(i);
      if (child != discardedVertex) {
        continue;
      }
      parentRel.replaceInput(i, preservedVertex);
    }
    graph.removeEdge(parent, discardedVertex);
    graph.addEdge(parent, preservedVertex);
    updateVertex(parent, parentRel);
  }

  // NOTE:  we don't actually do graph.removeVertex(discardedVertex),
  // because it might still be reachable from preservedVertex.
  // Leave that job for garbage collection.

  if (discardedVertex == root) {
    root = preservedVertex;
  }
}
 
Example 16
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void rewriteGeneric(RelNode rel) {
    RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs());
    List<RelNode> oldInputs = rel.getInputs();
    for (int i = 0; i < oldInputs.size(); ++i) {
        newRel.replaceInput(i, getNewForOldRel(oldInputs.get(i)));
    }
    setNewForOldRel(rel, newRel);
}
 
Example 17
Source File: OLAPRel.java    From kylin with Apache License 2.0 4 votes vote down vote up
public void fixSharedOlapTableScanAt(RelNode parent, int ordinalInParent) {
    OLAPTableScan copy = copyTableScanIfNeeded(parent.getInputs().get(ordinalInParent));
    if (copy != null)
        parent.replaceInput(ordinalInParent, copy);
}
 
Example 18
Source File: OLAPRel.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
public void fixSharedOlapTableScanAt(RelNode parent, int ordinalInParent) {
    OLAPTableScan copy = copyTableScanIfNeeded(parent.getInputs().get(ordinalInParent));
    if (copy != null)
        parent.replaceInput(ordinalInParent, copy);
}