You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2021/03/08 19:38:37 UTC

[GitHub] [iceberg] wypoon commented on a change in pull request #2275: Core: add schema id to snapshot and history entry

wypoon commented on a change in pull request #2275:
URL: https://github.com/apache/iceberg/pull/2275#discussion_r589689539



##########
File path: core/src/main/java/org/apache/iceberg/util/SnapshotUtil.java
##########
@@ -22,13 +22,21 @@
 import java.util.List;
 import java.util.function.Function;
 import org.apache.iceberg.DataFile;
+import org.apache.iceberg.HistoryEntry;
+import org.apache.iceberg.Schema;
 import org.apache.iceberg.Snapshot;
 import org.apache.iceberg.Table;
 import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
 import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
 import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class SnapshotUtil {

Review comment:
       I don't see the static methods added here, `snapshotIdFromTime` and `schemaOfSnapshot`, used anywhere in this change. If that is the case, this file can be excluded from this particular PR.

##########
File path: core/src/test/java/org/apache/iceberg/TestSchemaID.java
##########
@@ -0,0 +1,164 @@
+/*
+ * 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 java.util.List;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.apache.iceberg.TestHelpers.assertSameSchemaMap;
+import static org.apache.iceberg.types.Types.NestedField.optional;
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+@RunWith(Parameterized.class)
+public class TestSchemaID extends TableTestBase {
+
+  @Parameterized.Parameters(name = "formatVersion = {0}")
+  public static Object[] parameters() {
+    return new Object[] { 1, 2 };
+  }
+
+  public TestSchemaID(int formatVersion) {
+    super(formatVersion);
+  }
+
+  @Test
+  public void testNoChange() {
+    // add files to table
+    table.newAppend().appendFile(FILE_A).appendFile(FILE_B).commit();
+
+    validateSingleSchema();
+    validateSnapshotsAndHistoryEntries(1);
+
+    // remove file from table
+    table.newDelete().deleteFile(FILE_A).commit();
+
+    validateSingleSchema();
+    validateSnapshotsAndHistoryEntries(2);
+
+    // add file to table
+    table.newFastAppend().appendFile(FILE_A2).commit();
+
+    validateSingleSchema();
+    validateSnapshotsAndHistoryEntries(3);
+  }
+
+  @Test
+  public void testSchemaIdChangeInSchemaUpdate() {
+    // add files to table
+    table.newAppend().appendFile(FILE_A).appendFile(FILE_B).commit();
+
+    validateSingleSchema();
+    validateSnapshotsAndHistoryEntries(1);
+
+    // cache old schema
+    Schema oldSchema = table.schema();
+
+    // update schema
+    table.updateSchema().addColumn("data2", Types.StringType.get()).commit();
+
+    Schema updatedSchema = new Schema(1,
+        required(1, "id", Types.IntegerType.get()),
+        required(2, "data", Types.StringType.get()),
+        optional(3, "data2", Types.StringType.get())
+    );
+
+    validateTwoSchemas(updatedSchema, oldSchema);
+    validateSnapshotsAndHistoryEntries(1);
+
+    // remove file from table
+    table.newDelete().deleteFile(FILE_A).commit();
+
+    validateTwoSchemas(updatedSchema, oldSchema);
+
+    List<Snapshot> snapshots = Lists.newArrayList(table.snapshots());
+    Assert.assertEquals("Number of snapshot should match",
+        2, snapshots.size());
+    Assert.assertEquals("First schema id within snapshots should match",
+        Integer.valueOf(0), snapshots.get(0).schemaId());
+    Assert.assertEquals("Second schema id within snapshots should match",
+        Integer.valueOf(1), snapshots.get(1).schemaId());
+
+    Assert.assertEquals("Number of history entries should match",
+        2, table.history().size());
+    Assert.assertEquals("First history entry id within snapshots should match",
+        Integer.valueOf(0), table.history().get(0).schemaId());
+    Assert.assertEquals("Second history entry id within snapshots should match",
+        Integer.valueOf(1), table.history().get(1).schemaId());
+
+    // add files to table
+    table.newAppend().appendFile(FILE_A2).commit();
+
+    validateTwoSchemas(updatedSchema, oldSchema);
+
+    snapshots = Lists.newArrayList(table.snapshots());
+    Assert.assertEquals("Number of snapshot should match",
+        3, snapshots.size());
+    Assert.assertEquals("First schema id within snapshots should match",
+        Integer.valueOf(0), snapshots.get(0).schemaId());
+    Assert.assertEquals("Second schema id within snapshots should match",
+        Integer.valueOf(1), snapshots.get(1).schemaId());
+    Assert.assertEquals("Third schema id within snapshots should match",
+        Integer.valueOf(1), snapshots.get(2).schemaId());
+
+    Assert.assertEquals("Number of history entries should match",
+        3, table.history().size());
+    Assert.assertEquals("First history entry id within snapshots should match",
+        Integer.valueOf(0), table.history().get(0).schemaId());
+    Assert.assertEquals("Second history entry id within snapshots should match",
+        Integer.valueOf(1), table.history().get(1).schemaId());
+    Assert.assertEquals("Third history entry id within snapshots should match",
+        Integer.valueOf(1), table.history().get(2).schemaId());
+  }
+
+  private void validateSingleSchema() {
+    Assert.assertEquals("Current schema ID should match",
+        0, table.schema().schemaId());
+    assertSameSchemaMap(ImmutableMap.of(0, table.schema()), table.schemas());
+  }
+
+  private void validateTwoSchemas(Schema updatedSchema, Schema oldSchema) {
+    Assert.assertEquals("Current schema ID should match",
+        1, table.schema().schemaId());
+    Assert.assertEquals("Current schema should match",
+        updatedSchema.asStruct(), table.schema().asStruct());
+    assertSameSchemaMap(ImmutableMap.of(0, oldSchema, 1, updatedSchema), table.schemas());
+  }
+
+  private void validateSnapshotsAndHistoryEntries(int numElement) {

Review comment:
       You could have this helper method take a `List<Integer>` containing the schema ids instead of just an int (number of entries). Then `testSchemaIdChangeInSchemaUpdate` would be able to use this helper method for the cases after updating the schema. `snapshots.size()` should then match the `List`'s size, and the `snapshot.schemaId()` in each snapshot in `snapshots` should match the corresponding `Integer` in the `List`.

##########
File path: core/src/test/resources/TableMetadataV2Valid.json
##########
@@ -81,8 +81,39 @@
     }
   ],
   "properties": {},
-  "current-snapshot-id": -1,
-  "snapshots": [],
-  "snapshot-log": [],
+  "current-snapshot-id": 3055729675574597004,
+  "snapshots": [
+    {
+      "snapshot-id": 3051729675574597004,
+      "timestamp-ms": 1515100955770,
+      "sequence-number": 0,
+      "summary": {
+        "operation": "append"
+      },
+      "manifest-list": "s3://a/b/1.avro"
+    },
+    {
+      "snapshot-id": 3055729675574597004,
+      "parent-snapshot-id": 3051729675574597004,
+      "timestamp-ms": 1555100955770,
+      "sequence-number": 1,
+      "summary": {
+        "operation": "append"
+      },
+      "manifest-list": "s3://a/b/2.avro",
+      "schema-id": 1
+    }
+  ],
+  "snapshot-log": [
+    {
+      "snapshot-id": 3051729675574597004,
+      "timestamp-ms": 1515100955770
+    },

Review comment:
       This suggests to me that not all snapshots in the `snapshot-log` need to have a `schema-id`. Is this what happens if the snapshot was written when the format was v1?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org