Program: 95904
Project: 95904
Buggy source:
/** 
 * Composes a query that unions two aggregate results. The select list is inferred from a given query. For min and max aggregates, the extreme of two downstream aggregates is taken. For sum and count aggregates, their sum is computed.
 * @param rightQuery The query from which to infer a select list
 * @param leftBase The left individual aggregate
 * @param rightBase The right individual aggregate
 * @return The query that properly unions two aggregate queries.
 */
static SelectQuery composeUnionQuery(SelectQuery rightQuery,BaseTable leftBase,BaseTable rightBase){
  List<SelectItem> allItems=new ArrayList<>();
  List<String> groupAliasNames=new ArrayList<>();
  for (  SelectItem item : rightQuery.getSelectList()) {
    if (item.isAggregateColumn()) {
      if (!(item instanceof AliasedColumn) || !(((AliasedColumn)item).getColumn() instanceof ColumnOp)) {
        continue;
      }
      ColumnOp column=(ColumnOp)((AliasedColumn)item).getColumn();
      if (column.getOpType().equals("String_Node_Str") || column.getOpType().equals("String_Node_Str")) {
        AliasedColumn newColumn=new AliasedColumn(new ColumnOp(column.getOpType(),new BaseColumn(unionTableAlias,((AliasedColumn)item).getAliasName())),((AliasedColumn)item).getAliasName());
        allItems.add(newColumn);
      }
 else {
        AliasedColumn newColumn=new AliasedColumn(ColumnOp.sum(new BaseColumn(unionTableAlias,((AliasedColumn)item).getAliasName())),((AliasedColumn)item).getAliasName());
        allItems.add(newColumn);
      }
    }
 else {
      allItems.add(new AliasedColumn(new BaseColumn(unionTableAlias,((AliasedColumn)item).getAliasName()),((AliasedColumn)item).getAliasName()));
      groupAliasNames.add(((AliasedColumn)item).getAliasName());
    }
  }
  SelectQuery left=SelectQuery.create(new AsteriskColumn(),leftBase);
  SelectQuery right=SelectQuery.create(new AsteriskColumn(),rightBase);
  SetOperationRelation newBase=new SetOperationRelation(right,left,SetOperationRelation.SetOpType.unionAll);
  newBase.setAliasName(unionTableAlias);
  SelectQuery unionQuery=SelectQuery.create(allItems,newBase);
  for (  String a : groupAliasNames) {
    unionQuery.addGroupby(new AliasReference(a));
  }
  return unionQuery;
}

Fixed version (ground truth):
/** 
 * Composes a query that unions two aggregate results. The select list is inferred from a given query. For min and max aggregates, the extreme of two downstream aggregates is taken. For sum and count aggregates, their sum is computed.
 * @param rightQuery The query from which to infer a select list
 * @param leftBase The left individual aggregate
 * @param rightBase The right individual aggregate
 * @return The query that properly unions two aggregate queries.
 */
static SelectQuery composeUnionQuery(SelectQuery rightQuery,BaseTable leftBase,BaseTable rightBase){
  List<SelectItem> allItems=new ArrayList<>();
  List<GroupingAttribute> groupingAttributes=new ArrayList<>();
  for (  SelectItem item : rightQuery.getSelectList()) {
    if (item.isAggregateColumn()) {
      if (!(item instanceof AliasedColumn) || !(((AliasedColumn)item).getColumn() instanceof ColumnOp)) {
        continue;
      }
      ColumnOp column=(ColumnOp)((AliasedColumn)item).getColumn();
      if (column.getOpType().equals("String_Node_Str") || column.getOpType().equals("String_Node_Str")) {
        AliasedColumn newColumn=new AliasedColumn(new ColumnOp(column.getOpType(),new BaseColumn(unionTableAlias,((AliasedColumn)item).getAliasName())),((AliasedColumn)item).getAliasName());
        allItems.add(newColumn);
      }
 else {
        AliasedColumn newColumn=new AliasedColumn(ColumnOp.sum(new BaseColumn(unionTableAlias,((AliasedColumn)item).getAliasName())),((AliasedColumn)item).getAliasName());
        allItems.add(newColumn);
      }
    }
 else {
      UnnamedColumn col=new BaseColumn(unionTableAlias,((AliasedColumn)item).getAliasName());
      allItems.add(new AliasedColumn(col,((AliasedColumn)item).getAliasName()));
      groupingAttributes.add(col);
    }
  }
  SelectQuery left=SelectQuery.create(new AsteriskColumn(),leftBase);
  SelectQuery right=SelectQuery.create(new AsteriskColumn(),rightBase);
  SetOperationRelation newBase=new SetOperationRelation(right,left,SetOperationRelation.SetOpType.unionAll);
  newBase.setAliasName(unionTableAlias);
  SelectQuery unionQuery=SelectQuery.create(allItems,newBase);
  for (  GroupingAttribute a : groupingAttributes) {
    unionQuery.addGroupby(a);
  }
  return unionQuery;
}