Program: 51443
Project: 51443
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>See also   {@link SqlTupleReader#makeLevelMembersSql}.
 */
String makeChildMemberSql(RolapMember member,Connection jdbcConnection,MemberChildrenConstraint constraint){
  SqlQuery sqlQuery=newQuery(jdbcConnection,"String_Node_Str" + member);
  AggStar aggStar=chooseAggStar(constraint,member);
  constraint.addMemberConstraint(sqlQuery,aggStar,member);
  RolapLevel[] levels=(RolapLevel[])hierarchy.getLevels();
  RolapLevel level=levels[member.getLevel().getDepth() + 1];
  hierarchy.addToFrom(sqlQuery,level.getKeyExp());
  String q=level.getKeyExp().getExpression(sqlQuery);
  sqlQuery.addSelect(q);
  sqlQuery.addGroupBy(q);
  constraint.addLevelConstraint(sqlQuery,aggStar,level,null);
  if (level.hasCaptionColumn()) {
    MondrianDef.Expression captionExp=level.getCaptionExp();
    hierarchy.addToFrom(sqlQuery,captionExp);
    String captionSql=captionExp.getExpression(sqlQuery);
    sqlQuery.addSelect(captionSql);
    sqlQuery.addGroupBy(captionSql);
  }
  hierarchy.addToFrom(sqlQuery,level.getOrdinalExp());
  String orderBy=level.getOrdinalExp().getExpression(sqlQuery);
  sqlQuery.addOrderBy(orderBy,true,false,true);
  if (!orderBy.equals(q)) {
    sqlQuery.addGroupBy(orderBy);
  }
  RolapProperty[] properties=level.getRolapProperties();
  for (int j=0; j < properties.length; j++) {
    RolapProperty property=properties[j];
    final MondrianDef.Expression exp=property.getExp();
    hierarchy.addToFrom(sqlQuery,exp);
    final String s=exp.getExpression(sqlQuery);
    sqlQuery.addSelect(s);
    sqlQuery.addGroupBy(s);
  }
  return sqlQuery.toString();
}

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>See also   {@link SqlTupleReader#makeLevelMembersSql}.
 */
String makeChildMemberSql(RolapMember member,Connection jdbcConnection,MemberChildrenConstraint constraint){
  SqlQuery sqlQuery=newQuery(jdbcConnection,"String_Node_Str" + member);
  AggStar aggStar=chooseAggStar(constraint,member);
  constraint.addMemberConstraint(sqlQuery,aggStar,member);
  RolapLevel level=(RolapLevel)member.getLevel().getChildLevel();
  hierarchy.addToFrom(sqlQuery,level.getKeyExp());
  String q=level.getKeyExp().getExpression(sqlQuery);
  sqlQuery.addSelect(q);
  sqlQuery.addGroupBy(q);
  constraint.addLevelConstraint(sqlQuery,aggStar,level,null);
  if (level.hasCaptionColumn()) {
    MondrianDef.Expression captionExp=level.getCaptionExp();
    hierarchy.addToFrom(sqlQuery,captionExp);
    String captionSql=captionExp.getExpression(sqlQuery);
    sqlQuery.addSelect(captionSql);
    sqlQuery.addGroupBy(captionSql);
  }
  hierarchy.addToFrom(sqlQuery,level.getOrdinalExp());
  String orderBy=level.getOrdinalExp().getExpression(sqlQuery);
  sqlQuery.addOrderBy(orderBy,true,false,true);
  if (!orderBy.equals(q)) {
    sqlQuery.addGroupBy(orderBy);
  }
  RolapProperty[] properties=level.getRolapProperties();
  for (int j=0; j < properties.length; j++) {
    RolapProperty property=properties[j];
    final MondrianDef.Expression exp=property.getExp();
    hierarchy.addToFrom(sqlQuery,exp);
    final String s=exp.getExpression(sqlQuery);
    sqlQuery.addSelect(s);
    sqlQuery.addGroupBy(s);
  }
  return sqlQuery.toString();
}