Program: 80132
Project: 80132
Buggy source:
/** 
 * @see Optimizer#getNextPermutation
 * @exception StandardException		Thrown on error
 */
public boolean getNextPermutation() throws StandardException {
  if (numOptimizables < 1) {
    if (optimizerTrace) {
      trace(NO_TABLES,0,0,0.0,null);
    }
    endOfRoundCleanup();
    return false;
  }
  optimizableList.initAccessPaths(this);
  if ((!timeExceeded) && (numTablesInQuery > 6) && (!noTimeout)) {
    currentTime=System.currentTimeMillis();
    timeExceeded=(currentTime - timeOptimizationStarted) > timeLimit;
    if (optimizerTrace && timeExceeded) {
      trace(TIME_EXCEEDED,0,0,0.0,null);
    }
  }
  if (bestCost.isUninitialized() && foundABestPlan && ((!usingPredsPushedFromAbove && !bestJoinOrderUsedPredsFromAbove) || timeExceeded)) {
    if (permuteState != JUMPING) {
      if (firstLookOrder == null)       firstLookOrder=new int[numOptimizables];
      for (int i=0; i < numOptimizables; i++)       firstLookOrder[i]=bestJoinOrder[i];
      permuteState=JUMPING;
      if (joinPosition >= 0) {
        rewindJoinOrder();
        joinPosition=-1;
      }
    }
    timeExceeded=false;
  }
  boolean joinPosAdvanced=false;
  boolean alreadyCostsMore=!bestCost.isUninitialized() && (currentCost.compare(bestCost) > 0) && ((requiredRowOrdering == null) || (currentSortAvoidanceCost.compare(bestCost) > 0));
  if ((joinPosition < (numOptimizables - 1)) && !alreadyCostsMore && (!timeExceeded)) {
    if ((joinPosition < 0) || optimizableList.getOptimizable(proposedJoinOrder[joinPosition]).getBestAccessPath().getCostEstimate() != null) {
      joinPosition++;
      joinPosAdvanced=true;
      bestRowOrdering.copy(currentRowOrdering);
    }
  }
 else {
    if (optimizerTrace) {
      if (joinPosition < (numOptimizables - 1)) {
        trace(SHORT_CIRCUITING,0,0,0.0,null);
      }
    }
    if (joinPosition < (numOptimizables - 1))     reloadBestPlan=true;
  }
  if (permuteState == JUMPING && !joinPosAdvanced && joinPosition >= 0) {
    reloadBestPlan=true;
    rewindJoinOrder();
    permuteState=NO_JUMP;
  }
  while (joinPosition >= 0) {
    int nextOptimizable=0;
    if (desiredJoinOrderFound || timeExceeded) {
      nextOptimizable=numOptimizables;
    }
 else     if (permuteState == JUMPING) {
      int idealOptimizable=firstLookOrder[joinPosition];
      nextOptimizable=idealOptimizable;
      int lookPos=numOptimizables;
      int lastSwappedOpt=-1;
      Optimizable nextOpt;
      for (nextOpt=optimizableList.getOptimizable(nextOptimizable); !(nextOpt.legalJoinOrder(assignedTableMap)); nextOpt=optimizableList.getOptimizable(nextOptimizable)) {
        if (lastSwappedOpt >= 0) {
          firstLookOrder[joinPosition]=idealOptimizable;
          firstLookOrder[lookPos]=lastSwappedOpt;
        }
        if (lookPos > joinPosition + 1) {
          lastSwappedOpt=firstLookOrder[--lookPos];
          firstLookOrder[joinPosition]=lastSwappedOpt;
          firstLookOrder[lookPos]=idealOptimizable;
          nextOptimizable=lastSwappedOpt;
        }
 else {
          if (joinPosition > 0) {
            joinPosition--;
            reloadBestPlan=true;
            rewindJoinOrder();
          }
          permuteState=NO_JUMP;
          break;
        }
      }
      if (permuteState == NO_JUMP)       continue;
      if (joinPosition == numOptimizables - 1) {
        permuteState=WALK_HIGH;
      }
    }
 else {
      nextOptimizable=proposedJoinOrder[joinPosition] + 1;
      for (; nextOptimizable < numOptimizables; nextOptimizable++) {
        boolean found=false;
        for (int posn=0; posn < joinPosition; posn++) {
          if (proposedJoinOrder[posn] == nextOptimizable) {
            found=true;
            break;
          }
        }
        if (nextOptimizable < numOptimizables) {
          Optimizable nextOpt=optimizableList.getOptimizable(nextOptimizable);
          if (!(nextOpt.legalJoinOrder(assignedTableMap))) {
            if (optimizerTrace) {
              trace(SKIPPING_JOIN_ORDER,nextOptimizable,0,0.0,null);
            }
            if (!optimizableList.optimizeJoinOrder()) {
              if (optimizerTrace) {
                trace(ILLEGAL_USER_JOIN_ORDER,0,0,0.0,null);
              }
              throw StandardException.newException(SQLState.LANG_ILLEGAL_FORCED_JOIN_ORDER);
            }
            continue;
          }
        }
        if (!found) {
          break;
        }
      }
    }
    if (proposedJoinOrder[joinPosition] >= 0) {
      Optimizable pullMe=optimizableList.getOptimizable(proposedJoinOrder[joinPosition]);
      double prevRowCount;
      double prevSingleScanRowCount;
      int prevPosition=0;
      if (joinPosition == 0) {
        prevRowCount=outermostCostEstimate.rowCount();
        prevSingleScanRowCount=outermostCostEstimate.singleScanRowCount();
      }
 else {
        prevPosition=proposedJoinOrder[joinPosition - 1];
        CostEstimate localCE=optimizableList.getOptimizable(prevPosition).getBestAccessPath().getCostEstimate();
        prevRowCount=localCE.rowCount();
        prevSingleScanRowCount=localCE.singleScanRowCount();
      }
      double newCost=currentCost.getEstimatedCost();
      double pullCost=0.0;
      CostEstimate pullCostEstimate=pullMe.getBestAccessPath().getCostEstimate();
      if (pullCostEstimate != null) {
        pullCost=pullCostEstimate.getEstimatedCost();
        newCost-=pullCost;
        if (newCost <= 0.0) {
          if (joinPosition == 0)           newCost=0.0;
 else           newCost=recoverCostFromProposedJoinOrder();
        }
      }
      if (joinPosition == 0) {
        if (outermostCostEstimate != null) {
          newCost=outermostCostEstimate.getEstimatedCost();
        }
 else {
          newCost=0.0;
        }
      }
      currentCost.setCost(newCost,prevRowCount,prevSingleScanRowCount);
      if (requiredRowOrdering != null) {
        if (pullMe.considerSortAvoidancePath()) {
          AccessPath ap=pullMe.getBestSortAvoidancePath();
          double prevEstimatedCost=0.0d;
          if (joinPosition == 0) {
            prevRowCount=outermostCostEstimate.rowCount();
            prevSingleScanRowCount=outermostCostEstimate.singleScanRowCount();
            prevEstimatedCost=outermostCostEstimate.getEstimatedCost();
          }
 else {
            CostEstimate localCE=optimizableList.getOptimizable(prevPosition).getBestSortAvoidancePath().getCostEstimate();
            prevRowCount=localCE.rowCount();
            prevSingleScanRowCount=localCE.singleScanRowCount();
            prevEstimatedCost=currentSortAvoidanceCost.getEstimatedCost() - ap.getCostEstimate().getEstimatedCost();
          }
          if (prevEstimatedCost <= 0.0) {
            if (joinPosition == 0)             prevEstimatedCost=0.0;
 else {
              prevEstimatedCost=recoverCostFromProposedJoinOrder();
            }
          }
          currentSortAvoidanceCost.setCost(prevEstimatedCost,prevRowCount,prevSingleScanRowCount);
          bestRowOrdering.removeOptimizable(pullMe.getTableNumber());
          bestRowOrdering.copy(currentRowOrdering);
        }
      }
      pullMe.pullOptPredicates(predicateList);
      if (reloadBestPlan)       pullMe.updateBestPlanMap(FromTable.LOAD_PLAN,this);
      proposedJoinOrder[joinPosition]=-1;
    }
    if (nextOptimizable >= numOptimizables) {
      if (!optimizableList.optimizeJoinOrder()) {
        if (!optimizableList.legalJoinOrder(numTablesInQuery)) {
          if (optimizerTrace) {
            trace(ILLEGAL_USER_JOIN_ORDER,0,0,0.0,null);
          }
          throw StandardException.newException(SQLState.LANG_ILLEGAL_FORCED_JOIN_ORDER);
        }
        if (optimizerTrace) {
          trace(USER_JOIN_ORDER_OPTIMIZED,0,0,0.0,null);
        }
        desiredJoinOrderFound=true;
      }
      if (permuteState == READY_TO_JUMP && joinPosition > 0 && joinPosition == numOptimizables - 1) {
        permuteState=JUMPING;
        double rc[]=new double[numOptimizables];
        for (int i=0; i < numOptimizables; i++) {
          firstLookOrder[i]=i;
          CostEstimate ce=optimizableList.getOptimizable(i).getBestAccessPath().getCostEstimate();
          if (ce == null) {
            permuteState=READY_TO_JUMP;
            break;
          }
          rc[i]=ce.singleScanRowCount();
        }
        if (permuteState == JUMPING) {
          boolean doIt=false;
          int temp;
          for (int i=0; i < numOptimizables; i++) {
            int k=i;
            for (int j=i + 1; j < numOptimizables; j++)             if (rc[j] < rc[k])             k=j;
            if (k != i) {
              rc[k]=rc[i];
              temp=firstLookOrder[i];
              firstLookOrder[i]=firstLookOrder[k];
              firstLookOrder[k]=temp;
              doIt=true;
            }
          }
          if (doIt) {
            joinPosition--;
            rewindJoinOrder();
            continue;
          }
 else           permuteState=NO_JUMP;
        }
      }
      joinPosition--;
      if (joinPosition >= 0) {
        Optimizable pullMe=optimizableList.getOptimizable(proposedJoinOrder[joinPosition]);
        assignedTableMap.xor(pullMe.getReferencedTableMap());
      }
      if (joinPosition < 0 && permuteState == WALK_HIGH) {
        joinPosition=0;
        permuteState=WALK_LOW;
      }
      continue;
    }
    proposedJoinOrder[joinPosition]=nextOptimizable;
    if (permuteState == WALK_LOW) {
      boolean finishedCycle=true;
      for (int i=0; i < numOptimizables; i++) {
        if (proposedJoinOrder[i] < firstLookOrder[i]) {
          finishedCycle=false;
          break;
        }
 else         if (proposedJoinOrder[i] > firstLookOrder[i])         break;
      }
      if (finishedCycle) {
        proposedJoinOrder[joinPosition]=-1;
        joinPosition--;
        if (joinPosition >= 0) {
          reloadBestPlan=true;
          rewindJoinOrder();
          joinPosition=-1;
        }
        permuteState=READY_TO_JUMP;
        endOfRoundCleanup();
        return false;
      }
    }
    optimizableList.getOptimizable(nextOptimizable).getBestAccessPath().setCostEstimate((CostEstimate)null);
    assignedTableMap.clearAll();
    for (int index=0; index <= joinPosition; index++) {
      assignedTableMap.or(optimizableList.getOptimizable(proposedJoinOrder[index]).getReferencedTableMap());
    }
    if (optimizerTrace) {
      trace(CONSIDERING_JOIN_ORDER,0,0,0.0,null);
    }
    Optimizable nextOpt=optimizableList.getOptimizable(nextOptimizable);
    nextOpt.startOptimizing(this,currentRowOrdering);
    pushPredicates(optimizableList.getOptimizable(nextOptimizable),assignedTableMap);
    return true;
  }
  endOfRoundCleanup();
  return false;
}

Fixed version (ground truth):
/** 
 * @see Optimizer#getNextPermutation
 * @exception StandardException		Thrown on error
 */
public boolean getNextPermutation() throws StandardException {
  if (numOptimizables < 1) {
    if (optimizerTrace) {
      trace(NO_TABLES,0,0,0.0,null);
    }
    endOfRoundCleanup();
    return false;
  }
  optimizableList.initAccessPaths(this);
  if ((!timeExceeded) && (numTablesInQuery > 6) && (!noTimeout)) {
    currentTime=System.currentTimeMillis();
    timeExceeded=(currentTime - timeOptimizationStarted) > timeLimit;
    if (optimizerTrace && timeExceeded) {
      trace(TIME_EXCEEDED,0,0,0.0,null);
    }
  }
  if (bestCost.isUninitialized() && foundABestPlan && ((!usingPredsPushedFromAbove && !bestJoinOrderUsedPredsFromAbove) || timeExceeded)) {
    if (permuteState != JUMPING) {
      if (firstLookOrder == null)       firstLookOrder=new int[numOptimizables];
      for (int i=0; i < numOptimizables; i++)       firstLookOrder[i]=bestJoinOrder[i];
      permuteState=JUMPING;
      if (joinPosition >= 0) {
        rewindJoinOrder();
        joinPosition=-1;
      }
    }
    timeExceeded=false;
  }
  boolean joinPosAdvanced=false;
  boolean alreadyCostsMore=!bestCost.isUninitialized() && (currentCost.compare(bestCost) > 0) && ((requiredRowOrdering == null) || (currentSortAvoidanceCost.compare(bestCost) > 0));
  if ((joinPosition < (numOptimizables - 1)) && !alreadyCostsMore && (!timeExceeded)) {
    if ((joinPosition < 0) || optimizableList.getOptimizable(proposedJoinOrder[joinPosition]).getBestAccessPath().getCostEstimate() != null) {
      joinPosition++;
      joinPosAdvanced=true;
      bestRowOrdering.copy(currentRowOrdering);
    }
  }
 else {
    if (optimizerTrace) {
      if (joinPosition < (numOptimizables - 1)) {
        trace(SHORT_CIRCUITING,0,0,0.0,null);
      }
    }
    if (joinPosition < (numOptimizables - 1))     reloadBestPlan=true;
  }
  if (permuteState == JUMPING && !joinPosAdvanced && joinPosition >= 0) {
    reloadBestPlan=true;
    rewindJoinOrder();
    permuteState=NO_JUMP;
  }
  while (joinPosition >= 0) {
    int nextOptimizable=0;
    if (desiredJoinOrderFound || timeExceeded) {
      nextOptimizable=numOptimizables;
    }
 else     if (permuteState == JUMPING) {
      int idealOptimizable=firstLookOrder[joinPosition];
      nextOptimizable=idealOptimizable;
      int lookPos=numOptimizables;
      int lastSwappedOpt=-1;
      Optimizable nextOpt;
      for (nextOpt=optimizableList.getOptimizable(nextOptimizable); !(nextOpt.legalJoinOrder(assignedTableMap)); nextOpt=optimizableList.getOptimizable(nextOptimizable)) {
        if (lastSwappedOpt >= 0) {
          firstLookOrder[joinPosition]=idealOptimizable;
          firstLookOrder[lookPos]=lastSwappedOpt;
        }
        if (lookPos > joinPosition + 1) {
          lastSwappedOpt=firstLookOrder[--lookPos];
          firstLookOrder[joinPosition]=lastSwappedOpt;
          firstLookOrder[lookPos]=idealOptimizable;
          nextOptimizable=lastSwappedOpt;
        }
 else {
          if (joinPosition > 0) {
            joinPosition--;
            reloadBestPlan=true;
            rewindJoinOrder();
          }
          permuteState=NO_JUMP;
          break;
        }
      }
      if (permuteState == NO_JUMP)       continue;
      if (joinPosition == numOptimizables - 1) {
        permuteState=WALK_HIGH;
      }
    }
 else {
      nextOptimizable=proposedJoinOrder[joinPosition] + 1;
      for (; nextOptimizable < numOptimizables; nextOptimizable++) {
        boolean found=false;
        for (int posn=0; posn < joinPosition; posn++) {
          if (proposedJoinOrder[posn] == nextOptimizable) {
            found=true;
            break;
          }
        }
        if (nextOptimizable < numOptimizables) {
          Optimizable nextOpt=optimizableList.getOptimizable(nextOptimizable);
          if (!(nextOpt.legalJoinOrder(assignedTableMap))) {
            if (optimizerTrace) {
              trace(SKIPPING_JOIN_ORDER,nextOptimizable,0,0.0,null);
            }
            if (!optimizableList.optimizeJoinOrder()) {
              if (optimizerTrace) {
                trace(ILLEGAL_USER_JOIN_ORDER,0,0,0.0,null);
              }
              throw StandardException.newException(SQLState.LANG_ILLEGAL_FORCED_JOIN_ORDER);
            }
            continue;
          }
        }
        if (!found) {
          break;
        }
      }
    }
    if (proposedJoinOrder[joinPosition] >= 0) {
      Optimizable pullMe=optimizableList.getOptimizable(proposedJoinOrder[joinPosition]);
      double prevRowCount;
      double prevSingleScanRowCount;
      int prevPosition=0;
      if (joinPosition == 0) {
        prevRowCount=outermostCostEstimate.rowCount();
        prevSingleScanRowCount=outermostCostEstimate.singleScanRowCount();
      }
 else {
        prevPosition=proposedJoinOrder[joinPosition - 1];
        CostEstimate localCE=optimizableList.getOptimizable(prevPosition).getBestAccessPath().getCostEstimate();
        prevRowCount=localCE.rowCount();
        prevSingleScanRowCount=localCE.singleScanRowCount();
      }
      double newCost=currentCost.getEstimatedCost();
      double pullCost=0.0;
      CostEstimate pullCostEstimate=pullMe.getBestAccessPath().getCostEstimate();
      if (pullCostEstimate != null) {
        pullCost=pullCostEstimate.getEstimatedCost();
        newCost-=pullCost;
        if (newCost <= 0.0) {
          if (joinPosition == 0)           newCost=0.0;
 else           newCost=recoverCostFromProposedJoinOrder(false);
        }
      }
      if (joinPosition == 0) {
        if (outermostCostEstimate != null) {
          newCost=outermostCostEstimate.getEstimatedCost();
        }
 else {
          newCost=0.0;
        }
      }
      currentCost.setCost(newCost,prevRowCount,prevSingleScanRowCount);
      if (requiredRowOrdering != null) {
        if (pullMe.considerSortAvoidancePath()) {
          AccessPath ap=pullMe.getBestSortAvoidancePath();
          double prevEstimatedCost=0.0d;
          if (joinPosition == 0) {
            prevRowCount=outermostCostEstimate.rowCount();
            prevSingleScanRowCount=outermostCostEstimate.singleScanRowCount();
            prevEstimatedCost=outermostCostEstimate.getEstimatedCost();
          }
 else {
            CostEstimate localCE=optimizableList.getOptimizable(prevPosition).getBestSortAvoidancePath().getCostEstimate();
            prevRowCount=localCE.rowCount();
            prevSingleScanRowCount=localCE.singleScanRowCount();
            prevEstimatedCost=currentSortAvoidanceCost.getEstimatedCost() - ap.getCostEstimate().getEstimatedCost();
          }
          if (prevEstimatedCost <= 0.0) {
            if (joinPosition == 0)             prevEstimatedCost=0.0;
 else {
              prevEstimatedCost=recoverCostFromProposedJoinOrder(true);
            }
          }
          currentSortAvoidanceCost.setCost(prevEstimatedCost,prevRowCount,prevSingleScanRowCount);
          bestRowOrdering.removeOptimizable(pullMe.getTableNumber());
          bestRowOrdering.copy(currentRowOrdering);
        }
      }
      pullMe.pullOptPredicates(predicateList);
      if (reloadBestPlan)       pullMe.updateBestPlanMap(FromTable.LOAD_PLAN,this);
      proposedJoinOrder[joinPosition]=-1;
    }
    if (nextOptimizable >= numOptimizables) {
      if (!optimizableList.optimizeJoinOrder()) {
        if (!optimizableList.legalJoinOrder(numTablesInQuery)) {
          if (optimizerTrace) {
            trace(ILLEGAL_USER_JOIN_ORDER,0,0,0.0,null);
          }
          throw StandardException.newException(SQLState.LANG_ILLEGAL_FORCED_JOIN_ORDER);
        }
        if (optimizerTrace) {
          trace(USER_JOIN_ORDER_OPTIMIZED,0,0,0.0,null);
        }
        desiredJoinOrderFound=true;
      }
      if (permuteState == READY_TO_JUMP && joinPosition > 0 && joinPosition == numOptimizables - 1) {
        permuteState=JUMPING;
        double rc[]=new double[numOptimizables];
        for (int i=0; i < numOptimizables; i++) {
          firstLookOrder[i]=i;
          CostEstimate ce=optimizableList.getOptimizable(i).getBestAccessPath().getCostEstimate();
          if (ce == null) {
            permuteState=READY_TO_JUMP;
            break;
          }
          rc[i]=ce.singleScanRowCount();
        }
        if (permuteState == JUMPING) {
          boolean doIt=false;
          int temp;
          for (int i=0; i < numOptimizables; i++) {
            int k=i;
            for (int j=i + 1; j < numOptimizables; j++)             if (rc[j] < rc[k])             k=j;
            if (k != i) {
              rc[k]=rc[i];
              temp=firstLookOrder[i];
              firstLookOrder[i]=firstLookOrder[k];
              firstLookOrder[k]=temp;
              doIt=true;
            }
          }
          if (doIt) {
            joinPosition--;
            rewindJoinOrder();
            continue;
          }
 else           permuteState=NO_JUMP;
        }
      }
      joinPosition--;
      if (joinPosition >= 0) {
        Optimizable pullMe=optimizableList.getOptimizable(proposedJoinOrder[joinPosition]);
        assignedTableMap.xor(pullMe.getReferencedTableMap());
      }
      if (joinPosition < 0 && permuteState == WALK_HIGH) {
        joinPosition=0;
        permuteState=WALK_LOW;
      }
      continue;
    }
    proposedJoinOrder[joinPosition]=nextOptimizable;
    if (permuteState == WALK_LOW) {
      boolean finishedCycle=true;
      for (int i=0; i < numOptimizables; i++) {
        if (proposedJoinOrder[i] < firstLookOrder[i]) {
          finishedCycle=false;
          break;
        }
 else         if (proposedJoinOrder[i] > firstLookOrder[i])         break;
      }
      if (finishedCycle) {
        proposedJoinOrder[joinPosition]=-1;
        joinPosition--;
        if (joinPosition >= 0) {
          reloadBestPlan=true;
          rewindJoinOrder();
          joinPosition=-1;
        }
        permuteState=READY_TO_JUMP;
        endOfRoundCleanup();
        return false;
      }
    }
    optimizableList.getOptimizable(nextOptimizable).getBestAccessPath().setCostEstimate((CostEstimate)null);
    assignedTableMap.clearAll();
    for (int index=0; index <= joinPosition; index++) {
      assignedTableMap.or(optimizableList.getOptimizable(proposedJoinOrder[index]).getReferencedTableMap());
    }
    if (optimizerTrace) {
      trace(CONSIDERING_JOIN_ORDER,0,0,0.0,null);
    }
    Optimizable nextOpt=optimizableList.getOptimizable(nextOptimizable);
    nextOpt.startOptimizing(this,currentRowOrdering);
    pushPredicates(optimizableList.getOptimizable(nextOptimizable),assignedTableMap);
    return true;
  }
  endOfRoundCleanup();
  return false;
}