cryptoe commented on code in PR #13952:
URL: https://github.com/apache/druid/pull/13952#discussion_r1170874096
##########
processing/src/main/java/org/apache/druid/query/scan/ScanQueryQueryToolChest.java:
##########
@@ -217,77 +218,106 @@ public Optional<Sequence<FrameSignaturePair>>
resultsAsFrames(
)
{
final AtomicLong memoryLimitAccumulator = memoryLimitBytes != null ? new
AtomicLong(memoryLimitBytes) : null;
- Yielder<ScanResultValue> yielder = Yielders.each(resultSequence);
- RowSignature prevSignature = null;
- List<Cursor> unwrittenCursors = null;
- List<FrameSignaturePair> frameSignaturePairs = new ArrayList<>();
- while (!yielder.isDone()) {
- ScanResultValue scanResultValue = yielder.get();
- final List rows = (List) scanResultValue.getEvents();
- final Function<?, Object[]> mapper = getResultFormatMapper(query);
- final Iterable<Object[]> formattedRows = Iterables.transform(rows,
(Function) mapper);
+ Iterable<FrameSignaturePair> iterable = () -> new
Iterator<FrameSignaturePair>()
+ {
+ ScanResultValue nextScanResultValue = null;
+ Yielder<ScanResultValue> yielder = Yielders.each(resultSequence);
+ boolean started = false;
+
+ @Override
+ public boolean hasNext()
+ {
+ return !yielder.isDone() || nextScanResultValue != null;
Review Comment:
This looks to me a impl of peeking iterator.
`com.google.common.collect.PeekingIterator`
##########
processing/src/main/java/org/apache/druid/query/scan/ScanQueryQueryToolChest.java:
##########
@@ -217,77 +218,106 @@ public Optional<Sequence<FrameSignaturePair>>
resultsAsFrames(
)
{
final AtomicLong memoryLimitAccumulator = memoryLimitBytes != null ? new
AtomicLong(memoryLimitBytes) : null;
- Yielder<ScanResultValue> yielder = Yielders.each(resultSequence);
- RowSignature prevSignature = null;
- List<Cursor> unwrittenCursors = null;
- List<FrameSignaturePair> frameSignaturePairs = new ArrayList<>();
- while (!yielder.isDone()) {
- ScanResultValue scanResultValue = yielder.get();
- final List rows = (List) scanResultValue.getEvents();
- final Function<?, Object[]> mapper = getResultFormatMapper(query);
- final Iterable<Object[]> formattedRows = Iterables.transform(rows,
(Function) mapper);
+ Iterable<FrameSignaturePair> iterable = () -> new
Iterator<FrameSignaturePair>()
+ {
+ ScanResultValue nextScanResultValue = null;
+ Yielder<ScanResultValue> yielder = Yielders.each(resultSequence);
+ boolean started = false;
+
+ @Override
+ public boolean hasNext()
+ {
+ return !yielder.isDone() || nextScanResultValue != null;
+ }
- if (prevSignature == null ||
!prevSignature.equals(scanResultValue.getRowSignature())) {
-
- if (unwrittenCursors != null && prevSignature != null) {
- FrameWriterFactory frameWriterFactory =
FrameWriters.makeFrameWriterFactory(
- FrameType.ROW_BASED,
- new
SingleMemoryAllocatorFactory(HeapMemoryAllocator.unlimited()),
- prevSignature,
- new ArrayList<>(),
- true
- );
- Cursor concatCursor = new ConcatCursor(unwrittenCursors);
- Frame frame = FrameCursorUtils.cursorToFrame(
- concatCursor,
- frameWriterFactory,
- memoryLimitAccumulator != null ? memoryLimitAccumulator.get() :
null
- );
- if (memoryLimitAccumulator != null) {
- memoryLimitAccumulator.getAndAdd(-frame.numBytes());
+ @Override
+ public FrameSignaturePair next()
+ {
+ if (nextScanResultValue == null) {
+ if (!started) {
+ nextScanResultValue = yielder.get();
+ yielder = yielder.next(null);
+ started = true;
+ return next();
+ } else {
+ throw new NoSuchElementException();
+ }
+ }
+ List<ScanResultValue> batch = new ArrayList<>();
+ RowSignature signature = nextScanResultValue.getRowSignature();
+ batch.add(nextScanResultValue);
+ boolean updatedNextScanResultValue = false;
+ while (!yielder.isDone()) {
+ ScanResultValue potentiallyBatchableScanResultValue = yielder.get();
+ if (signature != null &&
signature.equals(potentiallyBatchableScanResultValue.getRowSignature())) {
+ batch.add(potentiallyBatchableScanResultValue);
+ yielder = yielder.next(null);
+ } else {
+ nextScanResultValue = potentiallyBatchableScanResultValue;
+ updatedNextScanResultValue = true;
+ yielder = yielder.next(null);
+ break;
}
- frameSignaturePairs.add(new FrameSignaturePair(frame,
prevSignature));
}
- unwrittenCursors = new ArrayList<>();
- unwrittenCursors.add(IterableRowsCursorHelper.getCursorFromIterable(
- formattedRows,
- scanResultValue.getRowSignature()
- ));
+ // We are done iterating over the elements, and next call to hasNext()
should return false
+ if (yielder.isDone() && !updatedNextScanResultValue) {
+ nextScanResultValue = null;
+ }
- } else {
- unwrittenCursors.add(IterableRowsCursorHelper.getCursorFromIterable(
- formattedRows,
- scanResultValue.getRowSignature()
- ));
- }
+ FrameSignaturePair retVal = convertScanResultValuesToFrame(batch,
signature, query, memoryLimitAccumulator);
- prevSignature = scanResultValue.getRowSignature();
- yielder = yielder.next(null);
- }
+ if (memoryLimitAccumulator != null) {
Review Comment:
Nit: Please push memoryLimitAccumulator inside convertScanResultValuesToFrame
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]