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/13 02:39:32 UTC

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

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



##########
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:
       yes, we couldn't get the past schema/id for old snapshots, so I think this is the time when we need to go back to your PR's change to figure out the correct id. 

##########
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:
       sounds good, will remove and create a separate PR for that.

##########
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:
       Great suggestion, this makes the code in this class much easier to read; thank you! 




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