aokolnychyi commented on a change in pull request #2362:
URL: https://github.com/apache/iceberg/pull/2362#discussion_r601061225



##########
File path: spark/src/test/java/org/apache/iceberg/TestTableSerialization.java
##########
@@ -0,0 +1,221 @@
+/*
+ * 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 com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Map;
+import org.apache.iceberg.hadoop.HadoopTables;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.spark.SparkUtil;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.SparkConf;
+import org.apache.spark.serializer.KryoSerializer;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.apache.iceberg.types.Types.NestedField.optional;
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+public class TestTableSerialization {
+
+  private static final HadoopTables TABLES = new HadoopTables();
+
+  private static final Schema SCHEMA = new Schema(
+      required(1, "id", Types.LongType.get()),
+      optional(2, "data", Types.StringType.get()),
+      required(3, "date", Types.StringType.get()),
+      optional(4, "double", Types.DoubleType.get()));
+
+  private static final PartitionSpec SPEC = PartitionSpec
+      .builderFor(SCHEMA)
+      .identity("date")
+      .build();
+
+  private static final SortOrder SORT_ORDER = SortOrder.builderFor(SCHEMA)
+      .asc("id")
+      .build();
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+  private Table table;
+
+  @Before
+  public void initTable() throws IOException {
+    Map<String, String> props = ImmutableMap.of("k1", "v1", "k2", "v2");
+
+    File tableLocation = temp.newFolder();
+    Assert.assertTrue(tableLocation.delete());
+
+    this.table = TABLES.create(SCHEMA, SPEC, SORT_ORDER, props, 
tableLocation.toString());
+  }
+
+  @Test
+  public void testTableJavaSerialization() throws IOException, 
ClassNotFoundException {
+    File data = temp.newFile();
+    Assert.assertTrue(data.delete());
+
+    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+    try (ObjectOutputStream out = new ObjectOutputStream(bytes)) {
+      out.writeObject(table);
+    }
+
+    try (ObjectInputStream in = new ObjectInputStream(new 
ByteArrayInputStream(bytes.toByteArray()))) {
+      Object obj = in.readObject();
+      Assert.assertTrue("Should be a Table", obj instanceof Table);
+      checkTable(table, (Table) obj);
+    }
+  }
+
+  @Test
+  public void testSerializedTableKryoSerialization() throws IOException {
+    File data = temp.newFile();
+    Assert.assertTrue(data.delete());
+
+    Kryo kryo = new KryoSerializer(new SparkConf()).newKryo();
+
+    Table serializedTable = SparkUtil.toSerializedTable(table);
+    checkTable(table, serializedTable);
+
+    try (Output out = new Output(new FileOutputStream(data))) {
+      kryo.writeClassAndObject(out, serializedTable);
+    }
+
+    try (Input in = new Input(new FileInputStream(data))) {
+      Object obj = kryo.readClassAndObject(in);
+      Assert.assertTrue("Should be a Table", obj instanceof Table);
+      checkTable(table, (Table) obj);
+    }
+  }
+
+  @Test
+  public void testSerializedTableJavaSerialization() throws IOException, 
ClassNotFoundException {
+    File data = temp.newFile();
+    Assert.assertTrue(data.delete());
+
+    Table serializedTable = SparkUtil.toSerializedTable(table);
+    checkTable(table, serializedTable);
+
+    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+    try (ObjectOutputStream out = new ObjectOutputStream(bytes)) {
+      out.writeObject(serializedTable);
+    }
+
+    try (ObjectInputStream in = new ObjectInputStream(new 
ByteArrayInputStream(bytes.toByteArray()))) {
+      Object obj = in.readObject();
+      Assert.assertTrue("Should be a Table", obj instanceof Table);
+      checkTable(table, (Table) obj);
+    }
+  }
+
+  @Test
+  public void testMetadataTableJavaSerialization() throws IOException, 
ClassNotFoundException {
+    for (MetadataTableType type : MetadataTableType.values()) {
+      File data = temp.newFile();
+      Assert.assertTrue(data.delete());
+
+      TableOperations ops = ((HasTableOperations) table).operations();
+      Table metadataTable = 
MetadataTableUtils.createMetadataTableInstance(ops, table.name(), "meta", type);
+
+      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+      try (ObjectOutputStream out = new ObjectOutputStream(bytes)) {
+        out.writeObject(metadataTable);
+      }
+
+      try (ObjectInputStream in = new ObjectInputStream(new 
ByteArrayInputStream(bytes.toByteArray()))) {
+        Object obj = in.readObject();
+        Assert.assertTrue("Should be a Table", obj instanceof Table);
+        checkTable(metadataTable, (Table) obj);
+      }
+    }
+  }
+
+  @Test
+  public void testSerializedMetadataTableKryoSerialization() throws 
IOException {
+    for (MetadataTableType type : MetadataTableType.values()) {
+      File data = temp.newFile();
+      Assert.assertTrue(data.delete());
+
+      Kryo kryo = new KryoSerializer(new SparkConf()).newKryo();
+
+      TableOperations ops = ((HasTableOperations) table).operations();
+      Table metadataTable = 
MetadataTableUtils.createMetadataTableInstance(ops, table.name(), "meta", type);
+      Table serializedMetadataTable = 
SparkUtil.toSerializedTable(metadataTable);
+      checkTable(metadataTable, serializedMetadataTable);
+
+      try (Output out = new Output(new FileOutputStream(data))) {
+        kryo.writeClassAndObject(out, serializedMetadataTable);
+      }
+
+      try (Input in = new Input(new FileInputStream(data))) {
+        Object obj = kryo.readClassAndObject(in);
+        Assert.assertTrue("Should be a Table", obj instanceof Table);
+        checkTable(metadataTable, (Table) obj);
+      }
+    }
+  }
+
+  @Test
+  public void testSerializedMetadataTableJavaSerialization() throws 
IOException, ClassNotFoundException {
+    for (MetadataTableType type : MetadataTableType.values()) {
+      File data = temp.newFile();
+      Assert.assertTrue(data.delete());
+
+      TableOperations ops = ((HasTableOperations) table).operations();
+      Table metadataTable = 
MetadataTableUtils.createMetadataTableInstance(ops, table.name(), "meta", type);
+      Table serializedMetadataTable = 
SparkUtil.toSerializedTable(metadataTable);
+      checkTable(metadataTable, serializedMetadataTable);
+
+      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+      try (ObjectOutputStream out = new ObjectOutputStream(bytes)) {
+        out.writeObject(serializedMetadataTable);
+      }
+
+      try (ObjectInputStream in = new ObjectInputStream(new 
ByteArrayInputStream(bytes.toByteArray()))) {
+        Object obj = in.readObject();
+        Assert.assertTrue("Should be a Table", obj instanceof Table);
+        checkTable(metadataTable, (Table) obj);
+      }
+    }
+  }
+
+  private void checkTable(Table expected, Table actual) {
+    Assert.assertEquals("Name must match", expected.name(), actual.name());
+    Assert.assertEquals("Location must match", expected.location(), 
actual.location());
+    Assert.assertEquals("Props must match", expected.properties(), 
actual.properties());
+    Assert.assertEquals("Schema must match", expected.schema().asStruct(), 
actual.schema().asStruct());

Review comment:
       This will now fail, unfortunately. The way we compare struct fields is 
broken with Kryo serialization. We rely on the `PrimitiveHolder` proxy but Kryo 
ignores that.




-- 
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to