joyhaldar commented on code in PR #16638:
URL: https://github.com/apache/iceberg/pull/16638#discussion_r3353246254


##########
data/src/test/java/org/apache/iceberg/data/BaseFormatModelTests.java:
##########
@@ -696,49 +733,390 @@ void testReaderBuilderReuseContainers(FileFormat 
fileFormat) throws IOException
   }
 
   @ParameterizedTest
-  @FieldSource("FILE_FORMATS")
-  void testReaderSchemaEvolutionNewColumnWithDefault(FileFormat fileFormat) 
throws IOException {
+  @FieldSource("FORMAT_AND_PRIMITIVE_DEFAULTS")
+  void testReaderSchemaEvolutionNewColumnWithDefault(
+      FileFormat fileFormat, Type.PrimitiveType type, Literal<?> defaultValue) 
throws IOException {
 
     assumeSupports(fileFormat, FEATURE_READER_DEFAULT);
+
     DataGenerator dataGenerator = new DataGenerators.DefaultSchema();
     Schema writeSchema = dataGenerator.schema();
 
     List<Record> genericRecords = dataGenerator.generateRecords();
     writeGenericRecords(fileFormat, writeSchema, genericRecords);
 
-    String defaultStringValue = "default_value";
-    int defaultIntValue = 42;
-
     int maxFieldId =
         
writeSchema.columns().stream().mapToInt(Types.NestedField::fieldId).max().orElse(0);
 
     List<Types.NestedField> evolvedColumns = 
Lists.newArrayList(writeSchema.columns());
     evolvedColumns.add(
-        Types.NestedField.required("col_f")
+        Types.NestedField.optional("col_with_default")
             .withId(maxFieldId + 1)
-            .ofType(Types.StringType.get())
-            .withInitialDefault(Literal.of(defaultStringValue))
-            .build());
-    evolvedColumns.add(
-        Types.NestedField.optional("col_g")
-            .withId(maxFieldId + 2)
-            .ofType(Types.IntegerType.get())
-            .withInitialDefault(Literal.of(defaultIntValue))
+            .ofType(type)
+            .withInitialDefault(defaultValue)
             .build());
 
     Schema evolvedSchema = new Schema(evolvedColumns);
-    readAndAssertGenericRecords(
+    readAndAssertGenericRecords(fileFormat, evolvedSchema, genericRecords);
+  }
+
+  @ParameterizedTest
+  @FieldSource("FILE_FORMATS")
+  void testDefaultValues(FileFormat fileFormat) throws IOException {
+    assumeSupports(fileFormat, FEATURE_READER_DEFAULT);
+
+    Schema writeSchema =
+        new Schema(
+            Types.NestedField.required(1, "id", Types.LongType.get()),
+            Types.NestedField.optional("data")
+                .withId(2)
+                .ofType(Types.StringType.get())
+                .withInitialDefault(Literal.of("wrong!"))
+                .withDoc("Should not produce default value")
+                .build());
+
+    List<Record> genericRecords = RandomGenericData.generate(writeSchema, 10, 
1L);
+    writeGenericRecords(fileFormat, writeSchema, genericRecords);
+
+    Schema expectedSchema =
+        new Schema(
+            Types.NestedField.required(1, "id", Types.LongType.get()),
+            Types.NestedField.optional("data")
+                .withId(2)
+                .ofType(Types.StringType.get())
+                .withInitialDefault(Literal.of("wrong!"))
+                .build(),
+            Types.NestedField.required("missing_str")
+                .withId(6)
+                .ofType(Types.StringType.get())
+                .withInitialDefault(Literal.of("orange"))
+                .build(),
+            Types.NestedField.optional("missing_int")
+                .withId(7)
+                .ofType(Types.IntegerType.get())
+                .withInitialDefault(Literal.of(34))
+                .build());
+
+    readAndAssertEngineRecords(
+        fileFormat,
+        expectedSchema,
+        genericRecords,
+        record -> {
+          Record expected = GenericRecord.create(expectedSchema);
+          expected.setField("id", record.getField("id"));
+          expected.setField("data", record.getField("data"));
+          expected.setField("missing_str", "orange");
+          expected.setField("missing_int", 34);
+          return expected;
+        });
+  }
+
+  @ParameterizedTest
+  @FieldSource("FILE_FORMATS")
+  void testNullDefaultValue(FileFormat fileFormat) throws IOException {
+    assumeSupports(fileFormat, FEATURE_READER_DEFAULT);
+
+    Schema writeSchema =
+        new Schema(
+            Types.NestedField.required(1, "id", Types.LongType.get()),
+            Types.NestedField.optional("data")
+                .withId(2)
+                .ofType(Types.StringType.get())
+                .withInitialDefault(Literal.of("wrong!"))
+                .withDoc("Should not produce default value")
+                .build());
+
+    List<Record> genericRecords = RandomGenericData.generate(writeSchema, 10, 
1L);
+    writeGenericRecords(fileFormat, writeSchema, genericRecords);
+
+    Schema expectedSchema =
+        new Schema(
+            Types.NestedField.required(1, "id", Types.LongType.get()),
+            Types.NestedField.optional("data")
+                .withId(2)
+                .ofType(Types.StringType.get())
+                .withInitialDefault(Literal.of("wrong!"))
+                .build(),
+            Types.NestedField.optional("missing_date")
+                .withId(3)
+                .ofType(Types.DateType.get())
+                .build());
+
+    readAndAssertEngineRecords(
+        fileFormat,
+        expectedSchema,
+        genericRecords,
+        record -> {
+          Record expected = GenericRecord.create(expectedSchema);
+          expected.setField("id", record.getField("id"));
+          expected.setField("data", record.getField("data"));
+          expected.setField("missing_date", null);
+          return expected;
+        });
+  }
+
+  @ParameterizedTest
+  @FieldSource("FILE_FORMATS")
+  void testNestedDefaultValue(FileFormat fileFormat) throws IOException {
+    assumeSupports(fileFormat, FEATURE_READER_DEFAULT);
+
+    Schema writeSchema =
+        new Schema(
+            Types.NestedField.required(1, "id", Types.LongType.get()),
+            Types.NestedField.optional("data")
+                .withId(2)
+                .ofType(Types.StringType.get())
+                .withInitialDefault(Literal.of("wrong!"))
+                .withDoc("Should not produce default value")
+                .build(),
+            Types.NestedField.optional("nested")
+                .withId(3)
+                .ofType(
+                    Types.StructType.of(
+                        Types.NestedField.required(4, "inner", 
Types.StringType.get())))
+                .withDoc("Used to test nested field defaults")
+                .build());
+
+    List<Record> genericRecords = RandomGenericData.generate(writeSchema, 10, 
1L);
+    writeGenericRecords(fileFormat, writeSchema, genericRecords);
+
+    Schema expectedSchema =

Review Comment:
   Extracted `idField` and `dataField` and shared them between the write and 
expected schemas.



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