Program: 73875
Project: 73875
Buggy source:
/** 
 * Populate all aggregation member in CubeQueryDefinition. For initial implementation: we only consider IMeasureDefintion we will take into consider to handle the aggregation definition in binding expression;
 * @param queryDefn
 * @return
 * @throws DataException 
 */
static CalculatedMember[] getCalculatedMembers(ICubeQueryDefinition queryDefn,Scriptable scope) throws DataException {
  List measureList=queryDefn.getMeasures();
  ICubeAggrDefn[] cubeAggrs=OlapExpressionUtil.getAggrDefns(queryDefn.getBindings());
  List cubeAggrBindingList=new ArrayList();
  for (int i=0; i < cubeAggrs.length; i++) {
    if (cubeAggrs[i].getAggrName() != null)     cubeAggrBindingList.add(cubeAggrs[i]);
  }
  populateMeasureFromBinding(queryDefn);
  populateMeasureFromFilter(queryDefn);
  populateMeasureFromSort(queryDefn);
  if (measureList == null)   return new CalculatedMember[0];
  CalculatedMember[] calculatedMember=new CalculatedMember[measureList.size() + cubeAggrBindingList.size()];
  int index=0;
  List calculatedMemberList=new ArrayList();
  if (!measureList.isEmpty()) {
    List levelList=populateMeasureAggrOns(queryDefn);
    Iterator measureIter=measureList.iterator();
    while (measureIter.hasNext()) {
      MeasureDefinition measureDefn=(MeasureDefinition)measureIter.next();
      calculatedMember[index]=new CalculatedMember(measureDefn.getName(),measureDefn.getName(),levelList,measureDefn.getAggrFunction() == null ? IBuildInAggregation.TOTAL_SUM_FUNC : measureDefn.getAggrFunction(),0);
      calculatedMemberList.add(calculatedMember[index]);
      index++;
    }
  }
  if (!cubeAggrBindingList.isEmpty()) {
    int rsID=1;
    for (int i=0; i < cubeAggrBindingList.size(); i++) {
      int id=getResultSetIndex(calculatedMemberList,((ICubeAggrDefn)cubeAggrBindingList.get(i)).getAggrLevels());
      if (id == -1) {
        calculatedMember[index]=new CalculatedMember((ICubeAggrDefn)cubeAggrBindingList.get(i),rsID);
        calculatedMemberList.add(calculatedMember[index]);
        rsID++;
      }
 else {
        calculatedMember[index]=new CalculatedMember((ICubeAggrDefn)cubeAggrBindingList.get(i),id);
      }
      if (((ICubeAggrDefn)cubeAggrBindingList.get(i)).getFilter() != null) {
        IJSMeasureFilterEvalHelper filterEvalHelper=new JSMeasureFilterEvalHelper(scope,new FilterDefinition(((ICubeAggrDefn)cubeAggrBindingList.get(i)).getFilter()));
        calculatedMember[index].setFilterEvalHelper(filterEvalHelper);
      }
      index++;
    }
  }
  return calculatedMember;
}

Fixed version (ground truth):
/** 
 * Populate all aggregation member in CubeQueryDefinition. For initial implementation: we only consider IMeasureDefintion we will take into consider to handle the aggregation definition in binding expression;
 * @param queryDefn
 * @param measureMapping 
 * @return
 * @throws DataException 
 */
static CalculatedMember[] getCalculatedMembers(ICubeQueryDefinition queryDefn,Scriptable scope,Map measureMapping) throws DataException {
  List measureList=queryDefn.getMeasures();
  ICubeAggrDefn[] cubeAggrs=OlapExpressionUtil.getAggrDefns(queryDefn.getBindings());
  List cubeAggrBindingList=new ArrayList();
  for (int i=0; i < cubeAggrs.length; i++) {
    if (cubeAggrs[i].getAggrName() != null)     cubeAggrBindingList.add(cubeAggrs[i]);
  }
  populateMeasureFromBinding(queryDefn);
  populateMeasureFromFilter(queryDefn);
  populateMeasureFromSort(queryDefn);
  if (measureList == null)   return new CalculatedMember[0];
  List measureAggrOns=populateMeasureAggrOns(queryDefn);
  List unreferencedMeasures=getUnreferencedMeasures(queryDefn,measureList,measureMapping,measureAggrOns);
  CalculatedMember[] calculatedMember=new CalculatedMember[unreferencedMeasures.size() + cubeAggrBindingList.size()];
  int index=0;
  List calculatedMemberList=new ArrayList();
  if (!unreferencedMeasures.isEmpty()) {
    Iterator measureIter=unreferencedMeasures.iterator();
    while (measureIter.hasNext()) {
      MeasureDefinition measureDefn=(MeasureDefinition)measureIter.next();
      String innerName=OlapExpressionUtil.createMeasureCalculateMemeberName(measureDefn.getName());
      measureMapping.put(measureDefn.getName(),innerName);
      calculatedMember[index]=new CalculatedMember(innerName,measureDefn.getName(),measureAggrOns,adaptAggrFunction(measureDefn),0);
      calculatedMemberList.add(calculatedMember[index]);
      index++;
    }
  }
  if (!cubeAggrBindingList.isEmpty()) {
    int rsID=index > 0 ? 1 : 0;
    for (int i=0; i < cubeAggrBindingList.size(); i++) {
      int id=getResultSetIndex(calculatedMemberList,((ICubeAggrDefn)cubeAggrBindingList.get(i)).getAggrLevels());
      if (id == -1) {
        calculatedMember[index]=new CalculatedMember((ICubeAggrDefn)cubeAggrBindingList.get(i),rsID);
        calculatedMemberList.add(calculatedMember[index]);
        rsID++;
      }
 else {
        calculatedMember[index]=new CalculatedMember((ICubeAggrDefn)cubeAggrBindingList.get(i),id);
      }
      if (((ICubeAggrDefn)cubeAggrBindingList.get(i)).getFilter() != null) {
        IJSMeasureFilterEvalHelper filterEvalHelper=new JSMeasureFilterEvalHelper(scope,new FilterDefinition(((ICubeAggrDefn)cubeAggrBindingList.get(i)).getFilter()));
        calculatedMember[index].setFilterEvalHelper(filterEvalHelper);
      }
      index++;
    }
  }
  return calculatedMember;
}