Program: 72159
Project: 72159
Buggy source:
/** 
 * @param query
 * @param categorySD
 * @param orthSD
 * @param orthAxisArray
 * @return
 * @throws ChartException
 */
private GroupDefinition initYGroupingNSortKey(BaseQueryDefinition query,SeriesDefinition categorySD,SeriesDefinition orthSD,Object[] orthAxisArray) throws ChartException {
  GroupDefinition yGroupingDefinition=createOrthogonalGroupingDefinition(orthSD);
  if (yGroupingDefinition != null) {
    query.addGroup(yGroupingDefinition);
    if (!orthSD.isSetSorting()) {
      return yGroupingDefinition;
    }
    SortDefinition sortDefinition=new SortDefinition();
    sortDefinition.setSortStrength(ISortDefinition.DEFAULT_SORT_STRENGTH);
    sortDefinition.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(orthSD.getSorting()));
    String sortKey=null;
    if (orthSD.getSortKey() != null && orthSD.getSortKey().getDefinition() != null) {
      sortKey=orthSD.getSortKey().getDefinition();
    }
    if (sortKey == null || yGroupingDefinition.getKeyExpression().equals(sortKey)) {
      sortDefinition.setExpression(yGroupingDefinition.getKeyExpression());
    }
 else {
      String name=generateUniqueBindingName(sortKey);
      Binding binding=new Binding(name);
      try {
        query.addBinding(binding);
        binding.setExpression(new ScriptExpression(sortKey));
        binding.setDataType(org.eclipse.birt.core.data.DataType.ANY_TYPE);
        binding.addAggregateOn(yGroupingDefinition.getName());
      }
 catch (      DataException e) {
        throw new ChartException(ChartReportItemPlugin.ID,ChartException.DATA_BINDING,e);
      }
      String aggFunc=getAggFunExpr(sortKey,categorySD,orthAxisArray);
      if (aggFunc != null) {
        binding.setAggrFunction(ChartReportItemUtil.convertToDtEAggFunction(aggFunc));
        IAggregateFunction aFunc=PluginSettings.instance().getAggregateFunction(aggFunc);
        if (aFunc.getParametersCount() > 0) {
          Object[] parameters=ChartUtil.getAggFunParameters(orthSD,categorySD);
          for (int i=0; i < parameters.length && i < aFunc.getParametersCount(); i++) {
            String param=(String)parameters[i];
            binding.addArgument(new ScriptExpression(param));
          }
        }
      }
      sortDefinition.setExpression(ExpressionUtil.createRowExpression(binding.getBindingName()));
    }
    yGroupingDefinition.addSort(sortDefinition);
  }
  return yGroupingDefinition;
}

Fixed version (ground truth):
/** 
 * @param query
 * @param categorySD
 * @param orthSD
 * @param orthAxisArray
 * @return
 * @throws ChartException
 */
private GroupDefinition initYGroupingNSortKey(BaseQueryDefinition query,SeriesDefinition categorySD,SeriesDefinition orthSD,Object[] orthAxisArray) throws ChartException {
  GroupDefinition yGroupingDefinition=createOrthogonalGroupingDefinition(orthSD);
  if (yGroupingDefinition != null) {
    query.addGroup(yGroupingDefinition);
    if (!orthSD.isSetSorting()) {
      return yGroupingDefinition;
    }
    SortDefinition sortDefinition=new SortDefinition();
    sortDefinition.setSortStrength(ISortDefinition.DEFAULT_SORT_STRENGTH);
    sortDefinition.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(orthSD.getSorting()));
    String sortKey=null;
    if (orthSD.getSortKey() != null && orthSD.getSortKey().getDefinition() != null) {
      sortKey=orthSD.getSortKey().getDefinition();
    }
    if (sortKey == null || yGroupingDefinition.getKeyExpression().equals(sortKey)) {
      sortDefinition.setExpression(yGroupingDefinition.getKeyExpression());
    }
 else {
      String name=generateUniqueBindingName(sortKey);
      Binding binding=new Binding(name);
      try {
        query.addBinding(binding);
        binding.setExpression(new ScriptExpression(sortKey));
        binding.setDataType(org.eclipse.birt.core.data.DataType.ANY_TYPE);
        binding.addAggregateOn(yGroupingDefinition.getName());
        binding.setExportable(false);
      }
 catch (      DataException e) {
        throw new ChartException(ChartReportItemPlugin.ID,ChartException.DATA_BINDING,e);
      }
      String aggFunc=getAggFunExpr(sortKey,categorySD,orthAxisArray);
      if (aggFunc != null) {
        binding.setAggrFunction(ChartReportItemUtil.convertToDtEAggFunction(aggFunc));
        IAggregateFunction aFunc=PluginSettings.instance().getAggregateFunction(aggFunc);
        if (aFunc.getParametersCount() > 0) {
          Object[] parameters=ChartUtil.getAggFunParameters(orthSD,categorySD);
          for (int i=0; i < parameters.length && i < aFunc.getParametersCount(); i++) {
            String param=(String)parameters[i];
            binding.addArgument(new ScriptExpression(param));
          }
        }
      }
      sortDefinition.setExpression(ExpressionUtil.createRowExpression(binding.getBindingName()));
    }
    yGroupingDefinition.addSort(sortDefinition);
  }
  return yGroupingDefinition;
}