Program: 51578
Project: 51578
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,Connection jdbcConnection,boolean[] mustCount){
  mustCount[0]=false;
  SqlQuery sqlQuery=newQuery(jdbcConnection,"String_Node_Str" + level);
  int levelDepth=level.getDepth();
  RolapLevel[] levels=(RolapLevel[])hierarchy.getLevels();
  if (levelDepth == levels.length) {
    sqlQuery.addSelect("String_Node_Str");
    hierarchy.addToFrom(sqlQuery,level.keyExp,null);
    return sqlQuery.toString();
  }
  if (!sqlQuery.allowsFromQuery()) {
    String columnList="String_Node_Str";
    for (int i=levelDepth; i >= 0; i--) {
      RolapLevel level2=levels[i];
      if (level2.isAll()) {
        continue;
      }
      if (columnList != "String_Node_Str") {
        if (!sqlQuery.allowsCompoundCountDistinct())         mustCount[0]=true;
        columnList+="String_Node_Str";
      }
      hierarchy.addToFrom(sqlQuery,level2.keyExp,null);
      columnList+=level2.keyExp.getExpression(sqlQuery);
      if (level2.unique) {
        break;
      }
    }
    if (mustCount[0]) {
      sqlQuery.addSelect(columnList);
      sqlQuery.addOrderBy(columnList);
    }
 else {
      sqlQuery.addSelect("String_Node_Str" + columnList + "String_Node_Str");
    }
    return sqlQuery.toString();
  }
 else {
    sqlQuery.setDistinct(true);
    for (int i=levelDepth; i >= 0; i--) {
      RolapLevel level2=levels[i];
      if (level2.isAll()) {
        continue;
      }
      hierarchy.addToFrom(sqlQuery,level2.keyExp,null);
      sqlQuery.addSelect(level2.keyExp.getExpression(sqlQuery));
      if (level2.unique) {
        break;
      }
    }
    SqlQuery outerQuery=newQuery(jdbcConnection,"String_Node_Str" + level);
    outerQuery.addSelect("String_Node_Str");
    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,Connection jdbcConnection,boolean[] mustCount){
  mustCount[0]=false;
  SqlQuery sqlQuery=newQuery(jdbcConnection,"String_Node_Str" + level);
  int levelDepth=level.getDepth();
  RolapLevel[] levels=(RolapLevel[])hierarchy.getLevels();
  if (levelDepth == levels.length) {
    sqlQuery.addSelect("String_Node_Str");
    hierarchy.addToFrom(sqlQuery,level.keyExp,null);
    return sqlQuery.toString();
  }
  if (!sqlQuery.allowsFromQuery()) {
    String columnList="String_Node_Str";
    for (int i=levelDepth; i >= 0; i--) {
      RolapLevel level2=levels[i];
      if (level2.isAll()) {
        continue;
      }
      if (columnList.length() > 0) {
        if (!sqlQuery.allowsCompoundCountDistinct())         mustCount[0]=true;
        columnList+="String_Node_Str";
      }
      hierarchy.addToFrom(sqlQuery,level2.keyExp,null);
      columnList+=level2.keyExp.getExpression(sqlQuery);
      if (level2.unique) {
        break;
      }
    }
    if (mustCount[0]) {
      sqlQuery.addSelect(columnList);
      sqlQuery.addOrderBy(columnList);
    }
 else {
      sqlQuery.addSelect("String_Node_Str" + columnList + "String_Node_Str");
    }
    return sqlQuery.toString();
  }
 else {
    sqlQuery.setDistinct(true);
    for (int i=levelDepth; i >= 0; i--) {
      RolapLevel level2=levels[i];
      if (level2.isAll()) {
        continue;
      }
      hierarchy.addToFrom(sqlQuery,level2.keyExp,null);
      sqlQuery.addSelect(level2.keyExp.getExpression(sqlQuery));
      if (level2.unique) {
        break;
      }
    }
    SqlQuery outerQuery=newQuery(jdbcConnection,"String_Node_Str" + level);
    outerQuery.addSelect("String_Node_Str");
    boolean failIfExists=true;
    outerQuery.addFrom(sqlQuery,"String_Node_Str",failIfExists);
    return outerQuery.toString();
  }
}