Skip to content

Commit 0782d71

Browse files
committed
Use topological stages ordering in EXPLAIN output
1 parent af814d4 commit 0782d71

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

core/trino-main/src/main/java/io/trino/execution/StagesInfo.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableSet;
2121
import com.google.common.collect.Maps;
2222

23+
import java.util.HashSet;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Optional;
@@ -121,6 +122,32 @@ private void collectSubStageIdsPreOrder(StageInfo stageInfo, ImmutableSet.Builde
121122
});
122123
}
123124

125+
@JsonIgnore
126+
public List<StageInfo> getSubStagesDeepTopological(StageId root, boolean includeRoot)
127+
{
128+
ImmutableList.Builder<StageInfo> builder = ImmutableList.builder();
129+
getSubStagesDeepTopologicalInner(root, builder, new HashSet<>(), includeRoot);
130+
131+
return builder.build().reverse();
132+
}
133+
134+
private void getSubStagesDeepTopologicalInner(StageId stageId, ImmutableList.Builder<StageInfo> builder, Set<StageId> visitedFragments, boolean includeRoot)
135+
{
136+
if (visitedFragments.contains(stageId)) {
137+
return;
138+
}
139+
140+
StageInfo stageInfo = stagesById.get(stageId);
141+
142+
for (StageId childId : stageInfo.getSubStages().reversed()) {
143+
getSubStagesDeepTopologicalInner(childId, builder, visitedFragments, true);
144+
}
145+
if (includeRoot) {
146+
builder.add(stageInfo);
147+
}
148+
visitedFragments.add(stageId);
149+
}
150+
124151
public static List<StageInfo> getAllStages(Optional<StagesInfo> stages)
125152
{
126153
return stages.map(StagesInfo::getStages).orElse(ImmutableList.of());

core/trino-main/src/main/java/io/trino/operator/ExplainAnalyzeOperator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.concurrent.TimeUnit;
2929

3030
import static com.google.common.base.Preconditions.checkState;
31-
import static com.google.common.collect.ImmutableList.toImmutableList;
3231
import static io.trino.spi.type.VarcharType.VARCHAR;
3332
import static io.trino.sql.planner.planprinter.PlanPrinter.textDistributedPlan;
3433
import static java.util.Objects.requireNonNull;
@@ -153,15 +152,14 @@ public Page getOutput()
153152

154153
QueryInfo queryInfo = queryPerformanceFetcher.getQueryInfo(operatorContext.getDriverContext().getTaskId().queryId());
155154
checkState(queryInfo.getStages().isPresent(), "Stages informations is missing");
156-
checkState(queryInfo.getStages().get().getOutputStage().getSubStages().size() == 1, "Expected one sub stage of explain node");
155+
StagesInfo stagesInfo = queryInfo.getStages().get();
156+
checkState(stagesInfo.getOutputStage().getSubStages().size() == 1, "Expected one sub stage of explain node");
157157

158-
if (!hasFinalStageInfo(queryInfo.getStages().get())) {
158+
if (!hasFinalStageInfo(stagesInfo)) {
159159
return null;
160160
}
161161

162-
List<StageInfo> stagesWithoutOutputStage = queryInfo.getStages().orElseThrow().getStages().stream()
163-
.filter(stage -> !stage.getStageId().equals(queryInfo.getStages().orElseThrow().getOutputStageId()))
164-
.collect(toImmutableList());
162+
List<StageInfo> stagesWithoutOutputStage = stagesInfo.getSubStagesDeepTopological(stagesInfo.getOutputStageId(), false);
165163

166164
String plan = textDistributedPlan(
167165
stagesWithoutOutputStage,

core/trino-main/src/main/java/io/trino/sql/planner/planprinter/PlanPrinter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ public static String textDistributedPlan(
428428
Anonymizer anonymizer,
429429
NodeVersion version)
430430
{
431-
return textDistributedPlan(stages.getStages(), queryStats, valuePrinter, verbose, anonymizer, version);
431+
return textDistributedPlan(stages.getSubStagesDeepTopological(stages.getOutputStageId(), true), queryStats, valuePrinter, verbose, anonymizer, version);
432432
}
433433

434434
public static String textDistributedPlan(

0 commit comments

Comments
 (0)