kbendick commented on a change in pull request #3435:
URL: https://github.com/apache/iceberg/pull/3435#discussion_r743940004



##########
File path: 
spark/v3.2/spark/src/test/java/org/apache/iceberg/spark/TestSparkTableImport.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.catalyst.TableIdentifier;
+import org.apache.spark.sql.functions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runners.Parameterized;
+
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+public class TestSparkTableImport extends SparkCatalogTestBase {
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+  private File fileTableDir;
+
+  public TestSparkTableImport(String catalogName, String implementation, 
Map<String, String> config) {
+    super(catalogName, implementation, config);
+  }
+
+  @Before
+  public void setupTempDirs() {
+    try {
+      fileTableDir = temp.newFolder();
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Parameterized.Parameters(name = "catalogName = {0}, implementation = {1}, 
config = {2}")
+  public static Object[][] parameters() {
+    return new Object[][] {
+        { "testhadoop", SparkCatalog.class.getName(),
+            ImmutableMap.of(
+                "type", "hadoop"
+            ) }
+    };
+  }
+
+  @Test
+  public void testImportTableForParquetPartitionedTable() throws IOException {
+    String parquetTableLocation = fileTableDir.toURI().toString();
+
+    Dataset<Row> df = spark.range(1, 2)
+        .withColumn("extra_col", functions.lit(-1))
+        .withColumn("struct", functions.expr("named_struct('nested_1', 'a', 
'nested_2', 'd', 'nested_3', 'f')"))
+        .withColumn("data", functions.lit("Z"));
+    df.coalesce(1).select("id", "extra_col", "struct", "data").write()

Review comment:
       Nit: Consider adding blank line between these two statements. It looked 
a little off to me at first.

##########
File path: 
api/src/main/java/org/apache/iceberg/types/ImportCompatibilityChecker.java
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.types;
+
+import java.util.List;
+import java.util.Locale;
+import java.util.function.Supplier;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+
+public class ImportCompatibilityChecker extends CheckCompatibility {
+
+  public ImportCompatibilityChecker(Schema schema, boolean checkOrdering, 
boolean checkNullability) {
+    super(schema, checkOrdering, checkNullability);
+  }
+
+  @Override
+  public List<String> field(Types.NestedField readField, 
Supplier<List<String>> fieldErrors) {
+    Types.StructType struct = currentType().asStructType();
+    Types.NestedField field = struct.field(readField.fieldId());
+    List<String> errors = Lists.newArrayList();
+
+    if (field == null) {
+      return NO_ERRORS;
+    }
+    setCurrentType(field.type());
+    try {
+      if (checkNullability() && field.isRequired() && readField.isOptional()) {
+        errors.add(readField.name() + " should be required, but is optional");
+      }

Review comment:
       Correct me if I'm wrong, but this is saying that if the field is marked 
as optional / nullable in the table to import, but it's marked as required / 
not-null in the file that's being imported, we'll throw an error when users 
request nullability checks.
   
   Is there benefit to being that strict? In the case that the target table 
allows the field to be optional, a file where none of the fields is null 
violates that constraint (per its data at least).
   
   Though I guess the imported file's own metadata would be incorrect in that 
case so perhaps it's not overly strict in case we wind up having to rely on the 
underlying file at some point.

##########
File path: 
spark/v3.2/spark/src/test/java/org/apache/iceberg/spark/TestSparkTableImport.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.catalyst.TableIdentifier;
+import org.apache.spark.sql.functions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runners.Parameterized;
+
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+public class TestSparkTableImport extends SparkCatalogTestBase {
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+  private File fileTableDir;
+
+  public TestSparkTableImport(String catalogName, String implementation, 
Map<String, String> config) {
+    super(catalogName, implementation, config);
+  }
+
+  @Before
+  public void setupTempDirs() {
+    try {
+      fileTableDir = temp.newFolder();
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Parameterized.Parameters(name = "catalogName = {0}, implementation = {1}, 
config = {2}")
+  public static Object[][] parameters() {
+    return new Object[][] {
+        { "testhadoop", SparkCatalog.class.getName(),
+            ImmutableMap.of(
+                "type", "hadoop"
+            ) }
+    };
+  }
+
+  @Test
+  public void testImportTableForParquetPartitionedTable() throws IOException {
+    String parquetTableLocation = fileTableDir.toURI().toString();
+
+    Dataset<Row> df = spark.range(1, 2)
+        .withColumn("extra_col", functions.lit(-1))
+        .withColumn("struct", functions.expr("named_struct('nested_1', 'a', 
'nested_2', 'd', 'nested_3', 'f')"))
+        .withColumn("data", functions.lit("Z"));
+    df.coalesce(1).select("id", "extra_col", "struct", "data").write()

Review comment:
       Nit: Consider adding blank line between these two statements. It looked 
a little off to me at first.

##########
File path: 
api/src/main/java/org/apache/iceberg/types/ImportCompatibilityChecker.java
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.types;
+
+import java.util.List;
+import java.util.Locale;
+import java.util.function.Supplier;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+
+public class ImportCompatibilityChecker extends CheckCompatibility {
+
+  public ImportCompatibilityChecker(Schema schema, boolean checkOrdering, 
boolean checkNullability) {
+    super(schema, checkOrdering, checkNullability);
+  }
+
+  @Override
+  public List<String> field(Types.NestedField readField, 
Supplier<List<String>> fieldErrors) {
+    Types.StructType struct = currentType().asStructType();
+    Types.NestedField field = struct.field(readField.fieldId());
+    List<String> errors = Lists.newArrayList();
+
+    if (field == null) {
+      return NO_ERRORS;
+    }
+    setCurrentType(field.type());
+    try {
+      if (checkNullability() && field.isRequired() && readField.isOptional()) {
+        errors.add(readField.name() + " should be required, but is optional");
+      }

Review comment:
       Correct me if I'm wrong, but this is saying that if the field is marked 
as optional / nullable in the table to import, but it's marked as required / 
not-null in the file that's being imported, we'll throw an error when users 
request nullability checks.
   
   Is there benefit to being that strict? In the case that the target table 
allows the field to be optional, a file where none of the fields is null 
violates that constraint (per its data at least).
   
   Though I guess the imported file's own metadata would be incorrect in that 
case so perhaps it's not overly strict in case we wind up having to rely on the 
underlying file at some point.




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