Program: 73021
Project: 73021
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 int runQueryOnRS(DataEngineImpl dataEngine,IQueryDefinition queryDefn) throws DataException {
  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.getSession().getTempDir(),dataEngine.getContext(),queryResultInfo);
  IBaseQueryDefinition previousQueryDefn=rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.PARENT_SCOPE);
  if (QueryCompUtil.isIVQueryDefnEqual(previousQueryDefn,queryDefn)) {
    return BASED_ON_PRESENTATION;
  }
  if (previousQueryDefn.getSorts() != null && previousQueryDefn.getSorts().size() > 0 && (queryDefn.getSorts() == null || queryDefn.getSorts().size() == 0)) {
    return BASED_ON_DATASET;
  }
  if (!queryDefn.usesDetails()) {
    queryDefn.getSorts().clear();
  }
  boolean runningOnRS=GroupDefnUtil.isEqualGroups(queryDefn.getGroups(),rdLoad.loadGroupDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE));
  if (runningOnRS == false)   return BASED_ON_DATASET;
  runningOnRS=!hasAggregationInFilter(queryDefn.getFilters());
  if (runningOnRS == false)   return BASED_ON_DATASET;
  runningOnRS=isCompatibleRSMap(rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE).getBindings(),queryDefn.getBindings());
  if (runningOnRS == false)   return BASED_ON_DATASET;
  IBaseQueryDefinition qd=rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE);
  runningOnRS=isCompatibleSubQuery(qd,queryDefn);
  if (runningOnRS == false)   return BASED_ON_DATASET;
  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 BASED_ON_DATASET;
  if (!QueryCompUtil.isEqualSorts(queryDefn.getSorts(),qd.getSorts())) {
    Iterator bindings=queryDefn.getBindings().values().iterator();
    while (bindings.hasNext()) {
      IBinding binding=(IBinding)bindings.next();
      final String aggrName=binding.getAggrFunction();
      if (aggrName != null) {
        IAggrFunction aggrFunction=AggregationManager.getInstance().getAggregation(aggrName);
        if (aggrFunction.isDataOrderSensitive()) {
          return BASED_ON_DATASET;
        }
      }
      if (binding.getExpression() != null && binding.getExpression() instanceof IScriptExpression) {
        IScriptExpression expr=(IScriptExpression)binding.getExpression();
        if (ExpressionUtil.hasAggregation(expr.getText())) {
          if (expr.getText().matches("String_Node_Str") || expr.getText().matches("String_Node_Str")) {
            return BASED_ON_DATASET;
          }
        }
      }
    }
  }
  if (!isBindingReferenceSort(queryDefn.getSorts()))   return BASED_ON_DATASET;
  if (hasSubquery(queryDefn)) {
    if (hasSubQueryInDetail(queryDefn.getSubqueries()))     return BASED_ON_DATASET;
    if (!QueryCompUtil.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();
      if (groups != null && !groups.isEmpty())       runningOnRS=false;
      if (runningOnRS == false)       break;
    }
  }
  if (runningOnRS == false)   return BASED_ON_DATASET;
  if (queryDefn.getFilters() != null && queryDefn.getFilters().size() > 0) {
    if (!isFiltersEquals(filters,queryDefn.getFilters()))     runningOnRS=queryDefn.getBindings().values() == null || !hasAggregationOnRowObjects(queryDefn.getBindings().values().iterator());
  }
  return runningOnRS ? BASED_ON_RESULTSET : BASED_ON_DATASET;
}

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 int runQueryOnRS(DataEngineImpl dataEngine,IQueryDefinition queryDefn) throws DataException {
  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.getSession().getTempDir(),dataEngine.getContext(),queryResultInfo);
  IBaseQueryDefinition previousQueryDefn=rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.PARENT_SCOPE);
  if (QueryCompUtil.isIVQueryDefnEqual(dataEngine.getContext().getMode(),previousQueryDefn,queryDefn)) {
    return BASED_ON_PRESENTATION;
  }
  if (previousQueryDefn.getSorts() != null && previousQueryDefn.getSorts().size() > 0 && (queryDefn.getSorts() == null || queryDefn.getSorts().size() == 0)) {
    return BASED_ON_DATASET;
  }
  if (!queryDefn.usesDetails()) {
    queryDefn.getSorts().clear();
  }
  boolean runningOnRS=GroupDefnUtil.isEqualGroups(queryDefn.getGroups(),rdLoad.loadGroupDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE));
  if (runningOnRS == false)   return BASED_ON_DATASET;
  runningOnRS=!hasAggregationInFilter(queryDefn.getFilters());
  if (runningOnRS == false)   return BASED_ON_DATASET;
  runningOnRS=isCompatibleRSMap(rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE).getBindings(),queryDefn.getBindings());
  if (runningOnRS == false)   return BASED_ON_DATASET;
  IBaseQueryDefinition qd=rdLoad.loadQueryDefn(StreamManager.ROOT_STREAM,StreamManager.BASE_SCOPE);
  runningOnRS=isCompatibleSubQuery(qd,queryDefn);
  if (runningOnRS == false)   return BASED_ON_DATASET;
  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 BASED_ON_DATASET;
  if (!QueryCompUtil.isEqualSorts(queryDefn.getSorts(),qd.getSorts())) {
    Iterator bindings=queryDefn.getBindings().values().iterator();
    while (bindings.hasNext()) {
      IBinding binding=(IBinding)bindings.next();
      final String aggrName=binding.getAggrFunction();
      if (aggrName != null) {
        IAggrFunction aggrFunction=AggregationManager.getInstance().getAggregation(aggrName);
        if (aggrFunction.isDataOrderSensitive()) {
          return BASED_ON_DATASET;
        }
      }
      if (binding.getExpression() != null && binding.getExpression() instanceof IScriptExpression) {
        IScriptExpression expr=(IScriptExpression)binding.getExpression();
        if (ExpressionUtil.hasAggregation(expr.getText())) {
          if (expr.getText().matches("String_Node_Str") || expr.getText().matches("String_Node_Str")) {
            return BASED_ON_DATASET;
          }
        }
      }
    }
  }
  if (!isBindingReferenceSort(queryDefn.getSorts()))   return BASED_ON_DATASET;
  if (hasSubquery(queryDefn)) {
    if (hasSubQueryInDetail(queryDefn.getSubqueries()))     return BASED_ON_DATASET;
    if (!QueryCompUtil.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();
      if (groups != null && !groups.isEmpty())       runningOnRS=false;
      if (runningOnRS == false)       break;
    }
  }
  if (runningOnRS == false)   return BASED_ON_DATASET;
  if (queryDefn.getFilters() != null && queryDefn.getFilters().size() > 0) {
    if (!isFiltersEquals(filters,queryDefn.getFilters()))     runningOnRS=queryDefn.getBindings().values() == null || !hasAggregationOnRowObjects(queryDefn.getBindings().values().iterator());
  }
  return runningOnRS ? BASED_ON_RESULTSET : BASED_ON_DATASET;
}