Program: 65529
Project: 65529
Buggy source:
/** 
 * @param query
 * @param dataSet
 * @return
 * @throws DataException
 */
public static Strategy getQueryExecutionStrategy(DataEngineSession session,IQueryDefinition query,IBaseDataSetDesign dataSet) throws DataException {
  SortingOptimizer opt=new SortingOptimizer(dataSet,query);
  if (session.getEngineContext().getMode() == DataEngineContext.DIRECT_PRESENTATION) {
    return Strategy.Complex;
  }
  if (session.getEngineContext().getMode() == DataEngineContext.MODE_UPDATE)   return Strategy.Complex;
  if (query.getGroups() != null && query.getGroups().size() > 0) {
    for (    IGroupDefinition group : (List<IGroupDefinition>)query.getGroups()) {
      if (group.getSubqueries() != null && group.getSubqueries().size() > 0)       return Strategy.Complex;
      if (!isDirectColumnRefGroupKey(group,query))       return Strategy.Complex;
      if (group.getFilters().isEmpty() && group.getSorts().isEmpty() && !query.getQueryExecutionHints().doSortBeforeGrouping())       continue;
      if (opt.acceptGroupSorting()) {
        continue;
      }
      return Strategy.Complex;
    }
  }
  if (query.getFilters() != null && query.getFilters().size() > 0) {
    if (FilterUtil.hasMutipassFilters(query.getFilters()))     return Strategy.Complex;
    Set<String> bindings=new HashSet<String>();
    for (    Object filter : query.getFilters()) {
      IBaseExpression baseExpr=((IFilterDefinition)filter).getExpression();
      if (ExpressionCompilerUtil.hasAggregationInExpr(baseExpr))       return Strategy.Complex;
      bindings.addAll(ExpressionCompilerUtil.extractColumnExpression(baseExpr,ExpressionUtil.ROW_INDICATOR));
      if (((IFilterDefinition)filter).updateAggregation() == false)       return Strategy.Complex;
    }
    if (PreparedQueryUtil.existAggregationBinding(bindings,query.getBindings())) {
      return Strategy.Complex;
    }
  }
  if (query.getSorts() != null && query.getSorts().size() > 0) {
    if (!opt.acceptQuerySorting())     return Strategy.Complex;
  }
  if (query.getSubqueries() != null && query.getSubqueries().size() > 0)   return Strategy.Complex;
  if (!query.usesDetails()) {
    return Strategy.Complex;
  }
  boolean hasAggregation=false;
  if (query.getBindings() != null) {
    Iterator bindingIt=query.getBindings().values().iterator();
    while (bindingIt.hasNext()) {
      IBinding binding=(IBinding)bindingIt.next();
      if (binding.getAggrFunction() != null) {
        hasAggregation=true;
        IAggrFunction aggr=AggregationManager.getInstance().getAggregation(binding.getAggrFunction());
        if (aggr != null && aggr.getNumberOfPasses() > 1) {
          return Strategy.Complex;
        }
        List exprs=new ArrayList();
        exprs.addAll(binding.getArguments());
        if (binding.getExpression() != null)         exprs.add(binding.getExpression());
        for (int i=0; i < exprs.size(); i++) {
          Object expr=exprs.get(i);
          if (!(expr instanceof IScriptExpression)) {
            return Strategy.Complex;
          }
          IScriptExpression scriptExpr=(IScriptExpression)expr;
          try {
            List<IColumnBinding> columnExprs=ExpressionUtil.extractColumnExpressions(scriptExpr.getText());
            for (            IColumnBinding temp : columnExprs) {
              Object obj=query.getBindings().get(temp.getResultSetColumnName());
              if (obj instanceof IBinding) {
                IBinding bindingObj=(IBinding)obj;
                if (bindingObj.getAggrFunction() != null)                 return Strategy.Complex;
                IBaseExpression baseExpr=((IBinding)obj).getExpression();
                if (baseExpr instanceof IScriptExpression) {
                  String cb=ExpressionUtil.getColumnName(((IScriptExpression)baseExpr).getText());
                  if (ScriptEvalUtil.compare(bindingObj.getBindingName(),cb) != 0)                   return Strategy.Complex;
                }
              }
            }
          }
 catch (          BirtException e) {
            return Strategy.Complex;
          }
        }
      }
      if (ExpressionCompilerUtil.hasAggregationInExpr(binding.getExpression())) {
        return Strategy.Complex;
      }
    }
  }
  if (dataSet != null) {
    if (dataSet.getFilters() != null) {
      if (FilterUtil.hasMutipassFilters(dataSet.getFilters())) {
        return Strategy.Complex;
      }
      for (      Object filter : dataSet.getFilters()) {
        IBaseExpression baseExpr=((IFilterDefinition)filter).getExpression();
        if (ExpressionCompilerUtil.hasAggregationInExpr(baseExpr))         return Strategy.Complex;
        if (((IFilterDefinition)filter).updateAggregation() == false)         return Strategy.Complex;
      }
    }
    if (dataSet.needDistinctValue())     return Strategy.Complex;
    if (dataSet.getComputedColumns() != null) {
      List computedColumns=dataSet.getComputedColumns();
      for (int i=0; i < computedColumns.size(); i++) {
        IComputedColumn computedColumn=(IComputedColumn)computedColumns.get(i);
        if (computedColumn.getAggregateFunction() != null)         return Strategy.Complex;
        if (computedColumn.getExpression() instanceof IScriptExpression) {
          if (ExpressionUtil.hasAggregation(((IScriptExpression)computedColumn.getExpression()).getText())) {
            return Strategy.Complex;
          }
        }
      }
    }
  }
  return hasAggregation ? Strategy.SimpleLookingFoward : Strategy.SimpleNoLookingFoward;
}

Fixed version (ground truth):
/** 
 * @param query
 * @param dataSet
 * @return
 * @throws DataException
 */
public static Strategy getQueryExecutionStrategy(DataEngineSession session,IQueryDefinition query,IBaseDataSetDesign dataSet) throws DataException {
  SortingOptimizer opt=new SortingOptimizer(dataSet,query);
  if (session.getEngineContext().getMode() == DataEngineContext.MODE_UPDATE)   return Strategy.Complex;
  if (query.getGroups() != null && query.getGroups().size() > 0) {
    for (    IGroupDefinition group : (List<IGroupDefinition>)query.getGroups()) {
      if (group.getSubqueries() != null && group.getSubqueries().size() > 0)       return Strategy.Complex;
      if (!isDirectColumnRefGroupKey(group,query))       return Strategy.Complex;
      if (group.getFilters().isEmpty() && group.getSorts().isEmpty() && !query.getQueryExecutionHints().doSortBeforeGrouping())       continue;
      if (opt.acceptGroupSorting()) {
        continue;
      }
      return Strategy.Complex;
    }
  }
  if (query.getFilters() != null && query.getFilters().size() > 0) {
    if (FilterUtil.hasMutipassFilters(query.getFilters()))     return Strategy.Complex;
    Set<String> bindings=new HashSet<String>();
    for (    Object filter : query.getFilters()) {
      IBaseExpression baseExpr=((IFilterDefinition)filter).getExpression();
      if (ExpressionCompilerUtil.hasAggregationInExpr(baseExpr))       return Strategy.Complex;
      bindings.addAll(ExpressionCompilerUtil.extractColumnExpression(baseExpr,ExpressionUtil.ROW_INDICATOR));
      if (((IFilterDefinition)filter).updateAggregation() == false)       return Strategy.Complex;
    }
    if (PreparedQueryUtil.existAggregationBinding(bindings,query.getBindings())) {
      return Strategy.Complex;
    }
  }
  if (query.getSorts() != null && query.getSorts().size() > 0) {
    if (!opt.acceptQuerySorting())     return Strategy.Complex;
  }
  if (query.getSubqueries() != null && query.getSubqueries().size() > 0)   return Strategy.Complex;
  if (!query.usesDetails()) {
    return Strategy.Complex;
  }
  boolean hasAggregation=false;
  if (query.getBindings() != null) {
    Iterator bindingIt=query.getBindings().values().iterator();
    while (bindingIt.hasNext()) {
      IBinding binding=(IBinding)bindingIt.next();
      if (binding.getAggrFunction() != null) {
        hasAggregation=true;
        IAggrFunction aggr=AggregationManager.getInstance().getAggregation(binding.getAggrFunction());
        if (aggr != null && aggr.getNumberOfPasses() > 1) {
          return Strategy.Complex;
        }
        List exprs=new ArrayList();
        exprs.addAll(binding.getArguments());
        if (binding.getExpression() != null)         exprs.add(binding.getExpression());
        for (int i=0; i < exprs.size(); i++) {
          Object expr=exprs.get(i);
          if (!(expr instanceof IScriptExpression)) {
            return Strategy.Complex;
          }
          IScriptExpression scriptExpr=(IScriptExpression)expr;
          try {
            List<IColumnBinding> columnExprs=ExpressionUtil.extractColumnExpressions(scriptExpr.getText());
            for (            IColumnBinding temp : columnExprs) {
              Object obj=query.getBindings().get(temp.getResultSetColumnName());
              if (obj instanceof IBinding) {
                IBinding bindingObj=(IBinding)obj;
                if (bindingObj.getAggrFunction() != null)                 return Strategy.Complex;
                IBaseExpression baseExpr=((IBinding)obj).getExpression();
                if (baseExpr instanceof IScriptExpression) {
                  String cb=ExpressionUtil.getColumnName(((IScriptExpression)baseExpr).getText());
                  if (ScriptEvalUtil.compare(bindingObj.getBindingName(),cb) != 0)                   return Strategy.Complex;
                }
              }
            }
          }
 catch (          BirtException e) {
            return Strategy.Complex;
          }
        }
      }
      if (ExpressionCompilerUtil.hasAggregationInExpr(binding.getExpression())) {
        return Strategy.Complex;
      }
    }
  }
  if (dataSet != null) {
    if (dataSet.getFilters() != null) {
      if (FilterUtil.hasMutipassFilters(dataSet.getFilters())) {
        return Strategy.Complex;
      }
      for (      Object filter : dataSet.getFilters()) {
        IBaseExpression baseExpr=((IFilterDefinition)filter).getExpression();
        if (ExpressionCompilerUtil.hasAggregationInExpr(baseExpr))         return Strategy.Complex;
        if (((IFilterDefinition)filter).updateAggregation() == false)         return Strategy.Complex;
      }
    }
    if (dataSet.needDistinctValue())     return Strategy.Complex;
    if (dataSet.getComputedColumns() != null) {
      List computedColumns=dataSet.getComputedColumns();
      for (int i=0; i < computedColumns.size(); i++) {
        IComputedColumn computedColumn=(IComputedColumn)computedColumns.get(i);
        if (computedColumn.getAggregateFunction() != null)         return Strategy.Complex;
        if (computedColumn.getExpression() instanceof IScriptExpression) {
          if (ExpressionUtil.hasAggregation(((IScriptExpression)computedColumn.getExpression()).getText())) {
            return Strategy.Complex;
          }
        }
      }
    }
  }
  return hasAggregation ? Strategy.SimpleLookingFoward : Strategy.SimpleNoLookingFoward;
}