|
| 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/data/deletion_vector_writer.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | +#include <map> |
| 24 | +#include <optional> |
| 25 | +#include <string> |
| 26 | +#include <utility> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +#include "iceberg/file_io.h" |
| 30 | +#include "iceberg/manifest/manifest_entry.h" |
| 31 | +#include "iceberg/metadata_columns.h" |
| 32 | +#include "iceberg/partition_spec.h" |
| 33 | +#include "iceberg/puffin/file_metadata.h" |
| 34 | +#include "iceberg/puffin/puffin_writer.h" |
| 35 | +#include "iceberg/util/content_file_util.h" |
| 36 | +#include "iceberg/util/macros.h" |
| 37 | +#include "iceberg/version.h" |
| 38 | + |
| 39 | +namespace iceberg { |
| 40 | + |
| 41 | +namespace { |
| 42 | +constexpr std::string_view kReferencedDataFile = "referenced-data-file"; |
| 43 | +constexpr std::string_view kCardinality = "cardinality"; |
| 44 | +} // namespace |
| 45 | + |
| 46 | +class DeletionVectorWriter::Impl { |
| 47 | + public: |
| 48 | + explicit Impl(DeletionVectorWriterOptions options) : options_(std::move(options)) {} |
| 49 | + |
| 50 | + // Accumulated positions and metadata for a single referenced data file. |
| 51 | + struct Deletes { |
| 52 | + PositionDeleteIndex positions; |
| 53 | + std::shared_ptr<PartitionSpec> spec; |
| 54 | + PartitionValues partition; |
| 55 | + }; |
| 56 | + |
| 57 | + Deletes& DeletesFor(std::string_view referenced_data_file, |
| 58 | + std::shared_ptr<PartitionSpec> spec, PartitionValues partition) { |
| 59 | + auto [it, inserted] = deletes_by_path_.try_emplace(std::string(referenced_data_file)); |
| 60 | + if (inserted) { |
| 61 | + it->second.spec = std::move(spec); |
| 62 | + it->second.partition = std::move(partition); |
| 63 | + } |
| 64 | + return it->second; |
| 65 | + } |
| 66 | + |
| 67 | + Status Delete(std::string_view referenced_data_file, int64_t pos, |
| 68 | + std::shared_ptr<PartitionSpec> spec, PartitionValues partition) { |
| 69 | + ICEBERG_CHECK(!closed_, "Cannot delete after the writer is closed"); |
| 70 | + ICEBERG_PRECHECK(!referenced_data_file.empty(), |
| 71 | + "Deletion vector requires a non-empty referenced data file"); |
| 72 | + DeletesFor(referenced_data_file, std::move(spec), std::move(partition)) |
| 73 | + .positions.Delete(pos); |
| 74 | + return {}; |
| 75 | + } |
| 76 | + |
| 77 | + Status Delete(std::string_view referenced_data_file, |
| 78 | + const PositionDeleteIndex& positions, std::shared_ptr<PartitionSpec> spec, |
| 79 | + PartitionValues partition) { |
| 80 | + ICEBERG_CHECK(!closed_, "Cannot delete after the writer is closed"); |
| 81 | + ICEBERG_PRECHECK(!referenced_data_file.empty(), |
| 82 | + "Deletion vector requires a non-empty referenced data file"); |
| 83 | + DeletesFor(referenced_data_file, std::move(spec), std::move(partition)) |
| 84 | + .positions.Merge(positions); |
| 85 | + return {}; |
| 86 | + } |
| 87 | + |
| 88 | + Status Close() { |
| 89 | + if (closed_) { |
| 90 | + return {}; |
| 91 | + } |
| 92 | + |
| 93 | + // No deletes: skip creating an orphan Puffin file that no metadata |
| 94 | + // references. |
| 95 | + if (deletes_by_path_.empty()) { |
| 96 | + closed_ = true; |
| 97 | + return {}; |
| 98 | + } |
| 99 | + |
| 100 | + // Merge previously written deletes and collect the file-scoped delete files |
| 101 | + // they came from so callers can remove them from table state. |
| 102 | + if (options_.load_previous_deletes) { |
| 103 | + for (auto& [path, deletes] : deletes_by_path_) { |
| 104 | + ICEBERG_ASSIGN_OR_RAISE(auto previous, options_.load_previous_deletes(path)); |
| 105 | + if (previous.index == nullptr) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + deletes.positions.Merge(*previous.index); |
| 109 | + for (const auto& delete_file : previous.delete_files) { |
| 110 | + ICEBERG_ASSIGN_OR_RAISE(bool file_scoped, |
| 111 | + ContentFileUtil::IsFileScoped(*delete_file)); |
| 112 | + if (file_scoped) { |
| 113 | + result_.rewritten_delete_files.push_back(delete_file); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + auto properties = options_.properties; |
| 120 | + if (const std::string created_by(puffin::StandardPuffinProperties::kCreatedBy); |
| 121 | + !properties.contains(created_by)) { |
| 122 | + properties.emplace(created_by, |
| 123 | + std::format("iceberg-cpp/{}.{}.{}", ICEBERG_VERSION_MAJOR, |
| 124 | + ICEBERG_VERSION_MINOR, ICEBERG_VERSION_PATCH)); |
| 125 | + } |
| 126 | + |
| 127 | + ICEBERG_ASSIGN_OR_RAISE(auto output_file, options_.io->NewOutputFile(options_.path)); |
| 128 | + ICEBERG_ASSIGN_OR_RAISE( |
| 129 | + auto writer, |
| 130 | + puffin::PuffinWriter::Make(std::move(output_file), std::move(properties))); |
| 131 | + |
| 132 | + struct Entry { |
| 133 | + std::string referenced_data_file; |
| 134 | + std::shared_ptr<PartitionSpec> spec; |
| 135 | + PartitionValues partition; |
| 136 | + int64_t offset; |
| 137 | + int64_t length; |
| 138 | + int64_t cardinality; |
| 139 | + }; |
| 140 | + std::vector<Entry> entries; |
| 141 | + entries.reserve(deletes_by_path_.size()); |
| 142 | + |
| 143 | + for (auto& [path, deletes] : deletes_by_path_) { |
| 144 | + const int64_t cardinality = deletes.positions.Cardinality(); |
| 145 | + ICEBERG_ASSIGN_OR_RAISE(auto data, deletes.positions.Serialize()); |
| 146 | + |
| 147 | + puffin::Blob blob{ |
| 148 | + .type = std::string(puffin::StandardBlobTypes::kDeletionVectorV1), |
| 149 | + .input_fields = {MetadataColumns::kFilePositionColumnId}, |
| 150 | + // Snapshot ID and sequence number are inherited; the spec requires -1. |
| 151 | + .snapshot_id = -1, |
| 152 | + .sequence_number = -1, |
| 153 | + .data = std::move(data), |
| 154 | + .requested_compression = puffin::PuffinCompressionCodec::kNone, |
| 155 | + }; |
| 156 | + blob.properties.emplace(std::string(kReferencedDataFile), path); |
| 157 | + blob.properties.emplace(std::string(kCardinality), std::format("{}", cardinality)); |
| 158 | + |
| 159 | + ICEBERG_ASSIGN_OR_RAISE(auto blob_metadata, writer->Write(blob)); |
| 160 | + entries.push_back(Entry{ |
| 161 | + .referenced_data_file = path, |
| 162 | + .spec = deletes.spec, |
| 163 | + .partition = deletes.partition, |
| 164 | + .offset = blob_metadata.offset, |
| 165 | + .length = blob_metadata.length, |
| 166 | + .cardinality = cardinality, |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + ICEBERG_RETURN_UNEXPECTED(writer->Finish()); |
| 171 | + ICEBERG_ASSIGN_OR_RAISE(const int64_t file_size, writer->FileSize()); |
| 172 | + |
| 173 | + for (auto& entry : entries) { |
| 174 | + result_.referenced_data_files.push_back(entry.referenced_data_file); |
| 175 | + result_.delete_files.push_back(std::make_shared<DataFile>(DataFile{ |
| 176 | + .content = DataFile::Content::kPositionDeletes, |
| 177 | + .file_path = options_.path, |
| 178 | + .file_format = FileFormatType::kPuffin, |
| 179 | + .partition = std::move(entry.partition), |
| 180 | + .record_count = entry.cardinality, |
| 181 | + .file_size_in_bytes = file_size, |
| 182 | + .referenced_data_file = std::move(entry.referenced_data_file), |
| 183 | + .content_offset = entry.offset, |
| 184 | + .content_size_in_bytes = entry.length, |
| 185 | + .partition_spec_id = |
| 186 | + entry.spec ? std::make_optional(entry.spec->spec_id()) : std::nullopt, |
| 187 | + })); |
| 188 | + } |
| 189 | + |
| 190 | + closed_ = true; |
| 191 | + return {}; |
| 192 | + } |
| 193 | + |
| 194 | + Result<DeleteWriteResult> Metadata() { |
| 195 | + ICEBERG_CHECK(closed_, "Cannot get metadata before closing the writer"); |
| 196 | + return result_; |
| 197 | + } |
| 198 | + |
| 199 | + private: |
| 200 | + DeletionVectorWriterOptions options_; |
| 201 | + // Ordered by referenced data file path for deterministic blob layout. |
| 202 | + std::map<std::string, Deletes> deletes_by_path_; |
| 203 | + DeleteWriteResult result_; |
| 204 | + bool closed_ = false; |
| 205 | +}; |
| 206 | + |
| 207 | +DeletionVectorWriter::DeletionVectorWriter(std::unique_ptr<Impl> impl) |
| 208 | + : impl_(std::move(impl)) {} |
| 209 | + |
| 210 | +DeletionVectorWriter::~DeletionVectorWriter() = default; |
| 211 | + |
| 212 | +Result<std::unique_ptr<DeletionVectorWriter>> DeletionVectorWriter::Make( |
| 213 | + DeletionVectorWriterOptions options) { |
| 214 | + ICEBERG_PRECHECK(options.io != nullptr, "DeletionVectorWriter requires a FileIO"); |
| 215 | + ICEBERG_PRECHECK(!options.path.empty(), "DeletionVectorWriter requires a path"); |
| 216 | + return std::unique_ptr<DeletionVectorWriter>( |
| 217 | + new DeletionVectorWriter(std::make_unique<Impl>(std::move(options)))); |
| 218 | +} |
| 219 | + |
| 220 | +Status DeletionVectorWriter::Delete(std::string_view referenced_data_file, int64_t pos, |
| 221 | + std::shared_ptr<PartitionSpec> spec, |
| 222 | + PartitionValues partition) { |
| 223 | + return impl_->Delete(referenced_data_file, pos, std::move(spec), std::move(partition)); |
| 224 | +} |
| 225 | + |
| 226 | +Status DeletionVectorWriter::Delete(std::string_view referenced_data_file, |
| 227 | + const PositionDeleteIndex& positions, |
| 228 | + std::shared_ptr<PartitionSpec> spec, |
| 229 | + PartitionValues partition) { |
| 230 | + return impl_->Delete(referenced_data_file, positions, std::move(spec), |
| 231 | + std::move(partition)); |
| 232 | +} |
| 233 | + |
| 234 | +Status DeletionVectorWriter::Close() { return impl_->Close(); } |
| 235 | + |
| 236 | +Result<DeleteWriteResult> DeletionVectorWriter::Metadata() { return impl_->Metadata(); } |
| 237 | + |
| 238 | +} // namespace iceberg |
0 commit comments