diff --git a/core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java b/core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java index 5a63e72940357..64e4e74b0cf37 100644 --- a/core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java +++ b/core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java @@ -265,7 +265,9 @@ public void insertRecord( public UnsafeSorterIterator getSortedIterator() throws IOException { final UnsafeSorterIterator inMemoryIterator = sorter.getSortedIterator(); - if (!spillWriters.isEmpty()) { + if (spillWriters.isEmpty()) { + return inMemoryIterator; + } else { final UnsafeSorterSpillMerger spillMerger = new UnsafeSorterSpillMerger(recordComparator, prefixComparator); for (UnsafeSorterSpillWriter spillWriter : spillWriters) { @@ -276,8 +278,6 @@ public UnsafeSorterIterator getSortedIterator() throws IOException { spillMerger.addSpill(inMemoryIterator); } return spillMerger.getSortedIterator(); - } else { - return inMemoryIterator; } } }