Program: 51027
Project: 51027
Buggy source:
/** 
 * Generates a SQL query to retrieve the values in this segment using an algorithm which converts distinct-aggregates to non-distinct aggregates over subqueries.
 * @param outerSqlQuery Query to modify
 * @param countOnly If true, only generate a single row: no need togenerate a GROUP BY clause or put any constraining columns in the SELECT clause
 */
protected void distinctGenerateSql(final SqlQuery outerSqlQuery,boolean countOnly){
  final Dialect dialect=outerSqlQuery.getDialect();
  final Dialect.DatabaseProduct databaseProduct=dialect.getDatabaseProduct();
  final SqlQuery innerSqlQuery=newSqlQuery();
  if (databaseProduct == Dialect.DatabaseProduct.GREENPLUM) {
    innerSqlQuery.setDistinct(false);
  }
 else {
    innerSqlQuery.setDistinct(true);
  }
  RolapStar.Column[] columns=getColumns();
  int arity=columns.length;
  for (int i=0; i < arity; i++) {
    RolapStar.Column column=columns[i];
    RolapStar.Table table=column.getTable();
    if (table.isFunky()) {
      continue;
    }
    table.addToFrom(innerSqlQuery,false,true);
    String expr=column.generateExprString(innerSqlQuery);
    StarColumnPredicate predicate=getColumnPredicate(i);
    final String where=RolapStar.Column.createInExpr(expr,predicate,column.getDatatype(),innerSqlQuery);
    if (!where.equals("String_Node_Str")) {
      innerSqlQuery.addWhere(where);
    }
    if (countOnly) {
      continue;
    }
    final String alias="String_Node_Str" + i;
    innerSqlQuery.addSelect(expr,null,alias);
    if (databaseProduct == Dialect.DatabaseProduct.GREENPLUM) {
      innerSqlQuery.addGroupBy(expr,alias);
    }
    final String quotedAlias=dialect.quoteIdentifier(alias);
    outerSqlQuery.addSelectGroupBy(quotedAlias,null);
  }
  extraPredicates(innerSqlQuery);
  for (int i=0, count=getMeasureCount(); i < count; i++) {
    RolapStar.Measure measure=getMeasure(i);
    Util.assertTrue(measure.getTable() == getStar().getFactTable());
    measure.getTable().addToFrom(innerSqlQuery,false,true);
    String alias=getMeasureAlias(i);
    String expr=measure.generateExprString(outerSqlQuery);
    innerSqlQuery.addSelect(expr,null,alias);
    if (databaseProduct == Dialect.DatabaseProduct.GREENPLUM) {
      innerSqlQuery.addGroupBy(expr,alias);
    }
    outerSqlQuery.addSelect(measure.getAggregator().getNonDistinctAggregator().getExpression(dialect.quoteIdentifier(alias)),null);
  }
  outerSqlQuery.addFrom(innerSqlQuery,"String_Node_Str",true);
}

Fixed version (ground truth):
/** 
 * Generates a SQL query to retrieve the values in this segment using an algorithm which converts distinct-aggregates to non-distinct aggregates over subqueries.
 * @param outerSqlQuery Query to modify
 * @param countOnly If true, only generate a single row: no need togenerate a GROUP BY clause or put any constraining columns in the SELECT clause
 * @return A map of aliases used in the inner query if grouping setswere enabled.
 */
protected Map<String,String> distinctGenerateSql(final SqlQuery outerSqlQuery,boolean countOnly){
  final Dialect dialect=outerSqlQuery.getDialect();
  final Dialect.DatabaseProduct databaseProduct=dialect.getDatabaseProduct();
  final Map<String,String> groupingSetsAliases=new HashMap<String,String>();
  final SqlQuery innerSqlQuery=newSqlQuery();
  if (databaseProduct == Dialect.DatabaseProduct.GREENPLUM) {
    innerSqlQuery.setDistinct(false);
  }
 else {
    innerSqlQuery.setDistinct(true);
  }
  RolapStar.Column[] columns=getColumns();
  int arity=columns.length;
  for (int i=0; i < arity; i++) {
    RolapStar.Column column=columns[i];
    RolapStar.Table table=column.getTable();
    if (table.isFunky()) {
      continue;
    }
    table.addToFrom(innerSqlQuery,false,true);
    String expr=column.generateExprString(innerSqlQuery);
    StarColumnPredicate predicate=getColumnPredicate(i);
    final String where=RolapStar.Column.createInExpr(expr,predicate,column.getDatatype(),innerSqlQuery);
    if (!where.equals("String_Node_Str")) {
      innerSqlQuery.addWhere(where);
    }
    if (countOnly) {
      continue;
    }
    String alias="String_Node_Str" + i;
    alias=innerSqlQuery.addSelect(expr,null,alias);
    if (databaseProduct == Dialect.DatabaseProduct.GREENPLUM) {
      innerSqlQuery.addGroupBy(expr,alias);
    }
    final String quotedAlias=dialect.quoteIdentifier(alias);
    outerSqlQuery.addSelectGroupBy(quotedAlias,null);
    groupingSetsAliases.put(expr,dialect.quoteIdentifier("String_Node_Str" + alias));
  }
  extraPredicates(innerSqlQuery);
  for (int i=0, count=getMeasureCount(); i < count; i++) {
    RolapStar.Measure measure=getMeasure(i);
    Util.assertTrue(measure.getTable() == getStar().getFactTable());
    measure.getTable().addToFrom(innerSqlQuery,false,true);
    String alias=getMeasureAlias(i);
    String expr=measure.generateExprString(outerSqlQuery);
    innerSqlQuery.addSelect(expr,null,alias);
    if (databaseProduct == Dialect.DatabaseProduct.GREENPLUM) {
      innerSqlQuery.addGroupBy(expr,alias);
    }
    outerSqlQuery.addSelect(measure.getAggregator().getNonDistinctAggregator().getExpression(dialect.quoteIdentifier(alias)),null);
  }
  outerSqlQuery.addFrom(innerSqlQuery,"String_Node_Str",true);
  return groupingSetsAliases;
}