Skip to content

Commit 060d9cb

Browse files
committed
Show stats in EXPLAIN/EXPLAIN ANALYZE with intermediate aggregation
1 parent d34e358 commit 060d9cb

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

core/trino-main/src/main/java/io/trino/cost/AggregationStatsRule.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public Pattern<AggregationNode> getPattern()
4949
@Override
5050
protected Optional<PlanNodeStatsEstimate> doCalculate(AggregationNode node, Context context)
5151
{
52-
if (node.getGroupingSetCount() != 1 || node.getStep() == INTERMEDIATE) {
52+
if (node.getGroupingSetCount() != 1) {
5353
return Optional.empty();
5454
}
5555

5656
PlanNodeStatsEstimate estimate;
5757

58-
if (node.getStep() == PARTIAL) {
58+
if (node.getStep() == PARTIAL || node.getStep() == INTERMEDIATE) {
5959
estimate = partialGroupBy(context.statsProvider().getStats(node.getSource()),
6060
node.getGroupingKeys(),
6161
node.getAggregations());
@@ -101,7 +101,8 @@ public static double getRowsCount(PlanNodeStatsEstimate sourceStats, Collection<
101101

102102
private static PlanNodeStatsEstimate partialGroupBy(PlanNodeStatsEstimate sourceStats, Collection<Symbol> groupBySymbols, Map<Symbol, Aggregation> aggregations)
103103
{
104-
// Pessimistic assumption of no reduction from PARTIAL aggregation, forwarding of the source statistics. This makes the CBO estimates in the EXPLAIN plan output easier to understand,
104+
// Pessimistic assumption of no reduction from PARTIAL and INTERMEDIATE aggregation, forwarding of the source statistics.
105+
// This makes the CBO estimates in the EXPLAIN plan output easier to understand,
105106
// even though partial aggregations are added after the CBO rules have been run.
106107
PlanNodeStatsEstimate.Builder result = PlanNodeStatsEstimate.builder();
107108
result.setOutputRowCount(sourceStats.getOutputRowCount());

core/trino-main/src/test/java/io/trino/cost/TestAggregationStatsRule.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,46 @@ public void testAggregationWithMoreGroupingSets()
185185
.build())
186186
.check(PlanNodeStatsAssertion::outputRowsCountUnknown);
187187
}
188+
189+
@Test
190+
void testAggregationStep()
191+
{
192+
testAggregationStep(AggregationNode.Step.PARTIAL);
193+
testAggregationStep(AggregationNode.Step.INTERMEDIATE);
194+
testAggregationStep(AggregationNode.Step.FINAL);
195+
testAggregationStep(AggregationNode.Step.SINGLE);
196+
}
197+
198+
void testAggregationStep(AggregationNode.Step step)
199+
{
200+
tester().assertStatsFor(pb -> pb
201+
.aggregation(ab -> ab
202+
.step(step)
203+
.addAggregation(pb.symbol("sum", BIGINT), aggregation("sum", ImmutableList.of(new Reference(BIGINT, "x"))), ImmutableList.of(BIGINT))
204+
.singleGroupingSet(pb.symbol("y", BIGINT), pb.symbol("z", BIGINT))
205+
.source(pb.values(pb.symbol("y", BIGINT), pb.symbol("z", BIGINT)))))
206+
.withSourceStats(PlanNodeStatsEstimate.builder()
207+
.setOutputRowCount(100)
208+
.addSymbolStatistics(new Symbol(BIGINT, "y"), SymbolStatsEstimate.builder()
209+
.setLowValue(1)
210+
.setHighValue(10)
211+
.setDistinctValuesCount(5)
212+
.setNullsFraction(0.3)
213+
.build())
214+
.addSymbolStatistics(new Symbol(BIGINT, "z"), SymbolStatsEstimate.builder()
215+
.setLowValue(0)
216+
.setHighValue(3)
217+
.setDistinctValuesCount(3)
218+
.setNullsFraction(0)
219+
.build())
220+
.build())
221+
.check(check -> {
222+
if (step.isOutputPartial()) {
223+
check.outputRowsCount(100);
224+
}
225+
else {
226+
check.outputRowsCount(18);
227+
}
228+
});
229+
}
188230
}

0 commit comments

Comments
 (0)