Program: 96114
Project: 96114
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<Expr> scaledExpr=new ArrayList<Expr>();
  List<ColNameExpr> samplingProbCols=newSource.accumulateSamplingProbColumns();
  List<Expr> groupby=new ArrayList<Expr>();
  if (source instanceof ApproxGroupedRelation) {
    groupby.addAll(((ApproxGroupedRelation)source).getGroupby());
  }
  final Map<TableUniqueName,String> sub=source.tableSubstitution();
  for (  Expr e : aggs) {
    Expr scaled=transformForSingleFunctionWithPartitionSize(e,samplingProbCols,groupby,newSource.partitionColumn(),sub,false);
    scaledExpr.add(scaled);
  }
  scaledExpr.add(FuncExpr.count());
  ExactRelation r=new AggregatedRelation(vc,newSource,scaledExpr);
  return r;
}

Fixed version (ground truth):
/** 
 * 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<Expr> scaledExpr=new ArrayList<Expr>();
  List<ColNameExpr> samplingProbCols=newSource.accumulateSamplingProbColumns();
  List<Expr> groupby=new ArrayList<Expr>();
  if (source instanceof ApproxGroupedRelation) {
    groupby.addAll(((ApproxGroupedRelation)source).groupbyWithTablesSubstituted());
  }
  final Map<TableUniqueName,String> sub=source.tableSubstitution();
  for (  Expr e : aggs) {
    Expr scaled=transformForSingleFunctionWithPartitionSize(e,samplingProbCols,groupby,newSource.partitionColumn(),sub,false);
    scaledExpr.add(scaled);
  }
  scaledExpr.add(FuncExpr.count());
  ExactRelation r=new AggregatedRelation(vc,newSource,scaledExpr);
  return r;
}