Program: 96116
Project: 96116
Buggy source:
@Override public ExactRelation rewriteWithSubsampledErrorBounds(){
  if (!(source instanceof ApproxAggregatedRelation)) {
    ExactRelation r=new ProjectedRelation(vc,source.rewriteWithSubsampledErrorBounds(),elems);
    r.setAliasName(getAlias());
    return r;
  }
  ExactRelation r=rewriteWithPartition(true);
  List<SelectElem> newElems=new ArrayList<SelectElem>();
  List<Expr> newAggs=new ArrayList<Expr>();
  List<SelectElem> elems=((ProjectedRelation)r).getSelectElems();
  for (int i=0; i < elems.size() - 1; i++) {
    SelectElem elem=elems.get(i);
    if (!elem.isagg()) {
      SelectElem newElem=null;
      if (elem.getAlias() == null) {
        newElem=new SelectElem(elem.getExpr(),elem.getAlias());
      }
 else {
        newElem=new SelectElem(ColNameExpr.from(elem.getAlias()),elem.getAlias());
      }
      newElems.add(newElem);
    }
 else {
      if (elem.getAlias().equals(partitionSizeAlias)) {
        continue;
      }
      ColNameExpr est=new ColNameExpr(elem.getAlias(),r.getAlias());
      ColNameExpr psize=new ColNameExpr(partitionSizeAlias,r.getAlias());
      Expr averaged=null;
      if (elem.getExpr().isCountDistinct()) {
        averaged=FuncExpr.round(FuncExpr.avg(est));
      }
 else {
        averaged=BinaryOpExpr.from(FuncExpr.sum(BinaryOpExpr.from(est,psize,"String_Node_Str")),FuncExpr.sum(psize),"String_Node_Str");
        if (elem.getExpr().isCount()) {
          averaged=FuncExpr.round(averaged);
        }
      }
      newElems.add(new SelectElem(averaged,elem.getAlias()));
      newAggs.add(averaged);
      Expr error=BinaryOpExpr.from(BinaryOpExpr.from(FuncExpr.stddev(est),FuncExpr.sqrt(FuncExpr.avg(psize)),"String_Node_Str"),FuncExpr.sqrt(FuncExpr.sum(psize)),"String_Node_Str");
      error=BinaryOpExpr.from(error,ConstantExpr.from(confidenceIntervalMultiplier()),"String_Node_Str");
      newElems.add(new SelectElem(error,Relation.errorBoundColumn(elem.getAlias())));
      newAggs.add(error);
    }
  }
  List<Expr> newGroupby=new ArrayList<Expr>();
  for (  SelectElem elem : elems) {
    if (!elem.isagg()) {
      if (elem.aliasPresent()) {
        if (!elem.getAlias().equals(partitionColumnName())) {
          newGroupby.add(ColNameExpr.from(elem.getAlias()));
        }
      }
 else {
        if (!elem.getExpr().toString().equals(partitionColumnName())) {
          newGroupby.add(elem.getExpr());
        }
      }
    }
  }
  if (newGroupby.size() > 0) {
    r=new GroupedRelation(vc,r,newGroupby);
  }
  r=new AggregatedRelation(vc,r,newAggs);
  r=new ProjectedRelation(vc,r,newElems);
  return r;
}

Fixed version (ground truth):
@Override public ExactRelation rewriteWithSubsampledErrorBounds(){
  if (!(source instanceof ApproxAggregatedRelation)) {
    ExactRelation r=new ProjectedRelation(vc,source.rewriteWithSubsampledErrorBounds(),elemsWithSubstitutedTables());
    r.setAliasName(getAlias());
    return r;
  }
  ExactRelation r=rewriteWithPartition(true);
  List<SelectElem> newElems=new ArrayList<SelectElem>();
  List<Expr> newAggs=new ArrayList<Expr>();
  List<SelectElem> elems=((ProjectedRelation)r).getSelectElems();
  for (int i=0; i < elems.size() - 1; i++) {
    SelectElem elem=elems.get(i);
    if (!elem.isagg()) {
      SelectElem newElem=null;
      if (elem.getAlias() == null) {
        Expr newExpr=elem.getExpr().withTableSubstituted(r.getAlias());
        newElem=new SelectElem(newExpr,elem.getAlias());
      }
 else {
        newElem=new SelectElem(new ColNameExpr(elem.getAlias(),r.getAlias()),elem.getAlias());
      }
      newElems.add(newElem);
    }
 else {
      if (elem.getAlias().equals(partitionSizeAlias)) {
        continue;
      }
      ColNameExpr est=new ColNameExpr(elem.getAlias(),r.getAlias());
      ColNameExpr psize=new ColNameExpr(partitionSizeAlias,r.getAlias());
      Expr averaged=null;
      if (elem.getExpr().isCountDistinct()) {
        averaged=FuncExpr.round(FuncExpr.avg(est));
      }
 else {
        averaged=BinaryOpExpr.from(FuncExpr.sum(BinaryOpExpr.from(est,psize,"String_Node_Str")),FuncExpr.sum(psize),"String_Node_Str");
        if (elem.getExpr().isCount()) {
          averaged=FuncExpr.round(averaged);
        }
      }
      newElems.add(new SelectElem(averaged,elem.getAlias()));
      newAggs.add(averaged);
      Expr error=BinaryOpExpr.from(BinaryOpExpr.from(FuncExpr.stddev(est),FuncExpr.sqrt(FuncExpr.avg(psize)),"String_Node_Str"),FuncExpr.sqrt(FuncExpr.sum(psize)),"String_Node_Str");
      error=BinaryOpExpr.from(error,ConstantExpr.from(confidenceIntervalMultiplier()),"String_Node_Str");
      newElems.add(new SelectElem(error,Relation.errorBoundColumn(elem.getAlias())));
      newAggs.add(error);
    }
  }
  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(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,newAggs);
  r=new ProjectedRelation(vc,r,newElems);
  return r;
}