presto-main/src/main/java/io/prestosql/sql/planner/optimizations/OptimizeMixedDistinctAggregations.java/OptimizeMixedDistinctAggregations/createNonDistinctAggregation
class OptimizeMixedDistinctAggregations:        private AggregationNode createNonDistinctAggregation(
                AggregateInfo aggregateInfo,
                Symbol distinctSymbol,
                Symbol duplicatedDistinctSymbol,
                Set<Symbol> groupByKeys,
                GroupIdNode groupIdNode,
                MarkDistinctNode originalNode,
                ImmutableMap.Builder<Symbol, Symbol> aggregationOutputSymbolsMapBuilder)
        {
            ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder();
            for (Map.Entry<Symbol, Aggregation> entry : aggregateInfo.getAggregations().entrySet()) {
                Aggregation aggregation = entry.getValue();
                if (!aggregation.getMask().isPresent()) {
                    Symbol newSymbol = symbolAllocator.newSymbol(entry.getKey().toSymbolReference(), symbolAllocator.getTypes().get(entry.getKey()));
                    aggregationOutputSymbolsMapBuilder.put(newSymbol, entry.getKey());
                    if (!duplicatedDistinctSymbol.equals(distinctSymbol)) {
                        // Handling for cases when mask symbol appears in non distinct aggregations too
                        // Now the aggregation should happen over the duplicate symbol added before
                        if (aggregation.getArguments().contains(distinctSymbol.toSymbolReference())) {
                            ImmutableList.Builder<Expression> arguments = ImmutableList.builder();
                            for (Expression argument : aggregation.getArguments()) {
                                if (distinctSymbol.toSymbolReference().equals(argument)) {
                                    arguments.add(duplicatedDistinctSymbol.toSymbolReference());
                                }
                                else {
                                    arguments.add(argument);
                                }
                            }
                            aggregation = new Aggregation(
                                    aggregation.getResolvedFunction(),
                                    arguments.build(),
                                    false,
                                    Optional.empty(),
                                    Optional.empty(),
                                    Optional.empty());
                        }
                    }
                    aggregations.put(newSymbol, aggregation);
                }
            }
            return new AggregationNode(
                    idAllocator.getNextId(),
                    groupIdNode,
                    aggregations.build(),
                    singleGroupingSet(ImmutableList.copyOf(groupByKeys)),
                    ImmutableList.of(),
                    SINGLE,
                    originalNode.getHashSymbol(),
                    Optional.empty());
        }