Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add singleton optimization for DateHistogramAggregator #17643

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .idea/runConfigurations/Debug_OpenSearch.xml

This file was deleted.

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

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

import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.ScoreMode;
Expand Down Expand Up @@ -201,13 +203,28 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCol
return LeafBucketCollector.NO_OP_COLLECTOR;
}

SortedNumericDocValues values = valuesSource.longValues(ctx);
final SortedNumericDocValues values = valuesSource.longValues(ctx);
final NumericDocValues singleton = DocValues.unwrapSingleton(values);

if (singleton != null) {
// Optimized path for single-valued fields
return new LeafBucketCollectorBase(sub, values) {
@Override
public void collect(int doc, long owningBucketOrd) throws IOException {
if (singleton.advanceExact(doc)) {
long value = singleton.longValue();
collectValue(sub, doc, owningBucketOrd, value);
}
}
};
}

// Original path for multi-valued fields
return new LeafBucketCollectorBase(sub, values) {
@Override
public void collect(int doc, long owningBucketOrd) throws IOException {
if (values.advanceExact(doc)) {
int valuesCount = values.docValueCount();

long previousRounded = Long.MIN_VALUE;
for (int i = 0; i < valuesCount; ++i) {
long value = values.nextValue();
Expand All @@ -216,22 +233,27 @@ public void collect(int doc, long owningBucketOrd) throws IOException {
if (rounded == previousRounded) {
continue;
}
if (hardBounds == null || hardBounds.contain(rounded)) {
long bucketOrd = bucketOrds.add(owningBucketOrd, rounded);
if (bucketOrd < 0) { // already seen
bucketOrd = -1 - bucketOrd;
collectExistingBucket(sub, doc, bucketOrd);
} else {
collectBucket(sub, doc, bucketOrd);
}
}
collectValue(sub, doc, owningBucketOrd, value);
previousRounded = rounded;
}
}
}
};
}

private void collectValue(LeafBucketCollector sub, int doc, long owningBucketOrd, long value) throws IOException {
long rounded = preparedRounding.round(value);
if (hardBounds == null || hardBounds.contain(rounded)) {
long bucketOrd = bucketOrds.add(owningBucketOrd, rounded);
if (bucketOrd < 0) { // already seen
bucketOrd = -1 - bucketOrd;
collectExistingBucket(sub, doc, bucketOrd);
} else {
collectBucket(sub, doc, bucketOrd);
}
}
}

private String fetchStarTreeCalendarUnit() {
if (this.rounding.unit() == null) {
return null;
Expand Down
Loading