Better mimick real reduction in test

This changes has `InternalAggregationTestCase` performs partial
reductions to better mimick `InternalAggregations#reduce`. In
particular, this fixes a bug when we'd attempt a partial reduction of
aggregations that were entirely unmapped but use a mapped aggregation as
the "lead" of the reduction. This was possible in the test but isn't
possible in production. The reduction "lead" is unmapped if all agg
results being reduced are unmapped.

Closes #65118
This commit is contained in:
Nik Everett 2020-11-17 09:55:33 -05:00
parent feca22729c
commit 956ae93fdb
1 changed files with 5 additions and 3 deletions

View File

@ -373,13 +373,15 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
// sometimes do a partial reduce // sometimes do a partial reduce
Collections.shuffle(toReduce, random()); Collections.shuffle(toReduce, random());
int r = randomIntBetween(1, inputs.size()); int r = randomIntBetween(1, inputs.size());
List<InternalAggregation> internalAggregations = toReduce.subList(0, r); List<InternalAggregation> toPartialReduce = toReduce.subList(0, r);
// Sort aggs so that unmapped come last. This mimicks the behavior of InternalAggregations.reduce()
toPartialReduce.sort(INTERNAL_AGG_COMPARATOR);
InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forPartialReduction( InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forPartialReduction(
bigArrays, mockScriptService, () -> PipelineAggregator.PipelineTree.EMPTY); bigArrays, mockScriptService, () -> PipelineAggregator.PipelineTree.EMPTY);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T reduced = (T) inputs.get(0).reduce(internalAggregations, context); T reduced = (T) toPartialReduce.get(0).reduce(toPartialReduce, context);
int initialBucketCount = 0; int initialBucketCount = 0;
for (InternalAggregation internalAggregation : internalAggregations) { for (InternalAggregation internalAggregation : toPartialReduce) {
initialBucketCount += countInnerBucket(internalAggregation); initialBucketCount += countInnerBucket(internalAggregation);
} }
int reducedBucketCount = countInnerBucket(reduced); int reducedBucketCount = countInnerBucket(reduced);