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 2019/10/12 18:29:20 UTC

[GitHub] [incubator-iceberg] andrei-ionescu commented on a change in pull request #514: Fix for cannot update an Iceberg dataset from a Parquet file (#510)

andrei-ionescu commented on a change in pull request #514: Fix for cannot update an Iceberg dataset from a Parquet file (#510)
URL: https://github.com/apache/incubator-iceberg/pull/514#discussion_r334248649
 
 

 ##########
 File path: spark/src/test/java/org/apache/iceberg/spark/source/TestNullableParquet.java
 ##########
 @@ -0,0 +1,133 @@
+/*
+ * 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.spark.source;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.hadoop.HadoopTables;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.api.java.JavaSparkContext;
+import org.apache.spark.sql.SaveMode;
+import org.apache.spark.sql.SparkSession;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class TestNullableParquet {
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  private String sourcePath;
+  private String targetPath;
+
+  private Map<String, String> tableProperties;
+
+  private SparkSession sparkSession;
+
+  private org.apache.spark.sql.types.StructType sparkSchema = new org.apache.spark.sql.types.StructType(
+      new org.apache.spark.sql.types.StructField[] {
+          new org.apache.spark.sql.types.StructField(
+              "optionalField",
+              org.apache.spark.sql.types.DataTypes.StringType,
+              true,
+              org.apache.spark.sql.types.Metadata.empty()),
+          new org.apache.spark.sql.types.StructField(
+              "requiredField",
+              org.apache.spark.sql.types.DataTypes.StringType,
+              false,
+              org.apache.spark.sql.types.Metadata.empty())
+      });
+
+  private Schema icebergSchema = new Schema(
+      Types.NestedField.optional(1, "optionalField", Types.StringType.get()),
+      Types.NestedField.required(2, "requiredField", Types.StringType.get()));
+
+  private List<String> data0 = Arrays.asList(
+      "{\"optionalField\": \"a1\", \"requiredField\": \"bid_001\"}",
+      "{\"optionalField\": \"a2\", \"requiredField\": \"bid_002\"}");
+  private List<String> data1 = Arrays.asList(
+      "{\"optionalField\": \"d1\", \"requiredField\": \"bid_101\"}",
+      "{\"optionalField\": \"d2\", \"requiredField\": \"bid_102\"}",
+      "{\"optionalField\": \"d3\", \"requiredField\": \"bid_103\"}",
+      "{\"optionalField\": \"d4\", \"requiredField\": \"bid_104\"}");
+
+  @Test
+  public void testNullablePoC() throws IOException {
+
+    // setup
+    File location = new File(temp.newFolder("parquet"), "test");
+    sourcePath = String.format("%s/nullable_poc/sourceFolder/", location.toString());
+    targetPath = String.format("%s/nullable_poc/targetFolder/", location.toString());
+    tableProperties = Collections.singletonMap(TableProperties.WRITE_NEW_DATA_LOCATION, targetPath);
+
+    sparkSession = SparkSession.builder()
+        .master("local[2]")
+        .appName("NullablePoC")
+        .getOrCreate();
+
+    // create base parquet
+    // we will read this and append to iceberg dataset
+    sparkSession
+        .read().schema(sparkSchema).json(
+        JavaSparkContext.fromSparkContext(sparkSession.sparkContext()).parallelize(data1))
+        .write().parquet(sourcePath);
+
+    // create base iceberg table
+    // this our iceberg dataset to which we will append data
+    new HadoopTables(sparkSession.sparkContext().hadoopConfiguration())
+        .create(
+            icebergSchema,
+            PartitionSpec.builderFor(icebergSchema).identity("requiredField").build(),
+            tableProperties,
+            targetPath);
+
+    // write base iceberg data
+    // this is the initial data inside the iceberg dataset
+    sparkSession
+        .read().schema(sparkSchema).json(
+        JavaSparkContext.fromSparkContext(sparkSession.sparkContext()).parallelize(data0))
+        .write().format("iceberg").mode(SaveMode.Append).save(targetPath);
+
+    // read from parquet and append to iceberg
+    // this ends with:
+    //
+    //     Cannot write incompatible dataset to table with schema
+    //     table {
+    //       1: optionalField: optional string
+    //       2: requiredField: required string
+    //     }
+    //     Problems:
+    //     * requiredField should be required, but is optional
+    //
 
 Review comment:
   👍 I will remove the comments.

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


With regards,
Apache Git Services

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