Buggy code:
public void rejectParameters ( ) throws StandardException { } void pushOrderByList ( OrderByList orderByList ) { } public ResultSetNode preprocess ( int numTables , GroupByList gbl , FromList fl ) throws StandardException { } private void performTransitiveClosure ( int numTables ) throws StandardException { } private ValueNode normExpressions ( ValueNode boolClause ) throws StandardException { } public ResultSetNode addNewPredicate ( Predicate predicate ) throws StandardException { } public boolean flattenableInFromSubquery ( FromList fromList ) { } public ResultSetNode genProjectRestrict ( int origFromListSize ) throws StandardException { boolean eliminateSort = false ; PredicateList restrictionList ; ResultColumnList prRCList ; ResultSetNode prnRSN ; prnRSN = ( ( ResultSetNode ) ( getNodeFactory ( ) . getNode ( PROJECT_RESTRICT_NODE , fromList . elementAt ( 0 ) , resultColumns , whereClause , wherePredicates , selectSubquerys , whereSubquerys , null , getContextManager ( ) ) ) ) ; if ( ( ( ( selectAggregates ) != null ) && ( ( selectAggregates . size ( ) ) > 0 ) ) || ( ( groupByList ) != null ) ) { Vector aggs = selectAggregates ; if ( ( ( havingAggregates ) != null ) && ( ! ( havingAggregates . isEmpty ( ) ) ) ) { havingAggregates . addAll ( selectAggregates ) ; aggs = havingAggregates ; } GroupByNode gbn = ( ( GroupByNode ) ( getNodeFactory ( ) . getNode ( GROUP_BY_NODE , prnRSN , groupByList , aggs , havingClause , havingSubquerys , null , new Integer ( nestingLevel ) , getContextManager ( ) ) ) ) ; gbn . considerPostOptimizeOptimizations ( ( ( originalWhereClause ) != null ) ) ; gbn . assignCostEstimate ( optimizer . getOptimizedCost ( ) ) ; groupByList = null ; prnRSN = gbn . getParent ( ) ; eliminateSort = eliminateSort || ( gbn . getIsInSortedOrder ( ) ) ; } if ( isDistinct ) { resultColumns . verifyAllOrderable ( ) ; boolean distinctScanPossible = false ; if ( ( origFromListSize == 1 ) && ( ! ( orderByAndDistinctMerged ) ) ) { boolean simpleColumns = true ; HashSet distinctColumns = new HashSet ( ) ; int size = resultColumns . size ( ) ; for ( int i = 1 ; i <= size ; i ++ ) { BaseColumnNode bc = resultColumns . getResultColumn ( i ) . getBaseColumnNode ( ) ; if ( bc == null ) { simpleColumns = false ; break ; } distinctColumns . add ( bc ) ; } if ( simpleColumns && ( prnRSN . isPossibleDistinctScan ( distinctColumns ) ) ) { prnRSN . markForDistinctScan ( ) ; distinctScanPossible = true ; } } if ( ! distinctScanPossible ) { boolean inSortedOrder = isOrderedResult ( resultColumns , prnRSN , ( ! ( orderByAndDistinctMerged ) ) ) ; prnRSN = ( ( ResultSetNode ) ( getNodeFactory ( ) . getNode ( DISTINCT_NODE , prnRSN , new Boolean ( inSortedOrder ) , null , getContextManager ( ) ) ) ) ; prnRSN . costEstimate = costEstimate . cloneMe ( ) ; eliminateSort = eliminateSort || inSortedOrder ; } } if ( ( orderByList ) != null ) { if ( orderByList . getSortNeeded ( ) ) { prnRSN = ( ( ResultSetNode ) ( getNodeFactory ( ) . getNode ( ORDER_BY_NODE , prnRSN , orderByList , null , getContextManager ( ) ) ) ) ; prnRSN . costEstimate = costEstimate . cloneMe ( ) ; } int orderBySelect = this . getResultColumns ( ) . getOrderBySelect ( ) ; if ( orderBySelect > 0 ) { ResultColumnList selectRCs = prnRSN . getResultColumns ( ) . copyListAndObjects ( ) ; selectRCs . removeOrderByColumns ( ) ; selectRCs . genVirtualColumnNodes ( prnRSN , prnRSN . getResultColumns ( ) ) ; prnRSN = ( ( ResultSetNode ) ( getNodeFactory ( ) . getNode ( PROJECT_RESTRICT_NODE , prnRSN , selectRCs , null , null , null , null , null , getContextManager ( ) ) ) ) ; } } if ( ( wasGroupBy ) && ( ( resultColumns . numGeneratedColumnsForGroupBy ( ) ) > 0 ) ) { ResultColumnList newSelectList = prnRSN . getResultColumns ( ) . copyListAndObjects ( ) ; newSelectList . removeGeneratedGroupingColumns ( ) ; newSelectList . genVirtualColumnNodes ( prnRSN , prnRSN . getResultColumns ( ) ) ; prnRSN = ( ( ResultSetNode ) ( getNodeFactory ( ) . getNode ( PROJECT_RESTRICT_NODE , prnRSN , newSelectList , null , null , null , null , null , getContextManager ( ) ) ) ) ; } if ( ( ! ( ( ( orderByList ) != null ) && ( orderByList . getSortNeeded ( ) ) ) ) && ( orderByQuery ) ) { eliminateSort = true ; } if ( eliminateSort ) { <START_BUG> prnRSN . adjustForSortElimination ( ) ; <END_BUG> } prnRSN . costEstimate = costEstimate . cloneMe ( ) ; return prnRSN ; } private boolean isOrderedResult ( ResultColumnList resultColumns , ResultSetNode newTopRSN , boolean permuteOrdering ) throws StandardException { } public ResultSetNode ensurePredicateList ( int numTables ) throws StandardException { } public ResultSetNode optimize ( DataDictionary dataDictionary , PredicateList predicateList , double outerRows ) throws StandardException { } public ResultSetNode modifyAccessPaths ( PredicateList predList ) throws StandardException { } public ResultSetNode modifyAccessPaths ( ) throws StandardException { } public CostEstimate getFinalCostEstimate ( ) throws StandardException { } boolean isUpdatableCursor ( DataDictionary dd ) throws StandardException { } FromTable getCursorTargetTable ( ) { } public boolean referencesTarget ( String name , boolean baseTable ) throws StandardException { } public void disablePrivilegeCollection ( ) { } boolean subqueryReferencesTarget ( String name , boolean baseTable ) throws StandardException { } public void bindUntypedNullsToResultColumns ( ResultColumnList bindingRCL ) throws StandardException { } void decrementLevel ( int decrement ) { } boolean uniqueSubquery ( boolean additionalEQ ) throws StandardException { } public int updateTargetLockMode ( ) { } boolean returnsAtMostOneRow ( ) { } public boolean referencesSessionSchema ( ) throws StandardException { } public Visitable accept ( Visitor v ) throws StandardException { } public boolean hasAggregatesInSelectList ( ) { } }

Fix:
prnRSN . adjustForSortElimination ( orderByList ) ;