-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Save memory when histogram agg is not on top #57277
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...a/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramAggregator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.aggregations.bucket.histogram; | ||
|
|
||
| import org.apache.lucene.util.CollectionUtil; | ||
| import org.elasticsearch.common.lease.Releasables; | ||
| import org.elasticsearch.search.DocValueFormat; | ||
| import org.elasticsearch.search.aggregations.Aggregator; | ||
| import org.elasticsearch.search.aggregations.AggregatorFactories; | ||
| import org.elasticsearch.search.aggregations.BucketOrder; | ||
| import org.elasticsearch.search.aggregations.InternalAggregation; | ||
| import org.elasticsearch.search.aggregations.bucket.BucketsAggregator; | ||
| import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram.EmptyBucketInfo; | ||
| import org.elasticsearch.search.aggregations.bucket.terms.LongKeyedBucketOrds; | ||
| import org.elasticsearch.search.internal.SearchContext; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.function.BiConsumer; | ||
|
|
||
| /** | ||
| * Base class for functionality shared between aggregators for this | ||
| * {@code histogram} aggregation. | ||
| */ | ||
| public abstract class AbstractHistogramAggregator extends BucketsAggregator { | ||
| protected final DocValueFormat formatter; | ||
| protected final double interval; | ||
| protected final double offset; | ||
| protected final BucketOrder order; | ||
| protected final boolean keyed; | ||
| protected final long minDocCount; | ||
| protected final double minBound; | ||
| protected final double maxBound; | ||
| protected final LongKeyedBucketOrds bucketOrds; | ||
|
|
||
| public AbstractHistogramAggregator( | ||
| String name, | ||
| AggregatorFactories factories, | ||
| double interval, | ||
| double offset, | ||
| BucketOrder order, | ||
| boolean keyed, | ||
| long minDocCount, | ||
| double minBound, | ||
| double maxBound, | ||
| DocValueFormat formatter, | ||
| SearchContext context, | ||
| Aggregator parent, | ||
| boolean collectsFromSingleBucket, | ||
| Map<String, Object> metadata | ||
| ) throws IOException { | ||
| super(name, factories, context, parent, metadata); | ||
| if (interval <= 0) { | ||
| throw new IllegalArgumentException("interval must be positive, got: " + interval); | ||
| } | ||
| this.interval = interval; | ||
| this.offset = offset; | ||
| this.order = order; | ||
| order.validate(this); | ||
| this.keyed = keyed; | ||
| this.minDocCount = minDocCount; | ||
| this.minBound = minBound; | ||
| this.maxBound = maxBound; | ||
| this.formatter = formatter; | ||
| bucketOrds = LongKeyedBucketOrds.build(context.bigArrays(), collectsFromSingleBucket); | ||
| } | ||
|
|
||
| @Override | ||
| public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException { | ||
| return buildAggregationsForVariableBuckets(owningBucketOrds, bucketOrds, | ||
| (bucketValue, docCount, subAggregationResults) -> { | ||
| double roundKey = Double.longBitsToDouble(bucketValue); | ||
| double key = roundKey * interval + offset; | ||
| return new InternalHistogram.Bucket(key, docCount, keyed, formatter, subAggregationResults); | ||
| }, buckets -> { | ||
| // the contract of the histogram aggregation is that shards must return buckets ordered by key in ascending order | ||
| CollectionUtil.introSort(buckets, BucketOrder.key(true).comparator()); | ||
|
|
||
| EmptyBucketInfo emptyBucketInfo = null; | ||
| if (minDocCount == 0) { | ||
| emptyBucketInfo = new EmptyBucketInfo(interval, offset, minBound, maxBound, buildEmptySubAggregations()); | ||
| } | ||
| return new InternalHistogram(name, buckets, order, minDocCount, emptyBucketInfo, formatter, keyed, metadata()); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public InternalAggregation buildEmptyAggregation() { | ||
| InternalHistogram.EmptyBucketInfo emptyBucketInfo = null; | ||
| if (minDocCount == 0) { | ||
| emptyBucketInfo = new InternalHistogram.EmptyBucketInfo(interval, offset, minBound, maxBound, buildEmptySubAggregations()); | ||
| } | ||
| return new InternalHistogram(name, Collections.emptyList(), order, minDocCount, emptyBucketInfo, formatter, keyed, metadata()); | ||
| } | ||
|
|
||
| @Override | ||
| public void doClose() { | ||
| Releasables.close(bucketOrds); | ||
| } | ||
|
|
||
| @Override | ||
| public void collectDebugInfo(BiConsumer<String, Object> add) { | ||
| add.accept("total_buckets", bucketOrds.size()); | ||
| super.collectDebugInfo(add); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,6 @@ | |
| package org.elasticsearch.search.aggregations.bucket.histogram; | ||
|
|
||
| import org.elasticsearch.index.query.QueryShardContext; | ||
| import org.elasticsearch.search.DocValueFormat; | ||
| import org.elasticsearch.search.aggregations.AggregationExecutionException; | ||
| import org.elasticsearch.search.aggregations.Aggregator; | ||
| import org.elasticsearch.search.aggregations.AggregatorFactories; | ||
|
|
@@ -52,36 +51,11 @@ public final class HistogramAggregatorFactory extends ValuesSourceAggregatorFact | |
|
|
||
| static void registerAggregators(ValuesSourceRegistry.Builder builder) { | ||
| builder.register(HistogramAggregationBuilder.NAME, CoreValuesSourceType.RANGE, | ||
| new HistogramAggregatorSupplier() { | ||
| @Override | ||
| public Aggregator build(String name, AggregatorFactories factories, double interval, double offset, | ||
| BucketOrder order, boolean keyed, long minDocCount, double minBound, double maxBound, | ||
| ValuesSource valuesSource, DocValueFormat formatter, SearchContext context, | ||
| Aggregator parent, Map<String, Object> metadata) throws IOException { | ||
| ValuesSource.Range rangeValueSource = (ValuesSource.Range) valuesSource; | ||
| if (rangeValueSource.rangeType().isNumeric() == false) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this check into the ctor so I can use the ctor reference that we've been doing elsewhere. |
||
| throw new IllegalArgumentException("Expected numeric range type but found non-numeric range [" | ||
| + rangeValueSource.rangeType().name + "]"); | ||
| } | ||
| return new RangeHistogramAggregator(name, factories, interval, offset, order, keyed, minDocCount, minBound, | ||
| maxBound, rangeValueSource, formatter, context, parent, metadata); | ||
| } | ||
| } | ||
| ); | ||
| (HistogramAggregatorSupplier) RangeHistogramAggregator::new); | ||
|
|
||
| builder.register(HistogramAggregationBuilder.NAME, | ||
| List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), | ||
| new HistogramAggregatorSupplier() { | ||
| @Override | ||
| public Aggregator build(String name, AggregatorFactories factories, double interval, double offset, | ||
| BucketOrder order, boolean keyed, long minDocCount, double minBound, double maxBound, | ||
| ValuesSource valuesSource, DocValueFormat formatter, SearchContext context, | ||
| Aggregator parent, Map<String, Object> metadata) throws IOException { | ||
| return new NumericHistogramAggregator(name, factories, interval, offset, order, keyed, minDocCount, minBound, | ||
| maxBound, (ValuesSource.Numeric) valuesSource, formatter, context, parent, metadata); | ||
| } | ||
| } | ||
| ); | ||
| (HistogramAggregatorSupplier) NumericHistogramAggregator::new); | ||
| } | ||
|
|
||
| public HistogramAggregatorFactory(String name, | ||
|
|
@@ -117,10 +91,6 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource, | |
| Aggregator parent, | ||
| boolean collectsFromSingleBucket, | ||
| Map<String, Object> metadata) throws IOException { | ||
| if (collectsFromSingleBucket == false) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the important line! |
||
| return asMultiBucketAggregator(this, searchContext, parent); | ||
| } | ||
|
|
||
| AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(), | ||
| HistogramAggregationBuilder.NAME); | ||
| if (aggregatorSupplier instanceof HistogramAggregatorSupplier == false) { | ||
|
|
@@ -129,14 +99,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource, | |
| } | ||
| HistogramAggregatorSupplier histogramAggregatorSupplier = (HistogramAggregatorSupplier) aggregatorSupplier; | ||
| return histogramAggregatorSupplier.build(name, factories, interval, offset, order, keyed, minDocCount, minBound, maxBound, | ||
| valuesSource, config.format(), searchContext, parent, metadata); | ||
| valuesSource, config.format(), searchContext, parent, collectsFromSingleBucket, metadata); | ||
| } | ||
|
|
||
| @Override | ||
| protected Aggregator createUnmapped(SearchContext searchContext, | ||
| Aggregator parent, | ||
| Map<String, Object> metadata) throws IOException { | ||
| return new NumericHistogramAggregator(name, factories, interval, offset, order, keyed, minDocCount, minBound, maxBound, | ||
| null, config.format(), searchContext, parent, metadata); | ||
| null, config.format(), searchContext, parent, false, metadata); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a TODO that the range and numeric version of the aggregator shared a ton of code. Now they share a superclass that provides all that code.