Skip to content

Commit 138cee5

Browse files
committed
feat: adopt partition summary & add AddEntry variants
including AddEntry, AddExistingEntry, AddDeleteEntry
1 parent 6fe80fe commit 138cee5

29 files changed

+1242
-167
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
build/
19+
cmake-build/
1920
cmake-build-debug/
2021
cmake-build-release/
2122
.DS_Store

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(ICEBERG_SOURCES
3939
name_mapping.cc
4040
partition_field.cc
4141
partition_spec.cc
42+
partition_summary_internal.cc
4243
row/arrow_array_wrapper.cc
4344
row/manifest_wrapper.cc
4445
schema.cc

src/iceberg/expression/literal.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,17 @@ bool Literal::IsAboveMax() const { return std::holds_alternative<AboveMax>(value
504504

505505
bool Literal::IsNull() const { return std::holds_alternative<std::monostate>(value_); }
506506

507+
bool Literal::IsNan() const {
508+
if (type_->type_id() == TypeId::kFloat) {
509+
auto val = std::get<float>(value_);
510+
return std::isnan(val);
511+
} else if (type_->type_id() == TypeId::kDouble) {
512+
auto val = std::get<double>(value_);
513+
return std::isnan(val);
514+
}
515+
return false;
516+
}
517+
507518
// LiteralCaster implementation
508519

509520
Result<Literal> LiteralCaster::CastTo(const Literal& literal,

src/iceberg/expression/literal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,14 @@ class ICEBERG_EXPORT Literal : public util::Formattable {
150150
/// \return true if this literal represents a BelowMin value, false otherwise
151151
bool IsBelowMin() const;
152152

153-
/// Check if this literal is null.
153+
/// \brief Check if this literal is null.
154154
/// \return true if this literal is null, false otherwise
155155
bool IsNull() const;
156156

157+
/// \brief Check if this literal is NaN (Not a Number).
158+
/// \return true if this literal is NaN, false otherwise
159+
bool IsNan() const;
160+
157161
std::string ToString() const override;
158162

159163
private:

src/iceberg/manifest_adapter.cc

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919

2020
#include "iceberg/manifest_adapter.h"
2121

22+
#include <memory>
2223
#include <utility>
2324

2425
#include <nanoarrow/nanoarrow.h>
2526

2627
#include "iceberg/arrow/nanoarrow_status_internal.h"
2728
#include "iceberg/manifest_entry.h"
2829
#include "iceberg/manifest_list.h"
30+
#include "iceberg/partition_summary_internal.h"
2931
#include "iceberg/result.h"
3032
#include "iceberg/schema.h"
31-
#include "iceberg/schema_internal.h"
3233
#include "iceberg/util/checked_cast.h"
3334
#include "iceberg/util/macros.h"
3435

@@ -141,10 +142,12 @@ Result<ArrowArray*> ManifestAdapter::FinishAppending() {
141142
return &array_;
142143
}
143144

144-
ManifestEntryAdapter::ManifestEntryAdapter(std::shared_ptr<PartitionSpec> partition_spec,
145+
ManifestEntryAdapter::ManifestEntryAdapter(std::optional<int64_t> snapshot_id_,
146+
std::shared_ptr<PartitionSpec> partition_spec,
145147
std::shared_ptr<Schema> current_schema,
146148
ManifestContent content)
147-
: partition_spec_(std::move(partition_spec)),
149+
: snapshot_id_(snapshot_id_),
150+
partition_spec_(std::move(partition_spec)),
148151
current_schema_(std::move(current_schema)),
149152
content_(content) {
150153
if (!partition_spec_) {
@@ -161,6 +164,110 @@ ManifestEntryAdapter::~ManifestEntryAdapter() {
161164
}
162165
}
163166

167+
Status ManifestEntryAdapter::AddEntry(ManifestEntry& entry) {
168+
ICEBERG_RETURN_UNEXPECTED(CheckDataFile(*entry.data_file));
169+
entry.status = ManifestStatus::kAdded;
170+
entry.snapshot_id = snapshot_id_;
171+
if (entry.sequence_number.has_value() &&
172+
entry.sequence_number.value() < TableMetadata::kInitialSequenceNumber) {
173+
entry.sequence_number = std::nullopt;
174+
}
175+
entry.file_sequence_number = std::nullopt;
176+
return AddEntryInternal(entry);
177+
}
178+
179+
Status ManifestEntryAdapter::AddDeleteEntry(ManifestEntry& entry) {
180+
ICEBERG_RETURN_UNEXPECTED(CheckDataFile(*entry.data_file));
181+
entry.status = ManifestStatus::kDeleted;
182+
entry.snapshot_id = snapshot_id_;
183+
return AddEntryInternal(entry);
184+
}
185+
186+
Status ManifestEntryAdapter::AddExistingEntry(ManifestEntry& entry) {
187+
ICEBERG_RETURN_UNEXPECTED(CheckDataFile(*entry.data_file));
188+
entry.status = ManifestStatus::kExisting;
189+
return AddEntryInternal(entry);
190+
}
191+
192+
ManifestFile ManifestEntryAdapter::ToManifestFile() const {
193+
ManifestFile manifest_file;
194+
manifest_file.partition_spec_id = partition_spec_->spec_id();
195+
manifest_file.content = content_;
196+
// sequence_number and min_sequence_number with kInvalidSequenceNumber will be
197+
// replace with real sequence number in `ManifestListWriter`.
198+
manifest_file.sequence_number = TableMetadata::kInvalidSequenceNumber;
199+
manifest_file.min_sequence_number =
200+
min_sequence_number_.value_or(TableMetadata::kInvalidSequenceNumber);
201+
manifest_file.existing_files_count = existing_files_count_;
202+
manifest_file.added_snapshot_id = snapshot_id_.value_or(Snapshot::kInvalidSnapshotId);
203+
manifest_file.added_files_count = add_files_count_;
204+
manifest_file.existing_files_count = existing_files_count_;
205+
manifest_file.deleted_files_count = delete_files_count_;
206+
manifest_file.added_rows_count = add_rows_count_;
207+
manifest_file.existing_rows_count = existing_rows_count_;
208+
manifest_file.deleted_rows_count = delete_rows_count_;
209+
manifest_file.partitions = std::move(partition_summary_->Summaries());
210+
return manifest_file;
211+
}
212+
213+
Status ManifestEntryAdapter::CheckDataFile(const DataFile& file) const {
214+
switch (content_) {
215+
case ManifestContent::kData:
216+
if (file.content != DataFile::Content::kData) {
217+
return InvalidArgument(
218+
"Manifest content type: data, data file content should be: data, but got: {}",
219+
ToString(file.content));
220+
}
221+
break;
222+
case ManifestContent::kDeletes:
223+
if (file.content != DataFile::Content::kPositionDeletes &&
224+
file.content != DataFile::Content::kEqualityDeletes) {
225+
return InvalidArgument(
226+
"Manifest content type: deletes, data file content should be: "
227+
"position_deletes or equality_deletes, but got: {}",
228+
ToString(file.content));
229+
}
230+
break;
231+
default:
232+
std::unreachable();
233+
}
234+
return {};
235+
}
236+
237+
Status ManifestEntryAdapter::AddEntryInternal(const ManifestEntry& entry) {
238+
if (entry.data_file == nullptr) [[unlikely]] {
239+
return InvalidManifest("Missing required data_file field from manifest entry.");
240+
}
241+
242+
switch (entry.status) {
243+
case ManifestStatus::kAdded:
244+
add_files_count_++;
245+
add_rows_count_ += entry.data_file->record_count;
246+
break;
247+
case ManifestStatus::kExisting:
248+
existing_files_count_++;
249+
existing_rows_count_ += entry.data_file->record_count;
250+
break;
251+
case ManifestStatus::kDeleted:
252+
delete_files_count_++;
253+
delete_rows_count_ += entry.data_file->record_count;
254+
break;
255+
default:
256+
std::unreachable();
257+
}
258+
259+
ICEBERG_RETURN_UNEXPECTED(partition_summary_->Update(entry.data_file->partition));
260+
261+
if (entry.IsAlive() && entry.sequence_number.has_value()) {
262+
if (!min_sequence_number_.has_value() ||
263+
entry.sequence_number.value() < min_sequence_number_.value()) {
264+
min_sequence_number_ = entry.sequence_number.value();
265+
}
266+
}
267+
268+
return AppendInternal(entry);
269+
}
270+
164271
Status ManifestEntryAdapter::AppendPartitionValues(
165272
ArrowArray* array, const std::shared_ptr<StructType>& partition_type,
166273
const std::vector<Literal>& partition_values) {

src/iceberg/manifest_adapter.h

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <memory>
2626
#include <optional>
2727
#include <unordered_map>
28-
#include <unordered_set>
2928
#include <vector>
3029

3130
#include "iceberg/arrow_c_data.h"
@@ -61,18 +60,45 @@ class ICEBERG_EXPORT ManifestAdapter {
6160
/// Implemented by different versions with version-specific schemas.
6261
class ICEBERG_EXPORT ManifestEntryAdapter : public ManifestAdapter {
6362
public:
64-
ManifestEntryAdapter(std::shared_ptr<PartitionSpec> partition_spec,
63+
ManifestEntryAdapter(std::optional<int64_t> snapshot_id_,
64+
std::shared_ptr<PartitionSpec> partition_spec,
6565
std::shared_ptr<Schema> current_schema, ManifestContent content);
6666

6767
~ManifestEntryAdapter() override;
6868

69-
virtual Status Append(const ManifestEntry& entry) = 0;
69+
/// \brief Add a new entry to the manifest.
70+
///
71+
/// This method will update following status of the entry:
72+
/// - Update the entry status to `Added`
73+
/// - Set the snapshot id to the current snapshot id
74+
/// - Set the sequence number to nullopt if it is invalid(smaller than 0)
75+
/// - Set the file sequence number to nullopt
76+
virtual Status AddEntry(ManifestEntry& entry);
77+
78+
/// \brief Add a delete entry to the manifest.
79+
///
80+
/// This method will update following status of the entry:
81+
/// - Update the entry status to `Deleted`
82+
/// - Set the snapshot id to the current snapshot id
83+
virtual Status AddDeleteEntry(ManifestEntry& entry);
84+
85+
/// \brief Add an existing entry to the manifest.
86+
///
87+
/// This method will update following status of the entry:
88+
/// - Update the entry status to `Existing`
89+
virtual Status AddExistingEntry(ManifestEntry& entry);
7090

7191
const std::shared_ptr<Schema>& schema() const { return manifest_schema_; }
7292

7393
ManifestContent content() const { return content_; }
7494

95+
/// \brief Create a ManifestFile object without setting file metadata, such as
96+
/// location, file size, key metadata, etc.
97+
ManifestFile ToManifestFile() const;
98+
7599
protected:
100+
Status CheckDataFile(const DataFile& file) const;
101+
Status AddEntryInternal(const ManifestEntry& entry);
76102
Status AppendInternal(const ManifestEntry& entry);
77103
Status AppendDataFile(ArrowArray* array,
78104
const std::shared_ptr<StructType>& data_file_type,
@@ -91,10 +117,19 @@ class ICEBERG_EXPORT ManifestEntryAdapter : public ManifestAdapter {
91117
const DataFile& file) const;
92118

93119
protected:
120+
std::optional<int64_t> snapshot_id_;
94121
std::shared_ptr<PartitionSpec> partition_spec_;
95122
std::shared_ptr<Schema> current_schema_;
96123
std::shared_ptr<Schema> manifest_schema_;
97124
const ManifestContent content_;
125+
int32_t add_files_count_{0};
126+
int32_t existing_files_count_{0};
127+
int32_t delete_files_count_{0};
128+
int64_t add_rows_count_{0L};
129+
int64_t existing_rows_count_{0L};
130+
int64_t delete_rows_count_{0L};
131+
std::optional<int64_t> min_sequence_number_{std::nullopt};
132+
std::unique_ptr<PartitionSummary> partition_summary_;
98133
};
99134

100135
/// \brief Adapter for appending a list of `ManifestFile`s to an `ArrowArray`.
@@ -104,7 +139,7 @@ class ICEBERG_EXPORT ManifestFileAdapter : public ManifestAdapter {
104139
ManifestFileAdapter() = default;
105140
~ManifestFileAdapter() override;
106141

107-
virtual Status Append(const ManifestFile& file) = 0;
142+
virtual Status Append(ManifestFile& file) = 0;
108143

109144
const std::shared_ptr<Schema>& schema() const { return manifest_list_schema_; }
110145

src/iceberg/manifest_entry.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ ICEBERG_EXPORT constexpr Result<ManifestStatus> ManifestStatusFromInt(
5757
}
5858
}
5959

60-
enum class ManifestContent {
61-
kData = 0,
62-
kDeletes = 1,
63-
};
64-
65-
ICEBERG_EXPORT constexpr std::string_view ToString(ManifestContent content) noexcept;
66-
ICEBERG_EXPORT constexpr Result<ManifestContent> ManifestContentFromString(
67-
std::string_view str) noexcept;
68-
6960
/// \brief DataFile carries data file path, partition tuple, metrics, ...
7061
struct ICEBERG_EXPORT DataFile {
7162
/// \brief Content of a data file
@@ -315,6 +306,17 @@ struct ICEBERG_EXPORT ManifestEntry {
315306
inline static const SchemaField kFileSequenceNumber =
316307
SchemaField::MakeOptional(4, "file_sequence_number", iceberg::int64());
317308

309+
/// \brief Check if this manifest entry is deleted.
310+
constexpr bool IsAlive() const {
311+
return status == ManifestStatus::kAdded || status == ManifestStatus::kExisting;
312+
}
313+
314+
/// \brief Create a copy of this manifest entry.
315+
ManifestEntry Copy() const {
316+
ManifestEntry copy = *this;
317+
return copy;
318+
}
319+
318320
bool operator==(const ManifestEntry& other) const;
319321

320322
static std::shared_ptr<StructType> TypeFromPartitionType(
@@ -323,6 +325,19 @@ struct ICEBERG_EXPORT ManifestEntry {
323325
std::shared_ptr<StructType> datafile_type);
324326
};
325327

328+
/// \brief Get the relative datafile content type name
329+
ICEBERG_EXPORT constexpr std::string_view ToString(DataFile::Content type) noexcept {
330+
switch (type) {
331+
case DataFile::Content::kData:
332+
return "data";
333+
case DataFile::Content::kPositionDeletes:
334+
return "position_deletes";
335+
case DataFile::Content::kEqualityDeletes:
336+
return "equality_deletes";
337+
}
338+
std::unreachable();
339+
}
340+
326341
/// \brief Get the relative data file content type from int
327342
ICEBERG_EXPORT constexpr Result<DataFile::Content> DataFileContentFromInt(
328343
int content) noexcept {

0 commit comments

Comments
 (0)