Skip to content

Commit 9f55e45

Browse files
committed
add check that no aggs add empty buckets in their non final reduce phase
1 parent 5f9b40d commit 9f55e45

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,19 @@
119119
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
120120
import org.elasticsearch.search.aggregations.metrics.TopHitsAggregationBuilder;
121121
import org.elasticsearch.search.aggregations.metrics.ValueCountAggregationBuilder;
122-
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
123-
import org.elasticsearch.search.aggregations.pipeline.ParsedSimpleValue;
124-
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
122+
import org.elasticsearch.search.aggregations.pipeline.DerivativePipelineAggregationBuilder;
123+
import org.elasticsearch.search.aggregations.pipeline.ExtendedStatsBucketPipelineAggregationBuilder;
125124
import org.elasticsearch.search.aggregations.pipeline.InternalBucketMetricValue;
125+
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
126126
import org.elasticsearch.search.aggregations.pipeline.ParsedBucketMetricValue;
127+
import org.elasticsearch.search.aggregations.pipeline.ParsedDerivative;
128+
import org.elasticsearch.search.aggregations.pipeline.ParsedExtendedStatsBucket;
127129
import org.elasticsearch.search.aggregations.pipeline.ParsedPercentilesBucket;
128-
import org.elasticsearch.search.aggregations.pipeline.PercentilesBucketPipelineAggregationBuilder;
130+
import org.elasticsearch.search.aggregations.pipeline.ParsedSimpleValue;
129131
import org.elasticsearch.search.aggregations.pipeline.ParsedStatsBucket;
132+
import org.elasticsearch.search.aggregations.pipeline.PercentilesBucketPipelineAggregationBuilder;
133+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
130134
import org.elasticsearch.search.aggregations.pipeline.StatsBucketPipelineAggregationBuilder;
131-
import org.elasticsearch.search.aggregations.pipeline.ExtendedStatsBucketPipelineAggregationBuilder;
132-
import org.elasticsearch.search.aggregations.pipeline.ParsedExtendedStatsBucket;
133-
import org.elasticsearch.search.aggregations.pipeline.DerivativePipelineAggregationBuilder;
134-
import org.elasticsearch.search.aggregations.pipeline.ParsedDerivative;
135135

136136
import java.io.IOException;
137137
import java.util.ArrayList;
@@ -151,6 +151,7 @@
151151
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
152152
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
153153
import static org.hamcrest.Matchers.equalTo;
154+
import static org.hamcrest.Matchers.lessThanOrEqualTo;
154155

155156
public abstract class InternalAggregationTestCase<T extends InternalAggregation> extends AbstractWireSerializingTestCase<T> {
156157
public static final int DEFAULT_MAX_BUCKETS = 100000;
@@ -267,7 +268,14 @@ public void testReduceRandom() {
267268
new InternalAggregation.ReduceContext(bigArrays, mockScriptService, bucketConsumer,false);
268269
@SuppressWarnings("unchecked")
269270
T reduced = (T) inputs.get(0).reduce(internalAggregations, context);
270-
assertMultiBucketConsumer(reduced, bucketConsumer);
271+
int initialBucketCount = 0;
272+
for (InternalAggregation internalAggregation : internalAggregations) {
273+
initialBucketCount += countInnerBucket(internalAggregation);
274+
}
275+
int reducedBucketCount = countInnerBucket(reduced);
276+
//check that non final reduction never adds buckets
277+
assertThat(reducedBucketCount, lessThanOrEqualTo(initialBucketCount));
278+
assertMultiBucketConsumer(reducedBucketCount, bucketConsumer);
271279
toReduce = new ArrayList<>(toReduce.subList(r, toReduceSize));
272280
toReduce.add(reduced);
273281
}
@@ -332,14 +340,14 @@ protected NamedXContentRegistry xContentRegistry() {
332340

333341
public final void testFromXContent() throws IOException {
334342
final T aggregation = createTestInstance();
335-
final Aggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), false);
336-
assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation);
343+
final ParsedAggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), false);
344+
assertFromXContent(aggregation, parsedAggregation);
337345
}
338346

339347
public final void testFromXContentWithRandomFields() throws IOException {
340348
final T aggregation = createTestInstance();
341-
final Aggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), true);
342-
assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation);
349+
final ParsedAggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), true);
350+
assertFromXContent(aggregation, parsedAggregation);
343351
}
344352

345353
protected abstract void assertFromXContent(T aggregation, ParsedAggregation parsedAggregation) throws IOException;
@@ -423,6 +431,10 @@ protected static DocValueFormat randomNumericDocValueFormat() {
423431
}
424432

425433
public static void assertMultiBucketConsumer(Aggregation agg, MultiBucketConsumer bucketConsumer) {
426-
assertThat(bucketConsumer.getCount(), equalTo(countInnerBucket(agg)));
434+
assertMultiBucketConsumer(countInnerBucket(agg), bucketConsumer);
435+
}
436+
437+
private static void assertMultiBucketConsumer(int innerBucketCount, MultiBucketConsumer bucketConsumer) {
438+
assertThat(bucketConsumer.getCount(), equalTo(innerBucketCount));
427439
}
428440
}

0 commit comments

Comments
 (0)