/
GraphvizPrinter.java
573 lines (495 loc) · 21.1 KB
/
GraphvizPrinter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.util;
import com.facebook.presto.sql.planner.PlanFragment;
import com.facebook.presto.sql.planner.SubPlan;
import com.facebook.presto.sql.planner.Symbol;
import com.facebook.presto.sql.planner.plan.AggregationNode;
import com.facebook.presto.sql.planner.plan.DistinctLimitNode;
import com.facebook.presto.sql.planner.plan.ExchangeNode;
import com.facebook.presto.sql.planner.plan.FilterNode;
import com.facebook.presto.sql.planner.plan.IndexJoinNode;
import com.facebook.presto.sql.planner.plan.IndexSourceNode;
import com.facebook.presto.sql.planner.plan.JoinNode;
import com.facebook.presto.sql.planner.plan.LimitNode;
import com.facebook.presto.sql.planner.plan.MarkDistinctNode;
import com.facebook.presto.sql.planner.plan.OutputNode;
import com.facebook.presto.sql.planner.plan.PlanFragmentId;
import com.facebook.presto.sql.planner.plan.PlanNode;
import com.facebook.presto.sql.planner.plan.PlanVisitor;
import com.facebook.presto.sql.planner.plan.ProjectNode;
import com.facebook.presto.sql.planner.plan.RowNumberNode;
import com.facebook.presto.sql.planner.plan.SampleNode;
import com.facebook.presto.sql.planner.plan.SemiJoinNode;
import com.facebook.presto.sql.planner.plan.SinkNode;
import com.facebook.presto.sql.planner.plan.SortNode;
import com.facebook.presto.sql.planner.plan.TableScanNode;
import com.facebook.presto.sql.planner.plan.TopNNode;
import com.facebook.presto.sql.planner.plan.TopNRowNumberNode;
import com.facebook.presto.sql.planner.plan.UnionNode;
import com.facebook.presto.sql.planner.plan.UnnestNode;
import com.facebook.presto.sql.planner.plan.ValuesNode;
import com.facebook.presto.sql.planner.plan.WindowNode;
import com.facebook.presto.sql.tree.ComparisonExpression;
import com.facebook.presto.sql.tree.Expression;
import com.facebook.presto.sql.tree.FunctionCall;
import com.facebook.presto.sql.tree.QualifiedNameReference;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static com.google.common.collect.Maps.immutableEnumMap;
import static java.lang.String.format;
public final class GraphvizPrinter
{
private enum NodeType
{
EXCHANGE,
AGGREGATE,
FILTER,
PROJECT,
TOPN,
OUTPUT,
LIMIT,
TABLESCAN,
VALUES,
JOIN,
SINK,
WINDOW,
UNION,
SORT,
SAMPLE,
MARK_DISTINCT,
MATERIALIZE_SAMPLE,
INDEX_SOURCE,
UNNEST
}
private static final Map<NodeType, String> NODE_COLORS = immutableEnumMap(ImmutableMap.<NodeType, String>builder()
.put(NodeType.EXCHANGE, "gold")
.put(NodeType.AGGREGATE, "chartreuse3")
.put(NodeType.FILTER, "yellow")
.put(NodeType.PROJECT, "bisque")
.put(NodeType.TOPN, "darksalmon")
.put(NodeType.OUTPUT, "white")
.put(NodeType.LIMIT, "gray83")
.put(NodeType.TABLESCAN, "deepskyblue")
.put(NodeType.VALUES, "deepskyblue")
.put(NodeType.JOIN, "orange")
.put(NodeType.SORT, "aliceblue")
.put(NodeType.SINK, "indianred1")
.put(NodeType.WINDOW, "darkolivegreen4")
.put(NodeType.UNION, "turquoise4")
.put(NodeType.MARK_DISTINCT, "violet")
.put(NodeType.MATERIALIZE_SAMPLE, "hotpink")
.put(NodeType.INDEX_SOURCE, "dodgerblue3")
.put(NodeType.UNNEST, "crimson")
.put(NodeType.SAMPLE, "goldenrod4")
.build());
static {
Preconditions.checkState(NODE_COLORS.size() == NodeType.values().length);
}
private GraphvizPrinter() {}
public static String printLogical(List<PlanFragment> fragments)
{
Map<PlanFragmentId, PlanFragment> fragmentsById = Maps.uniqueIndex(fragments, new Function<PlanFragment, PlanFragmentId>()
{
@Override
public PlanFragmentId apply(PlanFragment input)
{
return input.getId();
}
});
PlanNodeIdGenerator idGenerator = new PlanNodeIdGenerator();
StringBuilder output = new StringBuilder();
output.append("digraph logical_plan {\n");
for (PlanFragment fragment : fragments) {
printFragmentNodes(output, fragment, idGenerator);
}
for (PlanFragment fragment : fragments) {
fragment.getRoot().accept(new EdgePrinter(output, fragmentsById, idGenerator), null);
}
output.append("}\n");
return output.toString();
}
public static String printDistributed(SubPlan plan)
{
List<PlanFragment> fragments = plan.getAllFragments();
Map<PlanFragmentId, PlanFragment> fragmentsById = Maps.uniqueIndex(fragments, PlanFragment.idGetter());
PlanNodeIdGenerator idGenerator = new PlanNodeIdGenerator();
StringBuilder output = new StringBuilder();
output.append("digraph distributed_plan {\n");
printSubPlan(plan, fragmentsById, idGenerator, output);
output.append("}\n");
return output.toString();
}
private static void printSubPlan(SubPlan plan, Map<PlanFragmentId, PlanFragment> fragmentsById, PlanNodeIdGenerator idGenerator, StringBuilder output)
{
PlanFragment fragment = plan.getFragment();
printFragmentNodes(output, fragment, idGenerator);
fragment.getRoot().accept(new EdgePrinter(output, fragmentsById, idGenerator), null);
for (SubPlan child : plan.getChildren()) {
printSubPlan(child, fragmentsById, idGenerator, output);
}
}
private static void printFragmentNodes(StringBuilder output, PlanFragment fragment, PlanNodeIdGenerator idGenerator)
{
String clusterId = "cluster_" + fragment.getId();
output.append("subgraph ")
.append(clusterId)
.append(" {")
.append('\n');
output.append(format("label = \"%s\"", fragment.getDistribution()))
.append('\n');
PlanNode plan = fragment.getRoot();
plan.accept(new NodePrinter(output, idGenerator), null);
output.append("}")
.append('\n');
}
private static class NodePrinter
extends PlanVisitor<Void, Void>
{
private static final int MAX_NAME_WIDTH = 100;
private final StringBuilder output;
private final PlanNodeIdGenerator idGenerator;
public NodePrinter(StringBuilder output, PlanNodeIdGenerator idGenerator)
{
this.output = output;
this.idGenerator = idGenerator;
}
@Override
protected Void visitPlan(PlanNode node, Void context)
{
throw new UnsupportedOperationException(format("Node %s does not have a Graphviz visitor", node.getClass().getName()));
}
@Override
public Void visitSample(SampleNode node, Void context)
{
printNode(node, format("Sample[type=%s, ratio=%f, rescaled=%s]", node.getSampleType(), node.getSampleRatio(), node.isRescaled()), NODE_COLORS.get(NodeType.SAMPLE));
return node.getSource().accept(this, context);
}
@Override
public Void visitSort(SortNode node, Void context)
{
printNode(node, format("Sort[%s]", Joiner.on(", ").join(node.getOrderBy())), NODE_COLORS.get(NodeType.SORT));
return node.getSource().accept(this, context);
}
@Override
public Void visitMarkDistinct(MarkDistinctNode node, Void context)
{
printNode(node, format("MarkDistinct[%s]", node.getMarkerSymbol()), format("%s => %s", node.getDistinctSymbols(), node.getMarkerSymbol()), NODE_COLORS.get(NodeType.MARK_DISTINCT));
return node.getSource().accept(this, context);
}
@Override
public Void visitSink(SinkNode node, Void context)
{
printNode(node, "Sink", NODE_COLORS.get(NodeType.SINK));
return node.getSource().accept(this, context);
}
@Override
public Void visitWindow(WindowNode node, Void context)
{
printNode(node, "Window", format("partition by = %s|order by = %s", Joiner.on(", ").join(node.getPartitionBy()), Joiner.on(", ").join(node.getOrderBy())), NODE_COLORS.get(NodeType.WINDOW));
return node.getSource().accept(this, context);
}
@Override
public Void visitRowNumber(RowNumberNode node, Void context)
{
printNode(node,
"RowNumber",
format("partition by = %s", Joiner.on(", ").join(node.getPartitionBy())),
NODE_COLORS.get(NodeType.WINDOW));
return node.getSource().accept(this, context);
}
@Override
public Void visitTopNRowNumber(TopNRowNumberNode node, Void context)
{
printNode(node,
"TopNRowNumber",
format("partition by = %s|order by = %s|n = %s", Joiner.on(", ").join(node.getPartitionBy()), Joiner.on(", ").join(node.getOrderBy()), node.getMaxRowCountPerPartition()),
NODE_COLORS.get(NodeType.WINDOW));
return node.getSource().accept(this, context);
}
@Override
public Void visitUnion(UnionNode node, Void context)
{
printNode(node, "Union", NODE_COLORS.get(NodeType.UNION));
for (PlanNode planNode : node.getSources()) {
planNode.accept(this, context);
}
return null;
}
@Override
public Void visitExchange(ExchangeNode node, Void context)
{
printNode(node, "Exchange 1:N", NODE_COLORS.get(NodeType.EXCHANGE));
return null;
}
@Override
public Void visitAggregation(AggregationNode node, Void context)
{
StringBuilder builder = new StringBuilder();
for (Map.Entry<Symbol, FunctionCall> entry : node.getAggregations().entrySet()) {
if (node.getMasks().containsKey(entry.getKey())) {
builder.append(format("%s := %s (mask = %s)\\n", entry.getKey(), entry.getValue(), node.getMasks().get(entry.getKey())));
}
else {
builder.append(format("%s := %s\\n", entry.getKey(), entry.getValue()));
}
}
printNode(node, format("Aggregate[%s]", node.getStep()), builder.toString(), NODE_COLORS.get(NodeType.AGGREGATE));
return node.getSource().accept(this, context);
}
@Override
public Void visitFilter(FilterNode node, Void context)
{
String expression = node.getPredicate().toString();
printNode(node, "Filter", expression, NODE_COLORS.get(NodeType.FILTER));
return node.getSource().accept(this, context);
}
@Override
public Void visitProject(ProjectNode node, Void context)
{
StringBuilder builder = new StringBuilder();
for (Map.Entry<Symbol, Expression> entry : node.getOutputMap().entrySet()) {
if ((entry.getValue() instanceof QualifiedNameReference) &&
((QualifiedNameReference) entry.getValue()).getName().equals(entry.getKey().toQualifiedName())) {
// skip identity assignments
continue;
}
builder.append(format("%s := %s\\n", entry.getKey(), entry.getValue()));
}
printNode(node, "Project", builder.toString(), NODE_COLORS.get(NodeType.PROJECT));
return node.getSource().accept(this, context);
}
@Override
public Void visitUnnest(UnnestNode node, Void context)
{
printNode(node, format("Unnest[%s]", node.getUnnestSymbols().keySet()), NODE_COLORS.get(NodeType.UNNEST));
return node.getSource().accept(this, context);
}
@Override
public Void visitTopN(final TopNNode node, Void context)
{
Iterable<String> keys = Iterables.transform(node.getOrderBy(), new Function<Symbol, String>()
{
@Override
public String apply(Symbol input)
{
return input + " " + node.getOrderings().get(input);
}
});
printNode(node, format("TopN[%s]", node.getCount()), Joiner.on(", ").join(keys), NODE_COLORS.get(NodeType.TOPN));
return node.getSource().accept(this, context);
}
@Override
public Void visitOutput(OutputNode node, Void context)
{
String columns = getColumns(node);
printNode(node, format("Output[%s]", columns), NODE_COLORS.get(NodeType.OUTPUT));
return node.getSource().accept(this, context);
}
@Override
public Void visitDistinctLimit(DistinctLimitNode node, Void context)
{
printNode(node, format("DistinctLimit[%s]", node.getLimit()), NODE_COLORS.get(NodeType.LIMIT));
return node.getSource().accept(this, context);
}
@Override
public Void visitLimit(LimitNode node, Void context)
{
printNode(node, format("Limit[%s]", node.getCount()), NODE_COLORS.get(NodeType.LIMIT));
return node.getSource().accept(this, context);
}
@Override
public Void visitTableScan(TableScanNode node, Void context)
{
printNode(node, format("TableScan[%s]", node.getTable()), format("original constraint=%s", node.getOriginalConstraint()), NODE_COLORS.get(NodeType.TABLESCAN));
return null;
}
@Override
public Void visitValues(ValuesNode node, Void context)
{
printNode(node, "Values", NODE_COLORS.get(NodeType.TABLESCAN));
return null;
}
@Override
public Void visitJoin(JoinNode node, Void context)
{
List<Expression> joinExpressions = new ArrayList<>();
for (JoinNode.EquiJoinClause clause : node.getCriteria()) {
joinExpressions.add(new ComparisonExpression(ComparisonExpression.Type.EQUAL,
new QualifiedNameReference(clause.getLeft().toQualifiedName()),
new QualifiedNameReference(clause.getRight().toQualifiedName())));
}
String criteria = Joiner.on(" AND ").join(joinExpressions);
printNode(node, node.getType().getJoinLabel(), criteria, NODE_COLORS.get(NodeType.JOIN));
node.getLeft().accept(this, context);
node.getRight().accept(this, context);
return null;
}
@Override
public Void visitSemiJoin(SemiJoinNode node, Void context)
{
printNode(node, "SemiJoin", format("%s = %s", node.getSourceJoinSymbol(), node.getFilteringSourceJoinSymbol()), NODE_COLORS.get(NodeType.JOIN));
node.getSource().accept(this, context);
node.getFilteringSource().accept(this, context);
return null;
}
@Override
public Void visitIndexSource(IndexSourceNode node, Void context)
{
printNode(node, format("IndexSource[%s]", node.getIndexHandle()), NODE_COLORS.get(NodeType.INDEX_SOURCE));
return null;
}
@Override
public Void visitIndexJoin(IndexJoinNode node, Void context)
{
List<Expression> joinExpressions = new ArrayList<>();
for (IndexJoinNode.EquiJoinClause clause : node.getCriteria()) {
joinExpressions.add(new ComparisonExpression(ComparisonExpression.Type.EQUAL,
new QualifiedNameReference(clause.getProbe().toQualifiedName()),
new QualifiedNameReference(clause.getIndex().toQualifiedName())));
}
String criteria = Joiner.on(" AND ").join(joinExpressions);
String joinLabel = format("%sIndexJoin", node.getType().getJoinLabel());
printNode(node, joinLabel, criteria, NODE_COLORS.get(NodeType.JOIN));
node.getProbeSource().accept(this, context);
node.getIndexSource().accept(this, context);
return null;
}
private void printNode(PlanNode node, String label, String color)
{
String nodeId = idGenerator.getNodeId(node);
label = escapeSpecialCharacters(label);
output.append(nodeId)
.append(format("[label=\"{%s}\", style=\"rounded, filled\", shape=record, fillcolor=%s]", label, color))
.append(';')
.append('\n');
}
private void printNode(PlanNode node, String label, String details, String color)
{
if (details.isEmpty()) {
printNode(node, label, color);
}
else {
String nodeId = idGenerator.getNodeId(node);
label = escapeSpecialCharacters(label);
details = escapeSpecialCharacters(details);
output.append(nodeId)
.append(format("[label=\"{%s|%s}\", style=\"rounded, filled\", shape=record, fillcolor=%s]", label, details, color))
.append(';')
.append('\n');
}
}
private static String getColumns(OutputNode node)
{
Iterator<String> columnNames = node.getColumnNames().iterator();
String columns = "";
int nameWidth = 0;
while (columnNames.hasNext()) {
String columnName = columnNames.next();
columns += columnName;
nameWidth += columnName.length();
if (columnNames.hasNext()) {
columns += ", ";
}
if (nameWidth >= MAX_NAME_WIDTH) {
columns += "\\n";
nameWidth = 0;
}
}
return columns;
}
/**
* Escape characters that are special to graphviz.
*/
private static String escapeSpecialCharacters(String label)
{
return label
.replace("<", "\\<")
.replace(">", "\\>")
.replace("\"", "\\\"");
}
}
private static class EdgePrinter
extends PlanVisitor<Void, Void>
{
private final StringBuilder output;
private final Map<PlanFragmentId, PlanFragment> fragmentsById;
private final PlanNodeIdGenerator idGenerator;
public EdgePrinter(StringBuilder output, Map<PlanFragmentId, PlanFragment> fragmentsById, PlanNodeIdGenerator idGenerator)
{
this.output = output;
this.fragmentsById = ImmutableMap.copyOf(fragmentsById);
this.idGenerator = idGenerator;
}
@Override
protected Void visitPlan(PlanNode node, Void context)
{
for (PlanNode child : node.getSources()) {
printEdge(node, child);
child.accept(this, context);
}
return null;
}
@Override
public Void visitExchange(ExchangeNode node, Void context)
{
for (PlanFragmentId planFragmentId : node.getSourceFragmentIds()) {
PlanFragment target = fragmentsById.get(planFragmentId);
printEdge(node, target.getRoot());
}
return null;
}
private void printEdge(PlanNode from, PlanNode to)
{
String fromId = idGenerator.getNodeId(from);
String toId = idGenerator.getNodeId(to);
output.append(fromId)
.append(" -> ")
.append(toId)
.append(';')
.append('\n');
}
}
private static class PlanNodeIdGenerator
{
private final Map<PlanNode, Integer> planNodeIds;
private int idCount;
public PlanNodeIdGenerator()
{
planNodeIds = new HashMap<>();
}
public String getNodeId(PlanNode from)
{
int nodeId;
if (planNodeIds.containsKey(from)) {
nodeId = planNodeIds.get(from);
}
else {
idCount++;
planNodeIds.put(from, idCount);
nodeId = idCount;
}
return ("plannode_" + nodeId);
}
}
}