summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorArne H Juul <arnej27959@users.noreply.github.com>2023-12-13 12:45:10 +0100
committerGitHub <noreply@github.com>2023-12-13 12:45:10 +0100
commitfc48d57ddad1ecae78748d47d2db5c3d05c76f59 (patch)
tree6b73f698ae721ea2134a11d213144712677cc402 /vespajlib
parent32b8a74701ade63457977a36aaa56cdcc5ddaf0a (diff)
parent6d32c4bfc58cd3ee9d3c88e6f694284287054fd1 (diff)
Merge pull request #29515 from vespa-engine/arnej/parse-special-number-strings
add parsing of special strings for inf/nan cell values
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java41
-rw-r--r--vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java47
2 files changed, 80 insertions, 8 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java b/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java
index 28f14c8d7ca..8cf36cbd038 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/JsonFormat.java
@@ -234,10 +234,11 @@ public class JsonFormat {
TensorAddress address = decodeAddress(cell.field("address"), builder.type());
Inspector value = cell.field("value");
- if (value.type() != Type.LONG && value.type() != Type.DOUBLE)
+ if (value.valid()) {
+ builder.cell(address, decodeNumeric(value));
+ } else {
throw new IllegalArgumentException("Excepted a cell to contain a numeric value called 'value'");
-
- builder.cell(address, value.asDouble());
+ }
}
private static void decodeSingleDimensionCell(String key, Inspector value, Tensor.Builder builder) {
@@ -268,8 +269,8 @@ public class JsonFormat {
values.traverse((ArrayTraverser) (__, value) -> {
if (value.type() == Type.ARRAY)
decodeNestedValues(value, builder, index);
- else if (value.type() == Type.LONG || value.type() == Type.DOUBLE)
- indexedBuilder.cellByDirectIndex(index.next(), value.asDouble());
+ else if (value.type() == Type.LONG || value.type() == Type.DOUBLE || value.type() == Type.STRING || value.type() == Type.NIX)
+ indexedBuilder.cellByDirectIndex(index.next(), decodeNumeric(value));
else
throw new IllegalArgumentException("Excepted the values array to contain numbers or nested arrays, not " + value.type());
});
@@ -446,9 +447,33 @@ public class JsonFormat {
}
private static double decodeNumeric(Inspector numericField) {
- if (numericField.type() != Type.LONG && numericField.type() != Type.DOUBLE)
- throw new IllegalArgumentException("Excepted a number, not " + numericField.type());
- return numericField.asDouble();
+ if (numericField.type() == Type.DOUBLE || numericField.type() == Type.LONG) {
+ return numericField.asDouble();
+ }
+ if (numericField.type() == Type.STRING) {
+ return decodeNumberString(numericField.asString());
+ }
+ if (numericField.type() == Type.NIX) {
+ return Double.NaN;
+ }
+ throw new IllegalArgumentException("Excepted a number, not " + numericField.type());
+ }
+
+ public static double decodeNumberString(String input) {
+ String s = input.toLowerCase();
+ if (s.equals("infinity") || s.equals("+infinity") || s.equals("inf") || s.equals("+inf")) {
+ return Double.POSITIVE_INFINITY;
+ }
+ if (s.equals("-infinity") || s.equals("-inf")) {
+ return Double.NEGATIVE_INFINITY;
+ }
+ if (s.equals("nan") || s.equals("+nan")) {
+ return Double.NaN;
+ }
+ if (s.equals("-nan")) {
+ return Math.copySign(Double.NaN, -1.0); // or Double.longBitsToDouble(0xfff8000000000000L);
+ }
+ throw new NumberFormatException("Excepted a number, got string '" + input + "'");
}
}
diff --git a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
index d95396aca50..52f44f0ac42 100644
--- a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
@@ -669,6 +669,53 @@ public class JsonFormatTestCase {
"{\"type\":\"tensor<float>(x[1])\",\"values\":[0.3333333432674408]}");
}
+ @Test
+ public void testSpecialNumberStrings() {
+ assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("Infinity"), 0.0);
+ assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("+Infinity"), 0.0);
+ assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("Inf"), 0.0);
+ assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("+Inf"), 0.0);
+ assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("infinity"), 0.0);
+ assertEquals(Double.NEGATIVE_INFINITY, JsonFormat.decodeNumberString("-Infinity"), 0.0);
+ assertEquals(Double.NEGATIVE_INFINITY, JsonFormat.decodeNumberString("-Inf"), 0.0);
+ assertEquals(Double.NEGATIVE_INFINITY, JsonFormat.decodeNumberString("-infinity"), 0.0);
+ assertEquals(Double.NEGATIVE_INFINITY, JsonFormat.decodeNumberString("-inf"), 0.0);
+ assertEquals(0x7FF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("nan")));
+ assertEquals(0x7FF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("NaN")));
+ assertEquals(0x7FF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("+NaN")));
+ assertEquals(0xFFF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("-nan")));
+ assertEquals(0xFFF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("-NaN")));
+ }
+
+ @Test
+ public void testWithNanVariants() {
+ TensorType x3 = TensorType.fromSpec("tensor(x[3])");
+ String json = "{\"cells\":[" +
+ "{\"address\":{\"x\":\"0\"},\"value\":\"nan\"}," +
+ "{\"address\":{\"x\":\"1\"},\"value\":null}," +
+ "{\"address\":{\"x\":\"2\"},\"value\":\"+NaN\"}" +
+ "]}";
+ var t = JsonFormat.decode(x3, json.getBytes(StandardCharsets.UTF_8));
+ checkThreeNans(t);
+ json = "['nan', null, '+NaN']";
+ t = JsonFormat.decode(x3, json.getBytes(StandardCharsets.UTF_8));
+ checkThreeNans(t);
+ json = "{'type':'tensor(x[3])','values':['nan', null, '+NaN']}";
+ t = JsonFormat.decode(x3, json.getBytes(StandardCharsets.UTF_8));
+ checkThreeNans(t);
+ }
+
+ private void checkThreeNans(Tensor t) {
+ final Double nan = Double.NaN;
+ int cnt = 0;
+ for (var iter = t.cellIterator(); iter.hasNext(); ) {
+ var cell = iter.next();
+ assertEquals(nan, cell.getValue());
+ ++cnt;
+ }
+ assertEquals(3, cnt);
+ }
+
private void assertEncodeShortForm(String tensor, String expected) {
assertEncodeShortForm(Tensor.from(tensor), expected);
}