Skip to content

Commit 32bfb4b

Browse files
author
Hendrik Muhs
authored
Deprecate specifying the same percentile multiple times in percentiles (#65252)
Deprecate specifying the same percentile multiple times in percentiles aggregation Regression of: #52257 Fixes: #65240
1 parent 6f50a7a commit 32bfb4b

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/metrics/PercentilesAggregationBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.common.ParseField;
2323
import org.elasticsearch.common.io.stream.StreamInput;
24+
import org.elasticsearch.common.logging.DeprecationLogger;
2425
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2526
import org.elasticsearch.common.xcontent.XContentParser;
2627
import org.elasticsearch.search.aggregations.AggregationBuilder;
@@ -44,6 +45,7 @@ public class PercentilesAggregationBuilder extends AbstractPercentilesAggregatio
4445

4546
private static final double[] DEFAULT_PERCENTS = new double[] { 1, 5, 25, 50, 75, 95, 99 };
4647
private static final ParseField PERCENTS_FIELD = new ParseField("percents");
48+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(PercentilesAggregationBuilder.class);
4749

4850
private static final ConstructingObjectParser<PercentilesAggregationBuilder, String> PARSER;
4951
static {
@@ -112,11 +114,17 @@ private static double[] validatePercentiles(double[] percents, String aggName) {
112114
throw new IllegalArgumentException("[percents] must not be empty: [" + aggName + "]");
113115
}
114116
double[] sortedPercents = Arrays.copyOf(percents, percents.length);
117+
double previousPercent = -1.0;
115118
Arrays.sort(sortedPercents);
116119
for (double percent : sortedPercents) {
117120
if (percent < 0.0 || percent > 100.0) {
118121
throw new IllegalArgumentException("percent must be in [0,100], got [" + percent + "]: [" + aggName + "]");
119122
}
123+
124+
if (percent == previousPercent) {
125+
deprecationLogger.deprecate("percents", "percent [{}] has been specified more than once, percents must be unique", percent);
126+
}
127+
previousPercent = percent;
120128
}
121129
return sortedPercents;
122130
}

server/src/test/java/org/elasticsearch/search/aggregations/metrics/PercentilesTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ public void testOutOfRangePercentilesThrows() throws IOException {
7878
assertEquals("percent must be in [0,100], got [104.0]: [testAgg]", ex.getMessage());
7979
}
8080

81+
public void testDuplicatePercentilesDeprecated() throws IOException {
82+
PercentilesAggregationBuilder builder = new PercentilesAggregationBuilder("testAgg");
83+
84+
// throws in 8.x, deprecated in 7.x
85+
builder.percentiles(5, 42, 10, 99, 42, 87);
86+
87+
assertWarnings("percent [42.0] has been specified more than once, percents must be unique");
88+
89+
builder.percentiles(5, 42, 42, 43, 43, 87);
90+
assertWarnings(
91+
"percent [42.0] has been specified more than once, percents must be unique",
92+
"percent [43.0] has been specified more than once, percents must be unique"
93+
);
94+
}
95+
8196
public void testExceptionMultipleMethods() throws IOException {
8297
final String illegalAgg = "{\n" +
8398
" \"percentiles\": {\n" +

x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void testGetAggregationOutputTypesPercentiles() {
151151
assertEquals("percentiles", outputTypes.get("percentiles.10"));
152152

153153
// note: using the constructor, omits validation, in reality this test might fail
154-
percentialAggregationBuilder = new PercentilesAggregationBuilder("percentiles").percentiles(1.0, 5.0, 5.0, 10.0);
154+
percentialAggregationBuilder = new PercentilesAggregationBuilder("percentiles", new double[] { 1, 5, 5, 10 }, null);
155155

156156
inputAndOutputTypes = TransformAggregations.getAggregationInputAndOutputTypes(percentialAggregationBuilder);
157157
assertTrue(inputAndOutputTypes.v1().isEmpty());

0 commit comments

Comments
 (0)