Program: 51531
Project: 51531
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.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().isSybase()) {
          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");
        }
      }
      hierarchy.addToFrom(sqlQuery,level2.getKeyExp());
      String keyExp=level2.getKeyExp().getExpression(sqlQuery);
      if (columnCount > 0 && !sqlQuery.getDialect().allowsCompoundCountDistinct() && sqlQuery.getDialect().isSybase()) {
        keyExp="String_Node_Str" + columnList + "String_Node_Str";
      }
      columnList+=keyExp;
      if (level2.isUnique()) {
        break;
      }
      ++columnCount;
    }
    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.getKeyExp());
      sqlQuery.addSelect(level2.getKeyExp().getExpression(sqlQuery));
      if (level2.isUnique()) {
        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.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().isSybase()) {
          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");
        }
      }
      hierarchy.addToFrom(sqlQuery,level2.getKeyExp());
      String keyExp=level2.getKeyExp().getExpression(sqlQuery);
      if (columnCount > 0 && !sqlQuery.getDialect().allowsCompoundCountDistinct() && sqlQuery.getDialect().isSybase()) {
        keyExp="String_Node_Str" + columnList + "String_Node_Str";
      }
      columnList+=keyExp;
      if (level2.isUnique()) {
        break;
      }
      ++columnCount;
    }
    if (mustCount[0]) {
      sqlQuery.addSelect(columnList);
      sqlQuery.addOrderBy(columnList,true,false);
    }
 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.getKeyExp());
      sqlQuery.addSelect(level2.getKeyExp().getExpression(sqlQuery));
      if (level2.isUnique()) {
        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();
  }
}