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_r337270530
 
 

 ##########
 File path: core/src/test/java/org/apache/iceberg/avro/TestAvroNameMapping.java
 ##########
 @@ -0,0 +1,257 @@
+/*
+ * 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.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.io.DatumWriter;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.Files;
+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.Types;
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.apache.avro.generic.GenericData.Record;
+
+public class TestAvroNameMapping extends TestAvroReadProjection {
+  @Test
+  public void testMapProjections() throws IOException {
+    Schema writeSchema = new Schema(
+        Types.NestedField.required(0, "id", Types.LongType.get()),
+        Types.NestedField.optional(5, "location", Types.MapType.ofOptional(6, 
7,
+            Types.StringType.get(),
+            Types.StructType.of(
+                Types.NestedField.required(1, "lat", Types.FloatType.get()),
+                Types.NestedField.optional(2, "long", Types.FloatType.get())
+            )
+        )));
+
+    Record record = new Record(AvroSchemaUtil.convert(writeSchema, "table"));
+    record.put("id", 34L);
+    Record location = new Record(AvroSchemaUtil.fromOption(
+        
AvroSchemaUtil.fromOption(record.getSchema().getField("location").schema())
+            .getValueType()));
+    location.put("lat", 52.995143f);
+    location.put("long", -1.539054f);
+    record.put("location", ImmutableMap.of("l1", location));
+
+    // Table mapping does not project `location` map
+    NameMapping nameMapping = MappingUtil.create(new Schema(
+        Types.NestedField.required(0, "id", Types.LongType.get())));
+
+    Schema readSchema = writeSchema;
+
+    Record projected = writeAndRead(writeSchema, readSchema, record, 
nameMapping);
+    // field id 5 comes from read schema
+    Assert.assertNotNull("Field missing from table mapping is renamed", 
projected.getSchema().getField("location_r5"));
+    Assert.assertNull("location field should not be read", 
projected.get("location_r5"));
+    Assert.assertEquals(34L, projected.get("id"));
+
+    // Table mapping partially project `location` map value
+    nameMapping = MappingUtil.create(new Schema(
+        Types.NestedField.required(0, "id", Types.LongType.get()),
+        Types.NestedField.optional(5, "location", Types.MapType.ofOptional(6, 
7,
+            Types.StringType.get(),
+            Types.StructType.of(
+                Types.NestedField.required(1, "lat", 
Types.FloatType.get()))))));
+
+    projected = writeAndRead(writeSchema, readSchema, record, nameMapping);
+    Record projectedL1 = ((Map<String, Record>) 
projected.get("location")).get("l1");
+    Assert.assertNotNull("Field missing from table mapping is renamed", 
projectedL1.getSchema().getField("long_r2"));
+    Assert.assertNull("location.value.long, should not be read", 
projectedL1.get("long_r2"));
+  }
+
+  @Test
+  public void testMissingRequiredFields() {
+    Schema writeSchema = new Schema(
+        Types.NestedField.required(19, "x", Types.IntegerType.get()),
+        Types.NestedField.optional(18, "y", Types.IntegerType.get()));
+
+    Record record = new Record(AvroSchemaUtil.convert(writeSchema, "table"));
+    record.put("x", 1);
+    record.put("y", 2);
+
+    // table mapping not projecting a required field 'x'
+    NameMapping nameMapping = MappingUtil.create(new Schema(
+        Types.NestedField.optional(18, "y", Types.IntegerType.get())));
+
+
+    Schema readSchema = writeSchema;
+    AssertHelpers.assertThrows("Missing required field in nameMapping",
+        IllegalArgumentException.class, "Missing required field: x",
+        // In this case, pruneColumns result is an empty record
+        () -> writeAndRead(writeSchema, readSchema, record, nameMapping));
+  }
+
+  @Test
+  public void testArrayProjections() throws Exception {
+    Schema writeSchema = new Schema(
+        Types.NestedField.required(0, "id", Types.LongType.get()),
+        Types.NestedField.optional(22, "point",
+            Types.ListType.ofOptional(21, Types.StructType.of(
+                Types.NestedField.required(19, "x", Types.IntegerType.get()),
+                Types.NestedField.optional(18, "y", Types.IntegerType.get())
+            ))
+        )
+    );
+
+    Record record = new Record(AvroSchemaUtil.convert(writeSchema, "table"));
+    record.put("id", 34L);
+    Record pointRecord = new Record(AvroSchemaUtil.fromOption(
+        
AvroSchemaUtil.fromOption(record.getSchema().getField("point").schema()).getElementType()));
+    pointRecord.put("x", 1);
+    pointRecord.put("y", 2);
+    record.put("point", ImmutableList.of(pointRecord));
+
+    NameMapping nameMapping = MappingUtil.create(new Schema(
+        // Optional array field missing.
+        Types.NestedField.required(0, "id", Types.LongType.get())));
+
+    Schema readSchema = new Schema(
 
 Review comment:
   Isn't this the write schema?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to