@@ -133,11 +133,12 @@ public Page getCheckedOutput() throws IOException {
133133 if (docCollector != null ) {
134134 blocks [blockIndex ++] = docCollector .build ().asBlock ();
135135 }
136- blocks [blockIndex ++] = tsHashesBuilder .build ().asBlock ();
136+ OrdinalBytesRefVector tsidVector = tsHashesBuilder .build ();
137+ blocks [blockIndex ++] = tsidVector .asBlock ();
137138 tsHashesBuilder = new TsidBuilder (blockFactory , Math .min (remainingDocs , maxPageSize ));
138139 blocks [blockIndex ++] = timestampsBuilder .build ().asBlock ();
139140 timestampsBuilder = blockFactory .newLongVectorBuilder (Math .min (remainingDocs , maxPageSize ));
140- System .arraycopy (fieldsReader .buildBlocks (), 0 , blocks , blockIndex , fieldsToExtracts .size ());
141+ System .arraycopy (fieldsReader .buildBlocks (tsidVector . getOrdinalsVector () ), 0 , blocks , blockIndex , fieldsToExtracts .size ());
141142 page = new Page (currentPagePos , blocks );
142143 currentPagePos = 0 ;
143144 }
@@ -217,6 +218,7 @@ void readDocsForNextPage() throws IOException {
217218 }
218219
219220 private boolean readValuesForOneTsid (PriorityQueue <LeafIterator > sub ) throws IOException {
221+ boolean first = true ;
220222 do {
221223 LeafIterator top = sub .top ();
222224 currentPagePos ++;
@@ -226,7 +228,8 @@ private boolean readValuesForOneTsid(PriorityQueue<LeafIterator> sub) throws IOE
226228 }
227229 tsHashesBuilder .appendOrdinal ();
228230 timestampsBuilder .appendLong (top .timestamp );
229- fieldsReader .readValues (top .segmentOrd , top .docID );
231+ fieldsReader .readValues (top .segmentOrd , top .docID , first == false );
232+ first = false ;
230233 if (top .nextDoc ()) {
231234 sub .updateTop ();
232235 } else if (top .docID == DocIdSetIterator .NO_MORE_DOCS ) {
@@ -350,6 +353,7 @@ static final class ShardLevelFieldsReader implements Releasable {
350353 private final BlockLoaderFactory blockFactory ;
351354 private final SegmentLevelFieldsReader [] segments ;
352355 private final BlockLoader [] loaders ;
356+ private final boolean [] dimensions ;
353357 private final Block .Builder [] builders ;
354358 private final StoredFieldsSpec storedFieldsSpec ;
355359 private final SourceLoader sourceLoader ;
@@ -377,10 +381,14 @@ static final class ShardLevelFieldsReader implements Releasable {
377381 sourceLoader = null ;
378382 }
379383 this .storedFieldsSpec = storedFieldsSpec ;
384+ this .dimensions = new boolean [fields .size ()];
385+ for (int i = 0 ; i < fields .size (); i ++) {
386+ dimensions [i ] = shardContext .fieldType (fields .get (i ).name ()).isDimension ();
387+ }
380388 }
381389
382- void readValues (int segment , int docID ) throws IOException {
383- segments [segment ].read (docID , builders );
390+ void readValues (int segment , int docID , boolean nonDimensionFieldsOnly ) throws IOException {
391+ segments [segment ].read (docID , builders , nonDimensionFieldsOnly , dimensions );
384392 }
385393
386394 void prepareForReading (int estimatedSize ) throws IOException {
@@ -396,12 +404,46 @@ void prepareForReading(int estimatedSize) throws IOException {
396404 }
397405 }
398406
399- Block [] buildBlocks () {
400- Block [] blocks = Block .Builder .buildAll (builders );
401- Arrays .fill (builders , null );
407+ Block [] buildBlocks (IntVector tsidOrdinals ) {
408+ final Block [] blocks = new Block [loaders .length ];
409+ try {
410+ for (int i = 0 ; i < builders .length ; i ++) {
411+ if (dimensions [i ]) {
412+ blocks [i ] = buildBlockForDimensionField (builders [i ], tsidOrdinals );
413+ } else {
414+ blocks [i ] = builders [i ].build ();
415+ }
416+ }
417+ Arrays .fill (builders , null );
418+ } finally {
419+ if (blocks .length > 0 && blocks [blocks .length - 1 ] == null ) {
420+ Releasables .close (blocks );
421+ }
422+ }
402423 return blocks ;
403424 }
404425
426+ private Block buildBlockForDimensionField (Block .Builder builder , IntVector tsidOrdinals ) {
427+ try (var values = builder .build ()) {
428+ if (values .asVector () instanceof BytesRefVector bytes ) {
429+ tsidOrdinals .incRef ();
430+ values .incRef ();
431+ return new OrdinalBytesRefVector (tsidOrdinals , bytes ).asBlock ();
432+ } else if (values .areAllValuesNull ()) {
433+ return blockFactory .factory .newConstantNullBlock (tsidOrdinals .getPositionCount ());
434+ } else {
435+ final int positionCount = tsidOrdinals .getPositionCount ();
436+ try (var newBuilder = values .elementType ().newBlockBuilder (positionCount , blockFactory .factory )) {
437+ for (int p = 0 ; p < positionCount ; p ++) {
438+ int pos = tsidOrdinals .getInt (p );
439+ newBuilder .copyFrom (values , pos , pos + 1 );
440+ }
441+ return newBuilder .build ();
442+ }
443+ }
444+ }
445+ }
446+
405447 @ Override
406448 public void close () {
407449 Releasables .close (builders );
@@ -435,10 +477,18 @@ private void reinitializeIfNeeded(SourceLoader sourceLoader, StoredFieldsSpec st
435477 }
436478 }
437479
438- void read (int docId , Block .Builder [] builder ) throws IOException {
480+ void read (int docId , Block .Builder [] builder , boolean nonDimensionFieldsOnly , boolean [] dimensions ) throws IOException {
439481 storedFields .advanceTo (docId );
440- for (int i = 0 ; i < rowStride .length ; i ++) {
441- rowStride [i ].read (docId , storedFields , builder [i ]);
482+ if (nonDimensionFieldsOnly ) {
483+ for (int i = 0 ; i < rowStride .length ; i ++) {
484+ if (dimensions [i ] == false ) {
485+ rowStride [i ].read (docId , storedFields , builder [i ]);
486+ }
487+ }
488+ } else {
489+ for (int i = 0 ; i < rowStride .length ; i ++) {
490+ rowStride [i ].read (docId , storedFields , builder [i ]);
491+ }
442492 }
443493 }
444494 }
@@ -480,9 +530,9 @@ public void close() {
480530 Releasables .close (dictBuilder , ordinalsBuilder );
481531 }
482532
483- BytesRefVector build () throws IOException {
533+ OrdinalBytesRefVector build () throws IOException {
484534 BytesRefVector dict = null ;
485- BytesRefVector result = null ;
535+ OrdinalBytesRefVector result = null ;
486536 IntVector ordinals = null ;
487537 try {
488538 dict = dictBuilder .build ();
0 commit comments