Program: 72743
Project: 72743
Buggy source:
private IExtractionResults extractByInstanceID(InstanceID iid) throws EngineException {
  DataID dataId=iid.getDataID();
  DataSetID dataSetId=dataId.getDataSetID();
  DataRequestSession dataSession=executionContext.getDataEngine().getDTESession();
  Scriptable scope=executionContext.getSharedScope();
  IResultIterator dataIter=null;
  IBaseQueryDefinition query=null;
  try {
    if (null == filterExpressions && null == sortExpressions && maxRows == -1) {
      dataIter=getResultSetIterator(dataSession,dataSetId,scope);
    }
 else {
      long id=iid.getComponentID();
      ReportItemDesign design=(ReportItemDesign)report.getReportItemByID(id);
      IDataQueryDefinition dataQuery=design.getQuery();
      if (!(dataQuery instanceof IBaseQueryDefinition)) {
        return null;
      }
 else {
        query=(IBaseQueryDefinition)dataQuery;
      }
      if (filterExpressions != null) {
        for (int iNum=0; iNum < filterExpressions.length; iNum++) {
          query.getFilters().add(filterExpressions[iNum]);
        }
      }
      if (sortExpressions != null) {
        for (int iNum=0; iNum < sortExpressions.length; iNum++) {
          query.getSorts().add(sortExpressions[iNum]);
        }
      }
      if (maxRows != -1) {
        query.setMaxRows(maxRows);
      }
      IBaseQueryDefinition rootQuery=query;
      while (rootQuery instanceof SubqueryDefinition) {
        rootQuery=rootQuery.getParentQuery();
      }
      QueryDefinition newRootQuery=queryCopy((QueryDefinition)rootQuery);
      HashMap queryIds=report.getQueryIDs();
      String queryId=(String)queryIds.get(rootQuery);
      String rsetId=queryId2rsetId(queryId);
      newRootQuery.setQueryResultsID(rsetId);
      IPreparedQuery preparedQuery=dataSession.prepare(newRootQuery);
      IQueryResults rootResults=preparedQuery.execute(scope);
      dataIter=getFilterResultSetIterator(dataSession,dataSetId,scope,rootResults);
    }
  }
 catch (  BirtException e) {
    throw new EngineException("String_Node_Str",e);
  }
 finally {
    if (null != query) {
      if (filterExpressions != null) {
        for (int iNum=0; iNum < filterExpressions.length; iNum++) {
          query.getFilters().remove(filterExpressions[iNum]);
        }
        filterExpressions=null;
      }
      if (sortExpressions != null) {
        for (int iNum=0; iNum < sortExpressions.length; iNum++) {
          query.getSorts().remove(sortExpressions[iNum]);
        }
        sortExpressions=null;
      }
      maxRows=-1;
    }
  }
  IResultMetaData metaData=getMetaDateByInstanceID(iid);
  if (null != metaData) {
    return new ExtractionResults(dataIter,metaData,this.selectedColumns,startRow,maxRows);
  }
 else {
    return null;
  }
}

Fixed version (ground truth):
private IExtractionResults extractByInstanceID(InstanceID iid) throws EngineException {
  DataID dataId=iid.getDataID();
  DataSetID dataSetId=dataId.getDataSetID();
  DataRequestSession dataSession=executionContext.getDataEngine().getDTESession();
  Scriptable scope=executionContext.getSharedScope();
  IResultIterator dataIter=null;
  IBaseQueryDefinition query=null;
  try {
    if (null == filterExpressions && null == sortExpressions && maxRows == -1) {
      dataIter=getResultSetIterator(dataSession,dataSetId,scope);
    }
 else {
      long id=iid.getComponentID();
      ReportItemDesign design=(ReportItemDesign)report.getReportItemByID(id);
      IDataQueryDefinition dataQuery=design.getQuery();
      if (!(dataQuery instanceof IBaseQueryDefinition)) {
        return null;
      }
 else {
        query=(IBaseQueryDefinition)dataQuery;
      }
      if (filterExpressions != null) {
        for (int iNum=0; iNum < filterExpressions.length; iNum++) {
          query.getFilters().add(filterExpressions[iNum]);
        }
      }
      if (sortExpressions != null) {
        for (int iNum=0; iNum < sortExpressions.length; iNum++) {
          query.getSorts().add(sortExpressions[iNum]);
        }
      }
      if (maxRows != -1) {
        query.setMaxRows(maxRows);
      }
      IBaseQueryDefinition rootQuery=query;
      while (rootQuery instanceof SubqueryDefinition) {
        rootQuery=rootQuery.getParentQuery();
      }
      QueryDefinition newRootQuery=queryCopy((QueryDefinition)rootQuery);
      HashMap queryIds=report.getQueryIDs();
      String queryId=(String)queryIds.get(rootQuery);
      String rsetId=queryId2rsetId(queryId);
      newRootQuery.setQueryResultsID(rsetId);
      IPreparedQuery preparedQuery=dataSession.prepare(newRootQuery);
      IQueryResults rootResults=preparedQuery.execute(scope);
      dataIter=getFilterResultSetIterator(dataSession,dataSetId,scope,rootResults);
    }
  }
 catch (  BirtException e) {
    throw new EngineException(MessageConstants.DATA_EXPORTION_ERROR,iid,e);
  }
 finally {
    if (null != query) {
      if (filterExpressions != null) {
        for (int iNum=0; iNum < filterExpressions.length; iNum++) {
          query.getFilters().remove(filterExpressions[iNum]);
        }
        filterExpressions=null;
      }
      if (sortExpressions != null) {
        for (int iNum=0; iNum < sortExpressions.length; iNum++) {
          query.getSorts().remove(sortExpressions[iNum]);
        }
        sortExpressions=null;
      }
      maxRows=-1;
    }
  }
  IResultMetaData metaData=getMetaDateByInstanceID(iid);
  if (null != metaData) {
    return new ExtractionResults(dataIter,metaData,this.selectedColumns,startRow,maxRows);
  }
 else {
    return null;
  }
}