|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.iceberg.data; |
| 20 | + |
| 21 | +import static org.apache.iceberg.MetadataColumns.DELETE_FILE_PATH; |
| 22 | +import static org.apache.iceberg.MetadataColumns.DELETE_FILE_POS; |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.List; |
| 28 | +import org.apache.iceberg.DataFile; |
| 29 | +import org.apache.iceberg.DeleteFile; |
| 30 | +import org.apache.iceberg.FileFormat; |
| 31 | +import org.apache.iceberg.Parameter; |
| 32 | +import org.apache.iceberg.ParameterizedTestExtension; |
| 33 | +import org.apache.iceberg.Parameters; |
| 34 | +import org.apache.iceberg.PartitionSpec; |
| 35 | +import org.apache.iceberg.Schema; |
| 36 | +import org.apache.iceberg.TestBase; |
| 37 | +import org.apache.iceberg.deletes.EqualityDeleteWriter; |
| 38 | +import org.apache.iceberg.deletes.PositionDelete; |
| 39 | +import org.apache.iceberg.deletes.PositionDeleteWriter; |
| 40 | +import org.apache.iceberg.encryption.EncryptedFiles; |
| 41 | +import org.apache.iceberg.encryption.EncryptedOutputFile; |
| 42 | +import org.apache.iceberg.encryption.EncryptionKeyMetadata; |
| 43 | +import org.apache.iceberg.formats.FileWriterBuilder; |
| 44 | +import org.apache.iceberg.formats.FormatModelRegistry; |
| 45 | +import org.apache.iceberg.inmemory.InMemoryFileIO; |
| 46 | +import org.apache.iceberg.io.CloseableIterable; |
| 47 | +import org.apache.iceberg.io.DataWriter; |
| 48 | +import org.apache.iceberg.io.InputFile; |
| 49 | +import org.apache.iceberg.io.OutputFile; |
| 50 | +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; |
| 51 | +import org.junit.jupiter.api.AfterEach; |
| 52 | +import org.junit.jupiter.api.BeforeEach; |
| 53 | +import org.junit.jupiter.api.TestTemplate; |
| 54 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 55 | +import org.junit.jupiter.api.io.TempDir; |
| 56 | + |
| 57 | +@ExtendWith(ParameterizedTestExtension.class) |
| 58 | +public class TestGenericFormatModels { |
| 59 | + @Parameters(name = "fileFormat = {0}") |
| 60 | + protected static List<Object> parameters() { |
| 61 | + return List.of(FileFormat.AVRO); |
| 62 | + } |
| 63 | + |
| 64 | + private static final List<Record> TEST_RECORDS = |
| 65 | + ImmutableList.of( |
| 66 | + GenericRecord.create(TestBase.SCHEMA).copy("id", 1, "data", "hello"), |
| 67 | + GenericRecord.create(TestBase.SCHEMA).copy("id", 2, "data", "world")); |
| 68 | + |
| 69 | + @Parameter(index = 0) |
| 70 | + private FileFormat fileFormat; |
| 71 | + |
| 72 | + @TempDir protected Path temp; |
| 73 | + |
| 74 | + private InMemoryFileIO fileIO; |
| 75 | + private EncryptedOutputFile encryptedFile; |
| 76 | + |
| 77 | + @BeforeEach |
| 78 | + public void before() { |
| 79 | + this.fileIO = new InMemoryFileIO(); |
| 80 | + OutputFile outputFile = fileIO.newOutputFile("test-file." + fileFormat.name().toLowerCase()); |
| 81 | + this.encryptedFile = EncryptedFiles.encryptedOutput(outputFile, EncryptionKeyMetadata.EMPTY); |
| 82 | + } |
| 83 | + |
| 84 | + @AfterEach |
| 85 | + public void after() throws IOException { |
| 86 | + fileIO.deleteFile(encryptedFile.encryptingOutputFile()); |
| 87 | + this.encryptedFile = null; |
| 88 | + if (fileIO != null) { |
| 89 | + fileIO.close(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @TestTemplate |
| 94 | + public void testDataWriter() throws IOException { |
| 95 | + FileWriterBuilder<DataWriter<Record>, Schema> writerBuilder = |
| 96 | + FormatModelRegistry.dataWriteBuilder(fileFormat, Record.class, encryptedFile); |
| 97 | + |
| 98 | + DataFile dataFile; |
| 99 | + DataWriter<Record> writer = |
| 100 | + writerBuilder.schema(TestBase.SCHEMA).spec(PartitionSpec.unpartitioned()).build(); |
| 101 | + try (writer) { |
| 102 | + for (Record record : TEST_RECORDS) { |
| 103 | + writer.write(record); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + dataFile = writer.toDataFile(); |
| 108 | + |
| 109 | + assertThat(dataFile).isNotNull(); |
| 110 | + assertThat(dataFile.recordCount()).isEqualTo(2); |
| 111 | + assertThat(dataFile.format()).isEqualTo(fileFormat); |
| 112 | + |
| 113 | + // Verify the file content by reading it back |
| 114 | + InputFile inputFile = fileIO.newInputFile(encryptedFile.encryptingOutputFile().location()); |
| 115 | + List<Record> readRecords; |
| 116 | + try (CloseableIterable<Record> reader = |
| 117 | + FormatModelRegistry.readBuilder(fileFormat, Record.class, inputFile) |
| 118 | + .project(TestBase.SCHEMA) |
| 119 | + .build()) { |
| 120 | + readRecords = ImmutableList.copyOf(reader); |
| 121 | + } |
| 122 | + |
| 123 | + assertThat(readRecords).hasSize(2); |
| 124 | + DataTestHelpers.assertEquals( |
| 125 | + TestBase.SCHEMA.asStruct(), TEST_RECORDS.get(0), readRecords.get(0)); |
| 126 | + DataTestHelpers.assertEquals( |
| 127 | + TestBase.SCHEMA.asStruct(), TEST_RECORDS.get(1), readRecords.get(1)); |
| 128 | + } |
| 129 | + |
| 130 | + @TestTemplate |
| 131 | + public void testEqualityDeleteWriter() throws IOException { |
| 132 | + FileWriterBuilder<EqualityDeleteWriter<Record>, Schema> writerBuilder = |
| 133 | + FormatModelRegistry.equalityDeleteWriteBuilder(fileFormat, Record.class, encryptedFile); |
| 134 | + |
| 135 | + DeleteFile deleteFile; |
| 136 | + EqualityDeleteWriter<Record> writer = |
| 137 | + writerBuilder |
| 138 | + .schema(TestBase.SCHEMA) |
| 139 | + .spec(PartitionSpec.unpartitioned()) |
| 140 | + .equalityFieldIds(3) |
| 141 | + .build(); |
| 142 | + try (writer) { |
| 143 | + for (Record record : TEST_RECORDS) { |
| 144 | + writer.write(record); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + deleteFile = writer.toDeleteFile(); |
| 149 | + |
| 150 | + assertThat(deleteFile).isNotNull(); |
| 151 | + assertThat(deleteFile.recordCount()).isEqualTo(2); |
| 152 | + assertThat(deleteFile.format()).isEqualTo(fileFormat); |
| 153 | + assertThat(deleteFile.equalityFieldIds()).containsExactly(3); |
| 154 | + |
| 155 | + // Verify the file content by reading it back |
| 156 | + InputFile inputFile = fileIO.newInputFile(encryptedFile.encryptingOutputFile().location()); |
| 157 | + List<Record> readRecords; |
| 158 | + try (CloseableIterable<Record> reader = |
| 159 | + FormatModelRegistry.readBuilder(fileFormat, Record.class, inputFile) |
| 160 | + .project(TestBase.SCHEMA) |
| 161 | + .build()) { |
| 162 | + readRecords = ImmutableList.copyOf(reader); |
| 163 | + } |
| 164 | + |
| 165 | + assertThat(readRecords).hasSize(2); |
| 166 | + DataTestHelpers.assertEquals( |
| 167 | + TestBase.SCHEMA.asStruct(), TEST_RECORDS.get(0), readRecords.get(0)); |
| 168 | + DataTestHelpers.assertEquals( |
| 169 | + TestBase.SCHEMA.asStruct(), TEST_RECORDS.get(1), readRecords.get(1)); |
| 170 | + } |
| 171 | + |
| 172 | + @TestTemplate |
| 173 | + public void testPositionDeleteWriter() throws IOException { |
| 174 | + Schema positionDeleteSchema = new Schema(DELETE_FILE_PATH, DELETE_FILE_POS); |
| 175 | + |
| 176 | + FileWriterBuilder<PositionDeleteWriter<Record>, ?> writerBuilder = |
| 177 | + FormatModelRegistry.positionDeleteWriteBuilder(fileFormat, encryptedFile); |
| 178 | + |
| 179 | + PositionDelete<Record> delete1 = PositionDelete.create(); |
| 180 | + delete1.set("data-file-1.parquet", 0L, null); |
| 181 | + |
| 182 | + PositionDelete<Record> delete2 = PositionDelete.create(); |
| 183 | + delete2.set("data-file-1.parquet", 1L, null); |
| 184 | + |
| 185 | + List<PositionDelete<Record>> positionDeletes = ImmutableList.of(delete1, delete2); |
| 186 | + |
| 187 | + DeleteFile deleteFile; |
| 188 | + PositionDeleteWriter<Record> writer = writerBuilder.spec(PartitionSpec.unpartitioned()).build(); |
| 189 | + try (writer) { |
| 190 | + for (PositionDelete<Record> delete : positionDeletes) { |
| 191 | + writer.write(delete); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + deleteFile = writer.toDeleteFile(); |
| 196 | + |
| 197 | + assertThat(deleteFile).isNotNull(); |
| 198 | + assertThat(deleteFile.recordCount()).isEqualTo(2); |
| 199 | + assertThat(deleteFile.format()).isEqualTo(fileFormat); |
| 200 | + |
| 201 | + // Verify the file content by reading it back |
| 202 | + InputFile inputFile = fileIO.newInputFile(encryptedFile.encryptingOutputFile().location()); |
| 203 | + List<Record> readRecords; |
| 204 | + try (CloseableIterable<Record> reader = |
| 205 | + FormatModelRegistry.readBuilder(fileFormat, Record.class, inputFile) |
| 206 | + .project(positionDeleteSchema) |
| 207 | + .build()) { |
| 208 | + readRecords = ImmutableList.copyOf(reader); |
| 209 | + } |
| 210 | + |
| 211 | + assertThat(readRecords).hasSize(2); |
| 212 | + assertThat(readRecords.get(0).getField("file_path")).isEqualTo("data-file-1.parquet"); |
| 213 | + assertThat(readRecords.get(0).getField("pos")).isEqualTo(0L); |
| 214 | + assertThat(readRecords.get(1).getField("file_path")).isEqualTo("data-file-1.parquet"); |
| 215 | + assertThat(readRecords.get(1).getField("pos")).isEqualTo(1L); |
| 216 | + } |
| 217 | +} |
0 commit comments