Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#12598] fix(common): Reject out-of-range numeric statistic values (#12599)

YangJie09e68277b2d8
diff --git a/common/src/main/java/org/apache/gravitino/json/JsonUtils.java b/common/src/main/java/org/apache/gravitino/json/JsonUtils.java
index ad71feb557..e98bc7e387 100644
--- a/common/src/main/java/org/apache/gravitino/json/JsonUtils.java
+++ b/common/src/main/java/org/apache/gravitino/json/JsonUtils.java
@@ -49,7 +49,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -1388,13 +1387,27 @@ public class JsonUtils {
}
}
- private static StatisticValue<?> getStatisticValue(JsonNode node) throws IOException {
+ private static StatisticValue<?> getStatisticValue(JsonNode node) {
Preconditions.checkArgument(
node != null && !node.isNull(), "Cannot parse statistic value from invalid JSON: %s", node);
if (node.isIntegralNumber()) {
+ // BigInteger nodes are integral as well, and asLong() would wrap them around silently.
+ Preconditions.checkArgument(
+ node.canConvertToLong(),
+ "Statistic value is out of the range of a 64-bit signed integer: %s",
+ node);
return StatisticValues.longValue(node.asLong());
} else if (node.isFloatingPointNumber()) {
- return StatisticValues.doubleValue(node.asDouble());
+ // Jackson parses a literal past the double range into a DoubleNode that already holds
+ // infinity (USE_BIG_DECIMAL_FOR_FLOATS is off), and the serializer would write that back
+ // out as the JSON string "Infinity", so the value would come back as a string.
+ double doubleValue = node.asDouble();
+ Preconditions.checkArgument(
+ Double.isFinite(doubleValue),
+ "Statistic value is out of the range of a 64-bit floating point number, the literal"
+ + " parsed to %s",
+ doubleValue);
+ return StatisticValues.doubleValue(doubleValue);
} else if (node.isTextual()) {
return StatisticValues.stringValue(node.asText());
} else if (node.isBoolean()) {
@@ -1403,10 +1416,7 @@ public class JsonUtils {
ArrayNode arrayNode = (ArrayNode) node;
List<StatisticValue<Object>> values = Lists.newArrayListWithCapacity(arrayNode.size());
for (JsonNode arrayElement : arrayNode) {
- StatisticValue<?> value = getStatisticValue(arrayElement);
- if (value != null) {
- values.add((StatisticValue<Object>) value);
- }
+ values.add((StatisticValue<Object>) getStatisticValue(arrayElement));
}
return StatisticValues.listValue(values);
} else if (node.isObject()) {
@@ -1414,20 +1424,10 @@ public class JsonUtils {
Map<String, StatisticValue<?>> map = Maps.newHashMap();
objectNode
.fields()
- .forEachRemaining(
- entry -> {
- try {
- StatisticValue<?> value = getStatisticValue(entry.getValue());
- if (value != null) {
- map.put(entry.getKey(), value);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- });
+ .forEachRemaining(entry -> map.put(entry.getKey(), getStatisticValue(entry.getValue())));
return StatisticValues.objectValue(map);
} else {
- throw new UnsupportedEncodingException(
+ throw new IllegalArgumentException(
String.format("Don't support json node type %s", node.getNodeType()));
}
}
diff --git a/common/src/test/java/org/apache/gravitino/json/TestJsonUtils.java b/common/src/test/java/org/apache/gravitino/json/TestJsonUtils.java
index 84497a0505..2649667c4b 100644
--- a/common/src/test/java/org/apache/gravitino/json/TestJsonUtils.java
+++ b/common/src/test/java/org/apache/gravitino/json/TestJsonUtils.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -36,6 +37,7 @@ import org.apache.gravitino.dto.rel.partitions.IdentityPartitionDTO;
import org.apache.gravitino.dto.rel.partitions.ListPartitionDTO;
import org.apache.gravitino.dto.rel.partitions.PartitionDTO;
import org.apache.gravitino.dto.rel.partitions.RangePartitionDTO;
+import org.apache.gravitino.dto.requests.StatisticsUpdateRequest;
import org.apache.gravitino.rel.indexes.Index;
import org.apache.gravitino.rel.types.Type;
import org.apache.gravitino.rel.types.Types;
@@ -47,6 +49,11 @@ import org.junit.jupiter.api.Test;
public class TestJsonUtils {
+ private static final String INTEGRAL_RANGE_MESSAGE =
+ "out of the range of a 64-bit signed integer";
+ private static final String FLOATING_RANGE_MESSAGE =
+ "out of the range of a 64-bit floating point number";
+
private static ObjectMapper objectMapper;
@BeforeAll
@@ -560,4 +567,90 @@ public class TestJsonUtils {
objectMapper.readValue(expectJson, StatisticValue.class),
objectMapper.readValue(objectValue, StatisticValue.class));
}
+
+ @Test
+ void testStatisticValueRejectsOutOfRangeIntegral() throws JsonProcessingException {
+ // The 64-bit boundaries themselves must still be accepted.
+ Assertions.assertEquals(
+ StatisticValues.longValue(Long.MAX_VALUE),
+ objectMapper.readValue("9223372036854775807", StatisticValue.class));
+ Assertions.assertEquals(
+ StatisticValues.longValue(Long.MIN_VALUE),
+ objectMapper.readValue("-9223372036854775808", StatisticValue.class));
+
+ // Anything past a boundary has no lossless long representation and must be rejected rather
+ // than wrapped around.
+ assertRejected("9223372036854775808", INTEGRAL_RANGE_MESSAGE);
+ assertRejected("-9223372036854775809", INTEGRAL_RANGE_MESSAGE);
+ assertRejected("123456789012345678901234567890", INTEGRAL_RANGE_MESSAGE);
+ assertRejected("-123456789012345678901234567890", INTEGRAL_RANGE_MESSAGE);
+
+ // The deserializer reads the whole tree and recurses in plain Java, so an element nested in a
+ // list or an object is rejected at the same level as a scalar. This covers those two branches,
+ // not Jackson's own nesting - see testStatisticsUpdateRequestRejectsOutOfRangeValue for that.
+ assertRejected("[1,123456789012345678901234567890]", INTEGRAL_RANGE_MESSAGE);
+ assertRejected("{\"key\":123456789012345678901234567890}", INTEGRAL_RANGE_MESSAGE);
+ }
+
+ @Test
+ void testStatisticValueRejectsNonFiniteFloatingPoint() {
+ // A magnitude beyond the double range is only representable as an infinity, which the
+ // serializer writes back out as the JSON string "Infinity" - the value would come back as a
+ // string on the next round trip. The message reports the parsed double rather than echoing the
+ // literal, because the node Jackson hands us already holds the infinity; assert it in full so
+ // that stays visible.
+ assertRejected(
+ "1.5E400", FLOATING_RANGE_MESSAGE + ", the literal parsed to " + Double.POSITIVE_INFINITY);
+ assertRejected(
+ "-1.5E400", FLOATING_RANGE_MESSAGE + ", the literal parsed to " + Double.NEGATIVE_INFINITY);
+ }
+
+ @Test
+ void testStatisticsUpdateRequestRejectsOutOfRangeValue() {
+ // The shape the REST layer actually deserializes: the value is Map content, so Jackson wraps
+ // the rejection into a JsonMappingException, which the server maps to 400. Use a bare mapper
+ // rather than the shared one, because setUp registers a StatisticValue deserializer onto that
+ // singleton and the assertion would then hold even without the DTO's @JsonDeserialize
+ // annotation. The server's ObjectMapperProvider registers no such module, so the annotation is
+ // what has to carry the deserializer here.
+ ObjectMapper mapper = new ObjectMapper();
+ JsonMappingException e =
+ Assertions.assertThrows(
+ JsonMappingException.class,
+ () ->
+ mapper.readValue(
+ "{\"updates\":{\"rowCount\":9223372036854775808}}",
+ StatisticsUpdateRequest.class));
+
+ Assertions.assertInstanceOf(IllegalArgumentException.class, e.getCause());
+ Assertions.assertTrue(e.getCause().getMessage().contains(INTEGRAL_RANGE_MESSAGE));
+ }
+
+ @Test
+ void testStatisticValueRejectsUnsupportedNodeType() {
+ // A BINARY node cannot come from JSON text, but convertValue reaches the terminal branch
+ // through an embedded-object token. Note that convertValue itself wraps any IOException the
+ // deserializer throws into an IllegalArgumentException carrying the same message, so the cause
+ // is what distinguishes our own rejection from a laundered checked exception.
+ IllegalArgumentException e =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> objectMapper.convertValue(new byte[] {1, 2}, StatisticValue.class));
+
+ Assertions.assertTrue(
+ e.getMessage().contains("Don't support json node type BINARY"),
+ () -> "Unexpected rejection reason: " + e.getMessage());
+ Assertions.assertNull(e.getCause());
+ }
+
+ private static void assertRejected(String json, String expectedMessagePart) {
+ IllegalArgumentException e =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> objectMapper.readValue(json, StatisticValue.class));
+
+ Assertions.assertTrue(
+ e.getMessage().contains(expectedMessagePart),
+ () -> "Unexpected rejection reason: " + e.getMessage());
+ }
}