Github user hvanhovell commented on a diff in the pull request: https://github.com/apache/spark/pull/18541#discussion_r125631060 --- Diff: core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java --- @@ -596,17 +600,37 @@ public long getKeyPrefix() { * * TODO: support forced spilling */ - public UnsafeSorterIterator getIterator() throws IOException { + public UnsafeSorterIterator getIterator(int startIndex) throws IOException { if (spillWriters.isEmpty()) { assert(inMemSorter != null); - return inMemSorter.getSortedIterator(); + UnsafeSorterIterator iter = inMemSorter.getSortedIterator(); + for (int i = 0; i < startIndex && iter.hasNext(); i++) { + iter.loadNext(); + } + return iter; } else { LinkedList<UnsafeSorterIterator> queue = new LinkedList<>(); + int i = 0; for (UnsafeSorterSpillWriter spillWriter : spillWriters) { - queue.add(spillWriter.getReader(serializerManager)); + if (i + spillWriter.recordsSpilled() <= startIndex) { + i += spillWriter.recordsSpilled(); + continue; + } else { + UnsafeSorterIterator iter = spillWriter.getReader(serializerManager); + for (int j = i; j < startIndex && iter.hasNext(); j++) { + iter.loadNext(); + } + queue.add(iter); + i += spillWriter.recordsSpilled(); + } } if (inMemSorter != null) { - queue.add(inMemSorter.getSortedIterator()); + UnsafeSorterIterator iter = inMemSorter.getSortedIterator(); --- End diff -- This is almost similar to the code in the first if clause. Let's try to get rid of this duplication, and move this logic into a separate function.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org