forked from apache/iceberg-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_loader.cc
More file actions
173 lines (137 loc) · 5.76 KB
/
delete_loader.cc
File metadata and controls
173 lines (137 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "iceberg/data/delete_loader.h"
#include <string>
#include <vector>
#include "iceberg/arrow_c_data_guard_internal.h"
#include "iceberg/deletes/position_delete_index.h"
#include "iceberg/file_reader.h"
#include "iceberg/manifest/manifest_entry.h"
#include "iceberg/metadata_columns.h"
#include "iceberg/row/arrow_array_wrapper.h"
#include "iceberg/schema.h"
#include "iceberg/util/macros.h"
#include "iceberg/util/struct_like_set.h"
namespace iceberg {
namespace {
/// \brief Build the projection schema for reading position delete files.
std::shared_ptr<Schema> PosDeleteSchema() {
return std::make_shared<Schema>(std::vector<SchemaField>{
MetadataColumns::kDeleteFilePath,
MetadataColumns::kDeleteFilePos,
});
}
/// \brief Open a delete file with the given projection schema.
Result<std::unique_ptr<Reader>> OpenDeleteFile(const DataFile& file,
std::shared_ptr<Schema> projection,
const std::shared_ptr<FileIO>& io) {
ReaderOptions options{
.path = file.file_path,
.length = static_cast<size_t>(file.file_size_in_bytes),
.io = io,
.projection = std::move(projection),
};
return ReaderFactoryRegistry::Open(file.file_format, options);
}
} // namespace
DeleteLoader::DeleteLoader(std::shared_ptr<FileIO> io) : io_(std::move(io)) {}
DeleteLoader::~DeleteLoader() = default;
Status DeleteLoader::LoadPositionDelete(const DataFile& file, PositionDeleteIndex& index,
std::string_view data_file_path) const {
// TODO(gangwu): push down path filter to open the file.
ICEBERG_ASSIGN_OR_RAISE(auto reader, OpenDeleteFile(file, PosDeleteSchema(), io_));
ICEBERG_ASSIGN_OR_RAISE(auto arrow_schema, reader->Schema());
internal::ArrowSchemaGuard schema_guard(&arrow_schema);
while (true) {
ICEBERG_ASSIGN_OR_RAISE(auto batch_opt, reader->Next());
if (!batch_opt.has_value()) break;
auto& batch = batch_opt.value();
internal::ArrowArrayGuard batch_guard(&batch);
ICEBERG_ASSIGN_OR_RAISE(
auto row, ArrowArrayStructLike::Make(arrow_schema, batch, /*row_index=*/0));
for (int64_t i = 0; i < batch.length; ++i) {
if (i > 0) {
ICEBERG_RETURN_UNEXPECTED(row->Reset(i));
}
// Field 0: file_path
ICEBERG_ASSIGN_OR_RAISE(auto path_scalar, row->GetField(0));
auto path = std::get<std::string_view>(path_scalar);
if (path == data_file_path) {
// Field 1: pos
ICEBERG_ASSIGN_OR_RAISE(auto pos_scalar, row->GetField(1));
index.Delete(std::get<int64_t>(pos_scalar));
}
}
}
return reader->Close();
}
Status DeleteLoader::LoadDV(const DataFile& file, PositionDeleteIndex& index) const {
return NotSupported("Loading deletion vectors is not yet supported");
}
Result<PositionDeleteIndex> DeleteLoader::LoadPositionDeletes(
std::span<const std::shared_ptr<DataFile>> delete_files,
std::string_view data_file_path) const {
PositionDeleteIndex index;
for (const auto& file : delete_files) {
if (file->referenced_data_file.has_value() &&
file->referenced_data_file.value() != data_file_path) {
continue;
}
if (file->IsDeletionVector()) {
ICEBERG_RETURN_UNEXPECTED(LoadDV(*file, index));
continue;
}
ICEBERG_PRECHECK(file->content == DataFile::Content::kPositionDeletes,
"Expected position delete file but got content type {}",
ToString(file->content));
ICEBERG_RETURN_UNEXPECTED(LoadPositionDelete(*file, index, data_file_path));
}
return index;
}
Result<std::unique_ptr<UncheckedStructLikeSet>> DeleteLoader::LoadEqualityDeletes(
std::span<const std::shared_ptr<DataFile>> delete_files,
const StructType& equality_type) const {
auto eq_set = std::make_unique<UncheckedStructLikeSet>(equality_type);
std::shared_ptr<Schema> projection = equality_type.ToSchema();
for (const auto& file : delete_files) {
ICEBERG_PRECHECK(file->content == DataFile::Content::kEqualityDeletes,
"Expected equality delete file but got content type {}",
static_cast<int>(file->content));
ICEBERG_ASSIGN_OR_RAISE(auto reader, OpenDeleteFile(*file, projection, io_));
ICEBERG_ASSIGN_OR_RAISE(auto arrow_schema, reader->Schema());
internal::ArrowSchemaGuard schema_guard(&arrow_schema);
while (true) {
ICEBERG_ASSIGN_OR_RAISE(auto batch_opt, reader->Next());
if (!batch_opt.has_value()) break;
auto& batch = batch_opt.value();
internal::ArrowArrayGuard batch_guard(&batch);
ICEBERG_ASSIGN_OR_RAISE(
auto row, ArrowArrayStructLike::Make(arrow_schema, batch, /*row_index=*/0));
for (int64_t i = 0; i < batch.length; ++i) {
if (i > 0) {
ICEBERG_RETURN_UNEXPECTED(row->Reset(i));
}
ICEBERG_RETURN_UNEXPECTED(eq_set->Insert(*row));
}
}
ICEBERG_RETURN_UNEXPECTED(reader->Close());
}
return eq_set;
}
} // namespace iceberg