Skip to content

Commit 2f48777

Browse files
committed
Add test and fix bug for sorting empty arrays
1 parent d1e28bc commit 2f48777

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,12 @@ public void insertRecord(
263263

264264
public UnsafeSorterIterator getSortedIterator() throws IOException {
265265
final UnsafeSorterIterator inMemoryIterator = sorter.getSortedIterator();
266+
int numIteratorsToMerge = spillWriters.size() + (inMemoryIterator.hasNext() ? 1 : 0);
266267
if (spillWriters.isEmpty()) {
267268
return inMemoryIterator;
268269
} else {
269270
final UnsafeSorterSpillMerger spillMerger =
270-
new UnsafeSorterSpillMerger(recordComparator, prefixComparator);
271+
new UnsafeSorterSpillMerger(recordComparator, prefixComparator, numIteratorsToMerge);
271272
for (UnsafeSorterSpillWriter spillWriter : spillWriters) {
272273
spillMerger.addSpill(spillWriter.getReader(blockManager));
273274
}

core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeSorterSpillMerger.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ final class UnsafeSorterSpillMerger {
2727

2828
public UnsafeSorterSpillMerger(
2929
final RecordComparator recordComparator,
30-
final PrefixComparator prefixComparator) {
30+
final PrefixComparator prefixComparator,
31+
final int numSpills) {
3132
final Comparator<UnsafeSorterIterator> comparator = new Comparator<UnsafeSorterIterator>() {
3233

3334
@Override
@@ -43,8 +44,7 @@ public int compare(UnsafeSorterIterator left, UnsafeSorterIterator right) {
4344
}
4445
}
4546
};
46-
// TODO: the size is often known; incorporate size hints here.
47-
priorityQueue = new PriorityQueue<UnsafeSorterIterator>(10, comparator);
47+
priorityQueue = new PriorityQueue<UnsafeSorterIterator>(numSpills, comparator);
4848
}
4949

5050
public void addSpill(UnsafeSorterIterator spillReader) throws IOException {

core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeSorterSpillWriter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ public void write(
128128
dataRemaining -= toTransfer;
129129
freeSpaceInWriteBuffer = DISK_WRITE_BUFFER_SIZE;
130130
}
131+
if (freeSpaceInWriteBuffer < DISK_WRITE_BUFFER_SIZE) {
132+
writer.write(writeBuffer, 0, (DISK_WRITE_BUFFER_SIZE - freeSpaceInWriteBuffer));
133+
}
131134
writer.recordWritten();
132135
}
133136

core/src/test/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorterSuite.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,34 @@ public void testSortingOnlyByPrefix() throws Exception {
169169
// assert(tempDir.isEmpty)
170170
}
171171

172+
@Test
173+
public void testSortingEmptyArrays() throws Exception {
174+
175+
final UnsafeExternalSorter sorter = new UnsafeExternalSorter(
176+
memoryManager,
177+
shuffleMemoryManager,
178+
blockManager,
179+
taskContext,
180+
recordComparator,
181+
prefixComparator,
182+
1024,
183+
new SparkConf());
184+
185+
sorter.insertRecord(null, 0, 0, 0);
186+
sorter.insertRecord(null, 0, 0, 0);
187+
sorter.spill();
188+
sorter.insertRecord(null, 0, 0, 0);
189+
sorter.spill();
190+
sorter.insertRecord(null, 0, 0, 0);
191+
sorter.insertRecord(null, 0, 0, 0);
192+
193+
UnsafeSorterIterator iter = sorter.getSortedIterator();
194+
195+
for (int i = 1; i <= 5; i++) {
196+
iter.loadNext();
197+
assertEquals(0, iter.getKeyPrefix());
198+
assertEquals(0, iter.getRecordLength());
199+
}
200+
}
201+
172202
}

0 commit comments

Comments
 (0)