[aggregation] Move logic from template.