Github user nongli commented on a diff in the pull request:
https://github.com/apache/spark/pull/9969#discussion_r46178344
--- Diff:
core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java
---
@@ -521,4 +522,66 @@ public long getKeyPrefix() {
return upstream.getKeyPrefix();
}
}
+
+ /**
+ * Returns a iterator, which will return the rows in the order as
inserted.
+ *
+ * It is the caller's responsibility to call `cleanupResources()`
+ * after consuming this iterator.
+ */
+ public UnsafeSorterIterator getIterator() throws IOException {
+ if (spillWriters.isEmpty()) {
+ assert(inMemSorter != null);
+ return inMemSorter.getIterator();
+ } else {
+ LinkedList<UnsafeSorterIterator> queue = new LinkedList<>();
+ for (UnsafeSorterSpillWriter spillWriter : spillWriters) {
+ queue.add(spillWriter.getReader(blockManager));
+ }
+ if (inMemSorter != null) {
+ queue.add(inMemSorter.getIterator());
+ }
+ return new ChainedIterator(queue);
+ }
+ }
+
+ /**
+ * Chain multiple UnsafeSorterIterator together as single one.
+ */
+ class ChainedIterator extends UnsafeSorterIterator {
+
+ private final Queue<UnsafeSorterIterator> iterators;
+ private UnsafeSorterIterator current;
+
+ public ChainedIterator(Queue<UnsafeSorterIterator> iterators) {
+ assert iterators.size() > 0;
+ this.iterators = iterators;
+ this.current = iterators.remove();
+ }
+
+ @Override
+ public boolean hasNext() {
+ if (!current.hasNext() && !iterators.isEmpty()) {
+ current = iterators.remove();
--- End diff --
This doesnt work if iterators contain empty iterators. Fix or assert that
can't be.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]