Skip to content

Commit 6b38878

Browse files
author
xuan.zhao
committed
feat(puffin): support deletion-vector-v1 blob read/write
1 parent 3c9d13d commit 6b38878

15 files changed

Lines changed: 1091 additions & 5 deletions

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ set(ICEBERG_DATA_SOURCES
188188
data/data_writer.cc
189189
data/delete_filter.cc
190190
data/delete_loader.cc
191+
data/deletion_vector_writer.cc
191192
data/equality_delete_writer.cc
192193
data/file_scan_task_reader.cc
193194
data/position_delete_writer.cc

src/iceberg/data/delete_loader.cc

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
#include "iceberg/data/delete_loader.h"
2121

22+
#include <cstdint>
2223
#include <cstring>
24+
#include <limits>
2325
#include <span>
2426
#include <string>
27+
#include <utility>
2528
#include <vector>
2629

2730
#include <nanoarrow/nanoarrow.h>
@@ -30,6 +33,7 @@
3033
#include "iceberg/arrow_c_data_guard_internal.h"
3134
#include "iceberg/deletes/position_delete_index.h"
3235
#include "iceberg/deletes/position_delete_range_consumer.h"
36+
#include "iceberg/file_io.h"
3337
#include "iceberg/file_reader.h"
3438
#include "iceberg/manifest/manifest_entry.h"
3539
#include "iceberg/metadata_columns.h"
@@ -171,7 +175,44 @@ Status DeleteLoader::LoadPositionDelete(const DataFile& file, PositionDeleteInde
171175
}
172176

173177
Status DeleteLoader::LoadDV(const DataFile& file, PositionDeleteIndex& index) const {
174-
return NotSupported("Loading deletion vectors is not yet supported");
178+
// A deletion vector must reference exactly one data file; without it the
179+
// caller cannot know which data file the positions apply to.
180+
ICEBERG_PRECHECK(file.referenced_data_file.has_value(),
181+
"Deletion vector requires referenced_data_file: {}", file.file_path);
182+
183+
// For deletion vectors, content_offset and content_size_in_bytes point directly
184+
// at the DV blob bytes within the Puffin file and are required by the spec.
185+
ICEBERG_PRECHECK(
186+
file.content_offset.has_value() && file.content_size_in_bytes.has_value(),
187+
"Deletion vector requires content_offset and content_size_in_bytes: {}",
188+
file.file_path);
189+
190+
const int64_t offset = file.content_offset.value();
191+
const int64_t length = file.content_size_in_bytes.value();
192+
ICEBERG_PRECHECK(offset >= 0 && length >= 0,
193+
"Invalid deletion vector offset/length: offset={}, length={}", offset,
194+
length);
195+
ICEBERG_PRECHECK(length <= std::numeric_limits<int32_t>::max(),
196+
"Cannot read deletion vector larger than 2GB: {}", length);
197+
198+
ICEBERG_ASSIGN_OR_RAISE(auto input_file, io_->NewInputFile(file.file_path));
199+
ICEBERG_ASSIGN_OR_RAISE(auto stream, input_file->Open());
200+
201+
std::vector<std::byte> bytes(static_cast<size_t>(length));
202+
ICEBERG_RETURN_UNEXPECTED(stream->ReadFully(offset, bytes));
203+
ICEBERG_RETURN_UNEXPECTED(stream->Close());
204+
205+
std::span<const uint8_t> blob(reinterpret_cast<const uint8_t*>(bytes.data()),
206+
bytes.size());
207+
ICEBERG_ASSIGN_OR_RAISE(auto dv, PositionDeleteIndex::Deserialize(blob));
208+
209+
// The bitmap cardinality must match the record count recorded in metadata.
210+
ICEBERG_PRECHECK(std::cmp_equal(dv.Cardinality(), file.record_count),
211+
"Deletion vector cardinality {} does not match record count {}: {}",
212+
dv.Cardinality(), file.record_count, file.file_path);
213+
214+
index.Merge(dv);
215+
return {};
175216
}
176217

177218
Result<PositionDeleteIndex> DeleteLoader::LoadPositionDeletes(
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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

Comments
 (0)