Source:
private ProjectNode createProjectNode( AggregationNode source, AggregateInfo aggregateInfo, VariableReferenceExpression distinctVariable, VariableReferenceExpression groupVariable, List<VariableReferenceExpression> groupByVariables, Map<VariableReferenceExpression, VariableReferenceExpression> aggregationOutputVariablesMap) { Assignments.Builder outputVariables = Assignments.builder(); ImmutableMap.Builder<VariableReferenceExpression, VariableReferenceExpression> outputNonDistinctAggregateVariables = ImmutableMap.builder(); for (VariableReferenceExpression variable : source.getOutputVariables()) { if (distinctVariable.equals(variable)) { VariableReferenceExpression newVariable = variableAllocator.newVariable("expr", variable.getType()); aggregateInfo.setNewDistinctAggregateSymbol(newVariable); SpecialFormExpression ifExpression = new SpecialFormExpression( IF, variable.getType(), ImmutableList.of( new CallExpression( EQUAL.name(), functionResolution.comparisonFunction(EQUAL, BIGINT, BIGINT), BOOLEAN, ImmutableList.of(groupVariable, new ConstantExpression(Long.valueOf(1), BIGINT))), variable, constantNull(variable.getType()))); outputVariables.put(newVariable, ifExpression); } else if (aggregationOutputVariablesMap.containsKey(variable)) { VariableReferenceExpression newVariable = variableAllocator.newVariable("expr", variable.getType()); outputNonDistinctAggregateVariables.put(aggregationOutputVariablesMap.get(variable), newVariable); SpecialFormExpression ifExpression = new SpecialFormExpression( IF, variable.getType(), ImmutableList.of( new CallExpression( EQUAL.name(), functionResolution.comparisonFunction(EQUAL, BIGINT, BIGINT), BOOLEAN, ImmutableList.of(groupVariable, new ConstantExpression(Long.valueOf(0), BIGINT))), variable, constantNull(variable.getType()))); outputVariables.put(newVariable, ifExpression); } if (groupByVariables.contains(variable)) { outputVariables.put(variable, variable); } } outputVariables.put(aggregateInfo.getMask(), constantNull(aggregateInfo.getMask().getType())); aggregateInfo.setNewNonDistinctAggregateSymbols(outputNonDistinctAggregateVariables.build()); return new ProjectNode(idAllocator.getNextId(), source, outputVariables.build()); }

Target:
private ProjectNode createProjectNode( AggregationNode source, AggregateInfo aggregateInfo, VariableReferenceExpression distinctVariable, VariableReferenceExpression groupVariable, List<VariableReferenceExpression> groupByVariables, Map<VariableReferenceExpression, VariableReferenceExpression> aggregationOutputVariablesMap) { Assignments.Builder outputVariables = Assignments.builder(); ImmutableMap.Builder<VariableReferenceExpression, VariableReferenceExpression> outputNonDistinctAggregateVariables = ImmutableMap.builder(); for (VariableReferenceExpression variable : source.getOutputVariables()) { if (distinctVariable.equals(variable)) { VariableReferenceExpression newVariable = variableAllocator.newVariable("expr", variable.getType()); aggregateInfo.setNewDistinctAggregateSymbol(newVariable); RowExpression ifExpression = new SpecialFormExpression( IF, variable.getType(), ImmutableList.of( call( EQUAL.name(), functionResolution.comparisonFunction(EQUAL, BIGINT, BIGINT), BOOLEAN, ImmutableList.of(groupVariable, constant(1L, BIGINT))), variable, constantNull(variable.getType()))); outputVariables.put(newVariable, ifExpression); } else if (aggregationOutputVariablesMap.containsKey(variable)) { VariableReferenceExpression newVariable = variableAllocator.newVariable("expr", variable.getType()); outputNonDistinctAggregateVariables.put(aggregationOutputVariablesMap.get(variable), newVariable); RowExpression ifExpression = new SpecialFormExpression( IF, variable.getType(), ImmutableList.of( call( EQUAL.name(), functionResolution.comparisonFunction(EQUAL, BIGINT, BIGINT), BOOLEAN, ImmutableList.of(groupVariable, constant(0L, BIGINT))), variable, constantNull(variable.getType()))); outputVariables.put(newVariable, ifExpression); } if (groupByVariables.contains(variable)) { outputVariables.put(variable, variable); } } outputVariables.put(aggregateInfo.getMask(), constantNull(aggregateInfo.getMask().getType())); aggregateInfo.setNewNonDistinctAggregateSymbols(outputNonDistinctAggregateVariables.build()); return new ProjectNode(idAllocator.getNextId(), source, outputVariables.build()); }