2626#include " iceberg/arrow/nanoarrow_status_internal.h"
2727#include " iceberg/manifest_entry.h"
2828#include " iceberg/manifest_list.h"
29+ #include " iceberg/partition_summary_internal.h"
2930#include " iceberg/result.h"
3031#include " iceberg/schema.h"
3132#include " iceberg/schema_internal.h"
@@ -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,82 @@ ManifestEntryAdapter::~ManifestEntryAdapter() {
161164 }
162165}
163166
167+ Status ManifestEntryAdapter::AddEntry (ManifestEntry& entry) {
168+ entry.status = ManifestStatus::kAdded ;
169+ entry.snapshot_id = snapshot_id_;
170+ if (entry.sequence_number .has_value () &&
171+ entry.sequence_number .value () < TableMetadata::kInitialSequenceNumber ) {
172+ entry.sequence_number = std::nullopt ;
173+ }
174+ entry.file_sequence_number = std::nullopt ;
175+ return AddEntryInternal (entry);
176+ }
177+
178+ Status ManifestEntryAdapter::AddDeleteEntry (ManifestEntry& entry) {
179+ entry.status = ManifestStatus::kDeleted ;
180+ entry.snapshot_id = snapshot_id_;
181+ return AddEntryInternal (entry);
182+ }
183+
184+ Status ManifestEntryAdapter::AddExistingEntry (ManifestEntry& entry) {
185+ entry.status = ManifestStatus::kExisting ;
186+ return AddEntryInternal (entry);
187+ }
188+
189+ std::unique_ptr<ManifestFile> ManifestEntryAdapter::ToManifestFile () const {
190+ auto manifest_file = std::make_unique<ManifestFile>();
191+ manifest_file->partition_spec_id = partition_spec_->spec_id ();
192+ manifest_file->content = content_;
193+ // sequence_number and min_sequence_number with UNASSIGNED_SEQUENCE_NUMBER will be
194+ // replace with real sequence number in `ManifestListWriter`.
195+ manifest_file->sequence_number = TableMetadata::kInvalidSequenceNumber ;
196+ manifest_file->min_sequence_number =
197+ min_sequence_number_.value_or (TableMetadata::kInvalidSequenceNumber );
198+ manifest_file->existing_files_count = existing_files_count_;
199+ manifest_file->added_snapshot_id = snapshot_id_.value_or (Snapshot::kInvalidSnapshotId );
200+ manifest_file->added_files_count = add_files_count_;
201+ manifest_file->existing_files_count = existing_files_count_;
202+ manifest_file->deleted_files_count = delete_files_count_;
203+ manifest_file->added_rows_count = add_rows_count_;
204+ manifest_file->existing_rows_count = existing_rows_count_;
205+ manifest_file->deleted_rows_count = delete_rows_count_;
206+ manifest_file->partitions = std::move (partition_summary_->Summaries ());
207+ return manifest_file;
208+ }
209+
210+ Status ManifestEntryAdapter::AddEntryInternal (const ManifestEntry& entry) {
211+ if (entry.data_file == nullptr ) [[unlikely]] {
212+ return InvalidManifest (" Missing required data_file field from manifest entry." );
213+ }
214+ switch (entry.status ) {
215+ case ManifestStatus::kAdded :
216+ add_files_count_++;
217+ add_rows_count_ += entry.data_file ->record_count ;
218+ break ;
219+ case ManifestStatus::kExisting :
220+ existing_files_count_++;
221+ existing_rows_count_ += entry.data_file ->record_count ;
222+ break ;
223+ case ManifestStatus::kDeleted :
224+ delete_files_count_++;
225+ delete_rows_count_ += entry.data_file ->record_count ;
226+ break ;
227+ default :
228+ std::unreachable ();
229+ }
230+
231+ partition_summary_->Update (entry.data_file ->partition );
232+
233+ if (entry.IsAlive () && entry.sequence_number .has_value ()) {
234+ if (!min_sequence_number_.has_value () ||
235+ entry.sequence_number .value () < min_sequence_number_.value ()) {
236+ min_sequence_number_ = entry.sequence_number .value ();
237+ }
238+ }
239+
240+ return AppendInternal (entry);
241+ }
242+
164243Status ManifestEntryAdapter::AppendPartitionValues (
165244 ArrowArray* array, const std::shared_ptr<StructType>& partition_type,
166245 const std::vector<Literal>& partition_values) {
0 commit comments