Buggy code:
public abstract class TermAllGroupHeadsCollector < GH extends GroupHead > extends AbstractAllGroupHeadsCollector < GH > { private static final int DEFAULT_INITIAL_SIZE = 128 ; final String groupField ; final BytesRef scratchBytesRef = new BytesRef ( ) ; DocTermsIndex groupIndex ; AtomicReaderContext readerContext ; protected TermAllGroupHeadsCollector ( String groupField , int numberOfSorts ) { } public static AbstractAllGroupHeadsCollector create ( String groupField , Sort sortWithinGroup ) throws IOException { } public static AbstractAllGroupHeadsCollector create ( String groupField , Sort sortWithinGroup , int initialSize ) throws IOException { } private static boolean needGeneralImpl ( SortField sortField ) { } static class GeneralAllGroupHeadsCollector extends TermAllGroupHeadsCollector < TermAllGroupHeadsCollector . GeneralAllGroupHeadsCollector . GroupHead > { private final Sort sortWithinGroup ; private final Map < BytesRef , TermAllGroupHeadsCollector . GeneralAllGroupHeadsCollector . GroupHead > groups ; private Scorer scorer ; GeneralAllGroupHeadsCollector ( String groupField , Sort sortWithinGroup ) throws IOException { } protected void retrieveGroupHeadAndAddIfNotExist ( int doc ) throws IOException { final int ord = groupIndex . getOrd ( doc ) ; final BytesRef groupValue = ( ord == 0 ) ? null : groupIndex . lookup ( ord , scratchBytesRef ) ; TermAllGroupHeadsCollector . GeneralAllGroupHeadsCollector . GroupHead groupHead = groups . get ( groupValue ) ; if ( groupHead == null ) { groupHead = new TermAllGroupHeadsCollector . GeneralAllGroupHeadsCollector . GroupHead ( groupValue , sortWithinGroup , doc ) ; <START_BUG> groups . put ( ( groupValue == null ? null : new BytesRef ( groupValue ) ) , groupHead ) ; <END_BUG> temporalResult . stop = true ; } else { temporalResult . stop = false ; } temporalResult . groupHead = groupHead ; } protected Collection < TermAllGroupHeadsCollector . GeneralAllGroupHeadsCollector . GroupHead > getCollectedGroupHeads ( ) { } public void setNextReader ( IndexReader . AtomicReaderContext context ) throws IOException { } public void setScorer ( Scorer scorer ) throws IOException { } class GroupHead extends AbstractAllGroupHeadsCollector . GroupHead < BytesRef > { final FieldComparator [ ] comparators ; private GroupHead ( BytesRef groupValue , Sort sort , int doc ) throws IOException { } public int compare ( int compIDX , int doc ) throws IOException { } public void updateDocHead ( int doc ) throws IOException { } } } static class OrdScoreAllGroupHeadsCollector extends TermAllGroupHeadsCollector < TermAllGroupHeadsCollector . OrdScoreAllGroupHeadsCollector . GroupHead > { private final SentinelIntSet ordSet ; private final List < TermAllGroupHeadsCollector . OrdScoreAllGroupHeadsCollector . GroupHead > collectedGroups ; private final SortField [ ] fields ; private DocTermsIndex [ ] sortsIndex ; private Scorer scorer ; private TermAllGroupHeadsCollector . OrdScoreAllGroupHeadsCollector . GroupHead [ ] segmentGroupHeads ; OrdScoreAllGroupHeadsCollector ( String groupField , Sort sortWithinGroup , int initialSize ) { } protected Collection < TermAllGroupHeadsCollector . OrdScoreAllGroupHeadsCollector . GroupHead > getCollectedGroupHeads ( ) { } public void setScorer ( Scorer scorer ) throws IOException { } protected void retrieveGroupHeadAndAddIfNotExist ( int doc ) throws IOException { int key = groupIndex . getOrd ( doc ) ; TermAllGroupHeadsCollector . OrdScoreAllGroupHeadsCollector . GroupHead groupHead ; if ( ! ( ordSet . exists ( key ) ) ) { ordSet . put ( key ) ; BytesRef term = ( key == 0 ) ? null : groupIndex . getTerm ( doc , new BytesRef ( ) ) ; groupHead = new TermAllGroupHeadsCollector . OrdScoreAllGroupHeadsCollector . GroupHead ( doc , term ) ; collectedGroups . add ( groupHead ) ; segmentGroupHeads [ key ] = groupHead ; temporalResult . stop = true ; } else { temporalResult . stop = false ; groupHead = segmentGroupHeads [ key ] ; } temporalResult . groupHead = groupHead ; } public void setNextReader ( IndexReader . AtomicReaderContext context ) throws IOException { } class GroupHead extends AbstractAllGroupHeadsCollector . GroupHead < BytesRef > { BytesRef [ ] sortValues ; int [ ] sortOrds ; float [ ] scores ; private GroupHead ( int doc , BytesRef groupValue ) throws IOException { } public int compare ( int compIDX , int doc ) throws IOException { } public void updateDocHead ( int doc ) throws IOException { } } } static class OrdAllGroupHeadsCollector extends TermAllGroupHeadsCollector < TermAllGroupHeadsCollector . OrdAllGroupHeadsCollector . GroupHead > { private final SentinelIntSet ordSet ; private final List < TermAllGroupHeadsCollector . OrdAllGroupHeadsCollector . GroupHead > collectedGroups ; private final SortField [ ] fields ; private DocTermsIndex [ ] sortsIndex ; private TermAllGroupHeadsCollector . OrdAllGroupHeadsCollector . GroupHead [ ] segmentGroupHeads ; OrdAllGroupHeadsCollector ( String groupField , Sort sortWithinGroup , int initialSize ) { } protected Collection < TermAllGroupHeadsCollector . OrdAllGroupHeadsCollector . GroupHead > getCollectedGroupHeads ( ) { } public void setScorer ( Scorer scorer ) throws IOException { } protected void retrieveGroupHeadAndAddIfNotExist ( int doc ) throws IOException { int key = groupIndex . getOrd ( doc ) ; TermAllGroupHeadsCollector . OrdAllGroupHeadsCollector . GroupHead groupHead ; if ( ! ( ordSet . exists ( key ) ) ) { ordSet . put ( key ) ; BytesRef term = ( key == 0 ) ? null : groupIndex . getTerm ( doc , new BytesRef ( ) ) ; groupHead = new TermAllGroupHeadsCollector . OrdAllGroupHeadsCollector . GroupHead ( doc , term ) ; collectedGroups . add ( groupHead ) ; segmentGroupHeads [ key ] = groupHead ; temporalResult . stop = true ; } else { temporalResult . stop = false ; groupHead = segmentGroupHeads [ key ] ; } temporalResult . groupHead = groupHead ; } public void setNextReader ( IndexReader . AtomicReaderContext context ) throws IOException { } class GroupHead extends AbstractAllGroupHeadsCollector . GroupHead < BytesRef > { BytesRef [ ] sortValues ; int [ ] sortOrds ; private GroupHead ( int doc , BytesRef groupValue ) throws IOException { } public int compare ( int compIDX , int doc ) throws IOException { } public void updateDocHead ( int doc ) throws IOException { } } } static class ScoreAllGroupHeadsCollector extends TermAllGroupHeadsCollector < TermAllGroupHeadsCollector . ScoreAllGroupHeadsCollector . GroupHead > { private final SentinelIntSet ordSet ; private final List < TermAllGroupHeadsCollector . ScoreAllGroupHeadsCollector . GroupHead > collectedGroups ; private final SortField [ ] fields ; private Scorer scorer ;

Fix:
groups . put ( ( groupValue == null ? null : BytesRef . deepCopyOf ( groupValue ) ) , groupHead ) ;