Program: 75960
Project: 75960
Buggy source:
/** 
 * Whether query is running based on the result set of report document or the data set.
 * @param dataEngine
 * @param queryDefn
 * @return true, running on result set
 * @throws DataException
 */
private static boolean runQueryOnRS(DataEngineImpl dataEngine,IQueryDefinition queryDefn) throws DataException {
  if (!queryDefn.usesDetails()) {
    queryDefn.getSorts().clear();
  }
  String queryResultID=queryDefn.getQueryResultsID();
  String rootQueryResultID=QueryResultIDUtil.get1PartID(queryResultID);
  String parentQueryResultID=null;
  if (rootQueryResultID != null)   parentQueryResultID=QueryResultIDUtil.get2PartID(queryResultID);
 else   rootQueryResultID=queryResultID;
  QueryResultInfo queryResultInfo=new QueryResultInfo(rootQueryResultID,parentQueryResultID,null,null,-1);
  RDLoad rdLoad=RDUtil.newLoad(dataEngine.getContext(),queryResultInfo);
  boolean runningOnRS=GroupDefnUtil.isEqualGroups(queryDefn.getGroups(),rdLoad.loadGroupDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE));
  if (runningOnRS == false)   return false;
  runningOnRS=!hasTopBottomNInFilter(queryDefn.getFilters());
  if (runningOnRS == false)   return false;
  runningOnRS=isCompatibleRSMap(rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE).getResultSetExpressions(),queryDefn.getResultSetExpressions());
  if (runningOnRS == false)   return false;
  runningOnRS=isCompatibleSubQuery(rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE),queryDefn);
  if (runningOnRS == false)   return false;
  IBaseQueryDefinition qd=rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE);
  List filters=qd.getFilters();
  if (FilterDefnUtil.isConflictFilter(filters,queryDefn.getFilters())) {
    runningOnRS=false;
    FilterDefnUtil.getRealFilterList(rdLoad.loadOriginalQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE).getFilters(),queryDefn.getFilters());
  }
  if (runningOnRS == false)   return false;
  if (hasSubquery(queryDefn)) {
    if (!QueryDefnUtil.isEqualSorts(queryDefn.getSorts(),qd.getSorts())) {
      runningOnRS=false;
    }
    Collection subqueries=queryDefn.getSubqueries();
    List gps=queryDefn.getGroups();
    if (gps != null && gps.size() > 0) {
      for (int i=0; i < gps.size(); i++) {
        subqueries.addAll(((IGroupDefinition)gps.get(i)).getSubqueries());
      }
    }
    Iterator it=subqueries.iterator();
    while (it.hasNext()) {
      IBaseQueryDefinition query=(IBaseQueryDefinition)it.next();
      if (!query.usesDetails())       query.getSorts().clear();
      if (query.getFilters() != null && query.getFilters().size() > 0) {
        runningOnRS=false;
        break;
      }
      List groups=query.getGroups();
      for (int i=0; i < groups.size(); i++) {
        List groupFilters=((IGroupDefinition)groups.get(i)).getFilters();
        if (groupFilters != null && groupFilters.size() > 0) {
          runningOnRS=false;
          break;
        }
      }
      if (runningOnRS == false)       break;
    }
  }
  if (runningOnRS == false)   return false;
  if (queryDefn.getFilters() != null && queryDefn.getFilters().size() > 0) {
    if (!isFiltersEquals(filters,queryDefn.getFilters()))     runningOnRS=queryDefn.getResultSetExpressions().values() == null || !hasAggregationOnRowObjects(queryDefn.getResultSetExpressions().values().iterator());
  }
  return runningOnRS;
}

Fixed version (ground truth):
/** 
 * Whether query is running based on the result set of report document or the data set.
 * @param dataEngine
 * @param queryDefn
 * @return true, running on result set
 * @throws DataException
 */
private static boolean runQueryOnRS(DataEngineImpl dataEngine,IQueryDefinition queryDefn) throws DataException {
  if (!queryDefn.usesDetails()) {
    queryDefn.getSorts().clear();
  }
  String queryResultID=queryDefn.getQueryResultsID();
  String rootQueryResultID=QueryResultIDUtil.get1PartID(queryResultID);
  String parentQueryResultID=null;
  if (rootQueryResultID != null)   parentQueryResultID=QueryResultIDUtil.get2PartID(queryResultID);
 else   rootQueryResultID=queryResultID;
  QueryResultInfo queryResultInfo=new QueryResultInfo(rootQueryResultID,parentQueryResultID,null,null,-1);
  RDLoad rdLoad=RDUtil.newLoad(dataEngine.getContext(),queryResultInfo);
  boolean runningOnRS=GroupDefnUtil.isEqualGroups(queryDefn.getGroups(),rdLoad.loadGroupDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE));
  if (runningOnRS == false)   return false;
  runningOnRS=!hasAggregationInFilter(queryDefn.getFilters());
  if (runningOnRS == false)   return false;
  runningOnRS=isCompatibleRSMap(rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE).getResultSetExpressions(),queryDefn.getResultSetExpressions());
  if (runningOnRS == false)   return false;
  runningOnRS=isCompatibleSubQuery(rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE),queryDefn);
  if (runningOnRS == false)   return false;
  IBaseQueryDefinition qd=rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE);
  List filters=qd.getFilters();
  if (FilterDefnUtil.isConflictFilter(filters,queryDefn.getFilters())) {
    runningOnRS=false;
    FilterDefnUtil.getRealFilterList(rdLoad.loadOriginalQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE).getFilters(),queryDefn.getFilters());
  }
  if (runningOnRS == false)   return false;
  if (hasSubquery(queryDefn)) {
    if (!QueryDefnUtil.isEqualSorts(queryDefn.getSorts(),qd.getSorts())) {
      runningOnRS=false;
    }
    Collection subqueries=queryDefn.getSubqueries();
    List gps=queryDefn.getGroups();
    if (gps != null && gps.size() > 0) {
      for (int i=0; i < gps.size(); i++) {
        subqueries.addAll(((IGroupDefinition)gps.get(i)).getSubqueries());
      }
    }
    Iterator it=subqueries.iterator();
    while (it.hasNext()) {
      IBaseQueryDefinition query=(IBaseQueryDefinition)it.next();
      if (!query.usesDetails())       query.getSorts().clear();
      if (query.getFilters() != null && query.getFilters().size() > 0) {
        runningOnRS=false;
        break;
      }
      List groups=query.getGroups();
      for (int i=0; i < groups.size(); i++) {
        List groupFilters=((IGroupDefinition)groups.get(i)).getFilters();
        if (groupFilters != null && groupFilters.size() > 0) {
          runningOnRS=false;
          break;
        }
      }
      if (runningOnRS == false)       break;
    }
  }
  if (runningOnRS == false)   return false;
  if (queryDefn.getFilters() != null && queryDefn.getFilters().size() > 0) {
    if (!isFiltersEquals(filters,queryDefn.getFilters()))     runningOnRS=queryDefn.getResultSetExpressions().values() == null || !hasAggregationOnRowObjects(queryDefn.getResultSetExpressions().values().iterator());
  }
  return runningOnRS;
}