Skip to content

Commit

Permalink
Add recursive digest helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
wnob committed Nov 3, 2021
1 parent cbe6a7b commit 3c1488d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions core/src/main/java/org/apache/calcite/plan/RelOptNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public interface RelOptNode {
*/
String getDigest();

/**
* Like {@link #getDigest()} but describes this relational expression as well as all of it's
* inputs, with each digest separated by newlines and children indented according to depth.
* This is mainly useful for visual comparisons of rel node trees while debugging.
*
* @return Digest string of this {@code RelNode}, with input digests indented recursively underneath
*/
String getDigestRecursive();

/**
* Retrieves this RelNode's traits. Note that although the RelTraitSet
* returned is modifiable, it <b>must not</b> be modified during
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ public RelNode getCurrentRel() {
@Override public String getDigest() {
return "HepRelVertex(" + currentRel + ')';
}

@Override public String getDigestRecursive() {
// For the purposes of the recursive digest (which is really only intended for debugging),
// pretend vertices don't exist.
return currentRel.getDigestRecursive();
}
}
22 changes: 22 additions & 0 deletions core/src/main/java/org/apache/calcite/rel/RelNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,28 @@ default String explain() {
return getRelDigest().toString();
}

/**
* Returns a digest string of this {@code RelNode}, as well as the digests of all its input rel
* nodes as an indented tree, useful for visual debugging.
*
* <p>Each call creates many new digest strings,
* so don't forget to cache the result if necessary.
*
* @return Digest string of this {@code RelNode} and all it's direct or indirect inputs.
*
* @see #getDigest()
*/
@Override default String getDigestRecursive() {
final String indentedNewline = "\n ";
final StringBuilder builder = new StringBuilder();
builder.append(getDigest());
for (RelNode input: getInputs()) {
builder.append(indentedNewline);
builder.append(input.getDigestRecursive().replace("\n", indentedNewline));
}
return builder.toString();
}

/**
* Returns a digest of this {@code RelNode}.
*
Expand Down

0 comments on commit 3c1488d

Please sign in to comment.