pvary commented on a change in pull request #3912:
URL: https://github.com/apache/iceberg/pull/3912#discussion_r790622663



##########
File path: 
hive-metastore/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -75,7 +83,42 @@ public static Schema convert(List<FieldSchema> fieldSchemas, 
boolean autoConvert
       typeInfos.add(TypeInfoUtils.getTypeInfoFromTypeString(col.getType()));
       comments.add(col.getComment());
     }
-    return HiveSchemaConverter.convert(names, typeInfos, comments, 
autoConvert);
+    Schema schema = HiveSchemaConverter.convert(names, typeInfos, comments, 
autoConvert);
+    return rebuildSchemaWithIdentifierFieldIds(schema, identifierFieldNames);
+  }
+
+  /**
+   * Rebuild a schema with given schema and identifierFieldNames
+   * @param schema The origin schema.
+   * @param identifierFieldNames The identifierFieldNames.
+   * @return New schema with IdentifierFieldIds.
+   */
+  public static Schema rebuildSchemaWithIdentifierFieldIds(Schema schema, 
Set<String> identifierFieldNames) {
+    // Identifier fields in nested field are not supported, so we just check 
the first level columns.
+    Map<String, Types.NestedField> columnsMap = schema.columns().stream()
+            .collect(Collectors.toMap(Types.NestedField::name, field -> 
field));
+    Set<Integer> identifierFieldIds = identifierFieldNames.stream()
+            .map(name -> {
+              Types.NestedField field = columnsMap.get(name);
+              if (field == null) {
+                // Does not exist or in nested field.
+                throw new IllegalArgumentException(
+                        String.format("Cannot add field `%s` as an identifier 
field: " +
+                                "must not in nested field and exist", name));
+              }
+              if (field.type().isNestedType()) {
+                // Field is nested.
+                throw new IllegalArgumentException(
+                        String.format("Cannot add field `%s` as an identifier 
field: " +

Review comment:
       I am not a native English speaker, but maybe this is better:
   ```
   Cannot set field `%s` as an identifier field: only primitive fields are 
allowed
   ```
   Or something better 😄




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