dianfu commented on code in PR #28947:
URL: https://github.com/apache/flink/pull/28947#discussion_r3764694036
##########
flink-models/flink-model-triton/src/main/java/org/apache/flink/model/triton/TritonTypeMapper.java:
##########
@@ -270,6 +279,50 @@ private static ArrayData deserializeArrayFromJson(JsonNode
dataNode, LogicalType
}
}
+ /** Returns whether the given JSON array contains at least one null
element. */
+ private static boolean containsNull(JsonNode dataNode) {
+ for (JsonNode element : dataNode) {
+ if (element.isNull()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Deserializes a JSON array that contains at least one null element into
a boxed {@link
+ * GenericArrayData}, which is the only representation able to report
{@code isNullAt(pos)}.
+ *
+ * @param dataNode The JSON array node
+ * @param elementType The element type
+ * @param size The number of elements
+ * @return The deserialized ArrayData preserving null elements
+ */
+ private static ArrayData deserializeNullableArrayFromJson(
+ JsonNode dataNode, LogicalType elementType, int size) {
+ // Nested arrays are not supported by the non-null path above either;
reject them here so
+ // that the presence of a null element cannot change which element
types are accepted.
+ Preconditions.checkArgument(
+ !(elementType instanceof ArrayType),
+ "Unsupported array element type: %s",
+ elementType);
+ // Writing a null into an array declared NOT NULL would violate the
output schema, so fail
+ // loudly rather than substituting a value the model never produced.
+ Preconditions.checkArgument(
+ elementType.isNullable(),
+ "Received a null array element but the declared element type
is NOT NULL: %s",
+ elementType);
+
+ Object[] array = new Object[size];
Review Comment:
Allocating `Object[]` violates `GenericArrayData`'s requirement that boxed
primitive arrays retain their concrete component type. For example, nullable
`ARRAY<INT>` advertises `Integer[]`, but Flink's external conversion fast path
returns this underlying `Object[]`; generated or user code casting it to
`Integer[]` can then fail with `ClassCastException`.
##########
flink-models/flink-model-triton/src/main/java/org/apache/flink/model/triton/TritonTypeMapper.java:
##########
@@ -270,6 +279,50 @@ private static ArrayData deserializeArrayFromJson(JsonNode
dataNode, LogicalType
}
}
+ /** Returns whether the given JSON array contains at least one null
element. */
+ private static boolean containsNull(JsonNode dataNode) {
+ for (JsonNode element : dataNode) {
+ if (element.isNull()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Deserializes a JSON array that contains at least one null element into
a boxed {@link
+ * GenericArrayData}, which is the only representation able to report
{@code isNullAt(pos)}.
+ *
+ * @param dataNode The JSON array node
+ * @param elementType The element type
+ * @param size The number of elements
+ * @return The deserialized ArrayData preserving null elements
+ */
+ private static ArrayData deserializeNullableArrayFromJson(
+ JsonNode dataNode, LogicalType elementType, int size) {
+ // Nested arrays are not supported by the non-null path above either;
reject them here so
+ // that the presence of a null element cannot change which element
types are accepted.
+ Preconditions.checkArgument(
+ !(elementType instanceof ArrayType),
+ "Unsupported array element type: %s",
+ elementType);
+ // Writing a null into an array declared NOT NULL would violate the
output schema, so fail
+ // loudly rather than substituting a value the model never produced.
+ Preconditions.checkArgument(
+ elementType.isNullable(),
+ "Received a null array element but the declared element type
is NOT NULL: %s",
+ elementType);
+
+ Object[] array = new Object[size];
Review Comment:
It's also good to call `toTritonDataType(elementType)` before the loop to
reject unsupported types.
--
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]