Program: 50917
Project: 50917
Buggy source:
/** 
 * Creates a RolapResult.
 * @param execution Execution of a statement
 * @param execute Whether to execute the query
 */
RolapResult(final Execution execution,boolean execute){
  super(execution,null);
  this.point=CellKey.Generator.newCellKey(axes.length);
  final AggregationManager aggMgr=execution.getMondrianStatement().getMondrianConnection().getServer().getAggregationManager();
  this.aggregatingReader=aggMgr.getCacheCellReader();
  final int expDeps=MondrianProperties.instance().TestExpDependencies.get();
  if (expDeps > 0) {
    this.evaluator=new RolapDependencyTestingEvaluator(this,expDeps);
  }
 else {
    final RolapEvaluatorRoot root=new RolapResultEvaluatorRoot(this);
    if (statement.getProfileHandler() != null) {
      this.evaluator=new RolapProfilingEvaluator(root);
    }
 else {
      this.evaluator=new RolapEvaluator(root);
    }
  }
  RolapCube cube=(RolapCube)query.getCube();
  this.batchingReader=new FastBatchingCellReader(execution,cube,aggMgr);
  this.cellInfos=(query.axes.length > 4) ? new CellInfoMap(point) : new CellInfoPool(query.axes.length);
  if (!execute) {
    return;
  }
  boolean normalExecution=true;
  try {
    cube.clearCachedAggregations();
    final AxisMember axisMembers=new AxisMember();
    final List<Member> nonDefaultAllMembers=new ArrayList<Member>();
    List<List<Member>> nonAllMembers=new ArrayList<List<Member>>();
    final List<Member> measureMembers=new ArrayList<Member>();
    loadSpecialMembers(nonDefaultAllMembers,nonAllMembers,measureMembers);
    query.clearEvalCache();
    query.putEvalCache("String_Node_Str",nonDefaultAllMembers);
    final List<List<Member>> emptyNonAllMembers=Collections.emptyList();
    slicerEvaluator=evaluator.push();
    axisMembers.setSlicer(true);
    loadMembers(emptyNonAllMembers,evaluator,query.getSlicerAxis(),query.slicerCalc,axisMembers);
    axisMembers.setSlicer(false);
    final RolapEvaluator savedEvaluator=evaluator.push();
    if (!axisMembers.isEmpty()) {
      for (      Member m : axisMembers) {
        if (m == null) {
          break;
        }
        evaluator.setSlicerContext(m);
        if (m.isMeasure()) {
          measureMembers.clear();
        }
      }
      replaceNonAllMembers(nonAllMembers,axisMembers);
      axisMembers.clearMembers();
    }
    slicerEvaluator=evaluator.push();
    boolean changed=false;
    axisMembers.clearTotalCellCount();
    for (int i=0; i < axes.length; i++) {
      final QueryAxis axis=query.axes[i];
      final Calc calc=query.axisCalcs[i];
      loadMembers(emptyNonAllMembers,evaluator,axis,calc,axisMembers);
    }
    if (!axisMembers.isEmpty()) {
      for (      Member m : axisMembers) {
        if (m.isMeasure()) {
          measureMembers.clear();
        }
      }
      changed=replaceNonAllMembers(nonAllMembers,axisMembers);
      axisMembers.clearMembers();
    }
    if (changed) {
      axisMembers.countOnly(true);
      axisMembers.clearTotalCellCount();
      final int savepoint=evaluator.savepoint();
      for (int i=0; i < axes.length; i++) {
        final QueryAxis axis=query.axes[i];
        final Calc calc=query.axisCalcs[i];
        loadMembers(nonAllMembers,evaluator,axis,calc,axisMembers);
        evaluator.restore(savepoint);
      }
    }
    axisMembers.checkLimit();
    RolapEvaluator slicerEvaluator;
    do {
      TupleIterable tupleIterable=evalExecute(nonAllMembers,nonAllMembers.size() - 1,savedEvaluator,query.getSlicerAxis(),query.slicerCalc);
      TupleList tupleList=TupleCollections.materialize(tupleIterable,true);
      this.slicerAxis=new RolapAxis(tupleList);
      slicerEvaluator=this.evaluator;
      if (tupleList.size() > 1) {
        tupleList=AggregateFunDef.AggregateCalc.optimizeTupleList(slicerEvaluator,tupleList);
        final Calc valueCalc=new ValueCalc(new DummyExp(new ScalarType()));
        final TupleList tupleList1=tupleList;
        final Calc calc=new GenericCalc(new DummyExp(query.slicerCalc.getType())){
          public Object evaluate(          Evaluator evaluator){
            return AggregateFunDef.AggregateCalc.aggregate(valueCalc,evaluator,tupleList1);
          }
        }
;
        final List<RolapHierarchy> hierarchyList=new AbstractList<RolapHierarchy>(){
          final List<Member> pos0=tupleList1.get(0);
          public RolapHierarchy get(          int index){
            return ((RolapMember)pos0.get(index)).getHierarchy();
          }
          public int size(){
            return pos0.size();
          }
        }
;
        evaluator.addCalculation(new RolapTupleCalculation(hierarchyList,calc),true);
      }
    }
 while (phase());
    final int savepoint=evaluator.savepoint();
    do {
      try {
        boolean redo;
        do {
          evaluator.restore(savepoint);
          redo=false;
          for (int i=0; i < axes.length; i++) {
            QueryAxis axis=query.axes[i];
            final Calc calc=query.axisCalcs[i];
            TupleIterable tupleIterable=evalExecute(nonAllMembers,nonAllMembers.size() - 1,evaluator,axis,calc);
            if (!nonAllMembers.isEmpty()) {
              final TupleIterator tupleIterator=tupleIterable.tupleIterator();
              if (tupleIterator.hasNext()) {
                List<Member> tuple0=tupleIterator.next();
                for (                Member m : tuple0) {
                  if (m.isCalculated()) {
                    CalculatedMeasureVisitor visitor=new CalculatedMeasureVisitor();
                    m.getExpression().accept(visitor);
                    Dimension dimension=visitor.dimension;
                    if (removeDimension(dimension,nonAllMembers)) {
                      redo=true;
                    }
                  }
                }
              }
            }
            this.axes[i]=new RolapAxis(TupleCollections.materialize(tupleIterable,false));
          }
        }
 while (redo);
      }
 catch (      CellRequestQuantumExceededException e) {
      }
    }
 while (phase());
    evaluator.restore(savepoint);
    final Locus locus=new Locus(execution,null,"String_Node_Str");
    Locus.push(locus);
    try {
      executeBody(slicerEvaluator,query,new int[axes.length]);
    }
  finally {
      Locus.pop(locus);
    }
    if (this.cellInfos.size() > 10000) {
      this.cellInfos.trimToSize();
    }
  }
 catch (  ResultLimitExceededException ex) {
    normalExecution=false;
    evaluator=null;
    slicerEvaluator=null;
    cellInfos=null;
    batchingReader=null;
    for (int i=0; i < axes.length; i++) {
      axes[i]=null;
    }
    slicerAxis=null;
    query.clearEvalCache();
    throw ex;
  }
 finally {
    if (normalExecution) {
      evaluator.clearExpResultCache(true);
    }
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("String_Node_Str" + Util.printMemory());
    }
  }
}

Fixed version (ground truth):
/** 
 * Creates a RolapResult.
 * @param execution Execution of a statement
 * @param execute Whether to execute the query
 */
RolapResult(final Execution execution,boolean execute){
  super(execution,null);
  this.point=CellKey.Generator.newCellKey(axes.length);
  final AggregationManager aggMgr=execution.getMondrianStatement().getMondrianConnection().getServer().getAggregationManager();
  this.aggregatingReader=aggMgr.getCacheCellReader();
  final int expDeps=MondrianProperties.instance().TestExpDependencies.get();
  if (expDeps > 0) {
    this.evaluator=new RolapDependencyTestingEvaluator(this,expDeps);
  }
 else {
    final RolapEvaluatorRoot root=new RolapResultEvaluatorRoot(this);
    if (statement.getProfileHandler() != null) {
      this.evaluator=new RolapProfilingEvaluator(root);
    }
 else {
      this.evaluator=new RolapEvaluator(root);
    }
  }
  RolapCube cube=(RolapCube)query.getCube();
  this.batchingReader=new FastBatchingCellReader(execution,cube,aggMgr);
  this.cellInfos=(query.axes.length > 4) ? new CellInfoMap(point) : new CellInfoPool(query.axes.length);
  if (!execute) {
    return;
  }
  boolean normalExecution=true;
  try {
    cube.clearCachedAggregations();
    final AxisMember axisMembers=new AxisMember();
    final List<Member> nonDefaultAllMembers=new ArrayList<Member>();
    List<List<Member>> nonAllMembers=new ArrayList<List<Member>>();
    final List<Member> measureMembers=new ArrayList<Member>();
    loadSpecialMembers(nonDefaultAllMembers,nonAllMembers,measureMembers);
    query.clearEvalCache();
    query.putEvalCache("String_Node_Str",nonDefaultAllMembers);
    final List<List<Member>> emptyNonAllMembers=Collections.emptyList();
    slicerEvaluator=evaluator.push();
    axisMembers.setSlicer(true);
    loadMembers(emptyNonAllMembers,evaluator,query.getSlicerAxis(),query.slicerCalc,axisMembers);
    axisMembers.setSlicer(false);
    final RolapEvaluator savedEvaluator=evaluator.push();
    if (!axisMembers.isEmpty()) {
      for (      Member m : axisMembers) {
        if (m == null) {
          break;
        }
        evaluator.setSlicerContext(m);
        if (m.isMeasure()) {
          measureMembers.clear();
        }
      }
      replaceNonAllMembers(nonAllMembers,axisMembers);
      axisMembers.clearMembers();
    }
    slicerEvaluator=evaluator.push();
    boolean changed=false;
    axisMembers.clearTotalCellCount();
    for (int i=0; i < axes.length; i++) {
      final QueryAxis axis=query.axes[i];
      final Calc calc=query.axisCalcs[i];
      loadMembers(emptyNonAllMembers,evaluator,axis,calc,axisMembers);
    }
    if (!axisMembers.isEmpty()) {
      for (      Member m : axisMembers) {
        if (m.isMeasure()) {
          measureMembers.clear();
        }
      }
      changed=replaceNonAllMembers(nonAllMembers,axisMembers);
      axisMembers.clearMembers();
    }
    if (changed) {
      axisMembers.countOnly(true);
      axisMembers.clearTotalCellCount();
      final int savepoint=evaluator.savepoint();
      for (int i=0; i < axes.length; i++) {
        final QueryAxis axis=query.axes[i];
        final Calc calc=query.axisCalcs[i];
        loadMembers(nonAllMembers,evaluator,axis,calc,axisMembers);
        evaluator.restore(savepoint);
      }
    }
    axisMembers.checkLimit();
    RolapEvaluator slicerEvaluator;
    do {
      TupleIterable tupleIterable=evalExecute(nonAllMembers,nonAllMembers.size() - 1,savedEvaluator,query.getSlicerAxis(),query.slicerCalc);
      TupleList tupleList=TupleCollections.materialize(tupleIterable,true);
      this.slicerAxis=new RolapAxis(tupleList);
      slicerEvaluator=this.evaluator;
      if (tupleList.size() > 1) {
        tupleList=AggregateFunDef.AggregateCalc.optimizeTupleList(slicerEvaluator,tupleList,false);
        final Calc valueCalc=new ValueCalc(new DummyExp(new ScalarType()));
        final TupleList tupleList1=tupleList;
        final Calc calc=new GenericCalc(new DummyExp(query.slicerCalc.getType())){
          public Object evaluate(          Evaluator evaluator){
            return AggregateFunDef.AggregateCalc.aggregate(valueCalc,evaluator,tupleList1);
          }
        }
;
        final List<RolapHierarchy> hierarchyList=new AbstractList<RolapHierarchy>(){
          final List<Member> pos0=tupleList1.get(0);
          public RolapHierarchy get(          int index){
            return ((RolapMember)pos0.get(index)).getHierarchy();
          }
          public int size(){
            return pos0.size();
          }
        }
;
        evaluator.addCalculation(new RolapTupleCalculation(hierarchyList,calc),true);
      }
    }
 while (phase());
    final int savepoint=evaluator.savepoint();
    do {
      try {
        boolean redo;
        do {
          evaluator.restore(savepoint);
          redo=false;
          for (int i=0; i < axes.length; i++) {
            QueryAxis axis=query.axes[i];
            final Calc calc=query.axisCalcs[i];
            TupleIterable tupleIterable=evalExecute(nonAllMembers,nonAllMembers.size() - 1,evaluator,axis,calc);
            if (!nonAllMembers.isEmpty()) {
              final TupleIterator tupleIterator=tupleIterable.tupleIterator();
              if (tupleIterator.hasNext()) {
                List<Member> tuple0=tupleIterator.next();
                for (                Member m : tuple0) {
                  if (m.isCalculated()) {
                    CalculatedMeasureVisitor visitor=new CalculatedMeasureVisitor();
                    m.getExpression().accept(visitor);
                    Dimension dimension=visitor.dimension;
                    if (removeDimension(dimension,nonAllMembers)) {
                      redo=true;
                    }
                  }
                }
              }
            }
            this.axes[i]=new RolapAxis(TupleCollections.materialize(tupleIterable,false));
          }
        }
 while (redo);
      }
 catch (      CellRequestQuantumExceededException e) {
      }
    }
 while (phase());
    evaluator.restore(savepoint);
    final Locus locus=new Locus(execution,null,"String_Node_Str");
    Locus.push(locus);
    try {
      executeBody(slicerEvaluator,query,new int[axes.length]);
    }
  finally {
      Locus.pop(locus);
    }
    if (this.cellInfos.size() > 10000) {
      this.cellInfos.trimToSize();
    }
  }
 catch (  ResultLimitExceededException ex) {
    normalExecution=false;
    evaluator=null;
    slicerEvaluator=null;
    cellInfos=null;
    batchingReader=null;
    for (int i=0; i < axes.length; i++) {
      axes[i]=null;
    }
    slicerAxis=null;
    query.clearEvalCache();
    throw ex;
  }
 finally {
    if (normalExecution) {
      evaluator.clearExpResultCache(true);
    }
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("String_Node_Str" + Util.printMemory());
    }
  }
}