From 956ae93fdb9db47c4a55bb201eddae0f05bbd2d4 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 17 Nov 2020 09:55:33 -0500 Subject: [PATCH] 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 --- .../elasticsearch/test/InternalAggregationTestCase.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java index bc68ee3e69f..d8d4c2f1747 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java @@ -373,13 +373,15 @@ public abstract class InternalAggregationTestCase // sometimes do a partial reduce Collections.shuffle(toReduce, random()); int r = randomIntBetween(1, inputs.size()); - List internalAggregations = toReduce.subList(0, r); + List 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( bigArrays, mockScriptService, () -> PipelineAggregator.PipelineTree.EMPTY); @SuppressWarnings("unchecked") - T reduced = (T) inputs.get(0).reduce(internalAggregations, context); + T reduced = (T) toPartialReduce.get(0).reduce(toPartialReduce, context); int initialBucketCount = 0; - for (InternalAggregation internalAggregation : internalAggregations) { + for (InternalAggregation internalAggregation : toPartialReduce) { initialBucketCount += countInnerBucket(internalAggregation); } int reducedBucketCount = countInnerBucket(reduced);