pvary commented on code in PR #15843:
URL: https://github.com/apache/iceberg/pull/15843#discussion_r3110133129


##########
data/src/test/java/org/apache/iceberg/data/BaseFormatModelTests.java:
##########
@@ -700,4 +1089,164 @@ private static String splitSizeProperty(FileFormat 
fileFormat) {
               "No split size property defined for format: " + fileFormat);
     };
   }
+
+  private void writeRecordsWithoutFieldIds(
+      FileFormat fileFormat, Schema schema, List<Record> records) throws 
IOException {
+    switch (fileFormat) {
+      case PARQUET -> writeParquetWithoutFieldIds(schema, records);
+      case AVRO -> writeAvroWithoutFieldIds(schema, records);
+      case ORC -> writeOrcWithoutFieldIds(schema, records);
+      default -> throw new UnsupportedOperationException("Unsupported file 
format: " + fileFormat);
+    }
+  }
+
+  private void writeAvroWithoutFieldIds(Schema schema, List<Record> records) 
throws IOException {
+    org.apache.avro.Schema avroSchemaWithoutIds = RemoveIds.removeIds(schema);
+
+    OutputFile outputFile = encryptedFile.encryptingOutputFile();
+    DatumWriter<GenericData.Record> datumWriter = new 
GenericDatumWriter<>(avroSchemaWithoutIds);
+    try (OutputStream out = outputFile.create();
+        DataFileWriter<GenericData.Record> writer = new 
DataFileWriter<>(datumWriter)) {
+      writer.create(avroSchemaWithoutIds, out);
+      for (Record record : records) {
+        GenericData.Record avroRecord = new 
GenericData.Record(avroSchemaWithoutIds);
+        for (Types.NestedField field : schema.columns()) {
+          avroRecord.put(field.name(), record.getField(field.name()));
+        }
+
+        writer.append(avroRecord);
+      }
+    }
+
+    try (DataFileStream<GenericData.Record> reader =
+        new DataFileStream<>(outputFile.toInputFile().newStream(), new 
GenericDatumReader<>())) {
+      assertThat(AvroSchemaUtil.hasIds(reader.getSchema())).isFalse();
+    }
+  }
+
+  private void writeParquetWithoutFieldIds(Schema schema, List<Record> 
records) throws IOException {
+    org.apache.avro.Schema avroSchemaWithoutIds = RemoveIds.removeIds(schema);
+
+    Path tempFile = Files.createTempFile("iceberg-test-no-ids-", ".parquet");
+    Files.deleteIfExists(tempFile);
+
+    try {
+      try (ParquetWriter<GenericData.Record> writer =
+          AvroParquetWriter.<GenericData.Record>builder(
+                  new org.apache.hadoop.fs.Path(tempFile.toUri()))
+              .withDataModel(GenericData.get())
+              .withSchema(avroSchemaWithoutIds)
+              .withConf(new org.apache.hadoop.conf.Configuration())
+              .build()) {
+        for (Record record : records) {
+          GenericData.Record avroRecord = new 
GenericData.Record(avroSchemaWithoutIds);
+          for (Types.NestedField field : schema.columns()) {
+            avroRecord.put(field.name(), record.getField(field.name()));
+          }
+
+          writer.write(avroRecord);
+        }
+      }
+
+      try (ParquetFileReader reader =
+          ParquetFileReader.open(
+              HadoopInputFile.fromPath(
+                  new org.apache.hadoop.fs.Path(tempFile.toUri()),
+                  new org.apache.hadoop.conf.Configuration()))) {
+        
assertThat(ParquetSchemaUtil.hasIds(reader.getFooter().getFileMetaData().getSchema()))
+            .isFalse();
+      }
+
+      byte[] bytes = Files.readAllBytes(tempFile);
+      try (OutputStream out = encryptedFile.encryptingOutputFile().create()) {
+        out.write(bytes);
+      }
+    } finally {
+      Files.deleteIfExists(tempFile);
+    }
+  }
+
+  private void writeOrcWithoutFieldIds(Schema schema, List<Record> records) 
throws IOException {
+    TypeDescription typeWithIds = ORCSchemaUtil.convert(schema);
+    TypeDescription typeWithoutIds = ORCSchemaUtil.removeIds(typeWithIds);
+
+    Configuration conf = new Configuration();
+    OrcFile.WriterOptions options =
+        
OrcFile.writerOptions(conf).useUTCTimestamp(true).setSchema(typeWithoutIds);
+
+    OrcRowWriter<Record> rowWriter = GenericOrcWriter.buildWriter(schema, 
typeWithIds);
+
+    Path tempFile = Files.createTempFile("iceberg-test-no-ids-", ".orc");
+    Files.deleteIfExists(tempFile);
+    org.apache.hadoop.fs.Path hadoopPath = new 
org.apache.hadoop.fs.Path(tempFile.toUri());
+
+    try {
+      try (Writer orcWriter = OrcFile.createWriter(hadoopPath, options)) {
+        VectorizedRowBatch batch = typeWithoutIds.createRowBatch();
+        for (Record record : records) {
+          rowWriter.write(record, batch);
+          if (batch.size == batch.getMaxSize()) {
+            orcWriter.addRowBatch(batch);
+            batch.reset();
+          }
+        }
+
+        if (batch.size > 0) {
+          orcWriter.addRowBatch(batch);
+          batch.reset();
+        }
+      }
+
+      try (Reader reader = OrcFile.createReader(hadoopPath, 
OrcFile.readerOptions(conf))) {
+        assertThat(ORCSchemaUtil.hasIds(reader.getSchema())).isFalse();
+      }
+
+      byte[] bytes = Files.readAllBytes(tempFile);
+      try (OutputStream out = encryptedFile.encryptingOutputFile().create()) {
+        out.write(bytes);
+      }
+    } finally {
+      Files.deleteIfExists(tempFile);
+    }
+  }
+
+  private void runTypePromotionCheck(
+      FileFormat fileFormat, Type fromType, Type toType, Function<Object, 
Object> promoteValue)
+      throws IOException {
+    String columnName = "col";
+    Schema writeSchema = new Schema(Types.NestedField.required(1, columnName, 
fromType));
+    Schema readSchema = new Schema(Types.NestedField.required(1, columnName, 
toType));
+
+    List<Record> genericRecords = RandomGenericData.generate(writeSchema, 10, 
1L);
+    writeGenericRecords(fileFormat, writeSchema, genericRecords);
+
+    List<Record> expectedGenericRecords =
+        genericRecords.stream()
+            .map(
+                record -> {
+                  Record expected = GenericRecord.create(readSchema);
+                  expected.setField(columnName, 
promoteValue.apply(record.getField(columnName)));
+                  return expected;
+                })
+            .toList();
+
+    readAndAssertEngineRecords(fileFormat, readSchema, expectedGenericRecords);

Review Comment:
   I still think that this would be better
   
   ```suggestion
       readAndAssertEngineRecords(fileFormat, readSchema, genericRecords, 
record -> {
                     Record expected = GenericRecord.create(readSchema);
                     expected.setField(columnName, 
promoteValue.apply(record.getField(columnName)));
                     return expected;
                   });
   ```



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