Skip to content

Commit cde5e65

Browse files
committed
Fix and test infinity handling
1 parent 611a7da commit cde5e65

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

x-pack/plugin/mapper-exponential-histogram/src/main/java/org/elasticsearch/xpack/exponentialhistogram/ExponentialHistogramFieldMapper.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ public void parse(DocumentParserContext context) throws IOException {
304304
);
305305
}
306306
} else if (fieldName.equals(SUM_FIELD.getPreferredName())) {
307-
token = subParser.nextToken();
308-
ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, subParser);
309-
sum = subParser.doubleValue();
307+
sum = parseDoubleAllowingInfinity(subParser);
310308
} else if (fieldName.equals(ZERO_FIELD.getPreferredName())) {
311309
zeroBucket = parseZeroBucket(subParser);
312310
} else if (fieldName.equals(POSITIVE_FIELD.getPreferredName())) {
@@ -407,6 +405,24 @@ public void parse(DocumentParserContext context) throws IOException {
407405
context.path().remove();
408406
}
409407

408+
private double parseDoubleAllowingInfinity(XContentParser parser) throws IOException {
409+
XContentParser.Token token = parser.nextToken();
410+
boolean isValidNumber = token == XContentParser.Token.VALUE_NUMBER;
411+
if (token == XContentParser.Token.VALUE_STRING) {
412+
String text = parser.text();
413+
if (text.equals("-Infinity") || text.equals("Infinity")) {
414+
isValidNumber = true;
415+
}
416+
}
417+
if (isValidNumber) {
418+
return parser.doubleValue();
419+
}
420+
throw new DocumentParsingException(
421+
parser.getTokenLocation(),
422+
"error parsing field [" + fullPath() + "], expected a number but got " + token
423+
);
424+
}
425+
410426
private static long getTotalValueCount(
411427
ParsedZeroBucket zeroBucket,
412428
List<IndexWithCount> positiveBuckets,

x-pack/plugin/mapper-exponential-histogram/src/test/java/org/elasticsearch/xpack/exponentialhistogram/ExponentialHistogramFieldMapperTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ private static Map<String, Object> createRandomHistogramValue(int maxBucketCount
129129
Map.of("indices", negativeIndices, "counts", negativeCounts)
130130
)
131131
);
132-
if (randomBoolean() && (positiveIndices.isEmpty() == false || negativeIndices.isEmpty() == false)) {
132+
if ((positiveIndices.isEmpty() == false || negativeIndices.isEmpty() == false)) {
133+
// we always add the sum field to avoid numeric problems with the estimation due to random buckets
134+
// sum generation is tested in the yaml tests
133135
result.put("sum", randomDoubleBetween(-1000, 1000, true));
134136
}
135137
return result;

x-pack/plugin/mapper-exponential-histogram/src/yamlRestTest/resources/rest-api-spec/test/10_synthetic_source.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,55 @@ setup:
279279
negative:
280280
indices: [0, 1]
281281
counts: [1, 5]
282+
283+
---
284+
"Positive infinity sum":
285+
- do:
286+
index:
287+
index: test_exponential_histogram
288+
id: "1"
289+
refresh: true
290+
body:
291+
my_histo:
292+
scale: 0
293+
positive:
294+
indices: [2000]
295+
counts: [1]
296+
297+
- do:
298+
get:
299+
index: test_exponential_histogram
300+
id: "1"
301+
- match:
302+
_source.my_histo:
303+
scale: 0
304+
sum: Infinity
305+
positive:
306+
indices: [2000]
307+
counts: [1]
308+
309+
---
310+
"negative infinity sum":
311+
- do:
312+
index:
313+
index: test_exponential_histogram
314+
id: "1"
315+
refresh: true
316+
body:
317+
my_histo:
318+
scale: 0
319+
negative:
320+
indices: [2000]
321+
counts: [1]
322+
323+
- do:
324+
get:
325+
index: test_exponential_histogram
326+
id: "1"
327+
- match:
328+
_source.my_histo:
329+
scale: 0
330+
sum: -Infinity
331+
negative:
332+
indices: [2000]
333+
counts: [1]

0 commit comments

Comments
 (0)