From 55a879ee8d8bb637199cf3cb83c7dbeabc24c29a Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Thu, 20 Apr 2017 12:37:33 +0200 Subject: [PATCH] Align behavior or HDR percentiles iterator with percentile() method (#24206) --- .../hdr/InternalHDRPercentiles.java | 4 ++- .../InternalPercentilesTestCase.java | 2 +- .../hdr/InternalHDRPercentilesTests.java | 25 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentiles.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentiles.java index 3967e94e488..a153e497f7b 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentiles.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentiles.java @@ -98,7 +98,9 @@ public class InternalHDRPercentiles extends AbstractInternalHDRPercentiles imple @Override public Percentile next() { - final Percentile next = new Percentile(percents[i], state.getValueAtPercentile(percents[i])); + double percent = percents[i]; + double value = (state.getTotalCount() == 0) ? Double.NaN : state.getValueAtPercentile(percent); + final Percentile next = new Percentile(percent, value); ++i; return next; } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/InternalPercentilesTestCase.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/InternalPercentilesTestCase.java index 0cfa07538e4..eb26c792eb8 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/InternalPercentilesTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/InternalPercentilesTestCase.java @@ -50,7 +50,7 @@ public abstract class InternalPercentilesTestCase protected abstract T createTestInstance(String name, List pipelineAggregators, Map metaData, boolean keyed, DocValueFormat format, double[] percents, double[] values); - private static double[] randomPercents() { + protected static double[] randomPercents() { List randomCdfValues = randomSubsetOf(randomIntBetween(1, 7), 0.01d, 0.05d, 0.25d, 0.50d, 0.75d, 0.95d, 0.99d); double[] percents = new double[randomCdfValues.size()]; for (int i = 0; i < randomCdfValues.size(); i++) { diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentilesTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentilesTests.java index bff026d5cf4..640f413734e 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentilesTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentilesTests.java @@ -23,12 +23,17 @@ import org.HdrHistogram.DoubleHistogram; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentilesTestCase; +import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import java.util.Map; +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; + public class InternalHDRPercentilesTests extends InternalPercentilesTestCase { @Override @@ -57,4 +62,24 @@ public class InternalHDRPercentilesTests extends InternalPercentilesTestCase instanceReader() { return InternalHDRPercentiles::new; } + + public void testIterator() { + final double[] percents = randomPercents(); + final double[] values = new double[frequently() ? randomIntBetween(1, 10) : 0]; + for (int i = 0; i < values.length; ++i) { + values[i] = randomDouble(); + } + + InternalHDRPercentiles aggregation = + createTestInstance("test", emptyList(), emptyMap(), false, randomNumericDocValueFormat(), percents, values); + + Iterator iterator = aggregation.iterator(); + for (double percent : percents) { + assertTrue(iterator.hasNext()); + + Percentile percentile = iterator.next(); + assertEquals(percent, percentile.getPercent(), 0.0d); + assertEquals(aggregation.percentile(percent), percentile.getValue(), 0.0d); + } + } }