Program: 15712
Project: 15712
Buggy source:
@Override public void prepare(){
  if (isPrepared) {
    return;
  }
  if (SysProperties.CHECK && !checkInit) {
    DbException.throwInternalError("String_Node_Str");
  }
  if (orderList != null) {
    sort=prepareOrder(orderList,expressions.size());
    orderList=null;
  }
  for (int i=0; i < expressions.size(); i++) {
    Expression e=expressions.get(i);
    expressions.set(i,e.optimize(session));
  }
  if (condition != null) {
    condition=condition.optimize(session);
    for (    TableFilter f : filters) {
      if (!f.isJoinOuter() && !f.isJoinOuterIndirect()) {
        condition.createIndexConditions(session,f);
      }
    }
  }
  if (isGroupQuery && groupIndex == null && havingIndex < 0 && filters.size() == 1) {
    if (condition == null) {
      Table t=filters.get(0).getTable();
      ExpressionVisitor optimizable=ExpressionVisitor.getOptimizableVisitor(t);
      isQuickAggregateQuery=isEverything(optimizable);
    }
  }
  cost=preparePlan(session.isParsingView());
  if (distinct && session.getDatabase().getSettings().optimizeDistinct && !isGroupQuery&& filters.size() == 1 && expressions.size() == 1 && condition == null) {
    Expression expr=expressions.get(0);
    expr=expr.getNonAliasExpression();
    if (expr instanceof ExpressionColumn) {
      Column column=((ExpressionColumn)expr).getColumn();
      int selectivity=column.getSelectivity();
      Index columnIndex=topTableFilter.getTable().getIndexForColumn(column);
      if (columnIndex != null && selectivity != Constants.SELECTIVITY_DEFAULT && selectivity < 20) {
        boolean ascending=columnIndex.getIndexColumns()[0].sortType == SortOrder.ASCENDING;
        Index current=topTableFilter.getIndex();
        if (columnIndex.canFindNext() && ascending && (current == null || current.getIndexType().isScan() || columnIndex == current)) {
          IndexType type=columnIndex.getIndexType();
          if (!type.isHash() && (!type.isUnique() || columnIndex.getColumns().length > 1)) {
            topTableFilter.setIndex(columnIndex);
            isDistinctQuery=true;
          }
        }
      }
    }
  }
  if (sort != null && !isQuickAggregateQuery && !isGroupQuery) {
    Index index=getSortIndex();
    Index current=topTableFilter.getIndex();
    if (index != null && current != null) {
      if (current.getIndexType().isScan() || current == index) {
        topTableFilter.setIndex(index);
        if (!topTableFilter.hasInComparisons()) {
          sortUsingIndex=true;
        }
      }
 else       if (index.getIndexColumns().length >= current.getIndexColumns().length) {
        IndexColumn[] sortColumns=index.getIndexColumns();
        IndexColumn[] currentColumns=current.getIndexColumns();
        boolean swapIndex=false;
        for (int i=0; i < currentColumns.length; i++) {
          if (sortColumns[i].column != currentColumns[i].column) {
            swapIndex=false;
            break;
          }
          if (sortColumns[i].sortType != currentColumns[i].sortType) {
            swapIndex=true;
          }
        }
        if (swapIndex) {
          topTableFilter.setIndex(index);
          sortUsingIndex=true;
        }
      }
    }
  }
  if (!isQuickAggregateQuery && isGroupQuery && getGroupByExpressionCount() > 0) {
    Index index=getGroupSortedIndex();
    Index current=topTableFilter.getIndex();
    if (index != null && current != null && (current.getIndexType().isScan() || current == index)) {
      topTableFilter.setIndex(index);
      isGroupSortedQuery=true;
    }
  }
  expressionArray=new Expression[expressions.size()];
  expressions.toArray(expressionArray);
  isPrepared=true;
}

Fixed version (ground truth):
@Override public void prepare(){
  if (isPrepared) {
    return;
  }
  if (SysProperties.CHECK && !checkInit) {
    DbException.throwInternalError("String_Node_Str");
  }
  if (orderList != null) {
    sort=prepareOrder(orderList,expressions.size());
    orderList=null;
  }
  for (int i=0; i < expressions.size(); i++) {
    Expression e=expressions.get(i);
    expressions.set(i,e.optimize(session));
  }
  if (condition != null) {
    condition=condition.optimize(session);
    for (    TableFilter f : filters) {
      if (!f.isJoinOuter() && !f.isJoinOuterIndirect()) {
        condition.createIndexConditions(session,f);
      }
    }
  }
  if (isGroupQuery && groupIndex == null && havingIndex < 0 && filters.size() == 1) {
    if (condition == null) {
      Table t=filters.get(0).getTable();
      ExpressionVisitor optimizable=ExpressionVisitor.getOptimizableVisitor(t);
      isQuickAggregateQuery=isEverything(optimizable);
    }
  }
  cost=preparePlan(session.isParsingView());
  if (distinct && session.getDatabase().getSettings().optimizeDistinct && !isGroupQuery&& filters.size() == 1 && expressions.size() == 1 && condition == null) {
    Expression expr=expressions.get(0);
    expr=expr.getNonAliasExpression();
    if (expr instanceof ExpressionColumn) {
      Column column=((ExpressionColumn)expr).getColumn();
      int selectivity=column.getSelectivity();
      Index columnIndex=topTableFilter.getTable().getIndexForColumn(column);
      if (columnIndex != null && selectivity != Constants.SELECTIVITY_DEFAULT && selectivity < 20) {
        boolean ascending=columnIndex.getIndexColumns()[0].sortType == SortOrder.ASCENDING;
        Index current=topTableFilter.getIndex();
        if (columnIndex.canFindNext() && ascending && (current == null || current.getIndexType().isScan() || columnIndex == current)) {
          IndexType type=columnIndex.getIndexType();
          if (!type.isHash() && (!type.isUnique() || columnIndex.getColumns().length > 1)) {
            topTableFilter.setIndex(columnIndex);
            isDistinctQuery=true;
          }
        }
      }
    }
  }
  if (sort != null && !isQuickAggregateQuery && !isGroupQuery) {
    Index index=getSortIndex();
    Index current=topTableFilter.getIndex();
    if (index != null && current != null) {
      if (current.getIndexType().isScan() || current == index) {
        topTableFilter.setIndex(index);
        if (!topTableFilter.hasInComparisons()) {
          sortUsingIndex=true;
        }
      }
 else       if (index.getIndexColumns() != null && index.getIndexColumns().length >= current.getIndexColumns().length) {
        IndexColumn[] sortColumns=index.getIndexColumns();
        IndexColumn[] currentColumns=current.getIndexColumns();
        boolean swapIndex=false;
        for (int i=0; i < currentColumns.length; i++) {
          if (sortColumns[i].column != currentColumns[i].column) {
            swapIndex=false;
            break;
          }
          if (sortColumns[i].sortType != currentColumns[i].sortType) {
            swapIndex=true;
          }
        }
        if (swapIndex) {
          topTableFilter.setIndex(index);
          sortUsingIndex=true;
        }
      }
    }
  }
  if (!isQuickAggregateQuery && isGroupQuery && getGroupByExpressionCount() > 0) {
    Index index=getGroupSortedIndex();
    Index current=topTableFilter.getIndex();
    if (index != null && current != null && (current.getIndexType().isScan() || current == index)) {
      topTableFilter.setIndex(index);
      isGroupSortedQuery=true;
    }
  }
  expressionArray=new Expression[expressions.size()];
  expressions.toArray(expressionArray);
  isPrepared=true;
}