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> 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 (entry.getValue().getFilter().isPresent()) { Expression filter = entry.getValue().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().getFunctionHandle(), 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()), castToRowExpression(predicate)), 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<VariableReferenceExpression, Aggregation> aggregations = ImmutableMap.builder(); ImmutableList.Builder<Expression> maskSymbols = ImmutableList.builder(); boolean aggregateWithoutFilterPresent = false; for (Map.Entry<VariableReferenceExpression, Aggregation> entry : aggregation.getAggregations().entrySet()) { VariableReferenceExpression output = entry.getKey(); Optional<VariableReferenceExpression> mask = entry.getValue().getMask(); if (entry.getValue().getFilter().isPresent()) { Expression filter = OriginalExpressionUtils.castToExpression(entry.getValue().getFilter().get()); VariableReferenceExpression variable = context.getSymbolAllocator().newVariable(filter, BOOLEAN); verify(!mask.isPresent(), "Expected aggregation without mask symbols, see Rule pattern"); newAssignments.put(variable, filter); mask = Optional.of(variable); maskSymbols.add(new SymbolReference(variable.getName())); } else { aggregateWithoutFilterPresent = true; } aggregations.put(output, new Aggregation( entry.getValue().getCall(), Optional.empty(), entry.getValue().getOrderBy(), entry.getValue().isDistinct(), mask)); } Expression predicate = TRUE_LITERAL; if (!aggregation.hasNonEmptyGroupingSet() && !aggregateWithoutFilterPresent) { predicate = combineDisjunctsWithDefault(maskSymbols.build(), TRUE_LITERAL); } newAssignments.putIdentities(aggregation.getSource().getOutputVariables()); return Result.ofPlanNode( new AggregationNode( context.getIdAllocator().getNextId(), new FilterNode( context.getIdAllocator().getNextId(), new ProjectNode( context.getIdAllocator().getNextId(), aggregation.getSource(), newAssignments.build()), castToRowExpression(predicate)), aggregations.build(), aggregation.getGroupingSets(), ImmutableList.of(), aggregation.getStep(), aggregation.getHashVariable(), aggregation.getGroupIdVariable())); }