dramaticlly commented on code in PR #17513:
URL: https://github.com/apache/iceberg/pull/17513#discussion_r3716012064
##########
api/src/main/java/org/apache/iceberg/types/TypeUtil.java:
##########
@@ -291,6 +291,28 @@ public static List<Types.NestedField>
ancestorFields(Schema schema, int fieldId)
return parents;
}
+ /**
+ * Returns whether a field can evaluate to null within the given schema.
+ *
+ * <p>A field can be null if it is declared optional, or if it is nested
inside an optional field.
+ * For example, a required field inside an optional struct is effectively
null whenever that
+ * struct is null.
+ *
+ * <p>If the field is not present in the schema, this method returns true
because its nullability
+ * cannot be determined.
+ *
+ * @param schema The schema that contains the field ID
+ * @param fieldId The field ID to check
+ * @return true if the field may be null, false if it cannot be null
+ */
+ public static boolean isNullable(Schema schema, int fieldId) {
+ Types.NestedField field = schema.findField(fieldId);
+
+ return field == null
+ || field.isOptional()
+ || ancestorFields(schema,
fieldId).stream().anyMatch(Types.NestedField::isOptional);
Review Comment:
ancestorFields calls `TypeUtil.indexParents` for each invocation and ends up
traverse the schema tree. I am wondering if worth caching the indexParents in
Schema. Currently it's only used for identifier field in constructor and
discarded right after
https://github.com/apache/iceberg/blob/1d8c71529e0013bb57716451a67dcd284ea35965/api/src/main/java/org/apache/iceberg/Schema.java#L153
##########
api/src/test/java/org/apache/iceberg/types/TestTypeUtil.java:
##########
@@ -972,6 +972,151 @@ public void ancestorFieldsInNestedSchema() {
assertThat(TypeUtil.ancestorFields(schema,
17)).containsExactly(pointsElement, points);
}
+ @Test
+ public void isNullableWithUnknownFieldId() {
+ Schema schema = new Schema(required(1, "id", IntegerType.get()));
+
+ assertThat(TypeUtil.isNullable(schema, 2)).isTrue();
Review Comment:
can we try the empty schema as well? `isNullable(new Schema(), 1)`
--
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]