Program: 72848
Project: 72848
Buggy source:
/** 
 * Check the aggregate object on group,if the aggregate object's type is GROUP_COLUMN_EXPR, its group level must be less than current group level. if type is FILTER_ON_GROUP_EXPR, its group level must be equals to current group level. if type is SORT_ON_GROUP_EXPR, its group level must be less or equal with current group level.
 * @param aggregateObj
 */
private int getCurrentGroupLevel(AggregateObject aggregateObj,Context context) throws DataException {
  AggregateExpression expr=aggregateObj.getAggregateExpr();
  IAggrFunction aggr=expr.getAggregation();
  List argList=expr.getArguments();
  int nFixedArgs=aggr.getParameterDefn().length;
  int groupLevel=currentGroupLevel;
  int nArgs=argList.size();
  CompiledExpression groupExpr=null;
  if (nArgs > nFixedArgs + 2 || nArgs < nFixedArgs) {
    DataException e=new DataException(ResourceConstants.INVALID_AGGR_PARAMETER,expr.getAggregation().getName());
    throw e;
  }
  if (nArgs == nFixedArgs + 2) {
    groupExpr=(CompiledExpression)argList.get(nArgs - 1);
  }
  if (groupExpr != null && !(groupExpr instanceof ConstantExpression)) {
    DataException e=new DataException(ResourceConstants.INVALID_AGGR_GROUP_EXPRESSION,aggr.getName());
    throw e;
  }
  Object groupLevelObj;
  Context cx=Context.enter();
  try {
    if (groupExpr != null)     groupLevelObj=groupExpr.evaluate(cx,scope);
 else {
      String currentGroupName=null;
      if (this.currentGroupLevelList.size() == 0)       currentGroupName=this.getScriptExpression().getGroupName();
 else       currentGroupName=this.currentGroupLevelList.get(this.currentGroupLevelList.size() - 1).toString();
      if (currentGroupName.equals(TOTAL_OVERALL))       groupLevelObj=new Integer(0);
 else       groupLevelObj=currentGroupName;
    }
  }
  finally {
    Context.exit();
  }
  if (groupLevelObj == null) {
  }
 else   if (groupLevelObj instanceof String) {
    int level=AggregationConstantsUtil.getGroupLevel((String)groupLevelObj,currentGroupLevel,this.baseQuery.getGrouping() == null ? 0 : this.baseQuery.getGrouping().length,false);
    if (level != -1) {
      groupLevel=level;
    }
 else {
      groupLevel=getGroupIndex((String)groupLevelObj);
    }
  }
 else   if (groupLevelObj instanceof Number) {
    int offset=((Number)groupLevelObj).intValue();
    if (offset < 0)     groupLevel=currentGroupLevel + offset;
 else     groupLevel=offset;
  }
switch (exprType) {
case IExpressionProcessor.FILTER_ON_GROUP_EXPR:
    if (groupLevel != currentGroupLevel) {
      DataException e=new DataException(ResourceConstants.INVALID_GROUP_LEVEL,aggr.getName());
      throw e;
    }
  break;
case IExpressionProcessor.SORT_ON_GROUP_EXPR:
if (groupLevel < 0 || groupLevel > currentGroupLevel) {
  DataException e=new DataException(ResourceConstants.INVALID_GROUP_LEVEL,aggr.getName());
  throw e;
}
break;
default :
if (groupLevel < 0) {
DataException e=new DataException(ResourceConstants.INVALID_GROUP_LEVEL,aggr.getName());
throw e;
}
break;
}
return groupLevel;
}

Fixed version (ground truth):
/** 
 * Check the aggregate object on group,if the aggregate object's type is GROUP_COLUMN_EXPR, its group level must be less than current group level. if type is FILTER_ON_GROUP_EXPR, its group level must be equals to current group level. if type is SORT_ON_GROUP_EXPR, its group level must be less or equal with current group level.
 * @param aggregateObj
 */
private int getCurrentGroupLevel(AggregateObject aggregateObj,Context context) throws DataException {
  AggregateExpression expr=aggregateObj.getAggregateExpr();
  IAggrFunction aggr=expr.getAggregation();
  List argList=expr.getArguments();
  int nFixedArgs=aggr.getParameterDefn().length;
  int groupLevel=currentGroupLevel;
  int nArgs=argList.size();
  CompiledExpression groupExpr=null;
  if (nArgs > nFixedArgs + 2 || nArgs < nFixedArgs) {
    if (nFixedArgs > 0 && !aggr.getParameterDefn()[0].isOptional()) {
      DataException e=new DataException(ResourceConstants.INVALID_AGGR_PARAMETER,expr.getAggregation().getName());
      throw e;
    }
  }
  if (nArgs == nFixedArgs + 2) {
    groupExpr=(CompiledExpression)argList.get(nArgs - 1);
  }
  if (groupExpr != null && !(groupExpr instanceof ConstantExpression)) {
    DataException e=new DataException(ResourceConstants.INVALID_AGGR_GROUP_EXPRESSION,aggr.getName());
    throw e;
  }
  Object groupLevelObj;
  Context cx=Context.enter();
  try {
    if (groupExpr != null)     groupLevelObj=groupExpr.evaluate(cx,scope);
 else {
      String currentGroupName=null;
      if (this.currentGroupLevelList.size() == 0)       currentGroupName=this.getScriptExpression().getGroupName();
 else       currentGroupName=this.currentGroupLevelList.get(this.currentGroupLevelList.size() - 1).toString();
      if (currentGroupName.equals(TOTAL_OVERALL))       groupLevelObj=new Integer(0);
 else       groupLevelObj=currentGroupName;
    }
  }
  finally {
    Context.exit();
  }
  if (groupLevelObj == null) {
  }
 else   if (groupLevelObj instanceof String) {
    int level=AggregationConstantsUtil.getGroupLevel((String)groupLevelObj,currentGroupLevel,this.baseQuery.getGrouping() == null ? 0 : this.baseQuery.getGrouping().length,false);
    if (level != -1) {
      groupLevel=level;
    }
 else {
      groupLevel=getGroupIndex((String)groupLevelObj);
    }
  }
 else   if (groupLevelObj instanceof Number) {
    int offset=((Number)groupLevelObj).intValue();
    if (offset < 0)     groupLevel=currentGroupLevel + offset;
 else     groupLevel=offset;
  }
switch (exprType) {
case IExpressionProcessor.FILTER_ON_GROUP_EXPR:
    if (groupLevel != currentGroupLevel) {
      DataException e=new DataException(ResourceConstants.INVALID_GROUP_LEVEL,aggr.getName());
      throw e;
    }
  break;
case IExpressionProcessor.SORT_ON_GROUP_EXPR:
if (groupLevel < 0 || groupLevel > currentGroupLevel) {
  DataException e=new DataException(ResourceConstants.INVALID_GROUP_LEVEL,aggr.getName());
  throw e;
}
break;
default :
if (groupLevel < 0) {
DataException e=new DataException(ResourceConstants.INVALID_GROUP_LEVEL,aggr.getName());
throw e;
}
break;
}
return groupLevel;
}