Skip to content

Commit 8b21830

Browse files
committed
refactor to terminate early if tablescan is empty
1 parent 966d8b4 commit 8b21830

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

core/src/main/java/org/apache/iceberg/rest/CatalogHandlers.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ public static PlanTableScanResponse planTableScan(
678678
initial.second() == null
679679
? Collections.emptyList()
680680
: IN_MEMORY_PLANNING_STATE.nextPlanTask(initial.second());
681-
682681
PlanTableScanResponse.Builder builder =
683682
PlanTableScanResponse.builder()
684683
.withPlanStatus(PlanStatus.COMPLETED)
@@ -777,20 +776,25 @@ static void clearPlanningState() {
777776
*/
778777
private static Pair<List<FileScanTask>, String> planFilesFor(
779778
TableScan tableScan, String planId, int tasksPerPlanTask) {
780-
Iterable<List<FileScanTask>> taskGroupings =
781-
Iterables.partition(tableScan.planFiles(), tasksPerPlanTask);
779+
Iterable<FileScanTask> planTasks = tableScan.planFiles();
780+
String planTaskPrefix = planId + "-" + tableScan.table().uuid() + "-";
781+
782+
// Handle empty table scans
783+
if (!planTasks.iterator().hasNext()) {
784+
String planTaskKey = planTaskPrefix + "0";
785+
// Add empty scan to planning state so async calls know the scan completed
786+
IN_MEMORY_PLANNING_STATE.addPlanTask(planTaskKey, Collections.emptyList());
787+
return Pair.of(Collections.emptyList(), planTaskKey);
788+
}
789+
790+
Iterable<List<FileScanTask>> taskGroupings = Iterables.partition(planTasks, tasksPerPlanTask);
782791
int planTaskSequence = 0;
783792
String previousPlanTask = null;
784793
String firstPlanTaskKey = null;
785794
List<FileScanTask> initialFileScanTasks = null;
786-
boolean hasPlanTasks = false;
787-
788795
for (List<FileScanTask> taskGrouping : taskGroupings) {
789-
hasPlanTasks = true;
790-
String planTaskKey =
791-
String.format("%s-%s-%s", planId, tableScan.table().uuid(), planTaskSequence++);
796+
String planTaskKey = planTaskPrefix + planTaskSequence++;
792797
IN_MEMORY_PLANNING_STATE.addPlanTask(planTaskKey, taskGrouping);
793-
794798
if (previousPlanTask != null) {
795799
IN_MEMORY_PLANNING_STATE.addNextPlanTask(previousPlanTask, planTaskKey);
796800
} else {
@@ -800,11 +804,6 @@ private static Pair<List<FileScanTask>, String> planFilesFor(
800804

801805
previousPlanTask = planTaskKey;
802806
}
803-
804-
if (!hasPlanTasks) {
805-
return Pair.of(Collections.emptyList(), null);
806-
}
807-
808807
return Pair.of(initialFileScanTasks, firstPlanTaskKey);
809808
}
810809

0 commit comments

Comments
 (0)