Inline agg

This is part 1: Serial Inline aggregation contain table agg and group by agg with index.

Waiting for Jenkins.

I understand that this code was just copied from somewhere else, but I'm pretty sure that it is broken.
What it is trying to do is prevent a limit from being applied before a (top) agg in any case where there is no distributed agg to produce groups in some predictable order -- in other words, in any case when an unknown number of rows must be sent to complete the top N groups. 
The existence of an AGG(DISTINCT ...) USUALLY has this effect because there is no efficient way for partial distinct aggs to be re-aggregated by the top agg. But this does not apply to "group by partition key" cases because the top agg receives complete groups with no re-agg required -- so this is a missed optimization. 
More serious is that this loop only looks at top-level display column expressions and will totally miss "SELECT COUNT(DISTINCT X) + 1" so it looks like we will get a pushed down limit on non-grouped rows that we do not want in this case.