Skip to content

Commit

Permalink
remove singleton DV related change
Browse files Browse the repository at this point in the history
Signed-off-by: bowenlan-amzn <[email protected]>
  • Loading branch information
bowenlan-amzn committed Feb 25, 2025
1 parent f3551bc commit 360b293
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.DocIdSetBuilder;
import org.opensearch.index.mapper.DocCountFieldMapper;
import org.opensearch.search.aggregations.BucketCollector;
import org.opensearch.search.aggregations.LeafBucketCollector;
import org.opensearch.search.internal.SearchContext;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -74,7 +72,6 @@ public FilterRewriteOptimizationContext(
private boolean canOptimize(final Object parent, final int subAggLength, SearchContext context) throws IOException {
if (context.maxAggRewriteFilters() == 0) return false;

// if (parent != null || subAggLength != 0) return false;
if (parent != null) return false;
this.subAggLength = subAggLength;

Expand Down Expand Up @@ -139,7 +136,6 @@ public boolean tryOptimize(
Ranges ranges = getRanges(leafCtx, segmentMatchAll);
if (ranges == null) return false;

// pass in the information of whether subagg exists
Supplier<DocIdSetBuilder> disBuilderSupplier = null;
if (subAggLength != 0) {
disBuilderSupplier = () -> {
Expand All @@ -150,7 +146,6 @@ public boolean tryOptimize(
}
};
}

OptimizeResult optimizeResult = aggregatorBridge.tryOptimize(values, incrementDocCount, ranges, disBuilderSupplier);
consumeDebugInfo(optimizeResult);

Expand Down Expand Up @@ -181,12 +176,6 @@ public boolean tryOptimize(
return true;
}

List<Weight> weights;

public List<Weight> getWeights() {
return weights;
}

Ranges getRanges(LeafReaderContext leafCtx, boolean segmentMatchAll) {
if (!preparedAtShardLevel) {
try {
Expand All @@ -196,7 +185,6 @@ Ranges getRanges(LeafReaderContext leafCtx, boolean segmentMatchAll) {
return null;
}
}
logger.debug("number of ranges: {}", ranges.lowers.length);
return ranges;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

package org.opensearch.search.aggregations.bucket.histogram;

import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SortedNumericDocValues;
Expand Down Expand Up @@ -247,8 +246,6 @@ public final DeferringBucketCollector getDeferringCollector() {

protected abstract LeafBucketCollector getLeafCollector(SortedNumericDocValues values, LeafBucketCollector sub) throws IOException;

protected abstract LeafBucketCollector getLeafCollector(NumericDocValues values, LeafBucketCollector sub) throws IOException;

@Override
public final LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException {
if (valuesSource == null) {
Expand All @@ -265,8 +262,7 @@ public final LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBuc
if (optimized) throw new CollectionTerminatedException();

final SortedNumericDocValues values = valuesSource.longValues(ctx);
final NumericDocValues singleton = DocValues.unwrapSingleton(values);
final LeafBucketCollector iteratingCollector = singleton != null ? getLeafCollector(singleton, sub) : getLeafCollector(values, sub);
final LeafBucketCollector iteratingCollector = getLeafCollector(values, sub);
return new LeafBucketCollectorBase(sub, values) {
@Override
public void collect(int doc, long owningBucketOrd) throws IOException {
Expand Down Expand Up @@ -330,7 +326,7 @@ protected final void merge(long[] mergeMap, long newNumBuckets) {
@Override
public void collectDebugInfo(BiConsumer<String, Object> add) {
super.collectDebugInfo(add);
// filterRewriteOptimizationContext.populateDebugInfo(add);
filterRewriteOptimizationContext.populateDebugInfo(add);
}

/**
Expand Down Expand Up @@ -794,89 +790,6 @@ private int increaseRoundingIfNeeded(long owningBucketOrd, int oldEstimatedBucke
};
}

@Override
protected LeafBucketCollector getLeafCollector(NumericDocValues values, LeafBucketCollector sub) throws IOException {
return new LeafBucketCollectorBase(sub, values) {
@Override
public void collect(int doc, long owningBucketOrd) throws IOException {
if (false == values.advanceExact(doc)) {
return;
}

int roundingIdx = roundingIndexFor(owningBucketOrd);
long value = values.longValue();
long rounded = preparedRoundings[roundingIdx].round(value);
collectValue(owningBucketOrd, roundingIdx, doc, rounded);
}

private int collectValue(long owningBucketOrd, int roundingIdx, int doc, long rounded) throws IOException {
long bucketOrd = bucketOrds.add(owningBucketOrd, rounded);
if (bucketOrd < 0) { // already seen
bucketOrd = -1 - bucketOrd;
collectExistingBucket(sub, doc, bucketOrd);
return roundingIdx;
}
collectBucket(sub, doc, bucketOrd);
liveBucketCountUnderestimate = context.bigArrays().grow(liveBucketCountUnderestimate, owningBucketOrd + 1);
int estimatedBucketCount = liveBucketCountUnderestimate.increment(owningBucketOrd, 1);
return increaseRoundingIfNeeded(owningBucketOrd, estimatedBucketCount, rounded, roundingIdx);
}

/**
* Increase the rounding of {@code owningBucketOrd} using
* estimated, bucket counts, {@link FromMany#rebucket()} rebucketing} the all
* buckets if the estimated number of wasted buckets is too high.
*/
private int increaseRoundingIfNeeded(long owningBucketOrd, int oldEstimatedBucketCount, long newKey, int oldRounding) {
if (oldRounding >= roundingInfos.length - 1) {
return oldRounding;
}
if (mins.size() < owningBucketOrd + 1) {
long oldSize = mins.size();
mins = context.bigArrays().grow(mins, owningBucketOrd + 1);
mins.fill(oldSize, mins.size(), Long.MAX_VALUE);
}
if (maxes.size() < owningBucketOrd + 1) {
long oldSize = maxes.size();
maxes = context.bigArrays().grow(maxes, owningBucketOrd + 1);
maxes.fill(oldSize, maxes.size(), Long.MIN_VALUE);
}

long min = Math.min(mins.get(owningBucketOrd), newKey);
mins.set(owningBucketOrd, min);
long max = Math.max(maxes.get(owningBucketOrd), newKey);
maxes.set(owningBucketOrd, max);
if (oldEstimatedBucketCount <= targetBuckets * roundingInfos[oldRounding].getMaximumInnerInterval()
&& max - min <= targetBuckets * roundingInfos[oldRounding].getMaximumRoughEstimateDurationMillis()) {
return oldRounding;
}
long oldRoughDuration = roundingInfos[oldRounding].roughEstimateDurationMillis;
int newRounding = oldRounding;
int newEstimatedBucketCount;
do {
newRounding++;
double ratio = (double) oldRoughDuration / (double) roundingInfos[newRounding].getRoughEstimateDurationMillis();
newEstimatedBucketCount = (int) Math.ceil(oldEstimatedBucketCount * ratio);
} while (newRounding < roundingInfos.length - 1
&& (newEstimatedBucketCount > targetBuckets * roundingInfos[newRounding].getMaximumInnerInterval()
|| max - min > targetBuckets * roundingInfos[newRounding].getMaximumRoughEstimateDurationMillis()));
setRounding(owningBucketOrd, newRounding);
mins.set(owningBucketOrd, preparedRoundings[newRounding].round(mins.get(owningBucketOrd)));
maxes.set(owningBucketOrd, preparedRoundings[newRounding].round(maxes.get(owningBucketOrd)));
wastedBucketsOverestimate += oldEstimatedBucketCount - newEstimatedBucketCount;
if (wastedBucketsOverestimate > nextRebucketAt) {
rebucket();
// Bump the threshold for the next rebucketing
wastedBucketsOverestimate = 0;
nextRebucketAt *= 2;
} else {
liveBucketCountUnderestimate.set(owningBucketOrd, newEstimatedBucketCount);
}
return newRounding;
}
};
}

private void rebucket() {
rebucketCount++;
try (LongKeyedBucketOrds oldOrds = bucketOrds) {
Expand Down

0 comments on commit 360b293

Please sign in to comment.