Program: 96113
Project: 96113
Buggy source:
private Expr transformForSingleFunctionWithPartitionSize(Expr f,final List<ColNameExpr> samplingProbCols,List<Expr> groupby,final ColNameExpr partitionCol,final Map<TableUniqueName,String> tablesNamesSub,final boolean forErrorEst){
  final List<Expr> groupbyExpr=new ArrayList<Expr>();
  for (  Expr 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,tablesNamesSub);
        List<Expr> samplingProbExprs=source.samplingProbabilityExprsFor(f);
        if (f.getFuncName().equals(FuncExpr.FuncName.COUNT)) {
          Expr est=FuncExpr.sum(scaleForSampling(samplingProbExprs));
          est=scaleWithPartitionSize(est,groupbyExpr,partitionCol,forErrorEst);
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.COUNT_DISTINCT)) {
          String dbname=vc.getDbms().getName();
          Expr scale=scaleForSampling(samplingProbExprs);
          Expr est=null;
          if (dbname.equals("String_Node_Str")) {
            est=new FuncExpr(FuncExpr.FuncName.IMPALA_APPROX_COUNT_DISTINCT,s.getUnaryExpr());
          }
 else {
            est=new FuncExpr(FuncExpr.FuncName.COUNT_DISTINCT,s.getUnaryExpr());
          }
          est=BinaryOpExpr.from(est,scale,"String_Node_Str");
          if (sampleType().equals("String_Node_Str")) {
            est=scaleWithPartitionSize(est,groupbyExpr,partitionCol,forErrorEst);
          }
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.SUM)) {
          Expr est=scaleForSampling(samplingProbExprs);
          est=FuncExpr.sum(BinaryOpExpr.from(s.getUnaryExpr(),est,"String_Node_Str"));
          est=scaleWithPartitionSize(est,groupbyExpr,partitionCol,forErrorEst);
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.AVG)) {
          Expr scale=scaleForSampling(samplingProbExprs);
          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,final List<Expr> groupby,final ColNameExpr partitionCol,final Map<TableUniqueName,String> tablesNamesSub,final boolean forErrorEst){
  ExprModifier v=new ExprModifier(){
    public Expr call(    Expr expr){
      if (expr instanceof FuncExpr) {
        FuncExpr f=(FuncExpr)expr;
        FuncExpr s=(FuncExpr)exprWithTableNamesSubstituted(expr,tablesNamesSub);
        List<Expr> samplingProbExprs=source.samplingProbabilityExprsFor(f);
        if (f.getFuncName().equals(FuncExpr.FuncName.COUNT)) {
          Expr est=FuncExpr.sum(scaleForSampling(samplingProbExprs));
          est=scaleWithPartitionSize(est,groupby,partitionCol,forErrorEst);
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.COUNT_DISTINCT)) {
          String dbname=vc.getDbms().getName();
          Expr scale=scaleForSampling(samplingProbExprs);
          Expr est=null;
          if (dbname.equals("String_Node_Str")) {
            est=new FuncExpr(FuncExpr.FuncName.IMPALA_APPROX_COUNT_DISTINCT,s.getUnaryExpr());
          }
 else {
            est=new FuncExpr(FuncExpr.FuncName.COUNT_DISTINCT,s.getUnaryExpr());
          }
          est=BinaryOpExpr.from(est,scale,"String_Node_Str");
          if (sampleType().equals("String_Node_Str")) {
            est=scaleWithPartitionSize(est,groupby,partitionCol,forErrorEst);
          }
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.SUM)) {
          Expr est=scaleForSampling(samplingProbExprs);
          est=FuncExpr.sum(BinaryOpExpr.from(s.getUnaryExpr(),est,"String_Node_Str"));
          est=scaleWithPartitionSize(est,groupby,partitionCol,forErrorEst);
          return est;
        }
 else         if (f.getFuncName().equals(FuncExpr.FuncName.AVG)) {
          Expr scale=scaleForSampling(samplingProbExprs);
          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);
}