rdblue commented on a change in pull request #2936:
URL: https://github.com/apache/iceberg/pull/2936#discussion_r685547192



##########
File path: core/src/main/java/org/apache/iceberg/Partitioning.java
##########
@@ -177,4 +184,42 @@ public Void alwaysNull(int fieldId, String sourceName, int 
sourceId) {
       return null;
     }
   }
+
+  /**
+   * Builds a common partition type for all specs in a table.
+   * <p>
+   * Whenever a table has multiple specs, the partition type is a struct 
containing
+   * all columns that have ever been a part of any spec in the table.
+   *
+   * @param table a table with one or many specs
+   * @return the constructed common partition type
+   */
+  public static StructType partitionType(Table table) {
+    if (table.specs().size() == 1) {
+      return table.spec().partitionType();
+    }
+
+    Map<Integer, NestedField> commonFields = Maps.newHashMap();
+
+    for (PartitionSpec spec : table.specs().values()) {
+      List<NestedField> fields = spec.partitionType().fields();
+
+      for (NestedField field : fields) {
+        int fieldId = field.fieldId();
+        NestedField existingField = commonFields.get(fieldId);
+
+        // partition fields may conflict in v1 tables
+        ValidationException.check(existingField == null || 
existingField.equals(field),
+            "Conflicting partition fields: ['%s', '%s']",
+            field, existingField);
+
+        commonFields.put(fieldId, field);
+      }
+    }
+
+    List<NestedField> sortedCommonFields = commonFields.values().stream()
+        .sorted(Comparator.comparingInt(NestedField::fieldId))
+        .collect(Collectors.toList());

Review comment:
       +1 for sorting by `fieldId`.




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