Program: 96095
Project: 96095
Buggy source:
/** 
 * This relation must include partition numbers, and the answers must be scaled properly. Note that   {@link ApproxRelation#rewriteWithSubsampledErrorBounds()}is used only for the statement including final error bounds; all internal manipulations must be performed by this method. The rewritten relation transforms original aggregate elements as follows. Every aggregate element is replaced with two aggregate elements. One is for mean estimate and the other is for error estimate. The rewritten relation includes an extra aggregate element: count(*). This is to compute the partition sizes. These partition sizes can be used by an upstream (or parent) relation for computing the final mean estimate. (note that computing weighted average provides higher accuracy compared to unweighted average.)
 * @return
 */
@Override protected ExactRelation rewriteWithPartition(){
  ExactRelation newSource=partitionedSource();
  List<SelectElem> scaledElems=new ArrayList<SelectElem>();
  List<Expr> groupby=new ArrayList<Expr>();
  if (source instanceof ApproxGroupedRelation) {
    groupby.addAll(((ApproxGroupedRelation)source).getGroupby());
  }
  ColNameExpr partitionColExpr=newSource.partitionColumn();
  Expr tupleSamplingProbExpr=source.tupleProbabilityColumn();
  Expr tableSamplingRatioExpr=source.tableSamplingRatio();
  SingleFunctionTransformerForSubsampling transformer=new SingleFunctionTransformerForSubsampling(vc,groupby,partitionColExpr,tupleSamplingProbExpr,tableSamplingRatioExpr);
  for (  SelectElem elem : elems) {
    if (!elem.isagg()) {
      scaledElems.add(elem);
    }
 else {
      Expr agg=elem.getExpr();
      Expr scaled=transformer.call(agg);
      scaledElems.add(new SelectElem(vc,scaled,elem.getAlias()));
    }
  }
  scaledElems.add(new SelectElem(vc,newSource.partitionColumn(),partitionColumnName()));
  scaledElems.add(new SelectElem(vc,FuncExpr.count(),partitionSizeAlias));
  scaledElems.add(new SelectElem(vc,FuncExpr.avg(ConstantExpr.from(vc,samplingProbability())),samplingProbabilityColumnName()));
  ExactRelation r=new AggregatedRelation(vc,newSource,scaledElems);
  r.setAlias(getAlias());
  return r;
}

Fixed version (ground truth):
/** 
 * @param projectUnprojectedGroups This option is used by {@link ApproxAggregatedRelation#rewriteWithSubsampledErrorBounds()}.
 * @return
 */
protected ExactRelation rewriteWithPartition(boolean projectUnprojectedGroups){
  ExactRelation newSource=partitionedSource();
  List<SelectElem> scaledElems=new ArrayList<SelectElem>();
  List<Expr> groupby=new ArrayList<Expr>();
  if (source instanceof ApproxGroupedRelation) {
    groupby.addAll(((ApproxGroupedRelation)source).getGroupby());
  }
  ColNameExpr partitionColExpr=newSource.partitionColumn();
  Expr tupleSamplingProbExpr=source.tupleProbabilityColumn();
  Expr tableSamplingRatioExpr=source.tableSamplingRatio();
  SingleFunctionTransformerForSubsampling transformer=new SingleFunctionTransformerForSubsampling(vc,groupby,partitionColExpr,tupleSamplingProbExpr,tableSamplingRatioExpr);
  List<ColNameExpr> unappearingGroups=new ArrayList<ColNameExpr>();
  if (source instanceof ApproxGroupedRelation) {
    for (    Expr e : ((ApproxGroupedRelation)source).getGroupby()) {
      if (e instanceof ColNameExpr) {
        unappearingGroups.add((ColNameExpr)e);
      }
    }
  }
  for (  SelectElem elem : elems) {
    if (!elem.isagg()) {
      scaledElems.add(elem);
      Expr e=elem.getExpr();
      if (e instanceof ColNameExpr) {
        int i=0;
        for (i=0; i < unappearingGroups.size(); i++) {
          if (unappearingGroups.get(i).getCol().equals(((ColNameExpr)e).getCol())) {
            break;
          }
        }
        if (i < unappearingGroups.size()) {
          unappearingGroups.remove(i);
        }
      }
    }
 else {
      Expr agg=elem.getExpr();
      Expr scaled=transformer.call(agg);
      scaledElems.add(new SelectElem(vc,scaled,elem.getAlias()));
    }
  }
  if (projectUnprojectedGroups) {
    for (    ColNameExpr e : unappearingGroups) {
      scaledElems.add(new SelectElem(vc,e));
    }
  }
  scaledElems.add(new SelectElem(vc,newSource.partitionColumn(),partitionColumnName()));
  scaledElems.add(new SelectElem(vc,FuncExpr.count(),partitionSizeAlias));
  scaledElems.add(new SelectElem(vc,FuncExpr.avg(ConstantExpr.from(vc,samplingProbability())),samplingProbabilityColumnName()));
  ExactRelation r=new AggregatedRelation(vc,newSource,scaledElems);
  r.setAlias(getAlias());
  return r;
}