Program: 51535
Project: 51535
Buggy source:
/** 
 * Generates the SQL statement to access members of <code>level</code>. For example, <blockquote> <pre>SELECT "country", "state_province", "city" FROM "customer" GROUP BY "country", "state_province", "city", "init", "bar"</pre> </blockquote> accesses the "City" level of the "Customers" hierarchy. Note that:<ul> <li><code>"country", "state_province"</code> are the parent keys;</li> <li><code>"city"</code> is the level key;</li> <li><code>"init", "bar"</code> are member properties.</li> </ul>
 */
void addLevelMemberSql(SqlQuery sqlQuery,RolapLevel level){
  RolapHierarchy hierarchy=(RolapHierarchy)level.getHierarchy();
  RolapLevel[] levels=(RolapLevel[])hierarchy.getLevels();
  int levelDepth=level.getDepth();
  for (int i=0; i <= levelDepth; i++) {
    RolapLevel level2=levels[i];
    if (level2.isAll()) {
      continue;
    }
    hierarchy.addToFrom(sqlQuery,level2.getKeyExp());
    String keySql=level2.getKeyExp().getExpression(sqlQuery);
    sqlQuery.addSelect(keySql);
    sqlQuery.addGroupBy(keySql);
    hierarchy.addToFrom(sqlQuery,level2.getOrdinalExp());
    constraint.addLevelConstraint(sqlQuery,level2);
    if (level2.hasCaptionColumn()) {
      MondrianDef.Expression captionExp=level2.getCaptionExp();
      hierarchy.addToFrom(sqlQuery,captionExp);
      String captionSql=captionExp.getExpression(sqlQuery);
      sqlQuery.addSelect(captionSql);
      sqlQuery.addGroupBy(captionSql);
    }
    String ordinalSql=level2.getOrdinalExp().getExpression(sqlQuery);
    sqlQuery.addGroupBy(ordinalSql);
    sqlQuery.addOrderBy(ordinalSql);
    RolapProperty[] properties=level2.getRolapProperties();
    for (int j=0; j < properties.length; j++) {
      RolapProperty property=properties[j];
      String propSql=property.getExp().getExpression(sqlQuery);
      sqlQuery.addSelect(propSql);
      sqlQuery.addGroupBy(propSql);
    }
  }
}

Fixed version (ground truth):
/** 
 * Generates the SQL statement to access members of <code>level</code>. For example, <blockquote> <pre>SELECT "country", "state_province", "city" FROM "customer" GROUP BY "country", "state_province", "city", "init", "bar"</pre> </blockquote> accesses the "City" level of the "Customers" hierarchy. Note that:<ul> <li><code>"country", "state_province"</code> are the parent keys;</li> <li><code>"city"</code> is the level key;</li> <li><code>"init", "bar"</code> are member properties.</li> </ul>
 */
void addLevelMemberSql(SqlQuery sqlQuery,RolapLevel level){
  RolapHierarchy hierarchy=(RolapHierarchy)level.getHierarchy();
  RolapLevel[] levels=(RolapLevel[])hierarchy.getLevels();
  int levelDepth=level.getDepth();
  for (int i=0; i <= levelDepth; i++) {
    RolapLevel level2=levels[i];
    if (level2.isAll()) {
      continue;
    }
    hierarchy.addToFrom(sqlQuery,level2.getKeyExp());
    String keySql=level2.getKeyExp().getExpression(sqlQuery);
    sqlQuery.addSelect(keySql);
    sqlQuery.addGroupBy(keySql);
    hierarchy.addToFrom(sqlQuery,level2.getOrdinalExp());
    constraint.addLevelConstraint(sqlQuery,level2);
    if (level2.hasCaptionColumn()) {
      MondrianDef.Expression captionExp=level2.getCaptionExp();
      hierarchy.addToFrom(sqlQuery,captionExp);
      String captionSql=captionExp.getExpression(sqlQuery);
      sqlQuery.addSelect(captionSql);
      sqlQuery.addGroupBy(captionSql);
    }
    String ordinalSql=level2.getOrdinalExp().getExpression(sqlQuery);
    sqlQuery.addGroupBy(ordinalSql);
    sqlQuery.addOrderBy(ordinalSql,true,false);
    RolapProperty[] properties=level2.getRolapProperties();
    for (int j=0; j < properties.length; j++) {
      RolapProperty property=properties[j];
      String propSql=property.getExp().getExpression(sqlQuery);
      sqlQuery.addSelect(propSql);
      sqlQuery.addGroupBy(propSql);
    }
  }
}