Program: 51496
Project: 51496
Buggy source:
/** 
 * Drops constraints, where the list of values is close to the values which would be returned anyway.
 */
public synchronized ColumnConstraint[][] optimizeConstraints(RolapStar.Column[] columns,ColumnConstraint[][] constraintses){
  Util.assertTrue(constraintses.length == columns.length);
  ColumnConstraint[][] newConstraintses=(ColumnConstraint[][])constraintses.clone();
  double[] bloats=new double[columns.length];
  List potentialParents=new ArrayList();
  for (int i=0; i < constraintses.length; i++) {
    if (constraintses[i] != null && constraintses[i].length == 1 && constraintses[i][0].isMember())     potentialParents.add(constraintses[i][0].getMember());
  }
  for (int i=0; i < newConstraintses.length; i++) {
    double constraintLength=(double)newConstraintses[i].length;
    if (newConstraintses[i] == null || newConstraintses[i].length < 2) {
      bloats[i]=0.0;
    }
 else     if (newConstraintses[i].length > maxConstraints) {
      bloats[i]=1.0;
      continue;
    }
    Member parent=null;
    Level level=null;
    for (int j=0; j < newConstraintses[i].length; j++) {
      if (!(newConstraintses[i][j].isMember())) {
        parent=null;
        level=null;
        bloats[i]=constraintLength / columns[i].getCardinality();
        break;
      }
 else {
        Member m=newConstraintses[i][j].getMember();
        if (j == 0) {
          parent=m.getParentMember();
          level=m.getLevel();
        }
 else {
          if (parent != null && !parent.equals(m.getParentMember())) {
            parent=null;
          }
          if (level != null && !level.equals(m.getLevel())) {
            level=null;
          }
        }
      }
    }
    boolean done=false;
    if (parent != null) {
      if (parent.isAll() || potentialParents.contains(parent)) {
        int nChildren=-1;
        SchemaReader scr=star.getSchema().getSchemaReader();
        nChildren=scr.getChildrenCountFromCache(parent);
        if (nChildren == -1) {
          if (!parent.isAll()) {
            bloats[i]=0.0;
            done=true;
          }
        }
 else {
          bloats[i]=constraintLength / nChildren;
          done=true;
        }
      }
    }
    if (!done && level != null) {
      int nMembers=-1;
      SchemaReader scr=star.getSchema().getSchemaReader();
      nMembers=scr.getLevelCardinalityFromCache(level);
      if (nMembers > 0) {
        bloats[i]=constraintLength / nMembers;
        done=true;
      }
    }
    if (!done) {
      bloats[i]=constraintLength / columns[i].getCardinality();
    }
  }
  ConstraintComparator comparator=new ConstraintComparator(bloats);
  Integer[] indexes=new Integer[columns.length];
  for (int i=0; i < columns.length; i++) {
    indexes[i]=new Integer(i);
  }
  Arrays.sort(indexes,comparator);
  double abloat=1.0;
  final double aBloatLimit=0.5;
  for (int i=0; i < indexes.length; i++) {
    int j=indexes[i].intValue();
    abloat=abloat * bloats[j];
    if (abloat > aBloatLimit) {
      newConstraintses[j]=null;
    }
 else {
      break;
    }
  }
  return newConstraintses;
}

Fixed version (ground truth):
/** 
 * Drops constraints, where the list of values is close to the values which would be returned anyway.
 */
public synchronized ColumnConstraint[][] optimizeConstraints(RolapStar.Column[] columns,ColumnConstraint[][] constraintses){
  Util.assertTrue(constraintses.length == columns.length);
  ColumnConstraint[][] newConstraintses=(ColumnConstraint[][])constraintses.clone();
  double[] bloats=new double[columns.length];
  List potentialParents=new ArrayList();
  for (int i=0; i < constraintses.length; i++) {
    if (constraintses[i] != null && constraintses[i].length == 1 && constraintses[i][0].isMember()) {
      potentialParents.add(constraintses[i][0].getMember());
    }
  }
  for (int i=0; i < newConstraintses.length; i++) {
    if (newConstraintses[i] == null || newConstraintses[i].length < 2) {
      bloats[i]=0.0;
      continue;
    }
    if (newConstraintses[i].length > maxConstraints) {
      bloats[i]=1.0;
      continue;
    }
    double constraintLength=(double)newConstraintses[i].length;
    Member parent=null;
    Level level=null;
    for (int j=0; j < newConstraintses[i].length; j++) {
      if (!(newConstraintses[i][j].isMember())) {
        parent=null;
        level=null;
        bloats[i]=constraintLength / columns[i].getCardinality();
        break;
      }
 else {
        Member m=newConstraintses[i][j].getMember();
        if (j == 0) {
          parent=m.getParentMember();
          level=m.getLevel();
        }
 else {
          if (parent != null && !parent.equals(m.getParentMember())) {
            parent=null;
          }
          if (level != null && !level.equals(m.getLevel())) {
            level=null;
          }
        }
      }
    }
    boolean done=false;
    if (parent != null) {
      if (parent.isAll() || potentialParents.contains(parent)) {
        int nChildren=-1;
        SchemaReader scr=star.getSchema().getSchemaReader();
        nChildren=scr.getChildrenCountFromCache(parent);
        if (nChildren == -1) {
          if (!parent.isAll()) {
            bloats[i]=0.0;
            done=true;
          }
        }
 else {
          bloats[i]=constraintLength / nChildren;
          done=true;
        }
      }
    }
    if (!done && level != null) {
      int nMembers=-1;
      SchemaReader scr=star.getSchema().getSchemaReader();
      nMembers=scr.getLevelCardinalityFromCache(level);
      if (nMembers > 0) {
        bloats[i]=constraintLength / nMembers;
        done=true;
      }
    }
    if (!done) {
      bloats[i]=constraintLength / columns[i].getCardinality();
    }
  }
  ConstraintComparator comparator=new ConstraintComparator(bloats);
  Integer[] indexes=new Integer[columns.length];
  for (int i=0; i < columns.length; i++) {
    indexes[i]=new Integer(i);
  }
  Arrays.sort(indexes,comparator);
  double abloat=1.0;
  final double aBloatLimit=0.5;
  for (int i=0; i < indexes.length; i++) {
    int j=indexes[i].intValue();
    abloat=abloat * bloats[j];
    if (abloat > aBloatLimit) {
      newConstraintses[j]=null;
    }
 else {
      break;
    }
  }
  return newConstraintses;
}