Program: 50995
Project: 50995
Buggy source:
/** 
 * Generates the SQL statement to count the members in <code>level</code>. For example, <blockquote> <pre>SELECT count(*) FROM ( SELECT DISTINCT "country", "state_province" FROM "customer") AS "init"</pre> </blockquote> counts the non-leaf "state_province" level. MySQL doesn't allow SELECT-in-FROM, so we use the syntax<blockquote> <pre>SELECT count(DISTINCT "country", "state_province") FROM "customer"</pre> </blockquote>. The leaf level requires a different query:<blockquote> <pre>SELECT count(*) FROM "customer"</pre> </blockquote> counts the leaf "name" level of the "customer" hierarchy.
 */
private String makeLevelMemberCountSql(RolapLevel level,DataSource dataSource,boolean[] mustCount){
  mustCount[0]=false;
  SqlQuery sqlQuery=SqlQuery.newQuery(dataSource,"String_Node_Str" + level);
  int levelDepth=level.getDepth();
  RolapLevel[] levels=(RolapLevel[])hierarchy.getLevels();
  if (levelDepth == levels.length) {
    sqlQuery.addSelect("String_Node_Str",null);
    hierarchy.addToFrom(sqlQuery,level.getKeyExp());
    return sqlQuery.toString();
  }
  if (!sqlQuery.getDialect().allowsFromQuery()) {
    String columnList="String_Node_Str";
    int columnCount=0;
    for (int i=levelDepth; i >= 0; i--) {
      RolapLevel level2=levels[i];
      if (level2.isAll()) {
        continue;
      }
      if (columnCount > 0) {
        if (sqlQuery.getDialect().allowsCompoundCountDistinct()) {
          columnList+="String_Node_Str";
        }
 else         if (true) {
          mustCount[0]=true;
        }
 else         if (sqlQuery.getDialect().getDatabaseProduct() == Dialect.DatabaseProduct.SYBASE) {
          if (columnCount == 1) {
            columnList="String_Node_Str" + columnList + "String_Node_Str";
          }
          columnList+="String_Node_Str";
        }
 else {
          throw Util.newInternal("String_Node_Str" + level.getUniqueName() + "String_Node_Str"+ "String_Node_Str");
        }
      }
      hierarchy.addToFrom(sqlQuery,level2.getKeyExp());
      String keyExp=level2.getKeyExp().getExpression(sqlQuery);
      if (columnCount > 0 && !sqlQuery.getDialect().allowsCompoundCountDistinct() && sqlQuery.getDialect().getDatabaseProduct() == Dialect.DatabaseProduct.SYBASE) {
        keyExp="String_Node_Str" + columnList + "String_Node_Str";
      }
      columnList+=keyExp;
      if (level2.isUnique()) {
        break;
      }
      ++columnCount;
    }
    if (mustCount[0]) {
      sqlQuery.addSelect(columnList,null);
      sqlQuery.addOrderBy(columnList,true,false,true);
    }
 else {
      sqlQuery.addSelect("String_Node_Str" + columnList + "String_Node_Str",null);
    }
    return sqlQuery.toString();
  }
 else {
    sqlQuery.setDistinct(true);
    for (int i=levelDepth; i >= 0; i--) {
      RolapLevel level2=levels[i];
      if (level2.isAll()) {
        continue;
      }
      MondrianDef.Expression keyExp=level2.getKeyExp();
      hierarchy.addToFrom(sqlQuery,keyExp);
      sqlQuery.addSelect(keyExp.getExpression(sqlQuery),null);
      if (level2.isUnique()) {
        break;
      }
    }
    SqlQuery outerQuery=SqlQuery.newQuery(dataSource,"String_Node_Str" + level);
    outerQuery.addSelect("String_Node_Str",null);
    boolean failIfExists=true;
    outerQuery.addFrom(sqlQuery,"String_Node_Str",failIfExists);
    return outerQuery.toString();
  }
}

Fixed version (ground truth):
/** 
 * Generates the SQL statement to count the members in <code>level</code>. For example, <blockquote> <pre>SELECT count(*) FROM ( SELECT DISTINCT "country", "state_province" FROM "customer") AS "init"</pre> </blockquote> counts the non-leaf "state_province" level. MySQL doesn't allow SELECT-in-FROM, so we use the syntax<blockquote> <pre>SELECT count(DISTINCT "country", "state_province") FROM "customer"</pre> </blockquote>. The leaf level requires a different query:<blockquote> <pre>SELECT count(*) FROM "customer"</pre> </blockquote> counts the leaf "name" level of the "customer" hierarchy.
 */
private String makeLevelMemberCountSql(RolapLevel level,DataSource dataSource,boolean[] mustCount){
  mustCount[0]=false;
  SqlQuery sqlQuery=SqlQuery.newQuery(dataSource,"String_Node_Str" + level);
  int levelDepth=level.getDepth();
  RolapLevel[] levels=(RolapLevel[])hierarchy.getLevels();
  if (levelDepth == levels.length) {
    sqlQuery.addSelect("String_Node_Str",null);
    hierarchy.addToFrom(sqlQuery,level.getKeyExp());
    return sqlQuery.toString();
  }
  if (!sqlQuery.getDialect().allowsFromQuery()) {
    List<String> columnList=new ArrayList<String>();
    int columnCount=0;
    for (int i=levelDepth; i >= 0; i--) {
      RolapLevel level2=levels[i];
      if (level2.isAll()) {
        continue;
      }
      if (columnCount > 0) {
        if (sqlQuery.getDialect().allowsCompoundCountDistinct()) {
        }
 else         if (true) {
          mustCount[0]=true;
        }
      }
      hierarchy.addToFrom(sqlQuery,level2.getKeyExp());
      String keyExp=level2.getKeyExp().getExpression(sqlQuery);
      if (columnCount > 0 && !sqlQuery.getDialect().allowsCompoundCountDistinct() && sqlQuery.getDialect().getDatabaseProduct() == Dialect.DatabaseProduct.SYBASE) {
        keyExp="String_Node_Str" + columnList + "String_Node_Str";
      }
      columnList.add(keyExp);
      if (level2.isUnique()) {
        break;
      }
      ++columnCount;
    }
    if (mustCount[0]) {
      for (      String colDef : columnList) {
        sqlQuery.addSelect(colDef,null);
        sqlQuery.addOrderBy(colDef,true,false,true);
      }
    }
 else {
      int i=0;
      StringBuilder sb=new StringBuilder();
      for (      String colDef : columnList) {
        if (i > 0) {
          sb.append("String_Node_Str");
        }
        sb.append(colDef);
      }
      sqlQuery.addSelect("String_Node_Str" + sb.toString() + "String_Node_Str",null);
    }
    return sqlQuery.toString();
  }
 else {
    sqlQuery.setDistinct(true);
    for (int i=levelDepth; i >= 0; i--) {
      RolapLevel level2=levels[i];
      if (level2.isAll()) {
        continue;
      }
      MondrianDef.Expression keyExp=level2.getKeyExp();
      hierarchy.addToFrom(sqlQuery,keyExp);
      sqlQuery.addSelect(keyExp.getExpression(sqlQuery),null);
      if (level2.isUnique()) {
        break;
      }
    }
    SqlQuery outerQuery=SqlQuery.newQuery(dataSource,"String_Node_Str" + level);
    outerQuery.addSelect("String_Node_Str",null);
    boolean failIfExists=true;
    outerQuery.addFrom(sqlQuery,"String_Node_Str",failIfExists);
    return outerQuery.toString();
  }
}