Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2d0e5c1
validate half float values
fred84 Jul 12, 2017
c0fff6f
Merge branch 'master' into 25534_reject_out_of_range_numbers
fred84 Jul 13, 2017
6e0a6ea
test upper bound for numeric mapper
fred84 Jul 17, 2017
7d4f315
merge master
fred84 Jul 21, 2017
d1ebd6f
test for upper bound for float, double and half_float
fred84 Jul 21, 2017
1358fed
Merge branch 'master' into 25533_reject_out_of_range_numbers
fred84 Jul 22, 2017
44983d8
more tests on NaN and Infinity for NumberFieldMapper
fred84 Jul 23, 2017
d072e69
Merge branch 'master' into 25534_reject_out_of_range_numbers
fred84 Jul 23, 2017
ecf3424
fix checkstyle errors
fred84 Jul 23, 2017
b5d231d
minor renaming
fred84 Jul 23, 2017
187982a
resolve merge conflict and cleanup NumberFieldMapper out of range tests
fred84 Jul 27, 2017
d236158
Merge remote-tracking branch 'upstream/master' into 25534_reject_out_…
fred84 Jul 27, 2017
7423c4d
comments for disabled test
fred84 Jul 27, 2017
8e88c9d
Merge remote-tracking branch 'upstream/master' into 25534_reject_out_…
fred84 Jul 30, 2017
11ce7c8
tests for byte/short/integer/long removed and will be added in separa…
fred84 Jul 30, 2017
3902911
remove unused import
fred84 Jul 30, 2017
656cf63
Merge remote-tracking branch 'upstream/master' into 25534_reject_out_…
fred84 Aug 4, 2017
cd38455
Fix scaledfloat out of range validation message
fred84 Aug 4, 2017
b578b5a
Merge remote-tracking branch 'upstream/master' into 25534_reject_out_…
fred84 Aug 8, 2017
3c666a9
1) delayed autoboxing in numbertype.parse(...)
fred84 Aug 8, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
return query;
}

@Override
void validateParsed(Number value) {
float val = value.floatValue();

if (
!Float.isFinite(val)
|| !Float.isFinite(HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(val)))
|| val > 65504
) {
throw new IllegalArgumentException("[half_float] supports only finite values");
}
}

@Override
public List<Field> createFields(String name, Number value,
boolean indexed, boolean docValued, boolean stored) {
Expand Down Expand Up @@ -292,6 +305,13 @@ Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
return query;
}

@Override
void validateParsed(Number value) {
if (!Float.isFinite(value.floatValue())) {
throw new IllegalArgumentException("[float] supports only finite values");
}
}

@Override
public List<Field> createFields(String name, Number value,
boolean indexed, boolean docValued, boolean stored) {
Expand Down Expand Up @@ -369,6 +389,13 @@ Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
return query;
}

@Override
void validateParsed(Number value) {
if (!Double.isFinite(value.doubleValue())) {
throw new IllegalArgumentException("[double] supports only finite values, but got [" + value.toString() + "]");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use this message format where we show what we got as an invalid value in the error messages for the other types too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated message format.

}
}

@Override
public List<Field> createFields(String name, Number value,
boolean indexed, boolean docValued, boolean stored) {
Expand Down Expand Up @@ -750,6 +777,12 @@ Number valueForSearch(Number value) {
return value;
}

/**
* @throws IllegalArgumentException if value is not finite for this type
*/
void validateParsed(Number value) {
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since every numeric type should implement this method, can we have this throw an UnsupportedOperationException here so we get an error if we implement a new numeric type and forget to override this method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just make it abstract

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made validation as a part of parse method in the same way as it did in byte/short/int/long. Thanks for suggestion, @rjernst!


/**
* Returns true if the object is a number and has a decimal part
*/
Expand Down Expand Up @@ -949,6 +982,8 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
numericValue = fieldType().type.parse(value, coerce.value());
}

fieldType().type.validateParsed(numericValue);

if (includeInAll) {
context.allEntries().addText(fieldType().name(), value.toString(), fieldType().boost());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import static org.hamcrest.Matchers.containsString;

Expand Down Expand Up @@ -314,4 +317,47 @@ public void testEmptyName() throws IOException {
assertThat(e.getMessage(), containsString("name cannot be empty string"));
}
}

public void testOutOfRangeValue() {
final Map<String, String> outOfRangeValues = new HashMap<>();
outOfRangeValues.put("float", new BigDecimal("3.4028235E39").toString());
outOfRangeValues.put("double", new BigDecimal("1.7976931348623157E309").toString());
outOfRangeValues.put("half_float", new BigDecimal("65504.1").toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As well as these values can we add infinite and Nan values to this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are ready. Also added out of range tests for byte/short/int/long


outOfRangeValues.forEach((type, value) -> {
try {
createDocumentMapper(type).parse(createIndexRequest(value));
fail("Mapper parsing exception expected for [" + type + "]");
} catch (MapperParsingException e) {
assertThat(e.getCause().getMessage(), containsString("[" + type + "] supports only finite values"));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

private DocumentMapper createDocumentMapper(String type) throws IOException {
String mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("type")
.startObject("properties")
.startObject("field")
.field("type", type)
.endObject()
.endObject()
.endObject()
.endObject()
.string();

return parser.parse("type", new CompressedXContent(mapping));
}

private SourceToParse createIndexRequest(String value) throws IOException {
return SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", value)
.endObject()
.bytes(),
XContentType.JSON);
}
}