Program: 169076
Project: 169076
Buggy source:
/** 
 * Method that creates the appropriate nested term aggregation properties to elasticsearch based on those specified by the "group by" clause
 * @throws ExecutionException
 */
private void createNestedTermAggregation(ProjectParsed queryData,Collection<Selector> groupingSelectors) throws ExecutionException {
  Select select=queryData.getSelect();
  OrderBy orderBy=queryData.getOrderBy();
  Integer limit=null;
  Integer internalLimit=10;
  if (queryData.getLimit() != null) {
    internalLimit=limit=queryData.getLimit().getLimit();
  }
  if (groupingSelectors.size() > 1 && orderBy != null && !orderBy.getIds().isEmpty()) {
    for (    OrderByClause clause : orderBy.getIds()) {
      if (clause.getSelector() instanceof FunctionSelector) {
        internalLimit=0;
        break;
      }
    }
  }
  Map<String,OrderByClause> orderByMap=new HashMap<>();
  if (orderBy != null && !orderBy.getIds().isEmpty()) {
    for (    OrderByClause clause : orderBy.getIds()) {
      orderByMap.put(SelectorUtils.calculateAlias(clause.getSelector()),clause);
    }
  }
  AggregationBuilder aggregationBuilder=null;
  AggregationBuilder lastAggregationBuilder=null;
  for (  Selector term : groupingSelectors) {
    String fieldName=SelectorUtils.getSelectorFieldName(term);
    if (aggregationBuilder == null) {
      lastAggregationBuilder=aggregationBuilder=AggregationBuilders.terms(fieldName).field(fieldName);
      if (limit != null) {
        ((TermsBuilder)lastAggregationBuilder).size(limit);
      }
      sortSubAggregation(fieldName,orderByMap,(TermsBuilder)lastAggregationBuilder);
    }
 else {
      lastAggregationBuilder=AggregationBuilders.terms(fieldName).field(fieldName);
      ((TermsBuilder)lastAggregationBuilder).size(internalLimit);
      sortSubAggregation(fieldName,orderByMap,(TermsBuilder)lastAggregationBuilder);
      aggregationBuilder.subAggregation(lastAggregationBuilder);
    }
  }
  if (aggregationBuilder != null) {
    for (    Selector selector : select.getColumnMap().keySet()) {
      if (SelectorUtils.isFunction(selector,"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str")) {
        FunctionSelector functionSelector=(FunctionSelector)selector;
        String fieldName=SelectorUtils.calculateAlias(selector);
        if (orderByMap.containsKey(fieldName)) {
          boolean asc=orderByMap.get(fieldName).getDirection().equals(OrderDirection.ASC);
          ((TermsBuilder)lastAggregationBuilder).order(Terms.Order.aggregation(fieldName,asc));
        }
        lastAggregationBuilder.subAggregation(buildAggregation((FunctionSelector)selector,functionSelector.getFunctionName().toLowerCase()));
      }
    }
  }
  requestBuilder.addAggregation(aggregationBuilder);
  requestBuilder.setSize(0);
}

Fixed version (ground truth):
/** 
 * Method that creates the appropriate nested term aggregation properties to elasticsearch based on those specified by the "group by" clause
 * @throws ExecutionException
 */
private void createNestedTermAggregation(ProjectParsed queryData,Collection<Selector> groupingSelectors) throws ExecutionException {
  OrderBy orderBy=queryData.getOrderBy();
  Integer limit=null;
  Integer internalLimit=10;
  if (queryData.getLimit() != null) {
    internalLimit=limit=queryData.getLimit().getLimit();
  }
  if (groupingSelectors.size() > 1 && orderBy != null && !orderBy.getIds().isEmpty()) {
    for (    OrderByClause clause : orderBy.getIds()) {
      if (clause.getSelector() instanceof FunctionSelector) {
        internalLimit=0;
        break;
      }
    }
  }
  AggregationBuilder aggregationBuilder=buildAggregationBuilder(groupingSelectors,limit,internalLimit,queryData);
  requestBuilder.addAggregation(aggregationBuilder);
  requestBuilder.setSize(0);
}