Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Added validation that vector is not NaN #100

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 @@ -250,10 +250,16 @@ public void parse(ParseContext context) throws IOException {
if (token == XContentParser.Token.START_ARRAY) {
token = context.parser().nextToken();
while (token != XContentParser.Token.END_ARRAY) {
if (Float.isNaN(context.parser().floatValue())) {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I think that makes sense.

throw new IllegalArgumentException("KNN vector values cannot be NaN");
}
vector.add(context.parser().floatValue());
token = context.parser().nextToken();
}
} else if (token == XContentParser.Token.VALUE_NUMBER) {
if (Float.isNaN(context.parser().floatValue())) {
throw new IllegalArgumentException("KNN vector values cannot be NaN");
}
vector.add(context.parser().floatValue());
context.parser().nextToken();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ public void testVectorMappingValidationInvalidDimension() {
KNNVectorFieldMapper.MAX_DIMENSION + " for vector: " + FIELD_NAME));
}

public void testVectorMappingValidationInvalidVectorNaN() throws IOException {
Settings settings = Settings.builder()
.put(getKNNDefaultIndexSettings())
.build();

createKnnIndex(INDEX_NAME, settings, createKnnIndexMapping(FIELD_NAME, 2));

Float[] vector = {Float.NaN, Float.NaN};
Exception ex = expectThrows(ResponseException.class, () -> addKnnDoc(INDEX_NAME, "3", FIELD_NAME, vector));
assertThat(ex.getMessage(), containsString("KNN vector values cannot be NaN"));
}

public void testVectorMappingValidationUpdateDimension() throws Exception {
Settings settings = Settings.builder()
.put(getKNNDefaultIndexSettings())
Expand Down