Program: 29914
Project: 29914
Buggy source:
private static ArrayList<AggregationMetric> BuildMetrics(Context context,TableDefinition definition){
  if (context == null)   throw new IllegalArgumentException("String_Node_Str");
  if (context.items.isEmpty())   return null;
  ArrayList<AggregationMetric> result=new ArrayList<AggregationMetric>();
  AggregationMetric metric=new AggregationMetric(definition);
  ArrayList<Item> items=extractTokens(context);
  TableDefinition tableDef=definition;
  boolean fieldDetected=false;
  int ptr=0;
  int inputPtr=0;
  for (int i=0; i < items.size(); i++) {
    Item item=items.get(i);
    if (item.item.getType().equals(SemanticNames.AGGREGATION_METRIC_FUNCTION_NAME)) {
      ptr=item.item.getPtr();
      metric.function=item.item.getValue();
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      inputPtr=item.item.getPtr();
      continue;
    }
    if (item.item.getType().equals("String_Node_Str") && item.item.getValue().equals("String_Node_Str")) {
      result.add(metric);
      tableDef=definition;
      fieldDetected=false;
      metric.sourceText=context.inputString.substring(ptr,inputPtr);
      metric=new AggregationMetric(definition);
      continue;
    }
    if (fieldDetected)     throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
    AggregationGroupItem ai=new AggregationGroupItem();
    if (metric.items == null)     metric.items=new ArrayList<AggregationGroupItem>();
    metric.items.add(ai);
    ai.name=item.item.getValue();
    if (tableDef != null) {
      FieldDefinition fd=tableDef.getFieldDef(ai.name);
      ai.fieldDef=fd;
      if (fd == null) {
        if (i != (items.size() - 1)) {
          throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        }
        if (!QueryUtils.isSystemField(ai.name)) {
          throw new IllegalArgumentException("String_Node_Str" + ai.name);
        }
      }
      if (tableDef.isLinkField(ai.name) || fd.isXLinkField()) {
        ai.isLink=true;
        if (fieldDetected)         throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        tableDef=tableDef.getLinkExtentTableDef(fd);
        if (tableDef == null) {
          throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        }
      }
 else {
        if (fd != null && fd.getType() == FieldType.GROUP)         throw new IllegalArgumentException("String_Node_Str");
        fieldDetected=true;
      }
      ai.tableDef=tableDef;
    }
    if (item.queryItems != null) {
      for (int j=0; j < item.queryItems.size(); j++) {
        ArrayList<GrammarItem> filterItems=item.queryItems.get(j);
        ai.query=CompileQuery(tableDef,ai.query,filterItems);
      }
    }
  }
  metric.sourceText=context.inputString.substring(ptr);
  result.add(metric);
  return result;
}

Fixed version (ground truth):
private static ArrayList<AggregationMetric> BuildMetrics(Context context,TableDefinition definition){
  if (context == null)   throw new IllegalArgumentException("String_Node_Str");
  if (context.items.isEmpty())   return null;
  ArrayList<AggregationMetric> result=new ArrayList<AggregationMetric>();
  AggregationMetric metric=new AggregationMetric(definition);
  ArrayList<Item> items=extractTokens(context);
  TableDefinition tableDef=definition;
  boolean fieldDetected=false;
  int ptr=0;
  int inputPtr=0;
  for (int i=0; i < items.size(); i++) {
    Item item=items.get(i);
    if (item.item.getType().equals(SemanticNames.AGGREGATION_METRIC_FUNCTION_NAME)) {
      ptr=item.item.getPtr();
      metric.function=item.item.getValue();
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      inputPtr=item.item.getPtr();
      continue;
    }
    if (item.item.getType().equals("String_Node_Str") && item.item.getValue().equals("String_Node_Str")) {
      result.add(metric);
      tableDef=definition;
      fieldDetected=false;
      metric.sourceText=context.inputString.substring(ptr,inputPtr);
      metric=new AggregationMetric(definition);
      continue;
    }
    if (fieldDetected)     throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
    AggregationGroupItem ai=new AggregationGroupItem();
    if (metric.items == null)     metric.items=new ArrayList<AggregationGroupItem>();
    metric.items.add(ai);
    ai.name=item.item.getValue();
    if (tableDef != null) {
      FieldDefinition fd=tableDef.getFieldDef(ai.name);
      ai.fieldDef=fd;
      if (fd == null) {
        if (i != (items.size() - 1)) {
          throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        }
        if (!QueryUtils.isSystemField(ai.name)) {
          throw new IllegalArgumentException("String_Node_Str" + ai.name);
        }
      }
      if (tableDef.isLinkField(ai.name) || (fd != null && fd.isXLinkField())) {
        ai.isLink=true;
        if (fieldDetected)         throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        tableDef=tableDef.getLinkExtentTableDef(fd);
        if (tableDef == null) {
          throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        }
      }
 else {
        if (fd != null && fd.getType() == FieldType.GROUP)         throw new IllegalArgumentException("String_Node_Str");
        fieldDetected=true;
      }
      ai.tableDef=tableDef;
    }
    if (item.queryItems != null) {
      for (int j=0; j < item.queryItems.size(); j++) {
        ArrayList<GrammarItem> filterItems=item.queryItems.get(j);
        ai.query=CompileQuery(tableDef,ai.query,filterItems);
      }
    }
  }
  metric.sourceText=context.inputString.substring(ptr);
  result.add(metric);
  return result;
}