nastra commented on code in PR #19143:
URL: https://github.com/apache/iceberg/pull/19143#discussion_r4782629769
##########
core/src/main/java/org/apache/iceberg/SchemaParser.java:
##########
@@ -210,6 +216,21 @@ private static Literal<?> defaultFromJson(String
defaultField, Type type, JsonNo
return null;
}
+ // non-finite float and double defaults are written as strings;
Expressions.lit rejects NaN
+ private static Literal<?> nonFiniteDefaultFromJson(Type type, JsonNode
defaultJson) {
Review Comment:
in SingleValueParser.fromJSON we can then have something like
```
case DOUBLE:
if (defaultValue.isTextual()) {
return nonFiniteFromJson(type, defaultValue);
}
```
And `nonFiniteFromJson` could be simplified to:
```
private static double nonFiniteFromJson(Type type, JsonNode defaultValue) {
return switch (defaultValue.textValue()) {
case "NaN" -> Double.NaN;
case "Infinity" -> Double.POSITIVE_INFINITY;
case "-Infinity" -> Double.NEGATIVE_INFINITY;
default ->
throw new IllegalArgumentException(
String.format("Cannot parse default as a %s value: %s", type,
defaultValue));
};
}
```
For testing we could simplify this down to
```
private static Stream<Arguments> nonFiniteValues() {
return Stream.of(
Arguments.of(Types.FloatType.get(), Float.NaN, "\"NaN\""),
Arguments.of(Types.FloatType.get(), Float.POSITIVE_INFINITY,
"\"Infinity\""),
Arguments.of(Types.FloatType.get(), Float.NEGATIVE_INFINITY,
"\"-Infinity\""),
Arguments.of(Types.DoubleType.get(), Double.NaN, "\"NaN\""),
Arguments.of(Types.DoubleType.get(), Double.POSITIVE_INFINITY,
"\"Infinity\""),
Arguments.of(Types.DoubleType.get(), Double.NEGATIVE_INFINITY,
"\"-Infinity\""));
}
@ParameterizedTest
@MethodSource("nonFiniteValues")
public void nonFiniteValueRoundTrip(Type type, Object value, String
expectedJson) {
String json = SingleValueParser.toJson(type, value);
assertThat(json).isEqualTo(expectedJson);
assertThat(SingleValueParser.fromJson(type, json)).isEqualTo(value);
}
````
And then it might be good to also have tests for either a Float or a Double
in a List/Map/Struct
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]