nastra commented on code in PR #9217: URL: https://github.com/apache/iceberg/pull/9217#discussion_r1415388740
########## core/src/test/java/org/apache/iceberg/TestBase.java: ########## @@ -0,0 +1,772 @@ +/* + * 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; + +import static org.apache.iceberg.types.Types.NestedField.required; + +import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import org.apache.iceberg.deletes.PositionDelete; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.io.OutputFile; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; +import org.apache.iceberg.relocated.com.google.common.collect.Iterators; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.relocated.com.google.common.io.Files; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.io.TempDir; + +public class TestBase { + // Schema passed to create tables + public static final Schema SCHEMA = + new Schema( + required(3, "id", Types.IntegerType.get()), required(4, "data", Types.StringType.get())); + + protected static final int BUCKETS_NUMBER = 16; + + // Partition spec used to create tables + public static final PartitionSpec SPEC = + PartitionSpec.builderFor(SCHEMA).bucket("data", BUCKETS_NUMBER).build(); + + static final DataFile FILE_A = + DataFiles.builder(SPEC) + .withPath("/path/to/data-a.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=0") // easy way to set partition data for now + .withRecordCount(1) + .build(); + static final DataFile FILE_A2 = + DataFiles.builder(SPEC) + .withPath("/path/to/data-a-2.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=0") // easy way to set partition data for now + .withRecordCount(1) + .build(); + static final DeleteFile FILE_A_DELETES = + FileMetadata.deleteFileBuilder(SPEC) + .ofPositionDeletes() + .withPath("/path/to/data-a-deletes.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=0") // easy way to set partition data for now + .withRecordCount(1) + .build(); + // Equality delete files. + static final DeleteFile FILE_A2_DELETES = + FileMetadata.deleteFileBuilder(SPEC) + .ofEqualityDeletes(1) + .withPath("/path/to/data-a2-deletes.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=0") + .withRecordCount(1) + .build(); + static final DataFile FILE_B = + DataFiles.builder(SPEC) + .withPath("/path/to/data-b.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=1") // easy way to set partition data for now + .withRecordCount(1) + .withSplitOffsets(ImmutableList.of(1L)) + .build(); + static final DeleteFile FILE_B_DELETES = + FileMetadata.deleteFileBuilder(SPEC) + .ofPositionDeletes() + .withPath("/path/to/data-b-deletes.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=1") // easy way to set partition data for now + .withRecordCount(1) + .build(); + static final DataFile FILE_C = + DataFiles.builder(SPEC) + .withPath("/path/to/data-c.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=2") // easy way to set partition data for now + .withRecordCount(1) + .withSplitOffsets(ImmutableList.of(2L, 8L)) + .build(); + static final DeleteFile FILE_C2_DELETES = + FileMetadata.deleteFileBuilder(SPEC) + .ofEqualityDeletes(1) + .withPath("/path/to/data-c-deletes.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=2") // easy way to set partition data for now + .withRecordCount(1) + .build(); + static final DataFile FILE_D = + DataFiles.builder(SPEC) + .withPath("/path/to/data-d.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=3") // easy way to set partition data for now + .withRecordCount(1) + .withSplitOffsets(ImmutableList.of(0L, 3L, 6L)) + .build(); + static final DeleteFile FILE_D2_DELETES = + FileMetadata.deleteFileBuilder(SPEC) + .ofEqualityDeletes(1) + .withPath("/path/to/data-d-deletes.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=3") // easy way to set partition data for now + .withRecordCount(1) + .build(); + static final DataFile FILE_WITH_STATS = + DataFiles.builder(SPEC) + .withPath("/path/to/data-with-stats.parquet") + .withMetrics( + new Metrics( + 10L, + ImmutableMap.of(3, 100L, 4, 200L), // column sizes + ImmutableMap.of(3, 90L, 4, 180L), // value counts + ImmutableMap.of(3, 10L, 4, 20L), // null value counts + ImmutableMap.of(3, 0L, 4, 0L), // nan value counts + ImmutableMap.of( + 3, + Conversions.toByteBuffer(Types.IntegerType.get(), 1), + 4, + Conversions.toByteBuffer(Types.IntegerType.get(), 2)), // lower bounds + ImmutableMap.of( + 3, + Conversions.toByteBuffer(Types.IntegerType.get(), 5), + 4, + Conversions.toByteBuffer(Types.IntegerType.get(), 10)) // upperbounds + )) + .withFileSizeInBytes(350) + .build(); + + static final FileIO FILE_IO = new TestTables.LocalFileIO(); + + @TempDir protected Path temp; + + @TempDir protected File tableDir = null; + protected File metadataDir = null; + public TestTables.TestTable table = null; + + protected final int formatVersion; + + @SuppressWarnings("checkstyle:MemberName") + protected final TableAssertions V1Assert; + + @SuppressWarnings("checkstyle:MemberName") + protected final TableAssertions V2Assert; + + public TestBase(int formatVersion) { + this.formatVersion = formatVersion; + this.V1Assert = new TableAssertions(1, formatVersion); + this.V2Assert = new TableAssertions(2, formatVersion); + } + + @BeforeEach + public void setupTable() throws Exception { + this.metadataDir = new File(tableDir, "metadata"); + this.table = create(SCHEMA, SPEC); + } + + @AfterEach + public void cleanupTables() { + TestTables.clearTables(); + } + + List<File> listManifestFiles() { + return listManifestFiles(tableDir); + } + + List<File> listManifestFiles(File tableDirToList) { + return Lists.newArrayList( + new File(tableDirToList, "metadata") + .listFiles( + (dir, name) -> + !name.startsWith("snap") + && Files.getFileExtension(name).equalsIgnoreCase("avro"))); + } + + public static long countAllMetadataFiles(File tableDir) { + return Arrays.stream(new File(tableDir, "metadata").listFiles()) + .filter(f -> f.isFile()) + .count(); + } + + protected TestTables.TestTable create(Schema schema, PartitionSpec spec) { + return TestTables.create(tableDir, "test", schema, spec, formatVersion); + } + + TestTables.TestTable load() { + return TestTables.load(tableDir, "test"); + } + + Integer version() { + return TestTables.metadataVersion("test"); + } + + public TableMetadata readMetadata() { + return TestTables.readMetadata("test"); + } + + ManifestFile writeManifest(DataFile... files) throws IOException { + return writeManifest(null, files); + } + + ManifestFile writeManifest(Long snapshotId, DataFile... files) throws IOException { + File manifestFile = temp.resolve("input.m0.avro").toFile(); + Assertions.assertThat(manifestFile.delete()).isTrue(); + OutputFile outputFile = table.ops().io().newOutputFile(manifestFile.getCanonicalPath()); + + ManifestWriter<DataFile> writer = + ManifestFiles.write(formatVersion, table.spec(), outputFile, snapshotId); + try { + for (DataFile file : files) { + writer.add(file); + } + } finally { + writer.close(); + } + + return writer.toManifestFile(); + } + + ManifestFile writeManifest(String fileName, ManifestEntry<?>... entries) throws IOException { + return writeManifest(null, fileName, entries); + } + + ManifestFile writeManifest(Long snapshotId, ManifestEntry<?>... entries) throws IOException { + return writeManifest(snapshotId, "input.m0.avro", entries); + } + + @SuppressWarnings("unchecked") + <F extends ContentFile<F>> ManifestFile writeManifest( + Long snapshotId, String fileName, ManifestEntry<?>... entries) throws IOException { + File manifestFile = temp.resolve(fileName).toFile(); + Assertions.assertThat(manifestFile.delete()).isTrue(); + OutputFile outputFile = table.ops().io().newOutputFile(manifestFile.getCanonicalPath()); + + ManifestWriter<F> writer; + if (entries[0].file() instanceof DataFile) { + writer = + (ManifestWriter<F>) + ManifestFiles.write(formatVersion, table.spec(), outputFile, snapshotId); + } else { + writer = + (ManifestWriter<F>) + ManifestFiles.writeDeleteManifest( + formatVersion, table.spec(), outputFile, snapshotId); + } + try { + for (ManifestEntry<?> entry : entries) { + writer.addEntry((ManifestEntry<F>) entry); + } + } finally { + writer.close(); + } + + return writer.toManifestFile(); + } + + ManifestFile writeDeleteManifest(int newFormatVersion, Long snapshotId, DeleteFile... deleteFiles) + throws IOException { + OutputFile manifestFile = + org.apache.iceberg.Files.localOutput( + FileFormat.AVRO.addExtension( + File.createTempFile("junit", null, temp.toFile()).toString())); + ManifestWriter<DeleteFile> writer = + ManifestFiles.writeDeleteManifest(newFormatVersion, SPEC, manifestFile, snapshotId); + try { + for (DeleteFile deleteFile : deleteFiles) { + writer.add(deleteFile); + } + } finally { + writer.close(); + } + return writer.toManifestFile(); + } + + ManifestFile writeManifestWithName(String name, DataFile... files) throws IOException { + File manifestFile = temp.resolve(name + ".avro").toFile(); + Assertions.assertThat(manifestFile.delete()).isTrue(); + OutputFile outputFile = table.ops().io().newOutputFile(manifestFile.getCanonicalPath()); + + ManifestWriter<DataFile> writer = + ManifestFiles.write(formatVersion, table.spec(), outputFile, null); + try { + for (DataFile file : files) { + writer.add(file); + } + } finally { + writer.close(); + } + + return writer.toManifestFile(); + } + + <F extends ContentFile<F>> ManifestEntry<F> manifestEntry( + ManifestEntry.Status status, Long snapshotId, F file) { + return manifestEntry(status, snapshotId, 0L, 0L, file); + } + + <F extends ContentFile<F>> ManifestEntry<F> manifestEntry( + ManifestEntry.Status status, + Long snapshotId, + Long dataSequenceNumber, + Long fileSequenceNumber, + F file) { + + GenericManifestEntry<F> entry = new GenericManifestEntry<>(table.spec().partitionType()); + switch (status) { + case ADDED: + if (dataSequenceNumber != null && dataSequenceNumber != 0) { + return entry.wrapAppend(snapshotId, dataSequenceNumber, file); + } else { + return entry.wrapAppend(snapshotId, file); + } + case EXISTING: + return entry.wrapExisting(snapshotId, dataSequenceNumber, fileSequenceNumber, file); + case DELETED: + return entry.wrapDelete(snapshotId, dataSequenceNumber, fileSequenceNumber, file); + default: + throw new IllegalArgumentException("Unexpected entry status: " + status); + } + } + + void validateSnapshot(Snapshot old, Snapshot snap, DataFile... newFiles) { + validateSnapshot(old, snap, null, newFiles); + } + + void validateSnapshot(Snapshot old, Snapshot snap, long sequenceNumber, DataFile... newFiles) { + validateSnapshot(old, snap, (Long) sequenceNumber, newFiles); + } + + @SuppressWarnings("checkstyle:HiddenField") + Snapshot commit(Table table, SnapshotUpdate snapshotUpdate, String branch) { + Snapshot snapshot; + if (branch.equals(SnapshotRef.MAIN_BRANCH)) { + snapshotUpdate.commit(); + snapshot = table.currentSnapshot(); + } else { + ((SnapshotProducer) snapshotUpdate.toBranch(branch)).commit(); + snapshot = table.snapshot(branch); + } + + return snapshot; + } + + Snapshot apply(SnapshotUpdate snapshotUpdate, String branch) { + if (branch.equals(SnapshotRef.MAIN_BRANCH)) { + return ((SnapshotProducer) snapshotUpdate).apply(); + } else { + return ((SnapshotProducer) snapshotUpdate.toBranch(branch)).apply(); + } + } + + void validateSnapshot(Snapshot old, Snapshot snap, Long sequenceNumber, DataFile... newFiles) { + Assertions.assertThat( + old != null ? Sets.newHashSet(old.deleteManifests(FILE_IO)) : ImmutableSet.of()) + .as("Should not change delete manifests") + .isEqualTo(Sets.newHashSet(snap.deleteManifests(FILE_IO))); + List<ManifestFile> oldManifests = old != null ? old.dataManifests(FILE_IO) : ImmutableList.of(); + + // copy the manifests to a modifiable list and remove the existing manifests + List<ManifestFile> newManifests = Lists.newArrayList(snap.dataManifests(FILE_IO)); + for (ManifestFile oldManifest : oldManifests) { + Assertions.assertThat(newManifests.remove(oldManifest)) + .as("New snapshot should contain old manifests") + .isTrue(); + } + + Assertions.assertThat(newManifests.size()) Review Comment: ```suggestion Assertions.assertThat(newManifests).hasSize(1); ``` -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org