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


##########
parquet/src/test/java/org/apache/iceberg/parquet/TestParquetFileMerger.java:
##########
@@ -0,0 +1,1069 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.parquet;
+
+import static org.apache.iceberg.parquet.Parquet.writeData;
+import static 
org.apache.iceberg.parquet.ParquetWritingTestUtils.createTempFile;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.FileContent;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Files;
+import org.apache.iceberg.MetadataColumns;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.TestTables;
+import org.apache.iceberg.data.GenericRecord;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.data.parquet.GenericParquetReaders;
+import org.apache.iceberg.data.parquet.GenericParquetWriter;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types;
+import org.apache.parquet.column.page.PageReadStore;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.io.ColumnIOFactory;
+import org.apache.parquet.io.MessageColumnIO;
+import org.apache.parquet.io.RecordReader;
+import org.apache.parquet.io.api.Converter;
+import org.apache.parquet.io.api.GroupConverter;
+import org.apache.parquet.io.api.PrimitiveConverter;
+import org.apache.parquet.schema.MessageType;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+public class TestParquetFileMerger {
+
+  private static final Schema SCHEMA =
+      new Schema(
+          Types.NestedField.required(1, "id", Types.IntegerType.get()),
+          Types.NestedField.optional(2, "data", Types.StringType.get()));
+
+  private static final PartitionSpec UNPARTITIONED_SPEC = 
PartitionSpec.unpartitioned();
+  private static final long DEFAULT_ROW_GROUP_SIZE =
+      TableProperties.PARQUET_ROW_GROUP_SIZE_BYTES_DEFAULT;
+
+  @TempDir private Path temp;
+
+  private FileIO fileIO;
+
+  @BeforeEach
+  public void setupFileIO() {
+    this.fileIO = new TestTables.LocalFileIO();
+  }
+
+  @Test
+  public void testCanMergeReturnsFalseForEmptyList() {
+    MessageType result = 
ParquetFileMerger.canMergeAndGetSchema(Collections.emptyList());
+    assertThat(result).isNull();
+  }
+
+  @Test
+  public void testCanMergeReturnsFalseForNullInput() {
+    MessageType result = ParquetFileMerger.canMergeAndGetSchema(null);
+    assertThat(result).isNull();
+  }
+
+  @Test
+  public void testCanMergeReturnsFalseForNonParquetFile() throws IOException {
+    // Create a non-Parquet file (just a text file)
+    File textFile = createTempFile(temp);
+    textFile.getParentFile().mkdirs(); // Ensure directory exists
+    java.nio.file.Files.write(textFile.toPath(), "This is not a Parquet 
file".getBytes());
+
+    InputFile inputFile = Files.localInput(textFile);
+    List<InputFile> inputFiles = Lists.newArrayList(inputFile);
+
+    // Should return null because file is not valid Parquet
+    MessageType result = ParquetFileMerger.canMergeAndGetSchema(inputFiles);
+    assertThat(result).isNull();
+  }
+
+  @Test
+  public void testCanMergeReturnsFalseForDifferentSchemas() throws IOException 
{
+    // Create first Parquet file with schema1
+    Schema icebergSchema1 = SCHEMA;
+
+    File parquetFile1 = createTempFile(temp);
+    OutputFile outputFile1 = Files.localOutput(parquetFile1);
+    createParquetFileWithSchema(outputFile1, icebergSchema1);
+
+    // Create second Parquet file with different schema
+    Schema icebergSchema2 =
+        new Schema(
+            Types.NestedField.required(1, "id", Types.IntegerType.get()),
+            Types.NestedField.optional(3, "other", Types.LongType.get())); // 
Different field
+
+    File parquetFile2 = createTempFile(temp);
+    OutputFile outputFile2 = Files.localOutput(parquetFile2);
+    createParquetFileWithSchema(outputFile2, icebergSchema2);
+
+    // Try to validate - should return null due to different schemas
+    InputFile inputFile1 = Files.localInput(parquetFile1);
+    InputFile inputFile2 = Files.localInput(parquetFile2);
+    List<InputFile> inputFiles = Arrays.asList(inputFile1, inputFile2);
+
+    MessageType result = ParquetFileMerger.canMergeAndGetSchema(inputFiles);
+    assertThat(result).isNull();
+  }
+
+  @Test
+  public void testCanMergeReturnsTrueForIdenticalSchemas() throws IOException {
+    // Create two Parquet files with the same schema and some data
+    File parquetFile1 = createTempFile(temp);
+    writeRecordsToFile(parquetFile1, Arrays.asList(createRecord(1, "a")));
+
+    File parquetFile2 = createTempFile(temp);
+    writeRecordsToFile(parquetFile2, Arrays.asList(createRecord(2, "b")));
+
+    // Should return non-null MessageType for identical schemas
+    InputFile inputFile1 = Files.localInput(parquetFile1);
+    InputFile inputFile2 = Files.localInput(parquetFile2);
+    List<InputFile> inputFiles = Arrays.asList(inputFile1, inputFile2);
+
+    MessageType result = ParquetFileMerger.canMergeAndGetSchema(inputFiles);
+    assertThat(result).isNotNull();
+  }
+
+  @Test
+  public void testCanMergeReturnsTrueForSingleFile() throws IOException {

Review Comment:
   Just realized that this tests an invalid scenario. Why would we want to 
merge a single file? 😄 



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