rdblue commented on a change in pull request #207: Add external schema mappings 
for files written with name-based schemas #40
URL: https://github.com/apache/incubator-iceberg/pull/207#discussion_r334087328
 
 

 ##########
 File path: core/src/test/java/org/apache/iceberg/avro/TestAvroNameMapping.java
 ##########
 @@ -0,0 +1,261 @@
+/*
+ * 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.avro;
+
+import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Set;
+import org.apache.avro.SchemaNormalization;
+import org.apache.avro.generic.GenericData;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.mapping.MappedField;
+import org.apache.iceberg.mapping.MappedFields;
+import org.apache.iceberg.mapping.MappingUtil;
+import org.apache.iceberg.mapping.NameMapping;
+import org.apache.iceberg.types.TypeUtil;
+import org.apache.iceberg.types.Types;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestAvroNameMapping extends TestAvroReadProjection {
+  @Override
+  protected GenericData.Record writeAndRead(String desc,
+                                            Schema writeSchema,
+                                            Schema readSchema,
+                                            GenericData.Record inputRecord) 
throws IOException {
+
+    // Use all existing TestAvroReadProjection tests to verify that
+    // we get the same projected (Avro) read schema whether we use
+    // NameMapping together with file schema without field-ids or we
+    // use a file schema having field-ids
+    GenericData.Record record = super.writeAndRead(desc, writeSchema, 
readSchema, inputRecord);
+    org.apache.avro.Schema expected = record.getSchema();
+    org.apache.avro.Schema actual = projectWithNameMapping(writeSchema, 
readSchema, MappingUtil.create(writeSchema));
+    Assert.assertEquals(expected, actual);
+    return record;
+  }
+
+  @Test
+  public void testNameMappingProjections() {
+    Schema fileSchema = new Schema(
+        Types.NestedField.required(0, "id", Types.LongType.get()),
+        Types.NestedField.optional(5, "locations", Types.MapType.ofOptional(6, 
7,
+            Types.StringType.get(),
+            Types.StructType.of(
+                Types.NestedField.required(1, "lat", Types.FloatType.get()),
+                Types.NestedField.required(2, "long", Types.FloatType.get())
+            )
+        )));
+
+    // Table mapping does not project `locations` map
+    NameMapping nameMapping = MappingUtil.create(new Schema(
+        Types.NestedField.required(0, "id", Types.LongType.get())));
+
+    // Table read schema projects `locations` map's value
+    Schema readSchema = new Schema(
+        Types.NestedField.required(0, "id", Types.LongType.get()),
+        Types.NestedField.optional(5, "locations", Types.MapType.ofOptional(6, 
7,
+            Types.StringType.get(),
+            Types.StructType.of(
+                Types.NestedField.required(2, "long", Types.FloatType.get())
+            )
+        )));
+
+    check(fileSchema, readSchema, nameMapping);
 
 Review comment:
   Thanks! I thought you were saying that the schema with "locations" was 
produced by pruning. My mistake. It looks like this is a problem with how the 
new pruning interacts with `BuildAvroProjection`.
   
   `BuildAvroProjection` assumes that if a column is missing from the file 
schema that is passed in, it is missing from the actual file schema, but in 
this case it isn't. To build the projection, it adds the missing field with the 
expectation that the resolving Avro decoder won't read data for that column and 
it will be filled in with nulls. That's not going to happen because the 
resolving decoder will match by name.
   
   I think all we need to do is catch this case in `BuildAvroProjection` and 
change the field name to something that won't match in the actual file schema. 
Then it will have nulls filled in automatically.
   
   I think this is a good demonstration of why we need to test these cases with 
a full round-trip to a data file and back.

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


With regards,
Apache Git Services

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

Reply via email to