Program: 51687
Project: 51687
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 "foo"</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.
 */
String makeLevelMemberCountSql(RolapLevel level){
  SqlQuery sqlQuery=newQuery("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,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()) {
          throw Util.newInternal("String_Node_Str" + level.getUniqueName() + "String_Node_Str");
        }
        columnList+="String_Node_Str";
      }
      hierarchy.addToFrom(sqlQuery,level2,null);
      columnList+=level2.nameExp.getExpression(sqlQuery);
      if (level2.unique) {
        break;
      }
    }
    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,null);
      sqlQuery.addSelect(level2.nameExp.getExpression(sqlQuery));
      if (level2.unique) {
        break;
      }
    }
    SqlQuery outerQuery=newQuery("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 "foo"</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.
 */
String makeLevelMemberCountSql(RolapLevel level){
  SqlQuery sqlQuery=newQuery("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.nameExp,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()) {
          throw Util.newInternal("String_Node_Str" + level.getUniqueName() + "String_Node_Str");
        }
        columnList+="String_Node_Str";
      }
      hierarchy.addToFrom(sqlQuery,level2.nameExp,null);
      columnList+=level2.nameExp.getExpression(sqlQuery);
      if (level2.unique) {
        break;
      }
    }
    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.nameExp,null);
      sqlQuery.addSelect(level2.nameExp.getExpression(sqlQuery));
      if (level2.unique) {
        break;
      }
    }
    SqlQuery outerQuery=newQuery("String_Node_Str" + level);
    outerQuery.addSelect("String_Node_Str");
    boolean failIfExists=true;
    outerQuery.addFrom(sqlQuery,"String_Node_Str",failIfExists);
    return outerQuery.toString();
  }
}