rdblue commented on code in PR #18984: URL: https://github.com/apache/iceberg/pull/18984#discussion_r4590296723
########## core/src/test/java/org/apache/iceberg/TestV4ManifestWriter.java: ########## @@ -0,0 +1,979 @@ +/* + * 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.optional; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.iceberg.encryption.EncryptionUtil; +import org.apache.iceberg.inmemory.InMemoryOutputFile; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.OutputFile; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.Test; + +public class TestV4ManifestWriter { + private static final long COMMIT_SNAPSHOT_ID = 42L; + private static final long COMMIT_SEQUENCE_NUMBER = 50L; + private static final long COMMIT_FIRST_ROW_ID = 1_000L; + private static final long RECORD_COUNT = 100L; + private static final Schema SCHEMA = + new Schema( + 1, + optional(1, "id", Types.IntegerType.get()), + optional(2, "data", Types.StringType.get())); + + private static final PartitionSpec TRUNCATE_SPEC = + PartitionSpec.builderFor(SCHEMA).truncate("id", 100).build(); + private static final Map<Integer, PartitionSpec> SPECS = + Map.of(TRUNCATE_SPEC.specId(), TRUNCATE_SPEC); + + private static final MetricsConfig METRICS_CONFIG = + MetricsTestUtil.from(ImmutableMap.of(), SCHEMA); + + private static final Types.StructType STATS_TYPE = + StatsUtil.statsWriteSchema(SCHEMA, METRICS_CONFIG); + + private static final FieldStats<Integer> ID_STATS_0_99 = + StatsTestUtil.fieldStats(STATS_TYPE, 1, 0, 99, RECORD_COUNT, 0L, null, null); + private static final FieldStats<Integer> ID_STATS_100_199 = + StatsTestUtil.fieldStats(STATS_TYPE, 1, 100, 199, RECORD_COUNT, 0L, null, null); + private static final FieldStats<String> DATA_STATS = + StatsTestUtil.fieldStats(STATS_TYPE, 2, "a", "z", RECORD_COUNT, 20L, null, 2L); + + private static final ContentStatsStruct CONTENT_STATS_A = new ContentStatsStruct(STATS_TYPE); + private static final ContentStatsStruct CONTENT_STATS_B = new ContentStatsStruct(STATS_TYPE); + + static { + CONTENT_STATS_A.setStats(1, ID_STATS_0_99); + CONTENT_STATS_A.setStats(2, DATA_STATS); + CONTENT_STATS_B.setStats(1, ID_STATS_100_199); + CONTENT_STATS_B.setStats(2, DATA_STATS); + } + + // Tracking used for manifests produced in tests + private static final Tracking COMMIT_TRACKING = + new TrackingStruct( + EntryStatus.ADDED, + COMMIT_SNAPSHOT_ID, + COMMIT_SEQUENCE_NUMBER, + COMMIT_SEQUENCE_NUMBER, + null, // not modified + COMMIT_FIRST_ROW_ID, + null, // no change bitmaps + null); + + private static final DeletionVector DV_A = + DeletionVectorStruct.builder() + .location("s3://bucket/db/table/data/file-a-dv.puffin") + .offset(0) + .sizeInBytes(100) + .cardinality(10) + .build(); + + private static final ColumnFile COLUMN_FILE_A = + new ColumnFileStruct( + "s3://bucket/db/table/data/file-a-new-columns.parquet", + Set.of(2), + FileFormat.PARQUET, + 500L, + null); // key metadata + + private static final TrackedFile FILE_A = + new TrackedFileStruct( + new TrackingStruct(EntryStatus.EXISTING, 40L, 35L, 34L, null, 500L, null, null), + FileContent.DATA, + "s3://bucket/db/table/data/file-a.parquet", + FileFormat.PARQUET, + 100, + 1024, + null, // unpartitioned + null, // unpartitioned + CONTENT_STATS_A, + false, + SortOrder.unsorted().orderId(), + null, // no DV + null, // manifest info + null, // key metadata, + List.of(4L), + List.of(COLUMN_FILE_A)); + + private static final TrackedFile FILE_A_WITH_DV = + new TrackedFileStruct( + new TrackingStruct(EntryStatus.EXISTING, 40L, 35L, 34L, 41L, 500L, null, null), + FileContent.DATA, + "s3://bucket/db/table/data/file-a.parquet", + FileFormat.PARQUET, + 100, + 1024, + null, + null, + CONTENT_STATS_A, + false, + SortOrder.unsorted().orderId(), + DV_A, + null, // manifest info + null, // key metadata, + List.of(4L), + List.of(COLUMN_FILE_A)); + + private static final TrackedFile FILE_B = + new TrackedFileStruct( + null, // tracking + FileContent.DATA, + "s3://bucket/db/table/data/file-b.parquet", + FileFormat.PARQUET, + 100, + 1024, + TRUNCATE_SPEC.specId(), + V4TestHelpers.partition(TRUNCATE_SPEC, 100), + CONTENT_STATS_B, + false, + SortOrder.unsorted().orderId(), + null, // no DV + null, // manifest info + null, // key metadata, + List.of(4L), + List.of(COLUMN_FILE_A)); + + // Test plan: + // - Append, existing, delete, replace, modify? + // - Basic cases: + // - Data file, v4 data manifest, v3 data manifest, v3 delete manifest + // - Multiple data files, mixed partition specs + // - Paths are relativized (not done yet) + // - Invalid cases that are rejected + // - Cannot write data or delete manifest to leaf + // - data file with manifest info (tested by wrappers) + // - delete file (tested by wrappers) + // - + + @Test + public void rootDataFileAppend() throws IOException { + OutputFile output = new InMemoryOutputFile("manifest.parquet"); + V4ManifestWriter writer = + V4ManifestWriter.createRoot( + EncryptionUtil.plainAsEncryptedOutput(output), + Map.of(), + STATS_TYPE, + Map.of(), + SCHEMA.schemaId(), + COMMIT_SNAPSHOT_ID, + COMMIT_SEQUENCE_NUMBER, + COMMIT_FIRST_ROW_ID); + try (writer) { + writer.append(FILE_A); + } + + InputFile in = output.toInputFile(); + for (TrackedFile file : List.of(readOne(writer, in), rawReadOne(in))) { Review Comment: The trade-off is between not duplicating the assertions and having slightly more context. I opted not to duplicate the assertions because it is important to see that the assertions are identical for the two cases. Someone reading these tests can understand that the two cases must produce the exact same metadata and that's more important than additional context for the difference between `readOne` and `rawReadOne`. -- 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]
