rdblue commented on code in PR #19030:
URL: https://github.com/apache/iceberg/pull/19030#discussion_r4648158197


##########
core/src/test/java/org/apache/iceberg/TestAppend.java:
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.IOException;
+import java.util.List;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.io.OutputFile;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.FieldSource;
+
+public class TestAppend extends AdaptiveMetadataTestBase {
+  private static final List<String> BRANCHES = List.of("main", "branch");
+
+  @Test
+  public void appendNullFile() {
+    assertThatThrownBy(() -> 
defaultTable.newAppend().appendFile(null).commit())
+        .isInstanceOf(NullPointerException.class)
+        .hasMessage("Invalid data file: null");
+  }
+
+  @Test
+  public void appendManifest() throws IOException {
+    OutputFile output =
+        
io.newOutputFile("memory:/warehouse/table/metadata/append-manifest.parquet");
+    ManifestFile manifest = writeLeafManifest(defaultTable, output, FILE_A, 
FILE_B);
+    AppendFiles append = defaultTable.newAppend().appendManifest(manifest);
+    assertThatThrownBy(append::commit)
+        .isInstanceOf(UnsupportedOperationException.class)
+        .hasMessage("Cannot append manifest files in v4");
+  }
+
+  @Test
+  public void removeDataFile() {
+    defaultTable.newAppend().appendFile(FILE_A).appendFile(FILE_B).commit();
+    MergingSnapshotProducer<?> producer = (MergeAppend) 
defaultTable.newAppend();
+    producer.delete(FILE_A);
+    assertThatThrownBy(producer::commit)
+        .isInstanceOf(UnsupportedOperationException.class)
+        .hasMessage("Cannot remove data files in v4");
+  }
+
+  @Test
+  public void addDeleteFile() {
+    defaultTable.newAppend().appendFile(FILE_A).appendFile(FILE_B).commit();
+    MergingSnapshotProducer<?> producer = (MergeAppend) 
defaultTable.newAppend();
+    producer.add(FILE_A_DV);
+    assertThatThrownBy(producer::commit)
+        .isInstanceOf(UnsupportedOperationException.class)
+        .hasMessage("Cannot add new delete files in v4");
+  }
+
+  @Test
+  public void removeDeleteFile() {
+    Table v3Table = create("upgrade_v3_table", SCHEMA, SPEC, 3);
+    v3Table.newAppend().appendFile(FILE_A).appendFile(FILE_B).commit();
+    v3Table.newRowDelta().addDeletes(FILE_A_EQ_DELETES).commit();
+    upgrade("upgrade_v3_table", 4);
+
+    MergingSnapshotProducer<?> producer = (MergeAppend) 
defaultTable.newAppend();
+    producer.delete(FILE_A_EQ_DELETES);
+    assertThatThrownBy(producer::commit)
+        .isInstanceOf(UnsupportedOperationException.class)
+        .hasMessage("Cannot remove delete files in v4");
+  }
+
+  @ParameterizedTest
+  @FieldSource("BRANCHES")
+  public void emptyTableAppend(String branch) throws IOException {
+    assertThat(readMetadata().lastSequenceNumber()).isEqualTo(0);
+
+    TableMetadata base = readMetadata();
+    assertThat(base.currentSnapshot()).isNull();
+    assertThat(base.lastSequenceNumber()).isEqualTo(0);
+
+    Snapshot committedSnapshot =
+        commit(
+            defaultTable, 
defaultTable.newAppend().appendFile(FILE_A).appendFile(FILE_B), branch);
+
+    assertThat(committedSnapshot).isNotNull();
+    assertThat(defaultTable.ops().current().lastSequenceNumber()).isEqualTo(1);
+
+    long snapshotId = committedSnapshot.snapshotId();
+
+    List<TrackedFile> rootFiles = readRoot(defaultTable, committedSnapshot);
+    validateNoLeafManifests(rootFiles);
+    validateFiles(
+        rootFiles,
+        dataSeqs(1L, 1L),
+        fileSeqs(1L, 1L),
+        ids(snapshotId, snapshotId),
+        locations(FILE_A, FILE_B),
+        statuses(EntryStatus.ADDED, EntryStatus.ADDED));
+
+    validateBranchFiles(defaultTable, branch, FILE_A, FILE_B);
+  }
+
+  @Test
+  public void appendWithDifferentSpecs() throws IOException {
+    assertThat(readMetadata().lastSequenceNumber()).isEqualTo(0);
+
+    TableMetadata base = readMetadata();
+    assertThat(base.currentSnapshot()).as("Should not have a current 
snapshot").isNull();
+    assertThat(base.lastSequenceNumber()).as("Last sequence number should be 
0").isEqualTo(0);
+
+    defaultTable.updateSpec().addField("id").commit();
+    PartitionSpec newSpec = defaultTable.spec();
+
+    assertThat(defaultTable.specs()).as("Table should have 2 
specs").hasSize(2);
+
+    DataFile fileNewSpec =
+        DataFiles.builder(newSpec)
+            
.withPath("memory:/warehouse/table/data/data_bucket=0/id=0/data-b.parquet")
+            .withPartition(TestHelpers.Row.of(0, 0))
+            .withFileSizeInBytes(10)
+            .withRecordCount(1)
+            .build();
+
+    
defaultTable.newAppend().appendFile(FILE_A).appendFile(fileNewSpec).commit();
+    Snapshot committedSnapshot = defaultTable.currentSnapshot();
+
+    assertThat(committedSnapshot).as("Should create a snapshot").isNotNull();
+    assertThat(defaultTable.ops().current().lastSequenceNumber()).isEqualTo(1);
+
+    long snapshotId = committedSnapshot.snapshotId();
+
+    List<TrackedFile> rootFiles = readRoot(defaultTable, committedSnapshot);
+    validateNoLeafManifests(rootFiles);
+    validateFiles(
+        rootFiles,
+        dataSeqs(1L, 1L),
+        fileSeqs(1L, 1L),
+        ids(snapshotId, snapshotId),
+        locations(FILE_A, fileNewSpec),
+        statuses(EntryStatus.ADDED, EntryStatus.ADDED));
+  }
+
+  @Test
+  public void concurrentAppends() throws IOException {
+    defaultTable.updateProperties().set("random-snapshot-ids", "true");
+    AppendFiles append = defaultTable.newAppend().appendFile(FILE_A);
+    Snapshot pending = append.apply();
+    assertThat(defaultTable.currentSnapshot()).isNull();
+    assertThat(pending).isNotNull();
+    assertThat(pending.sequenceNumber()).isEqualTo(1);
+    validateFiles(
+        readRoot(defaultTable, pending),
+        dataSeqs(1L),
+        fileSeqs(1L),
+        ids(pending.snapshotId()),
+        locations(FILE_A),
+        statuses(EntryStatus.ADDED));
+
+    // use a separate table instance to test recovery
+    Table separateInstance = load("test");
+    separateInstance.newAppend().appendFile(FILE_B).commit();
+    Snapshot concurrent = separateInstance.currentSnapshot();
+    assertThat(concurrent).isNotNull();
+    assertThat(concurrent.sequenceNumber()).isEqualTo(1);
+    validateFiles(
+        readRoot(defaultTable, concurrent),
+        dataSeqs(1L),
+        fileSeqs(1L),
+        ids(concurrent.snapshotId()),
+        locations(FILE_B),
+        statuses(EntryStatus.ADDED));
+
+    assertThat(defaultTable.currentSnapshot()).as("Original Table is 
unchanged").isNull();
+    assertThat(listRootManifests(defaultTable))
+        .containsExactlyInAnyOrder(
+            pending.rootManifestLocation(), concurrent.rootManifestLocation());
+
+    append.commit();
+    Snapshot latest = defaultTable.currentSnapshot();
+    
assertThat(latest.rootManifestLocation()).isNotEqualTo(pending.rootManifestLocation());
+    assertThat(latest.snapshotId()).isEqualTo(pending.snapshotId());
+    assertThat(latest.parentId()).isEqualTo(concurrent.snapshotId());
+    assertThat(latest.sequenceNumber()).isEqualTo(2);
+    validateFiles(
+        readRoot(defaultTable, latest),
+        dataSeqs(2L, 1L),
+        fileSeqs(2L, 1L),
+        ids(pending.snapshotId(), concurrent.snapshotId()),
+        locations(FILE_A, FILE_B),
+        statuses(EntryStatus.ADDED, EntryStatus.EXISTING));
+
+    assertThat(listRootManifests(defaultTable))
+        .containsExactlyInAnyOrder(
+            latest.rootManifestLocation(), concurrent.rootManifestLocation());
+  }
+
+  @Test
+  public void appendToUpgradedTable() throws IOException {
+    Table v3Table = create("upgraded_table", SCHEMA, SPEC, 3);
+    v3Table.newAppend().appendFile(FILE_A).commit();
+    Snapshot v3Snapshot = v3Table.currentSnapshot();
+    long v3SnapshotId = v3Snapshot.snapshotId();
+
+    assertThat(v3Snapshot.allManifests(io)).hasSize(1);
+    ManifestFile v3Manifest = v3Snapshot.allManifests(io).get(0);
+
+    Table upgraded = upgrade("upgraded_table", 4);
+    upgraded.newAppend().appendFile(FILE_B).commit();
+    Snapshot snapshot = upgraded.currentSnapshot();
+    long snapshotId = snapshot.snapshotId();
+
+    List<TrackedFile> rootFiles = readRoot(defaultTable, snapshot);

Review Comment:
   Yes. The table metadata is nearly identical, which is why it works.



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