Program: 96137
Project: 96137
Buggy source:
private Expr transformForSingleFunctionWithPartitionSize(Expr f,final List<TableUniqueName> stratifiedSampleTables,List<ColNameExpr> groupby,final ColNameExpr partitionCol){
  final Map<String,String> sub=source.tableSubstitution();
  final List<Expr> groupbyExpr=new ArrayList<Expr>();
  for (  ColNameExpr c : groupby) {
    groupbyExpr.add((Expr)c);
  }
  ExprModifier v=new ExprModifier(){
    public Expr call(    Expr expr){
      if (expr instanceof FuncExpr) {
        FuncExpr f=(FuncExpr)expr;
        FuncExpr s=(FuncExpr)exprWithTableNamesSubstituted(expr,sub);
        double samplingProb=source.samplingProbabilityFor(f);
        if (f.getFuncName().equals(FuncExpr.FuncName.COUNT)) {
          Expr est=FuncExpr.sum(scaleForSampling(samplingProb,stratifiedSampleTables));
          est=scaleWithPartitionSize(est,groupbyExpr,partitionCol);
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.COUNT_DISTINCT)) {
          String dbname=vc.getDbms().getName();
          if (dbname.equals("String_Node_Str")) {
            Expr est=BinaryOpExpr.from(new FuncExpr(FuncExpr.FuncName.IMPALA_APPROX_COUNT_DISTINCT,s.getUnaryExpr()),ConstantExpr.from(1.0 / samplingProb),"String_Node_Str");
            est=scaleWithPartitionSize(est,groupbyExpr,partitionCol);
            return est;
          }
 else {
            return BinaryOpExpr.from(s,ConstantExpr.from(1.0 / samplingProb),"String_Node_Str");
          }
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.SUM)) {
          Expr est=scaleForSampling(samplingProb,stratifiedSampleTables);
          est=FuncExpr.sum(BinaryOpExpr.from(s.getUnaryExpr(),est,"String_Node_Str"));
          est=scaleWithPartitionSize(est,groupbyExpr,partitionCol);
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.AVG)) {
          Expr scale=scaleForSampling(samplingProb,stratifiedSampleTables);
          Expr sumEst=FuncExpr.sum(BinaryOpExpr.from(s.getUnaryExpr(),scale,"String_Node_Str"));
          Expr countEst=countNotNull(s.getUnaryExpr(),scale);
          return BinaryOpExpr.from(sumEst,countEst,"String_Node_Str");
        }
 else {
          return s;
        }
      }
 else {
        return expr;
      }
    }
  }
;
  return v.visit(f);
}

Fixed version (ground truth):
private Expr transformForSingleFunctionWithPartitionSize(Expr f,final List<ColNameExpr> samplingProbCols,List<ColNameExpr> groupby,final ColNameExpr partitionCol){
  final Map<String,String> sub=source.tableSubstitution();
  final List<Expr> groupbyExpr=new ArrayList<Expr>();
  for (  ColNameExpr c : groupby) {
    groupbyExpr.add((Expr)c);
  }
  ExprModifier v=new ExprModifier(){
    public Expr call(    Expr expr){
      if (expr instanceof FuncExpr) {
        FuncExpr f=(FuncExpr)expr;
        FuncExpr s=(FuncExpr)exprWithTableNamesSubstituted(expr,sub);
        double samplingProb=source.samplingProbabilityFor(f);
        if (f.getFuncName().equals(FuncExpr.FuncName.COUNT)) {
          Expr est=FuncExpr.sum(scaleForSampling(samplingProb,samplingProbCols));
          est=scaleWithPartitionSize(est,groupbyExpr,partitionCol);
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.COUNT_DISTINCT)) {
          String dbname=vc.getDbms().getName();
          if (dbname.equals("String_Node_Str")) {
            Expr est=BinaryOpExpr.from(new FuncExpr(FuncExpr.FuncName.IMPALA_APPROX_COUNT_DISTINCT,s.getUnaryExpr()),ConstantExpr.from(1.0 / samplingProb),"String_Node_Str");
            est=scaleWithPartitionSize(est,groupbyExpr,partitionCol);
            return est;
          }
 else {
            return BinaryOpExpr.from(s,ConstantExpr.from(1.0 / samplingProb),"String_Node_Str");
          }
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.SUM)) {
          Expr est=scaleForSampling(samplingProb,samplingProbCols);
          est=FuncExpr.sum(BinaryOpExpr.from(s.getUnaryExpr(),est,"String_Node_Str"));
          est=scaleWithPartitionSize(est,groupbyExpr,partitionCol);
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.AVG)) {
          Expr scale=scaleForSampling(samplingProb,samplingProbCols);
          Expr sumEst=FuncExpr.sum(BinaryOpExpr.from(s.getUnaryExpr(),scale,"String_Node_Str"));
          Expr countEst=countNotNull(s.getUnaryExpr(),scale);
          return BinaryOpExpr.from(sumEst,countEst,"String_Node_Str");
        }
 else {
          return s;
        }
      }
 else {
        return expr;
      }
    }
  }
;
  return v.visit(f);
}