Program: 51381
Project: 51381
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" ORDER BY "country", "state_province", "city"</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>
 * @param sqlQuery the query object being constructed
 * @param level level to be added to the sql query
 * @param levelToColumnMap set only in the case of virtual cubes;provides the appropriate mapping for the base cube being processed
 * @param relationNamesToStarTableMap map to disambiguate table aliases
 * @param whichSelect describes whether this select belongs to a largerselect containing unions or this is a non-union select
 */
private void addLevelMemberSql(SqlQuery sqlQuery,RolapLevel level,Map<RolapLevel,RolapStar.Column> levelToColumnMap,Map<String,RolapStar.Table> relationNamesToStarTableMap,WhichSelect whichSelect){
  RolapHierarchy hierarchy=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;
    }
    MondrianDef.Expression keyExp=level2.getKeyExp();
    MondrianDef.Expression ordinalExp=level2.getOrdinalExp();
    MondrianDef.Expression captionExp=level2.getCaptionExp();
    String keySql=level2.getExpressionWithAlias(sqlQuery,levelToColumnMap,keyExp);
    String ordinalSql=ordinalSql=level2.getExpressionWithAlias(sqlQuery,levelToColumnMap,ordinalExp);
    String captionSql=null;
    if (captionExp != null) {
      captionSql=level2.getExpressionWithAlias(sqlQuery,levelToColumnMap,captionExp);
    }
    if (levelToColumnMap != null && relationNamesToStarTableMap != null) {
      RolapStar.Table targetTable=levelToColumnMap.get(level2).getTable();
      hierarchy.addToFrom(sqlQuery,relationNamesToStarTableMap,targetTable);
    }
 else {
      hierarchy.addToFrom(sqlQuery,keyExp);
      hierarchy.addToFrom(sqlQuery,ordinalExp);
      if (captionExp != null) {
        hierarchy.addToFrom(sqlQuery,captionExp);
      }
    }
    sqlQuery.addSelect(keySql);
    sqlQuery.addGroupBy(keySql);
    if (!ordinalSql.equals(keySql)) {
      sqlQuery.addSelect(ordinalSql);
      sqlQuery.addGroupBy(ordinalSql);
    }
    if (captionSql != null) {
      sqlQuery.addSelect(captionSql);
      sqlQuery.addGroupBy(captionSql);
    }
    constraint.addLevelConstraint(sqlQuery,null,level2,levelToColumnMap);
switch (whichSelect) {
case LAST:
      sqlQuery.addOrderBy(Integer.toString(sqlQuery.getCurrentSelectListSize()),true,false,true);
    break;
case ONLY:
  sqlQuery.addOrderBy(ordinalSql,true,false,true);
break;
}
RolapProperty[] properties=level2.getProperties();
for (RolapProperty property : properties) {
String propSql=level2.getExpressionWithAlias(sqlQuery,levelToColumnMap,property.getExp());
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" ORDER BY "country", "state_province", "city"</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>
 * @param sqlQuery the query object being constructed
 * @param level level to be added to the sql query
 * @param levelToColumnMap set only in the case of virtual cubes;provides the appropriate mapping for the base cube being processed
 * @param relationNamesToStarTableMap map to disambiguate table aliases
 * @param whichSelect describes whether this select belongs to a largerselect containing unions or this is a non-union select
 */
private void addLevelMemberSql(SqlQuery sqlQuery,RolapLevel level,Map<RolapLevel,RolapStar.Column> levelToColumnMap,Map<String,RolapStar.Table> relationNamesToStarTableMap,WhichSelect whichSelect){
  RolapHierarchy hierarchy=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;
    }
    MondrianDef.Expression keyExp=level2.getKeyExp();
    MondrianDef.Expression ordinalExp=level2.getOrdinalExp();
    MondrianDef.Expression captionExp=level2.getCaptionExp();
    String keySql=level2.getExpressionWithAlias(sqlQuery,levelToColumnMap,keyExp);
    String ordinalSql=level2.getExpressionWithAlias(sqlQuery,levelToColumnMap,ordinalExp);
    String captionSql=null;
    if (captionExp != null) {
      captionSql=level2.getExpressionWithAlias(sqlQuery,levelToColumnMap,captionExp);
    }
    if (levelToColumnMap != null && relationNamesToStarTableMap != null) {
      RolapStar.Table targetTable=levelToColumnMap.get(level2).getTable();
      hierarchy.addToFrom(sqlQuery,relationNamesToStarTableMap,targetTable);
    }
 else {
      hierarchy.addToFrom(sqlQuery,keyExp);
      hierarchy.addToFrom(sqlQuery,ordinalExp);
      if (captionExp != null) {
        hierarchy.addToFrom(sqlQuery,captionExp);
      }
    }
    sqlQuery.addSelect(keySql);
    sqlQuery.addGroupBy(keySql);
    if (!ordinalSql.equals(keySql)) {
      sqlQuery.addSelect(ordinalSql);
      sqlQuery.addGroupBy(ordinalSql);
    }
    if (captionSql != null) {
      sqlQuery.addSelect(captionSql);
      sqlQuery.addGroupBy(captionSql);
    }
    constraint.addLevelConstraint(sqlQuery,null,level2,levelToColumnMap);
switch (whichSelect) {
case LAST:
      sqlQuery.addOrderBy(Integer.toString(sqlQuery.getCurrentSelectListSize()),true,false,true);
    break;
case ONLY:
  sqlQuery.addOrderBy(ordinalSql,true,false,true);
break;
}
RolapProperty[] properties=level2.getProperties();
for (RolapProperty property : properties) {
String propSql=level2.getExpressionWithAlias(sqlQuery,levelToColumnMap,property.getExp());
sqlQuery.addSelect(propSql);
sqlQuery.addGroupBy(propSql);
}
}
}