Skip to content

Commit

Permalink
Add exception to recognize cyclic relational graph
Browse files Browse the repository at this point in the history
  • Loading branch information
kramerul committed Dec 11, 2023
1 parent 849a65a commit b758f4a
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/src/main/java/org/apache/calcite/plan/volcano/RelSubset.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ void add(RelNode rel) {
*/
RelNode buildCheapestPlan(VolcanoPlanner planner) {
CheapestPlanReplacer replacer = new CheapestPlanReplacer(planner);
final RelNode cheapest = replacer.visit(this, -1, null);
final RelNode cheapest = replacer.visit(this, -1);

if (planner.getListener() != null) {
RelOptListener.RelChosenEvent event =
Expand Down Expand Up @@ -621,17 +621,22 @@ private static String traitDiff(RelTraitSet original, RelTraitSet desired) {
.collect(Collectors.joining(", ", "[", "]"));
}

public RelNode visit(
RelNode p,
int ordinal,
@Nullable RelNode parent) {
public RelNode visit(RelNode p, int ordinal) {
return visit(p,ordinal,0);
}

private RelNode visit(RelNode p, int ordinal, int depth) {
final int pId = p.getId();
RelNode prevVisit = visited.get(pId);
if (prevVisit != null) {
// return memoized result of previous visit if available
return prevVisit;
}

if ( depth > 256) {
throw new RuntimeException("Cyclic relation graph detected at " + p);
}

if (p instanceof RelSubset) {
RelSubset subset = (RelSubset) p;
RelNode cheapest = subset.best;
Expand Down Expand Up @@ -737,7 +742,7 @@ public RelNode visit(
List<RelNode> inputs = new ArrayList<>();
for (int i = 0; i < oldInputs.size(); i++) {
RelNode oldInput = oldInputs.get(i);
RelNode input = visit(oldInput, i, p);
RelNode input = visit(oldInput, i, depth+1);
inputs.add(input);
}
if (!inputs.equals(oldInputs)) {
Expand Down

0 comments on commit b758f4a

Please sign in to comment.