dramaticlly commented on code in PR #17513:
URL: https://github.com/apache/iceberg/pull/17513#discussion_r3770438786


##########
api/src/test/java/org/apache/iceberg/TestSchema.java:
##########
@@ -312,4 +314,162 @@ public void testIndexFieldsNestedSchema() {
     assertThat(fields.get(5).name()).isEqualTo("email");
     assertThat(((Types.StructType) fields.get(2).type()).fields()).hasSize(3);
   }
+
+  @Test
+  void isOptionalWithEmptySchemaOrUnknownFields() {
+    assertThatThrownBy(() -> new Schema().isOptional(1))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot find field with id: 1");
+
+    assertThatThrownBy(() -> new Schema(required(1, "id", 
Types.IntegerType.get())).isOptional(2))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot find field with id: 2");
+  }
+
+  @Test
+  void isOptionalWithTopLevelFields() {
+    Schema schema =
+        new Schema(
+            required(1, "id", Types.IntegerType.get()),
+            optional(2, "data", Types.StringType.get()));
+
+    assertThat(schema.isOptional(1)).isFalse();
+    assertThat(schema.isOptional(2)).isTrue();
+  }
+
+  @Test
+  void isOptionalWithNestedStructs() {
+    Schema schema =
+        new Schema(
+            required(
+                1,
+                "required_location",
+                Types.StructType.of(
+                    required(3, "required_lat", Types.DoubleType.get()),
+                    optional(4, "optional_lon", Types.DoubleType.get()),
+                    required(
+                        5,
+                        "required_inner",
+                        Types.StructType.of(
+                            required(6, "required_zip", 
Types.IntegerType.get()))))),
+            optional(
+                2,
+                "optional_location",
+                Types.StructType.of(
+                    required(7, "required_lat", Types.DoubleType.get()),
+                    required(
+                        8,
+                        "required_inner",
+                        Types.StructType.of(
+                            required(9, "required_zip", 
Types.IntegerType.get()))))));
+
+    // a required field is not nullable when every field that contains it is 
required
+    assertThat(schema.isOptional(1)).isFalse();
+    assertThat(schema.isOptional(3)).isFalse();
+    assertThat(schema.isOptional(5)).isFalse();
+    assertThat(schema.isOptional(6)).isFalse();
+
+    // an optional field is nullable regardless of the fields that contain it

Review Comment:
   nit: I think we renamed from isNullable to isOptional but some of the UT 
comment is lagging behind. Might consider rename as well, or better to inline 
using `as(...)`



-- 
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]

Reply via email to