Source:
public Result apply(AggregationNode aggregation, Captures captures, Context context) { Assignments.Builder newAssignments = Assignments.builder(); ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder(); ImmutableList.Builder<Expression> filterExpressions = ImmutableList.builder(); for (Map.Entry<Symbol, Aggregation> entry : aggregation.getAggregations().entrySet()) { Symbol output = entry.getKey(); FunctionCall call = entry.getValue().getCall(); Optional<Symbol> mask = entry.getValue().getMask(); if (call.getFilter().isPresent()) { Expression filter = call.getFilter().get(); Symbol symbol = context.getSymbolAllocator().newSymbol(filter, BOOLEAN); verify(!mask.isPresent(), "Expected aggregation without mask symbols, see Rule pattern"); newAssignments.put(symbol, filter); mask = Optional.of(symbol); filterExpressions.add(filter); } aggregations.put(output, new Aggregation( new FunctionCall(call.getName(), call.getWindow(), Optional.empty(), call.getOrderBy(), call.isDistinct(), call.getArguments()), entry.getValue().getSignature(), mask)); } newAssignments.putIdentities(aggregation.getSource().getOutputSymbols()); ProjectNode projectNode = new ProjectNode( context.getIdAllocator().getNextId(), aggregation.getSource(), newAssignments.build()); return Result.ofPlanNode( new AggregationNode( context.getIdAllocator().getNextId(), new FilterNode( context.getIdAllocator().getNextId(), projectNode, combineDisjuncts(filterExpressions.build())), aggregations.build(), aggregation.getGroupingSets(), ImmutableList.of(), aggregation.getStep(), aggregation.getHashSymbol(), aggregation.getGroupIdSymbol())); }

Target:
public Result apply(AggregationNode aggregation, Captures captures, Context context) { Assignments.Builder newAssignments = Assignments.builder(); ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder(); ImmutableList.Builder<Expression> maskSymbols = ImmutableList.builder(); boolean aggregateWithoutFilterPresent = false; for (Map.Entry<Symbol, Aggregation> entry : aggregation.getAggregations().entrySet()) { Symbol output = entry.getKey(); FunctionCall call = entry.getValue().getCall(); Optional<Symbol> mask = entry.getValue().getMask(); if (call.getFilter().isPresent()) { Expression filter = call.getFilter().get(); Symbol symbol = context.getSymbolAllocator().newSymbol(filter, BOOLEAN); verify(!mask.isPresent(), "Expected aggregation without mask symbols, see Rule pattern"); newAssignments.put(symbol, filter); mask = Optional.of(symbol); maskSymbols.add(symbol.toSymbolReference()); } else { aggregateWithoutFilterPresent = true; } aggregations.put(output, new Aggregation( new FunctionCall(call.getName(), call.getWindow(), Optional.empty(), call.getOrderBy(), call.isDistinct(), call.getArguments()), entry.getValue().getSignature(), mask)); } Expression predicate = TRUE_LITERAL; if (!aggregation.hasNonEmptyGroupingSet() && !aggregateWithoutFilterPresent) { predicate = combineDisjunctsWithDefault(maskSymbols.build(), TRUE_LITERAL); } newAssignments.putIdentities(aggregation.getSource().getOutputSymbols()); return Result.ofPlanNode( new AggregationNode( context.getIdAllocator().getNextId(), new FilterNode( context.getIdAllocator().getNextId(), new ProjectNode( context.getIdAllocator().getNextId(), aggregation.getSource(), newAssignments.build()), predicate), aggregations.build(), aggregation.getGroupingSets(), ImmutableList.of(), aggregation.getStep(), aggregation.getHashSymbol(), aggregation.getGroupIdSymbol())); }