Program: 96065
Project: 96065
Buggy source:
/** 
 * Parses a depth-one select statement. If there exist subqueries, this function will be called recursively.
 */
@Override public ExactRelation visitQuery_specification(VerdictSQLParser.Query_specificationContext ctx){
  List<ExactRelation> tableSources=new ArrayList<ExactRelation>();
  for (  Table_sourceContext s : ctx.table_source()) {
    TableSourceExtractor e=new TableSourceExtractor();
    ExactRelation r1=e.visit(s);
    tableSources.add(r1);
  }
  Cond where=null;
  if (ctx.WHERE() != null) {
    where=Cond.from(vc,ctx.where);
    ColNameResolver resolver=new ColNameResolver(tableAliasAndColNames);
    where=resolver.visit(where);
  }
  ExactRelation joinedTabeSource=null;
  while (where != null && tableSources.size() > 1) {
    Pair<Cond,Pair<ExactRelation,ExactRelation>> joinCondAndTabName=where.searchForJoinCondition(tableSources);
    if (joinCondAndTabName == null) {
      break;
    }
    Cond joinCond=joinCondAndTabName.getKey();
    Pair<ExactRelation,ExactRelation> pairsToJoin=joinCondAndTabName.getValue();
    List<ExactRelation> newTableSources=new ArrayList<ExactRelation>();
    ExactRelation joined=JoinedRelation.from(vc,pairsToJoin.getLeft(),pairsToJoin.getRight(),joinCond);
    newTableSources.add(joined);
    for (    ExactRelation t : tableSources) {
      if (t != pairsToJoin.getLeft() && t != pairsToJoin.getRight()) {
        newTableSources.add(t);
      }
    }
    tableSources=newTableSources;
    where=where.remove(joinCond);
  }
  for (  ExactRelation r : tableSources) {
    if (joinedTabeSource == null) {
      joinedTabeSource=r;
    }
 else {
      joinedTabeSource=new JoinedRelation(vc,joinedTabeSource,r,null);
    }
  }
  if (where != null) {
    joinedTabeSource=new FilteredRelation(vc,joinedTabeSource,where);
  }
  SelectListExtractor select=new SelectListExtractor();
  Triple<List<SelectElem>,List<SelectElem>,List<SelectElem>> elems=select.visit(ctx.select_list());
  List<SelectElem> nonaggs=elems.getLeft();
  List<SelectElem> aggs=elems.getMiddle();
  List<SelectElem> bothInOrder=elems.getRight();
  TableSourceResolver resolver=new TableSourceResolver(vc,tableAliasAndColNames);
  nonaggs=replaceTableNamesWithAliasesIn(nonaggs,resolver);
  aggs=replaceTableNamesWithAliasesIn(aggs,resolver);
  bothInOrder=replaceTableNamesWithAliasesIn(bothInOrder,resolver);
  selectElems=bothInOrder;
  if (aggs.size() == 0) {
    joinedTabeSource=new ProjectedRelation(vc,joinedTabeSource,bothInOrder);
  }
 else {
    if (ctx.GROUP() != null) {
      List<Expr> groupby=new ArrayList<Expr>();
      for (      Group_by_itemContext g : ctx.group_by_item()) {
        Expr gexpr=resolver.visit(Expr.from(vc,g.expression()));
        boolean aliasFound=false;
        if (!aliasFound) {
          groupby.add(gexpr);
        }
      }
      joinedTabeSource=new GroupedRelation(vc,joinedTabeSource,groupby);
    }
    joinedTabeSource=new AggregatedRelation(vc,joinedTabeSource,bothInOrder);
  }
  return joinedTabeSource;
}

Fixed version (ground truth):
/** 
 * Parses a depth-one select statement. If there exist subqueries, this function will be called recursively.
 */
@Override public ExactRelation visitQuery_specification(VerdictSQLParser.Query_specificationContext ctx){
  List<ExactRelation> tableSources=new ArrayList<ExactRelation>();
  for (  Table_sourceContext s : ctx.table_source()) {
    TableSourceExtractor e=new TableSourceExtractor();
    ExactRelation r1=e.visit(s);
    tableSources.add(r1);
  }
  Cond where=null;
  if (ctx.WHERE() != null) {
    where=Cond.from(vc,ctx.where);
    ColNameResolver resolver=new ColNameResolver(tableAliasAndColNames);
    where=resolver.visit(where);
  }
  ExactRelation joinedTabeSource=null;
  while (where != null && tableSources.size() > 0) {
    Pair<Cond,Pair<ExactRelation,ExactRelation>> joinCondAndTabName=where.searchForJoinCondition(tableSources);
    if (joinCondAndTabName == null) {
      break;
    }
    Cond joinCond=joinCondAndTabName.getKey();
    Pair<ExactRelation,ExactRelation> pairsToJoin=joinCondAndTabName.getValue();
    List<ExactRelation> newTableSources=new ArrayList<ExactRelation>();
    ExactRelation joined=JoinedRelation.from(vc,pairsToJoin.getLeft(),pairsToJoin.getRight(),joinCond);
    newTableSources.add(joined);
    for (    ExactRelation t : tableSources) {
      if (t != pairsToJoin.getLeft() && t != pairsToJoin.getRight()) {
        newTableSources.add(t);
      }
    }
    tableSources=newTableSources;
    where=where.remove(joinCond);
  }
  for (  ExactRelation r : tableSources) {
    if (joinedTabeSource == null) {
      joinedTabeSource=r;
    }
 else {
      joinedTabeSource=new JoinedRelation(vc,joinedTabeSource,r,null);
    }
  }
  if (where != null) {
    joinedTabeSource=new FilteredRelation(vc,joinedTabeSource,where);
  }
  SelectListExtractor select=new SelectListExtractor();
  Triple<List<SelectElem>,List<SelectElem>,List<SelectElem>> elems=select.visit(ctx.select_list());
  List<SelectElem> nonaggs=elems.getLeft();
  List<SelectElem> aggs=elems.getMiddle();
  List<SelectElem> bothInOrder=elems.getRight();
  TableSourceResolver resolver=new TableSourceResolver(vc,tableAliasAndColNames);
  nonaggs=replaceTableNamesWithAliasesIn(nonaggs,resolver);
  aggs=replaceTableNamesWithAliasesIn(aggs,resolver);
  bothInOrder=replaceTableNamesWithAliasesIn(bothInOrder,resolver);
  selectElems=bothInOrder;
  if (aggs.size() == 0) {
    joinedTabeSource=new ProjectedRelation(vc,joinedTabeSource,bothInOrder);
  }
 else {
    if (ctx.GROUP() != null) {
      List<Expr> groupby=new ArrayList<Expr>();
      for (      Group_by_itemContext g : ctx.group_by_item()) {
        Expr gexpr=resolver.visit(Expr.from(vc,g.expression()));
        boolean aliasFound=false;
        if (!aliasFound) {
          groupby.add(gexpr);
        }
      }
      joinedTabeSource=new GroupedRelation(vc,joinedTabeSource,groupby);
    }
    joinedTabeSource=new AggregatedRelation(vc,joinedTabeSource,bothInOrder);
  }
  return joinedTabeSource;
}