Program: 96096
Project: 96096
Buggy source:
@Override public ExactRelation rewriteWithSubsampledErrorBounds(){
  if (!doesIncludeSample()) {
    return getOriginalRelation();
  }
  ExactRelation r=rewriteWithPartition();
  List<SelectElem> newElems=new ArrayList<SelectElem>();
  List<SelectElem> oldElems=((AggregatedRelation)r).getElemList();
  for (int i=0; i < oldElems.size(); i++) {
    SelectElem elem=oldElems.get(i);
    Optional<SelectElem> originalElem=Optional.absent();
    if (i < this.elems.size()) {
      originalElem=Optional.fromNullable(this.elems.get(i));
    }
    if (!elem.isagg()) {
      if (elem.aliasPresent() && elem.getAlias().equals(partitionColumnName())) {
        continue;
      }
      SelectElem newElem=null;
      if (elem.getAlias() == null) {
        Expr newExpr=elem.getExpr().withTableSubstituted(r.getAlias());
        newElem=new SelectElem(vc,newExpr,elem.getAlias());
      }
 else {
        newElem=new SelectElem(vc,new ColNameExpr(vc,elem.getAlias(),r.getAlias()),elem.getAlias());
      }
      newElems.add(newElem);
    }
 else {
      if (elem.getAlias().equals(partitionSizeAlias)) {
        continue;
      }
      if (!originalElem.isPresent()) {
        continue;
      }
      ColNameExpr est=new ColNameExpr(vc,elem.getAlias(),r.getAlias());
      ColNameExpr psize=new ColNameExpr(vc,partitionSizeAlias,r.getAlias());
      Expr averaged=null;
      Expr originalExpr=originalElem.get().getExpr();
      if (originalExpr.isCountDistinct()) {
        averaged=FuncExpr.round(FuncExpr.avg(est));
      }
 else       if (originalExpr.isMax()) {
        averaged=FuncExpr.max(est);
      }
 else       if (originalExpr.isMin()) {
        averaged=FuncExpr.min(est);
      }
 else {
        averaged=BinaryOpExpr.from(vc,FuncExpr.sum(BinaryOpExpr.from(vc,est,psize,"String_Node_Str")),FuncExpr.sum(psize),"String_Node_Str");
        if (originalElem.get().getExpr().isCount()) {
          averaged=FuncExpr.round(averaged);
        }
      }
      newElems.add(new SelectElem(vc,averaged,elem.getAlias()));
      if (originalExpr.isMax() || originalExpr.isMin()) {
      }
 else {
        Expr error=BinaryOpExpr.from(vc,BinaryOpExpr.from(vc,FuncExpr.stddev(est),FuncExpr.sqrt(FuncExpr.avg(psize)),"String_Node_Str"),FuncExpr.sqrt(FuncExpr.sum(psize)),"String_Node_Str");
        error=BinaryOpExpr.from(vc,error,ConstantExpr.from(vc,confidenceIntervalMultiplier()),"String_Node_Str");
        newElems.add(new SelectElem(vc,error,Relation.errorBoundColumn(elem.getAlias())));
      }
    }
  }
  List<Expr> newGroupby=new ArrayList<Expr>();
  for (  SelectElem elem : elems) {
    if (!elem.isagg()) {
      if (elem.aliasPresent()) {
        if (!elem.getAlias().equals(partitionColumnName())) {
          newGroupby.add(new ColNameExpr(vc,elem.getAlias(),r.getAlias()));
        }
      }
 else {
        if (!elem.getExpr().toString().equals(partitionColumnName())) {
          newGroupby.add(elem.getExpr().withTableSubstituted(r.getAlias()));
        }
      }
    }
  }
  if (newGroupby.size() > 0) {
    r=new GroupedRelation(vc,r,newGroupby);
  }
  r=new AggregatedRelation(vc,r,newElems);
  r.setAlias(getAlias());
  return r;
}

Fixed version (ground truth):
@Override public ExactRelation rewriteWithSubsampledErrorBounds(){
  if (!doesIncludeSample()) {
    return getOriginalRelation();
  }
  ExactRelation r=rewriteWithPartition(true);
  List<SelectElem> newElems=new ArrayList<SelectElem>();
  List<SelectElem> oldElems=((AggregatedRelation)r).getElemList();
  List<Expr> newGroupby=new ArrayList<Expr>();
  for (int i=0; i < oldElems.size(); i++) {
    SelectElem elem=oldElems.get(i);
    Optional<SelectElem> originalElem=Optional.absent();
    if (i < this.elems.size()) {
      originalElem=Optional.fromNullable(this.elems.get(i));
    }
    if (!elem.isagg()) {
      if (elem.aliasPresent() && elem.getAlias().equals(partitionColumnName())) {
        continue;
      }
      SelectElem newElem=null;
      Expr newExpr=null;
      if (elem.getAlias() == null) {
        newExpr=elem.getExpr().withTableSubstituted(r.getAlias());
        newElem=new SelectElem(vc,newExpr,elem.getAlias());
      }
 else {
        newExpr=new ColNameExpr(vc,elem.getAlias(),r.getAlias());
        newElem=new SelectElem(vc,newExpr,elem.getAlias());
      }
      if (originalElem.isPresent()) {
        newElems.add(newElem);
      }
      newGroupby.add(newExpr);
    }
 else {
      if (elem.getAlias().equals(partitionSizeAlias)) {
        continue;
      }
      if (!originalElem.isPresent()) {
        continue;
      }
      ColNameExpr est=new ColNameExpr(vc,elem.getAlias(),r.getAlias());
      ColNameExpr psize=new ColNameExpr(vc,partitionSizeAlias,r.getAlias());
      Expr averaged=null;
      Expr originalExpr=originalElem.get().getExpr();
      if (originalExpr.isCountDistinct()) {
        averaged=FuncExpr.round(FuncExpr.avg(est));
      }
 else       if (originalExpr.isMax()) {
        averaged=FuncExpr.max(est);
      }
 else       if (originalExpr.isMin()) {
        averaged=FuncExpr.min(est);
      }
 else {
        averaged=BinaryOpExpr.from(vc,FuncExpr.sum(BinaryOpExpr.from(vc,est,psize,"String_Node_Str")),FuncExpr.sum(psize),"String_Node_Str");
        if (originalElem.get().getExpr().isCount()) {
          averaged=FuncExpr.round(averaged);
        }
      }
      newElems.add(new SelectElem(vc,averaged,elem.getAlias()));
      if (originalExpr.isMax() || originalExpr.isMin()) {
      }
 else {
        Expr error=BinaryOpExpr.from(vc,BinaryOpExpr.from(vc,FuncExpr.stddev(est),FuncExpr.sqrt(FuncExpr.avg(psize)),"String_Node_Str"),FuncExpr.sqrt(FuncExpr.sum(psize)),"String_Node_Str");
        error=BinaryOpExpr.from(vc,error,ConstantExpr.from(vc,confidenceIntervalMultiplier()),"String_Node_Str");
        newElems.add(new SelectElem(vc,error,Relation.errorBoundColumn(elem.getAlias())));
      }
    }
  }
  for (  SelectElem elem : elems) {
    if (!elem.isagg()) {
      if (elem.aliasPresent()) {
        if (!elem.getAlias().equals(partitionColumnName())) {
          newGroupby.add(new ColNameExpr(vc,elem.getAlias(),r.getAlias()));
        }
      }
 else {
        if (!elem.getExpr().toString().equals(partitionColumnName())) {
          newGroupby.add(elem.getExpr().withTableSubstituted(r.getAlias()));
        }
      }
    }
  }
  if (newGroupby.size() > 0) {
    r=new GroupedRelation(vc,r,newGroupby);
  }
  r=new AggregatedRelation(vc,r,newElems);
  r.setAlias(getAlias());
  return r;
}