3131
3232package org .opensearch .search .aggregations .bucket .histogram ;
3333
34- import org .apache .lucene .index .DocValues ;
3534import org .apache .lucene .index .LeafReaderContext ;
36- import org .apache .lucene .index .NumericDocValues ;
3735import org .apache .lucene .index .SortedNumericDocValues ;
3836import org .apache .lucene .search .CollectionTerminatedException ;
3937import org .apache .lucene .search .ScoreMode ;
@@ -247,8 +245,6 @@ public final DeferringBucketCollector getDeferringCollector() {
247245
248246 protected abstract LeafBucketCollector getLeafCollector (SortedNumericDocValues values , LeafBucketCollector sub ) throws IOException ;
249247
250- protected abstract LeafBucketCollector getLeafCollector (NumericDocValues values , LeafBucketCollector sub ) throws IOException ;
251-
252248 @ Override
253249 public final LeafBucketCollector getLeafCollector (LeafReaderContext ctx , LeafBucketCollector sub ) throws IOException {
254250 if (valuesSource == null ) {
@@ -265,8 +261,7 @@ public final LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBuc
265261 if (optimized ) throw new CollectionTerminatedException ();
266262
267263 final SortedNumericDocValues values = valuesSource .longValues (ctx );
268- final NumericDocValues singleton = DocValues .unwrapSingleton (values );
269- final LeafBucketCollector iteratingCollector = singleton != null ? getLeafCollector (singleton , sub ) : getLeafCollector (values , sub );
264+ final LeafBucketCollector iteratingCollector = getLeafCollector (values , sub );
270265 return new LeafBucketCollectorBase (sub , values ) {
271266 @ Override
272267 public void collect (int doc , long owningBucketOrd ) throws IOException {
@@ -330,7 +325,7 @@ protected final void merge(long[] mergeMap, long newNumBuckets) {
330325 @ Override
331326 public void collectDebugInfo (BiConsumer <String , Object > add ) {
332327 super .collectDebugInfo (add );
333- // filterRewriteOptimizationContext.populateDebugInfo(add);
328+ filterRewriteOptimizationContext .populateDebugInfo (add );
334329 }
335330
336331 /**
@@ -487,65 +482,6 @@ private void increaseRoundingIfNeeded(long rounded) {
487482 };
488483 }
489484
490- protected LeafBucketCollector getLeafCollector (NumericDocValues values , LeafBucketCollector sub ) throws IOException {
491- return new LeafBucketCollectorBase (sub , values ) {
492- @ Override
493- public void collect (int doc , long owningBucketOrd ) throws IOException {
494- assert owningBucketOrd == 0 ;
495- // if (false == values.advanceExact(doc)) {
496- // return;
497- // }
498- //
499- // long value = values.longValue();
500- // long rounded = preparedRounding.round(value);
501- // collectValue(doc, rounded);
502- if (values .advanceExact (doc )) {
503- collectValue (doc , preparedRounding .round (values .longValue ()));
504- }
505- }
506-
507- private void collectValue (int doc , long rounded ) throws IOException {
508- long bucketOrd = bucketOrds .add (0 , rounded );
509- if (bucketOrd < 0 ) { // already seen
510- bucketOrd = -1 - bucketOrd ;
511- collectExistingBucket (sub , doc , bucketOrd );
512- return ;
513- }
514- collectBucket (sub , doc , bucketOrd );
515- increaseRoundingIfNeeded (rounded );
516- }
517-
518- private void increaseRoundingIfNeeded (long rounded ) {
519- if (roundingIdx >= roundingInfos .length - 1 ) {
520- return ;
521- }
522- min = Math .min (min , rounded );
523- max = Math .max (max , rounded );
524- if (bucketOrds .size () <= targetBuckets * roundingInfos [roundingIdx ].getMaximumInnerInterval ()
525- && max - min <= targetBuckets * roundingInfos [roundingIdx ].getMaximumRoughEstimateDurationMillis ()) {
526- return ;
527- }
528- do {
529- try (LongKeyedBucketOrds oldOrds = bucketOrds ) {
530- preparedRounding = prepareRounding (++roundingIdx );
531- long [] mergeMap = new long [Math .toIntExact (oldOrds .size ())];
532- bucketOrds = new LongKeyedBucketOrds .FromSingle (context .bigArrays ());
533- LongKeyedBucketOrds .BucketOrdsEnum ordsEnum = oldOrds .ordsEnum (0 );
534- while (ordsEnum .next ()) {
535- long oldKey = ordsEnum .value ();
536- long newKey = preparedRounding .round (oldKey );
537- long newBucketOrd = bucketOrds .add (0 , newKey );
538- mergeMap [(int ) ordsEnum .ord ()] = newBucketOrd >= 0 ? newBucketOrd : -1 - newBucketOrd ;
539- }
540- merge (mergeMap , bucketOrds .size ());
541- }
542- } while (roundingIdx < roundingInfos .length - 1
543- && (bucketOrds .size () > targetBuckets * roundingInfos [roundingIdx ].getMaximumInnerInterval ()
544- || max - min > targetBuckets * roundingInfos [roundingIdx ].getMaximumRoughEstimateDurationMillis ()));
545- }
546- };
547- }
548-
549485 @ Override
550486 public InternalAggregation [] buildAggregations (long [] owningBucketOrds ) throws IOException {
551487 return buildAggregations (bucketOrds , l -> roundingIdx , owningBucketOrds );
@@ -794,89 +730,6 @@ private int increaseRoundingIfNeeded(long owningBucketOrd, int oldEstimatedBucke
794730 };
795731 }
796732
797- @ Override
798- protected LeafBucketCollector getLeafCollector (NumericDocValues values , LeafBucketCollector sub ) throws IOException {
799- return new LeafBucketCollectorBase (sub , values ) {
800- @ Override
801- public void collect (int doc , long owningBucketOrd ) throws IOException {
802- if (false == values .advanceExact (doc )) {
803- return ;
804- }
805-
806- int roundingIdx = roundingIndexFor (owningBucketOrd );
807- long value = values .longValue ();
808- long rounded = preparedRoundings [roundingIdx ].round (value );
809- collectValue (owningBucketOrd , roundingIdx , doc , rounded );
810- }
811-
812- private int collectValue (long owningBucketOrd , int roundingIdx , int doc , long rounded ) throws IOException {
813- long bucketOrd = bucketOrds .add (owningBucketOrd , rounded );
814- if (bucketOrd < 0 ) { // already seen
815- bucketOrd = -1 - bucketOrd ;
816- collectExistingBucket (sub , doc , bucketOrd );
817- return roundingIdx ;
818- }
819- collectBucket (sub , doc , bucketOrd );
820- liveBucketCountUnderestimate = context .bigArrays ().grow (liveBucketCountUnderestimate , owningBucketOrd + 1 );
821- int estimatedBucketCount = liveBucketCountUnderestimate .increment (owningBucketOrd , 1 );
822- return increaseRoundingIfNeeded (owningBucketOrd , estimatedBucketCount , rounded , roundingIdx );
823- }
824-
825- /**
826- * Increase the rounding of {@code owningBucketOrd} using
827- * estimated, bucket counts, {@link FromMany#rebucket()} rebucketing} the all
828- * buckets if the estimated number of wasted buckets is too high.
829- */
830- private int increaseRoundingIfNeeded (long owningBucketOrd , int oldEstimatedBucketCount , long newKey , int oldRounding ) {
831- if (oldRounding >= roundingInfos .length - 1 ) {
832- return oldRounding ;
833- }
834- if (mins .size () < owningBucketOrd + 1 ) {
835- long oldSize = mins .size ();
836- mins = context .bigArrays ().grow (mins , owningBucketOrd + 1 );
837- mins .fill (oldSize , mins .size (), Long .MAX_VALUE );
838- }
839- if (maxes .size () < owningBucketOrd + 1 ) {
840- long oldSize = maxes .size ();
841- maxes = context .bigArrays ().grow (maxes , owningBucketOrd + 1 );
842- maxes .fill (oldSize , maxes .size (), Long .MIN_VALUE );
843- }
844-
845- long min = Math .min (mins .get (owningBucketOrd ), newKey );
846- mins .set (owningBucketOrd , min );
847- long max = Math .max (maxes .get (owningBucketOrd ), newKey );
848- maxes .set (owningBucketOrd , max );
849- if (oldEstimatedBucketCount <= targetBuckets * roundingInfos [oldRounding ].getMaximumInnerInterval ()
850- && max - min <= targetBuckets * roundingInfos [oldRounding ].getMaximumRoughEstimateDurationMillis ()) {
851- return oldRounding ;
852- }
853- long oldRoughDuration = roundingInfos [oldRounding ].roughEstimateDurationMillis ;
854- int newRounding = oldRounding ;
855- int newEstimatedBucketCount ;
856- do {
857- newRounding ++;
858- double ratio = (double ) oldRoughDuration / (double ) roundingInfos [newRounding ].getRoughEstimateDurationMillis ();
859- newEstimatedBucketCount = (int ) Math .ceil (oldEstimatedBucketCount * ratio );
860- } while (newRounding < roundingInfos .length - 1
861- && (newEstimatedBucketCount > targetBuckets * roundingInfos [newRounding ].getMaximumInnerInterval ()
862- || max - min > targetBuckets * roundingInfos [newRounding ].getMaximumRoughEstimateDurationMillis ()));
863- setRounding (owningBucketOrd , newRounding );
864- mins .set (owningBucketOrd , preparedRoundings [newRounding ].round (mins .get (owningBucketOrd )));
865- maxes .set (owningBucketOrd , preparedRoundings [newRounding ].round (maxes .get (owningBucketOrd )));
866- wastedBucketsOverestimate += oldEstimatedBucketCount - newEstimatedBucketCount ;
867- if (wastedBucketsOverestimate > nextRebucketAt ) {
868- rebucket ();
869- // Bump the threshold for the next rebucketing
870- wastedBucketsOverestimate = 0 ;
871- nextRebucketAt *= 2 ;
872- } else {
873- liveBucketCountUnderestimate .set (owningBucketOrd , newEstimatedBucketCount );
874- }
875- return newRounding ;
876- }
877- };
878- }
879-
880733 private void rebucket () {
881734 rebucketCount ++;
882735 try (LongKeyedBucketOrds oldOrds = bucketOrds ) {
0 commit comments