presto-main/src/main/java/io/prestosql/operator/aggregation/builder/MergingHashAggregationBuilder.java/MergingHashAggregationBuilder/buildResult
class MergingHashAggregationBuilder:    public WorkProcessor<Page> buildResult()
    {
        return sortedPages.flatTransform(new Transformation<Page, WorkProcessor<Page>>()
        {
            boolean reset = true;
            long memorySize;

            @Override
            public TransformationState<WorkProcessor<Page>> process(Page inputPage)
            {
                if (reset) {
                    rebuildHashAggregationBuilder();
                    memorySize = 0;
                    reset = false;
                }

                boolean inputFinished = inputPage == null;
                if (inputFinished && memorySize == 0) {
                    // no more pages and aggregation builder is empty
                    return TransformationState.finished();
                }

                if (!inputFinished) {
                    boolean done = hashAggregationBuilder.processPage(inputPage).process();
                    // TODO: this class does not yield wrt memory limit; enable it
                    verify(done);
                    memorySize = hashAggregationBuilder.getSizeInMemory();
                    memoryContext.setBytes(memorySize);

                    if (!shouldProduceOutput(memorySize)) {
                        return TransformationState.needsMoreData();
                    }
                }

                reset = true;
                // we can produce output after every input page, because input pages do not have
                // hash values that span multiple pages (guaranteed by MergeHashSort)
                return TransformationState.ofResult(hashAggregationBuilder.buildResult(), !inputFinished);
            }
        });
    }