huaxingao commented on code in PR #14277:
URL: https://github.com/apache/iceberg/pull/14277#discussion_r2492939587


##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java:
##########
@@ -169,19 +177,82 @@ protected Set<PartitionSpec> specs() {
     return specs;
   }
 
+  private Object getPartitionValue(T task, String partitionFieldName) {
+    PartitionData partitionData = ((PartitionData) task.partition());
+    int pos = partitionData.getSchema().getField(partitionFieldName).pos();
+    return partitionData.get(pos);
+  }
+
+  private void performSplitOrdering(List<T> plannedTasks, Type partitionType) {
+    try {
+      // Use Iceberg's built-in type-safe comparator
+      Comparator<Object> comparator = Comparators.forType(partitionType);
+
+      plannedTasks.sort(
+          (a, b) -> {
+            Object valueA = getPartitionValue(a, splitOrderingPartitionField);
+            Object valueB = getPartitionValue(b, splitOrderingPartitionField);
+            return comparator.compare(valueA, valueB);
+          });
+    } catch (Exception e) {
+      LOG.error("Error while trying to sort partition fields", e);
+    }
+  }
+
   protected synchronized List<T> tasks() {
     if (tasks == null) {
       try (CloseableIterable<? extends ScanTask> taskIterable = 
scan.planFiles()) {
         List<T> plannedTasks = Lists.newArrayList();
 
-        for (ScanTask task : taskIterable) {
-          ValidationException.check(
-              taskJavaClass().isInstance(task),
-              "Unsupported task type, expected a subtype of %s: %s",
-              taskJavaClass().getName(),
-              task.getClass().getName());
-
-          plannedTasks.add(taskJavaClass().cast(task));
+        if (!this.preserveDataGrouping && this.splitOrderingPartitionField != 
null) {
+          Set<Type> partitionTypeSet = Sets.newHashSet();
+
+          for (ScanTask task : taskIterable) {
+            ValidationException.check(
+                taskJavaClass().isInstance(task),
+                "Unsupported task type, expected a subtype of %s: %s",
+                taskJavaClass().getName(),
+                task.getClass().getName());
+
+            T concreteTask = taskJavaClass().cast(task);
+            concreteTask.spec().fields().stream()
+                .filter(
+                    partitionField ->
+                        
partitionField.name().equals(this.splitOrderingPartitionField))
+                .forEach(
+                    partitionField -> {
+                      Type resultType =
+                          partitionField
+                              .transform()
+                              .getResultType(
+                                  
concreteTask.spec().schema().findType(partitionField.sourceId()));
+                      partitionTypeSet.add(resultType);
+                    });
+
+            plannedTasks.add(concreteTask);
+          }
+
+          if (partitionTypeSet.size() == 1) {

Review Comment:
   can we also verify the spec actually contains 
`this.splitOrderingPartitionField`



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