rdblue commented on code in PR #10678:
URL: https://github.com/apache/iceberg/pull/10678#discussion_r1707681345


##########
api/src/main/java/org/apache/iceberg/types/TypeUtil.java:
##########
@@ -181,11 +181,34 @@ public static Map<Integer, String> indexQuotedNameById(
     return indexer.byId();
   }
 
+  /**
+   * Creates a mapping from lower-case field names to their corresponding 
field IDs.
+   *
+   * <p>This method iterates over the fields of the provided struct and maps 
each field's name
+   * (converted to lower-case) to its ID. If two fields have the same 
lower-case name, an
+   * `IllegalArgumentException` is thrown.
+   *
+   * @param struct the struct type whose fields are to be indexed
+   * @return a map where the keys are lower-case field names and the values 
are field IDs
+   * @throws IllegalArgumentException if two fields have the same lower-case 
name
+   */
   public static Map<String, Integer> indexByLowerCaseName(Types.StructType 
struct) {
     Map<String, Integer> indexByLowerCaseName = Maps.newHashMap();
     indexByName(struct)
         .forEach(
-            (name, integer) -> 
indexByLowerCaseName.put(name.toLowerCase(Locale.ROOT), integer));
+            (name, integer) -> {
+              String key = name.toLowerCase(Locale.ROOT);
+              Integer prevValue = indexByLowerCaseName.get(key);
+              if (prevValue != null) {
+                Types.NestedField field1 = struct.field(prevValue);
+                Types.NestedField field2 = struct.field(integer);

Review Comment:
   In a struct, `field` returns a child field. But the index this `forEach` is 
traversing is the index of all fields, so there will be IDs that are not 
returned by `field`, resulting in a `NullPointerException` when formatting the 
exception message.
   
   The fix is to use `IndexByName` directly and get both the `byName()` and 
`byId()` maps:
   
   ```java
     IndexByName indexer = new IndexByName();
     visit(struct, indexer);
     Map<String, Integer> byName = indexer.byName();
     Map<Integer, String> byId = indexer.byId();
     ...
   ```



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to