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_r303257069
 
 

 ##########
 File path: core/src/main/java/org/apache/iceberg/avro/AssignIds.java
 ##########
 @@ -0,0 +1,86 @@
+/*
+ * 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.base.Preconditions;
+import com.google.common.collect.Lists;
+import java.util.List;
+import java.util.Map;
+import org.apache.avro.Schema;
+
+/**
+ * Assign field ids to the Avro schema using the given field name to field id 
map
+ */
+public class AssignIds extends AvroSchemaVisitor<Schema> {
+  // Mapping constructed from table properties
+  private final Map<String, Integer> nameToId;
+  // To be used for maps and list. We do not store ids for maps and lists in 
our mapping, and it is not
+  // really required. For maps and list we assign a default id.
+  private static final int DEFAULT_ID = -1;
+
+  AssignIds(Map<String, Integer> nameToId) {
+    this.nameToId = nameToId;
+  }
+
+  @Override
+  public Schema record(Schema record, List<String> names, List<Schema> types) {
+    List<Schema.Field> fields = record.getFields();
+    int length = fields.size();
+    List<Schema.Field> newFields = Lists.newArrayListWithExpectedSize(length);
+    for (int i = 0; i < length; i += 1) {
+      newFields.add(newField(fields.get(i), types.get(i)));
+    }
+    return AvroSchemaUtil.copyRecord(record, newFields, null);
+  }
+
+  @Override
+  public Schema map(Schema map, Schema value) {
+    Schema newMap = Schema.createMap(value);
+    newMap.addProp(AvroSchemaUtil.KEY_ID_PROP, DEFAULT_ID);
+    newMap.addProp(AvroSchemaUtil.VALUE_ID_PROP, DEFAULT_ID);
 
 Review comment:
   It isn't a good idea to assign a default ID. If a field has no mapping, the 
ID should be left null so that it can't be projeted.

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