Program: 50874
Project: 50874
Buggy source:
/** 
 * Generates the SQL statement to access the children of <code>member</code>. For example, <blockquote> <pre>SELECT "city" FROM "customer" WHERE "country" = 'USA' AND "state_province" = 'BC' GROUP BY "city"</pre> </blockquote> retrieves the children of the member <code>[Canada].[BC]</code>. <p>Note that this method is never called in the context of virtual cubes, it is only called on regular cubes. <p>See also   {@link SqlTupleReader#makeLevelMembersSql}.
 */
Pair<String,List<SqlStatement.Type>> makeChildMemberSql(RolapMember member,DataSource dataSource,MemberChildrenConstraint constraint){
  SqlQuery sqlQuery=SqlQuery.newQuery(dataSource,"String_Node_Str" + member);
  AggStar aggStar=chooseAggStar(constraint,member);
  constraint.addMemberConstraint(sqlQuery,null,aggStar,member);
  RolapLevel level=(RolapLevel)member.getLevel().getChildLevel();
  boolean levelCollapsed=(aggStar != null) && isLevelCollapsed(aggStar,(RolapCubeLevel)level);
  boolean multipleCols=SqlMemberSource.levelContainsMultipleColumns(level);
  if (levelCollapsed && !multipleCols) {
    RolapStar.Column starColumn=((RolapCubeLevel)level).getStarKeyColumn();
    int bitPos=starColumn.getBitPosition();
    AggStar.Table.Column aggColumn=aggStar.lookupColumn(bitPos);
    String q=aggColumn.generateExprString(sqlQuery);
    sqlQuery.addSelectGroupBy(q,starColumn.getInternalType());
    sqlQuery.addOrderBy(q,true,false,true);
    aggColumn.getTable().addToFrom(sqlQuery,false,true);
    return sqlQuery.toSqlAndTypes();
  }
  hierarchy.addToFrom(sqlQuery,level.getKeyExp());
  String q=level.getKeyExp().getExpression(sqlQuery);
  sqlQuery.addSelectGroupBy(q,level.getInternalType());
  constraint.addLevelConstraint(sqlQuery,null,aggStar,level);
  if (levelCollapsed) {
    RolapStar.Column starColumn=((RolapCubeLevel)level).getStarKeyColumn();
    int bitPos=starColumn.getBitPosition();
    AggStar.Table.Column aggColumn=aggStar.lookupColumn(bitPos);
    RolapStar.Condition condition=new RolapStar.Condition(level.getKeyExp(),aggColumn.getExpression());
    sqlQuery.addWhere(condition.toString(sqlQuery));
    hierarchy.addToFromInverse(sqlQuery,level.getKeyExp());
    RolapCubeLevel parentLevel=(RolapCubeLevel)level.getParentLevel();
    boolean isUnique=level.isUnique();
    while (parentLevel != null && !parentLevel.isAll() && !isUnique) {
      hierarchy.addToFromInverse(sqlQuery,parentLevel.getKeyExp());
      starColumn=parentLevel.getStarKeyColumn();
      bitPos=starColumn.getBitPosition();
      aggColumn=aggStar.lookupColumn(bitPos);
      condition=new RolapStar.Condition(parentLevel.getKeyExp(),aggColumn.getExpression());
      sqlQuery.addWhere(condition.toString(sqlQuery));
      parentLevel=parentLevel.getParentLevel();
    }
  }
  if (level.hasCaptionColumn()) {
    MondrianDef.Expression captionExp=level.getCaptionExp();
    if (!levelCollapsed) {
      hierarchy.addToFrom(sqlQuery,captionExp);
    }
    String captionSql=captionExp.getExpression(sqlQuery);
    sqlQuery.addSelectGroupBy(captionSql,null);
  }
  if (!levelCollapsed) {
    hierarchy.addToFrom(sqlQuery,level.getOrdinalExp());
  }
  String orderBy=level.getOrdinalExp().getExpression(sqlQuery);
  sqlQuery.addOrderBy(orderBy,true,false,true);
  if (!orderBy.equals(q)) {
    sqlQuery.addSelectGroupBy(orderBy,null);
  }
  RolapProperty[] properties=level.getProperties();
  for (  RolapProperty property : properties) {
    final MondrianDef.Expression exp=property.getExp();
    if (!levelCollapsed) {
      hierarchy.addToFrom(sqlQuery,exp);
    }
    final String s=exp.getExpression(sqlQuery);
    String alias=sqlQuery.addSelect(s,null);
    if (!sqlQuery.getDialect().allowsSelectNotInGroupBy() || !property.dependsOnLevelValue()) {
      sqlQuery.addGroupBy(s,alias);
    }
  }
  return sqlQuery.toSqlAndTypes();
}

Fixed version (ground truth):
/** 
 * Generates the SQL statement to access the children of <code>member</code>. For example, <blockquote> <pre>SELECT "city" FROM "customer" WHERE "country" = 'USA' AND "state_province" = 'BC' GROUP BY "city"</pre> </blockquote> retrieves the children of the member <code>[Canada].[BC]</code>. <p>Note that this method is never called in the context of virtual cubes, it is only called on regular cubes. <p>See also   {@link SqlTupleReader#makeLevelMembersSql}.
 */
Pair<String,List<SqlStatement.Type>> makeChildMemberSql(RolapMember member,DataSource dataSource,MemberChildrenConstraint constraint){
  SqlQuery sqlQuery=SqlQuery.newQuery(dataSource,"String_Node_Str" + member);
  AggStar aggStar=chooseAggStar(constraint,member);
  constraint.addMemberConstraint(sqlQuery,null,aggStar,member);
  RolapLevel level=(RolapLevel)member.getLevel().getChildLevel();
  boolean levelCollapsed=(aggStar != null) && isLevelCollapsed(aggStar,(RolapCubeLevel)level);
  boolean multipleCols=SqlMemberSource.levelContainsMultipleColumns(level);
  if (levelCollapsed && !multipleCols) {
    RolapStar.Column starColumn=((RolapCubeLevel)level).getStarKeyColumn();
    int bitPos=starColumn.getBitPosition();
    AggStar.Table.Column aggColumn=aggStar.lookupColumn(bitPos);
    String q=aggColumn.generateExprString(sqlQuery);
    final String qAlias=sqlQuery.addSelectGroupBy(q,starColumn.getInternalType());
    sqlQuery.addOrderBy(q,qAlias,true,false,true,true);
    aggColumn.getTable().addToFrom(sqlQuery,false,true);
    return sqlQuery.toSqlAndTypes();
  }
  hierarchy.addToFrom(sqlQuery,level.getKeyExp());
  String q=level.getKeyExp().getExpression(sqlQuery);
  String idAlias=sqlQuery.addSelectGroupBy(q,level.getInternalType());
  constraint.addLevelConstraint(sqlQuery,null,aggStar,level);
  if (levelCollapsed) {
    RolapStar.Column starColumn=((RolapCubeLevel)level).getStarKeyColumn();
    int bitPos=starColumn.getBitPosition();
    AggStar.Table.Column aggColumn=aggStar.lookupColumn(bitPos);
    RolapStar.Condition condition=new RolapStar.Condition(level.getKeyExp(),aggColumn.getExpression());
    sqlQuery.addWhere(condition.toString(sqlQuery));
    hierarchy.addToFromInverse(sqlQuery,level.getKeyExp());
    RolapCubeLevel parentLevel=(RolapCubeLevel)level.getParentLevel();
    boolean isUnique=level.isUnique();
    while (parentLevel != null && !parentLevel.isAll() && !isUnique) {
      hierarchy.addToFromInverse(sqlQuery,parentLevel.getKeyExp());
      starColumn=parentLevel.getStarKeyColumn();
      bitPos=starColumn.getBitPosition();
      aggColumn=aggStar.lookupColumn(bitPos);
      condition=new RolapStar.Condition(parentLevel.getKeyExp(),aggColumn.getExpression());
      sqlQuery.addWhere(condition.toString(sqlQuery));
      parentLevel=parentLevel.getParentLevel();
    }
  }
  if (level.hasCaptionColumn()) {
    MondrianDef.Expression captionExp=level.getCaptionExp();
    if (!levelCollapsed) {
      hierarchy.addToFrom(sqlQuery,captionExp);
    }
    String captionSql=captionExp.getExpression(sqlQuery);
    sqlQuery.addSelectGroupBy(captionSql,null);
  }
  if (!levelCollapsed) {
    hierarchy.addToFrom(sqlQuery,level.getOrdinalExp());
  }
  final String orderBy=level.getOrdinalExp().getExpression(sqlQuery);
  if (!orderBy.equals(q)) {
    String orderAlias=sqlQuery.addSelectGroupBy(orderBy,null);
    sqlQuery.addOrderBy(orderBy,orderAlias,true,false,true,true);
  }
 else {
    sqlQuery.addOrderBy(q,idAlias,true,false,true,true);
  }
  RolapProperty[] properties=level.getProperties();
  for (  RolapProperty property : properties) {
    final MondrianDef.Expression exp=property.getExp();
    if (!levelCollapsed) {
      hierarchy.addToFrom(sqlQuery,exp);
    }
    final String s=exp.getExpression(sqlQuery);
    String alias=sqlQuery.addSelect(s,null);
    if (!sqlQuery.getDialect().allowsSelectNotInGroupBy() || !property.dependsOnLevelValue()) {
      sqlQuery.addGroupBy(s,alias);
    }
  }
  return sqlQuery.toSqlAndTypes();
}