use of org.apache.flink.table.planner.plan.nodes.exec.visitor.ExecNodeVisitorImpl in project flink by apache.
the class JsonPlanGraph method fromExecNodeGraph.
static JsonPlanGraph fromExecNodeGraph(ExecNodeGraph execGraph) {
final List<ExecNode<?>> allNodes = new ArrayList<>();
final List<JsonPlanEdge> allEdges = new ArrayList<>();
final Set<Integer> nodesIds = new HashSet<>();
// for quick search
final Set<ExecNode<?>> visitedNodes = Sets.newIdentityHashSet();
// visit the nodes as topological ordering
final ExecNodeVisitor visitor = new ExecNodeVisitorImpl() {
@Override
public void visit(ExecNode<?> node) {
if (visitedNodes.contains(node)) {
return;
}
super.visitInputs(node);
final int id = node.getId();
if (nodesIds.contains(id)) {
throw new TableException(String.format("The id: %s is not unique for ExecNode: %s.\nplease check it.", id, node.getDescription()));
}
allNodes.add(node);
nodesIds.add(id);
visitedNodes.add(node);
for (ExecEdge execEdge : node.getInputEdges()) {
allEdges.add(JsonPlanEdge.fromExecEdge(execEdge));
}
}
};
execGraph.getRootNodes().forEach(visitor::visit);
checkArgument(allNodes.size() == nodesIds.size());
return new JsonPlanGraph(execGraph.getFlinkVersion(), allNodes, allEdges);
}
use of org.apache.flink.table.planner.plan.nodes.exec.visitor.ExecNodeVisitorImpl in project flink by apache.
the class ExecNodePlanDumper method dagToString.
/**
* Converts an {@link ExecNode} DAG to a string as a tree style.
*
* <p>The following DAG of {@link ExecNode}
*
* <pre>{@code
* Sink1 Sink2
* | |
* Filter3 Filter4
* \ /
* Join
* / \
* Filter1 Filter2
* \ /
* Project
* |
* Scan
* }</pre>
*
* <p>would be converted to the tree style as following:
*
* <pre>{@code
* Join(reuse_id=[2])
* :- Filter1
* : +- Project(reuse_id=[1])
* : +- Scan
* +- Filter2
* +- Reused(reference_id=[1])
*
* Sink1
* +- Filter3
* +- Reused(reference_id=[2])
*
* Sink2
* +- Filter4
* +- Reused(reference_id=[2])
* }</pre>
*
* @param nodes the ExecNodes to convert
* @return the plan of ExecNode
*/
public static String dagToString(List<ExecNode<?>> nodes) {
Preconditions.checkArgument(nodes != null && !nodes.isEmpty(), "nodes should not be null or empty.");
if (nodes.size() == 1) {
return treeToString(nodes.get(0));
}
// nodes that stop visit when meet them
final List<ExecNode<?>> stopVisitNodes = new ArrayList<>();
final StringBuilder sb = new StringBuilder();
final DagReuseInfo reuseInfo = new DagReuseInfo(nodes, new ArrayList<>());
final ExecNodeVisitor visitor = new ExecNodeVisitorImpl() {
@Override
public void visit(ExecNode<?> node) {
int visitedTimes = reuseInfo.addVisitedTimes(node);
boolean isFirstVisit = visitedTimes == 1;
if (isFirstVisit) {
super.visit(node);
}
int reuseId = reuseInfo.getReuseId(node);
boolean isReuseNode = reuseId >= 0;
if (node instanceof CommonExecLegacySink || node instanceof CommonExecSink || (isReuseNode && isFirstVisit)) {
if (isReuseNode) {
reuseInfo.setFirstVisited(node, true);
}
String reusePlan = doConvertTreeToString(node, reuseInfo, false, stopVisitNodes, false);
sb.append(reusePlan).append(System.lineSeparator());
if (isReuseNode) {
// update visit info after the reuse node visited
stopVisitNodes.add(node);
reuseInfo.setFirstVisited(node, false);
}
}
}
};
nodes.forEach(visitor::visit);
if (sb.length() > 0) {
// delete last line separator
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
Aggregations