|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/compaction_planner.h" |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <utility> |
| 24 | + |
| 25 | +#include "iceberg/manifest/manifest_entry.h" |
| 26 | +#include "iceberg/table_scan.h" |
| 27 | +#include "iceberg/util/content_file_util.h" |
| 28 | +#include "iceberg/util/macros.h" |
| 29 | + |
| 30 | +namespace iceberg { |
| 31 | +namespace { |
| 32 | + |
| 33 | +struct PendingGroup { |
| 34 | + CompactionGroup group; |
| 35 | + bool rewrite_independently = false; |
| 36 | +}; |
| 37 | + |
| 38 | +Result<CompactionFile> BuildCompactionFile( |
| 39 | + const std::shared_ptr<FileScanTask>& scan_task) { |
| 40 | + CompactionFile result{.scan_task = scan_task}; |
| 41 | + const auto& data_file = scan_task->data_file(); |
| 42 | + |
| 43 | + for (const auto& delete_file : scan_task->delete_files()) { |
| 44 | + if (delete_file->content != DataFile::Content::kPositionDeletes) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + ICEBERG_ASSIGN_OR_RAISE(auto referenced_file, |
| 49 | + ContentFileUtil::ReferencedDataFile(*delete_file)); |
| 50 | + if (referenced_file != data_file->file_path) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + ++result.file_scoped_delete_count; |
| 55 | + result.file_scoped_delete_record_count += delete_file->record_count; |
| 56 | + } |
| 57 | + |
| 58 | + return result; |
| 59 | +} |
| 60 | + |
| 61 | +PendingGroup& FindOrAddGroup(std::vector<PendingGroup>& groups, |
| 62 | + const DataFile& data_file) { |
| 63 | + auto group = std::ranges::find_if(groups, [&](const PendingGroup& candidate) { |
| 64 | + return candidate.group.partition_spec_id == data_file.partition_spec_id && |
| 65 | + candidate.group.partition == data_file.partition; |
| 66 | + }); |
| 67 | + if (group != groups.end()) { |
| 68 | + return *group; |
| 69 | + } |
| 70 | + |
| 71 | + groups.push_back(PendingGroup{.group = CompactionGroup{ |
| 72 | + .partition_spec_id = data_file.partition_spec_id, |
| 73 | + .partition = data_file.partition, |
| 74 | + }}); |
| 75 | + return groups.back(); |
| 76 | +} |
| 77 | + |
| 78 | +} // namespace |
| 79 | + |
| 80 | +Result<CompactionPlan> CompactionPlanner::Plan( |
| 81 | + std::span<const std::shared_ptr<FileScanTask>> scan_tasks, |
| 82 | + const CompactionPlannerConfig& config) { |
| 83 | + const long double min_file_size = |
| 84 | + config.target_file_size_bytes * config.min_file_size_ratio; |
| 85 | + const long double max_file_size = |
| 86 | + config.target_file_size_bytes * config.max_file_size_ratio; |
| 87 | + std::vector<PendingGroup> pending_groups; |
| 88 | + |
| 89 | + for (const auto& scan_task : scan_tasks) { |
| 90 | + ICEBERG_PRECHECK(scan_task != nullptr, "File scan task must not be null"); |
| 91 | + const auto& data_file = *scan_task->data_file(); |
| 92 | + ICEBERG_ASSIGN_OR_RAISE(auto file, BuildCompactionFile(scan_task)); |
| 93 | + |
| 94 | + const bool is_small = data_file.file_size_in_bytes < min_file_size; |
| 95 | + const bool is_oversized = data_file.file_size_in_bytes > max_file_size; |
| 96 | + const bool has_many_delete_files = |
| 97 | + file.file_scoped_delete_count >= config.delete_file_threshold; |
| 98 | + const bool has_high_delete_ratio = |
| 99 | + data_file.record_count > 0 && |
| 100 | + static_cast<double>(file.file_scoped_delete_record_count) / |
| 101 | + data_file.record_count >= |
| 102 | + config.delete_ratio_threshold; |
| 103 | + if (!is_small && !is_oversized && !has_many_delete_files && !has_high_delete_ratio) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + auto& group = FindOrAddGroup(pending_groups, data_file); |
| 108 | + group.rewrite_independently |= |
| 109 | + is_oversized || has_many_delete_files || has_high_delete_ratio; |
| 110 | + group.group.data_file_size_bytes += data_file.file_size_in_bytes; |
| 111 | + group.group.file_scoped_delete_record_count += file.file_scoped_delete_record_count; |
| 112 | + group.group.files.push_back(std::move(file)); |
| 113 | + } |
| 114 | + |
| 115 | + CompactionPlan plan; |
| 116 | + for (auto& pending : pending_groups) { |
| 117 | + if (pending.rewrite_independently || |
| 118 | + pending.group.files.size() >= config.min_input_files) { |
| 119 | + plan.groups.push_back(std::move(pending.group)); |
| 120 | + } |
| 121 | + } |
| 122 | + return plan; |
| 123 | +} |
| 124 | + |
| 125 | +} // namespace iceberg |
0 commit comments