Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(core): support Infinity & NaN number in float/double property #1578

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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 @@ -445,7 +445,7 @@ public <V> VertexProperty<V> property(
if (cardinality != VertexProperty.Cardinality.single) {
E.checkArgument(propertyKey.cardinality() ==
Cardinality.convert(cardinality),
"Invalid cardinalty '%s' for property key '%s', " +
"Invalid cardinality '%s' for property key '%s', " +
"expect '%s'", cardinality, key,
propertyKey.cardinality().string());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.baidu.hugegraph.util.Bytes;
import com.baidu.hugegraph.util.DateUtil;
import com.baidu.hugegraph.util.StringEncoding;
import com.google.common.collect.ImmutableSet;

public enum DataType implements SerialEnum {

Expand All @@ -49,9 +50,11 @@ public enum DataType implements SerialEnum {
private final byte code;
private final String name;
private final Class<?> clazz;
private final static ImmutableSet<String> specialNums;
imbajin marked this conversation as resolved.
Show resolved Hide resolved

static {
SerialEnum.register(DataType.class);
specialNums = ImmutableSet.of("-Infinity", "Infinity", "NaN");
}

DataType(int code, String name, Class<?> clazz) {
Expand Down Expand Up @@ -104,15 +107,20 @@ public boolean isUUID() {
return this == DataType.UUID;
}

private static <V> boolean isInfinityOrNaN(V value) {
return value instanceof String && specialNums.contains(value);
imbajin marked this conversation as resolved.
Show resolved Hide resolved
}

public <V> Number valueToNumber(V value) {
if (!(this.isNumber() && value instanceof Number)) {
if (!(this.isNumber() && value instanceof Number) &&
!isInfinityOrNaN(value)) {
return null;
}
if (this.clazz.isInstance(value)) {
return (Number) value;
}

Number number = null;
Number number;
try {
switch (this) {
case BYTE:
Expand All @@ -126,15 +134,11 @@ public <V> Number valueToNumber(V value) {
break;
case FLOAT:
Float fvalue = Float.valueOf(value.toString());
if (!fvalue.isInfinite() && !fvalue.isNaN()) {
number = fvalue;
}
number = fvalue;
break;
case DOUBLE:
Double dvalue = Double.valueOf(value.toString());
if (!dvalue.isInfinite() && !dvalue.isNaN()) {
number = dvalue;
}
number = dvalue;
break;
default:
throw new AssertionError(String.format(
Expand Down Expand Up @@ -219,6 +223,6 @@ public static DataType fromClass(Class<?> clazz) {
return type;
}
}
throw new HugeException("Unknow clazz '%s' for DataType", clazz);
throw new HugeException("Unknown clazz '%s' for DataType", clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.core;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -449,6 +450,21 @@ public void testTypeFloat() {
property("float", Float.MIN_VALUE));
Assert.assertEquals(Float.MAX_VALUE,
property("float", Float.MAX_VALUE));
Assert.assertEquals(Float.POSITIVE_INFINITY,
property("float", Float.POSITIVE_INFINITY));
Assert.assertEquals(Float.POSITIVE_INFINITY,
property("float", Double.MAX_VALUE));
Assert.assertEquals(Float.POSITIVE_INFINITY,
property("float", Double.POSITIVE_INFINITY));
Assert.assertEquals(Float.NEGATIVE_INFINITY,
property("float", Float.NEGATIVE_INFINITY));
Assert.assertEquals(Float.NEGATIVE_INFINITY,
property("float", -Double.MAX_VALUE));
Assert.assertEquals(Float.POSITIVE_INFINITY,
property("float", -Double.NEGATIVE_INFINITY));
Assert.assertEquals(Float.POSITIVE_INFINITY,
property("float", -2 * Double.NEGATIVE_INFINITY));
Assert.assertEquals(Float.NaN, property("float", Float.NaN));

List<Float> list = ImmutableList.of(1f, 3f, 3f, 127f, 128f);
Assert.assertEquals(list, propertyList("float",
Expand All @@ -462,13 +478,13 @@ public void testTypeFloat() {
1f, 3f, 3f, 127f, 128f));

Assert.assertThrows(IllegalArgumentException.class, () -> {
property("float", Double.MAX_VALUE);
property("float", 'a');
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the behavior of property("float", Double.MAX_VALUE)? can also test it

Copy link
Member Author

Choose a reason for hiding this comment

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

treat as Infinity. add a assertEquals for it

}, e -> {
Assert.assertContains("Invalid property value " +
"'1.7976931348623157E308' for key 'float'",
"'a' for key 'float'",
e.getMessage());
Assert.assertContains("expect a value of type Float, " +
"actual type Double",
"actual type Character",
e.getMessage());
});

Expand Down Expand Up @@ -501,6 +517,28 @@ public void testTypeDouble() {
property("double", Double.MIN_VALUE));
Assert.assertEquals(Double.MAX_VALUE,
property("double", Double.MAX_VALUE));
Assert.assertEquals(Double.POSITIVE_INFINITY,
property("double", Double.POSITIVE_INFINITY));
Assert.assertEquals(Double.POSITIVE_INFINITY,
property("double", 2 * Double.POSITIVE_INFINITY));

BigDecimal two = new BigDecimal(2);
BigDecimal value = BigDecimal.valueOf(Double.MAX_VALUE).multiply(two);
Assert.assertEquals(Double.POSITIVE_INFINITY,
property("double", value));
Assert.assertEquals(Double.POSITIVE_INFINITY,
property("double", -Double.NEGATIVE_INFINITY));
Assert.assertEquals(Double.POSITIVE_INFINITY,
property("double", -2 * Double.NEGATIVE_INFINITY));

value = BigDecimal.valueOf(-Double.MAX_VALUE).multiply(two);
Assert.assertEquals(Double.NEGATIVE_INFINITY,
property("double", value));
value = BigDecimal.valueOf(Double.MIN_VALUE).divide(two);
Assert.assertEquals(0.0, property("double", value));
Assert.assertEquals(Double.NEGATIVE_INFINITY,
property("double", Double.NEGATIVE_INFINITY));
Assert.assertEquals(Double.NaN, property("double", Double.NaN));

List<Double> list = ImmutableList.of(1d, 3d, 3d, 127d, 128d);
Assert.assertEquals(list, propertyList("double",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,6 @@ public void testAddVertexWithInvalidPropertValueOfFloat() {
schema.propertyKey("float").asFloat().create();
schema.vertexLabel("number").properties("float").create();

Assert.assertThrows(IllegalArgumentException.class, () -> {
double value = Float.MAX_VALUE * 2.0d;
graph.addVertex(T.label, "number", "float", value);
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
double value = -(Float.MAX_VALUE * 2.0d);
graph.addVertex(T.label, "number", "float", value);
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
graph.addVertex(T.label, "number", "float", Double.MAX_VALUE);
});

double value = Float.MIN_VALUE / 2.0d;
float fvalue = graph.addVertex(T.label, "number", "float", value)
.value("float");
Expand All @@ -318,17 +305,6 @@ public void testAddVertexWithInvalidPropertValueOfDouble() {
schema.propertyKey("double").asDouble().create();
schema.vertexLabel("number").properties("double").create();

Assert.assertThrows(IllegalArgumentException.class, () -> {
BigDecimal two = new BigDecimal(2);
BigDecimal value = new BigDecimal(Double.MAX_VALUE).multiply(two);
graph.addVertex(T.label, "number", "double", value);
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
BigDecimal two = new BigDecimal(2);
BigDecimal value = new BigDecimal(-Double.MAX_VALUE).multiply(two);
graph.addVertex(T.label, "number", "double", value);
});

BigDecimal two = new BigDecimal(2);
BigDecimal value = new BigDecimal(Double.MIN_VALUE).divide(two);
double dvalue = graph.addVertex(T.label, "number", "double", value)
Expand Down