forked from apache/iceberg-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerging_snapshot_update_test.cc
More file actions
2229 lines (1884 loc) · 95.9 KB
/
Copy pathmerging_snapshot_update_test.cc
File metadata and controls
2229 lines (1884 loc) · 95.9 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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/update/merging_snapshot_update.h"
#include <limits>
#include <memory>
#include <optional>
#include <ranges>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "iceberg/avro/avro_register.h"
#include "iceberg/constants.h"
#include "iceberg/expression/expressions.h"
#include "iceberg/manifest/manifest_entry.h"
#include "iceberg/manifest/manifest_reader.h"
#include "iceberg/manifest/manifest_writer.h"
#include "iceberg/metrics/commit_report.h"
#include "iceberg/metrics/metrics_reporter.h"
#include "iceberg/partition_spec.h"
#include "iceberg/puffin_dv_io.h"
#include "iceberg/row/partition_values.h"
#include "iceberg/schema.h"
#include "iceberg/snapshot.h"
#include "iceberg/table.h"
#include "iceberg/table_metadata.h"
#include "iceberg/table_properties.h"
#include "iceberg/test/executor.h"
#include "iceberg/test/matchers.h"
#include "iceberg/test/retry.h"
#include "iceberg/test/update_test_base.h"
#include "iceberg/transaction.h"
#include "iceberg/update/fast_append.h"
#include "iceberg/update/merge_append.h"
#include "iceberg/update/row_delta.h"
#include "iceberg/update/snapshot_manager.h"
#include "iceberg/update/update_properties.h"
#include "iceberg/util/macros.h"
namespace iceberg {
namespace {
class MergingSnapshotCapturingReporter final : public MetricsReporter {
public:
Status Report(const MetricsReport& report) override {
reports.push_back(report);
return {};
}
std::vector<MetricsReport> reports;
};
} // namespace
class RecordingPuffinDVIO final : public PuffinDVIO {
public:
struct Call {
std::string output_path;
std::vector<DeletionVectorMergeGroup> groups;
};
Result<std::vector<std::shared_ptr<DataFile>>> MergeAndWriteDVs(
std::span<const DeletionVectorMergeGroup> groups, std::string_view output_path,
const std::shared_ptr<FileIO>& /*io*/) override {
calls.push_back(Call{.output_path = std::string(output_path),
.groups = {groups.begin(), groups.end()}});
std::vector<std::shared_ptr<DataFile>> result;
result.reserve(groups.size());
for (const auto& group : groups) {
auto file = std::make_shared<DataFile>(*group.delete_files.front());
file->file_path = std::string(output_path);
file->file_format = FileFormatType::kPuffin;
file->referenced_data_file = group.referenced_data_file;
file->record_count = 0;
for (const auto& delete_file : group.delete_files) {
file->record_count += delete_file->record_count;
}
file->content_offset = static_cast<int64_t>(result.size()) * 100;
file->content_size_in_bytes = 100;
result.push_back(std::move(file));
}
return result;
}
std::vector<Call> calls;
};
class ScopedPuffinDVIORegistry {
public:
explicit ScopedPuffinDVIORegistry(PuffinDVIOFactory factory)
: previous_factory_(std::move(PuffinDVIORegistry::GetFactory())) {
PuffinDVIORegistry::GetFactory() = std::move(factory);
}
~ScopedPuffinDVIORegistry() {
PuffinDVIORegistry::GetFactory() = std::move(previous_factory_);
}
private:
PuffinDVIOFactory previous_factory_;
};
/// \brief Concrete subclass of MergingSnapshotUpdate for testing.
class TestMergeAppend : public MergingSnapshotUpdate {
public:
static Result<std::unique_ptr<TestMergeAppend>> Make(std::string table_name,
std::shared_ptr<Table> table) {
ICEBERG_ASSIGN_OR_RAISE(
auto ctx, TransactionContext::Make(std::move(table), TransactionKind::kUpdate));
return std::unique_ptr<TestMergeAppend>(
new TestMergeAppend(std::move(table_name), std::move(ctx)));
}
std::string operation() override { return "append"; }
// Expose protected API for test access
using MergingSnapshotUpdate::Apply;
using MergingSnapshotUpdate::CleanUncommitted;
using MergingSnapshotUpdate::Summary;
Status AddFile(std::shared_ptr<DataFile> file) { return AddDataFile(std::move(file)); }
Status AddDelete(std::shared_ptr<DataFile> file) {
return AddDeleteFile(std::move(file));
}
Status AddDelete(std::shared_ptr<DataFile> file, int64_t data_sequence_number) {
return AddDeleteFile(std::move(file), data_sequence_number);
}
Status ValidateNoNewDeletesForDataFiles(const TableMetadata& metadata,
int64_t starting_snapshot_id,
std::shared_ptr<Expression> data_filter,
const DataFileSet& replaced_files,
const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io) const {
return MergingSnapshotUpdate::ValidateNoNewDeletesForDataFiles(
metadata, starting_snapshot_id, std::move(data_filter), replaced_files, parent,
std::move(io), IsCaseSensitive());
}
Status RemoveDataFile(std::shared_ptr<DataFile> file) {
return DeleteDataFile(std::move(file));
}
Status RemoveDeleteFile(std::shared_ptr<DataFile> file) {
return DeleteDeleteFile(std::move(file));
}
Status AppendManifest(ManifestFile manifest) {
return AddManifest(std::move(manifest));
}
Result<std::shared_ptr<PartitionSpec>> DataSpec() const {
return MergingSnapshotUpdate::DataSpec();
}
Result<std::vector<ManifestFile>> WriteDeletesForTest(
std::span<const std::shared_ptr<DataFile>> files,
const std::shared_ptr<PartitionSpec>& spec) {
auto entries = files | std::views::transform([](const auto& file) {
return ContentFileWithSequenceNumber{
.file = file, .data_sequence_number = std::nullopt};
}) |
std::ranges::to<std::vector>();
return WriteDeleteManifests(entries, spec);
}
int64_t GeneratedSnapshotId() { return SnapshotId(); }
void SetDataSeqNumber(int64_t seq) { SetNewDataFilesDataSequenceNumber(seq); }
void SetCaseSensitive(bool case_sensitive) { CaseSensitive(case_sensitive); }
static Status ValidateAddedDataFilesForTest(const TableMetadata& metadata,
std::optional<int64_t> starting_snapshot_id,
const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io) {
return MergingSnapshotUpdate::ValidateAddedDataFiles(metadata, starting_snapshot_id,
nullptr, parent, std::move(io));
}
static Status ValidateAddedDataFilesForTest(const TableMetadata& metadata,
int64_t starting_snapshot_id,
const PartitionSet& partition_set,
const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io) {
return MergingSnapshotUpdate::ValidateAddedDataFiles(
metadata, starting_snapshot_id, partition_set, parent, std::move(io));
}
static Status ValidateDataFilesExistForTest(
const TableMetadata& metadata, int64_t starting_snapshot_id,
const std::unordered_set<std::string>& file_paths, bool skip_deletes,
std::shared_ptr<Expression> filter, const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io, bool case_sensitive = true) {
return MergingSnapshotUpdate::ValidateDataFilesExist(
metadata, starting_snapshot_id, file_paths, skip_deletes, std::move(filter),
parent, std::move(io), case_sensitive);
}
static Status ValidateNoNewDeletesForDataFilesForTest(
const TableMetadata& metadata, int64_t starting_snapshot_id,
const DataFileSet& replaced_files, const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io, bool ignore_equality_deletes = false) {
return MergingSnapshotUpdate::ValidateNoNewDeletesForDataFiles(
metadata, starting_snapshot_id, replaced_files, parent, std::move(io),
ignore_equality_deletes);
}
static Status ValidateNoNewDeletesForDataFilesForTest(
const TableMetadata& metadata, int64_t starting_snapshot_id,
std::shared_ptr<Expression> data_filter, const DataFileSet& replaced_files,
const std::shared_ptr<Snapshot>& parent, std::shared_ptr<FileIO> io) {
return MergingSnapshotUpdate::ValidateNoNewDeletesForDataFiles(
metadata, starting_snapshot_id, std::move(data_filter), replaced_files, parent,
std::move(io));
}
static Status ValidateNoNewDeleteFilesForTest(const TableMetadata& metadata,
int64_t starting_snapshot_id,
std::shared_ptr<Expression> data_filter,
const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io) {
return MergingSnapshotUpdate::ValidateNoNewDeleteFiles(
metadata, starting_snapshot_id, std::move(data_filter), parent, std::move(io));
}
static Status ValidateNoNewDeleteFilesForTest(const TableMetadata& metadata,
int64_t starting_snapshot_id,
const PartitionSet& partition_set,
const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io) {
return MergingSnapshotUpdate::ValidateNoNewDeleteFiles(
metadata, starting_snapshot_id, partition_set, parent, std::move(io));
}
static Status ValidateDeletedDataFilesForTest(const TableMetadata& metadata,
int64_t starting_snapshot_id,
std::shared_ptr<Expression> data_filter,
const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io) {
return MergingSnapshotUpdate::ValidateDeletedDataFiles(
metadata, starting_snapshot_id, std::move(data_filter), parent, std::move(io));
}
static Status ValidateDeletedDataFilesForTest(const TableMetadata& metadata,
int64_t starting_snapshot_id,
const PartitionSet& partition_set,
const std::shared_ptr<Snapshot>& parent,
std::shared_ptr<FileIO> io) {
return MergingSnapshotUpdate::ValidateDeletedDataFiles(
metadata, starting_snapshot_id, partition_set, parent, std::move(io));
}
static Status ValidateAddedDVsForTest(
const TableMetadata& metadata, int64_t starting_snapshot_id,
std::shared_ptr<Expression> conflict_filter,
const std::unordered_set<std::string>& referenced_data_files,
const std::shared_ptr<Snapshot>& parent, std::shared_ptr<FileIO> io) {
return MergingSnapshotUpdate::ValidateAddedDVs(
metadata, starting_snapshot_id, std::move(conflict_filter), referenced_data_files,
parent, std::move(io));
}
bool HasDataFiles() const { return AddsDataFiles(); }
bool HasDeleteFiles() const { return AddsDeleteFiles(); }
bool HasDataDeletes() const { return DeletesDataFiles(); }
private:
TestMergeAppend(std::string table_name, std::shared_ptr<TransactionContext> ctx)
: MergingSnapshotUpdate(std::move(table_name), std::move(ctx)) {}
};
class TestOverwriteUpdate : public MergingSnapshotUpdate {
public:
static Result<std::unique_ptr<TestOverwriteUpdate>> Make(std::string table_name,
std::shared_ptr<Table> table) {
ICEBERG_ASSIGN_OR_RAISE(
auto ctx, TransactionContext::Make(std::move(table), TransactionKind::kUpdate));
return std::unique_ptr<TestOverwriteUpdate>(
new TestOverwriteUpdate(std::move(table_name), std::move(ctx)));
}
std::string operation() override { return DataOperation::kOverwrite; }
int64_t GeneratedSnapshotId() { return SnapshotId(); }
using MergingSnapshotUpdate::Apply;
Status AddDelete(std::shared_ptr<DataFile> file) {
return AddDeleteFile(std::move(file));
}
Status AddDelete(std::shared_ptr<DataFile> file, int64_t data_sequence_number) {
return AddDeleteFile(std::move(file), data_sequence_number);
}
Status AddFile(std::shared_ptr<DataFile> file) { return AddDataFile(std::move(file)); }
Status RemoveDataFile(std::shared_ptr<DataFile> file) {
return DeleteDataFile(std::move(file));
}
private:
TestOverwriteUpdate(std::string table_name, std::shared_ptr<TransactionContext> ctx)
: MergingSnapshotUpdate(std::move(table_name), std::move(ctx)) {}
};
class MergingSnapshotUpdateTest : public MinimalUpdateTestBase {
protected:
static void SetUpTestSuite() { avro::RegisterAll(); }
void SetUp() override {
MinimalUpdateTestBase::SetUp();
ICEBERG_UNWRAP_OR_FAIL(spec_, table_->spec());
ICEBERG_UNWRAP_OR_FAIL(schema_, table_->schema());
file_a_ = MakeDataFile("/data/file_a.parquet", /*partition_x=*/1L);
file_b_ = MakeDataFile("/data/file_b.parquet", /*partition_x=*/2L);
file_c_ = MakeDataFile("/data/file_c.parquet", /*partition_x=*/3L);
}
std::shared_ptr<DataFile> MakeDataFile(const std::string& path, int64_t partition_x) {
auto f = std::make_shared<DataFile>();
f->content = DataFile::Content::kData;
f->file_path = table_location_ + path;
f->file_format = FileFormatType::kParquet;
f->partition = PartitionValues(std::vector<Literal>{Literal::Long(partition_x)});
f->file_size_in_bytes = 1024;
f->record_count = 100;
f->partition_spec_id = spec_->spec_id();
return f;
}
std::shared_ptr<DataFile> MakeDeleteFile(const std::string& path, int64_t partition_x) {
auto f = MakeDataFile(path, partition_x);
f->content = DataFile::Content::kPositionDeletes;
return f;
}
std::shared_ptr<DataFile> MakeDeletionVector(const std::string& path,
const std::shared_ptr<DataFile>& data_file,
int64_t record_count) {
auto f = MakeDeleteFile(path, 0);
f->file_format = FileFormatType::kPuffin;
f->partition = data_file->partition;
f->partition_spec_id = data_file->partition_spec_id;
f->referenced_data_file = data_file->file_path;
f->record_count = record_count;
f->content_offset = 0;
f->content_size_in_bytes = 100;
return f;
}
std::shared_ptr<DataFile> MakeEqualityDeleteFile(const std::string& path,
int64_t partition_x) {
auto f = MakeDeleteFile(path, partition_x);
f->content = DataFile::Content::kEqualityDeletes;
f->equality_ids = {1};
return f;
}
Result<std::unique_ptr<TestMergeAppend>> NewMergeAppend() {
return TestMergeAppend::Make(TableName(), table_);
}
Result<std::unique_ptr<TestOverwriteUpdate>> NewOverwriteUpdate() {
return TestOverwriteUpdate::Make(TableName(), table_);
}
void UpgradeTableToV3() {
ICEBERG_UNWRAP_OR_FAIL(auto props, table_->NewUpdateProperties());
props->Set(TableProperties::kFormatVersion.key(), "3");
EXPECT_THAT(props->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
}
void SetTableFormatVersion(int8_t format_version) {
table_->metadata()->format_version = format_version;
}
void SetManifestTargetSizeBytes(int64_t size_bytes) {
ICEBERG_UNWRAP_OR_FAIL(auto props, table_->NewUpdateProperties());
props->Set(std::string(TableProperties::kManifestTargetSizeBytes.key()),
std::to_string(size_bytes));
EXPECT_THAT(props->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
}
// Commit file_a_ with FastAppend and refresh the table.
void CommitFileA() {
ICEBERG_UNWRAP_OR_FAIL(auto fa, table_->NewFastAppend());
fa->AppendFile(file_a_);
EXPECT_THAT(fa->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
}
void CommitV2FilesBeforeUpgrade() {
ASSERT_EQ(table_->metadata()->format_version, 2);
ICEBERG_UNWRAP_OR_FAIL(auto append, table_->NewFastAppend());
append->AppendFile(file_a_).AppendFile(file_b_);
ASSERT_THAT(append->Commit(), IsOk());
ASSERT_THAT(table_->Refresh(), IsOk());
auto equality_delete =
MakeEqualityDeleteFile("/delete/upgrade_eq_delete.parquet", 1L);
ICEBERG_UNWRAP_OR_FAIL(auto delta, table_->NewRowDelta());
delta->AddDeletes(equality_delete);
ASSERT_THAT(delta->Commit(), IsOk());
ASSERT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto overwrite, NewOverwriteUpdate());
ASSERT_THAT(overwrite->RemoveDataFile(file_b_), IsOk());
ASSERT_THAT(overwrite->AddFile(file_c_), IsOk());
ASSERT_THAT(overwrite->Commit(), IsOk());
ASSERT_THAT(table_->Refresh(), IsOk());
}
// Read all entries from a list of ManifestFiles.
Result<std::vector<ManifestEntry>> ReadAllEntries(
const std::vector<ManifestFile>& manifests, const TableMetadata& metadata) {
std::vector<ManifestEntry> result;
for (const auto& m : manifests) {
ICEBERG_ASSIGN_OR_RAISE(auto spec, metadata.PartitionSpecById(m.partition_spec_id));
ICEBERG_ASSIGN_OR_RAISE(auto schema, metadata.Schema());
ICEBERG_ASSIGN_OR_RAISE(auto reader,
ManifestReader::Make(m, file_io_, schema, spec));
ICEBERG_ASSIGN_OR_RAISE(auto entries, reader->Entries());
result.insert(result.end(), entries.begin(), entries.end());
}
return result;
}
Result<std::unordered_map<std::string, std::optional<int64_t>>> DataFileFirstRowIds(
const std::shared_ptr<Snapshot>& snapshot, const TableMetadata& metadata) {
SnapshotCache snapshot_cache(snapshot.get());
ICEBERG_ASSIGN_OR_RAISE(auto manifest_range, snapshot_cache.DataManifests(file_io_));
std::vector<ManifestFile> manifests(manifest_range.begin(), manifest_range.end());
ICEBERG_ASSIGN_OR_RAISE(auto entries, ReadAllEntries(manifests, metadata));
std::unordered_map<std::string, std::optional<int64_t>> first_row_ids;
for (const auto& entry : entries) {
if (entry.data_file != nullptr) {
first_row_ids.emplace(entry.data_file->file_path, entry.data_file->first_row_id);
}
}
return first_row_ids;
}
// Write a manifest file containing the given data files.
// Returns a ManifestFile with added_snapshot_id = kInvalidSnapshotId so it
// is eligible for snapshot ID inheritance.
Result<ManifestFile> WriteManifest(
const std::string& path, const std::vector<std::shared_ptr<DataFile>>& files) {
ICEBERG_ASSIGN_OR_RAISE(
auto writer,
ManifestWriter::MakeWriter(/*format_version=*/2, kInvalidSnapshotId, path,
file_io_, spec_, schema_, ManifestContent::kData));
for (const auto& f : files) {
ManifestEntry entry;
entry.status = ManifestStatus::kAdded;
entry.snapshot_id = std::nullopt;
entry.data_file = f;
ICEBERG_RETURN_UNEXPECTED(writer->WriteAddedEntry(entry));
}
ICEBERG_RETURN_UNEXPECTED(writer->Close());
return writer->ToManifestFile();
}
Result<ManifestFile> WriteDataManifest(
const TableMetadata& metadata, const std::string& path,
const std::vector<std::shared_ptr<DataFile>>& files, int64_t snapshot_id,
int64_t sequence_number) {
ICEBERG_ASSIGN_OR_RAISE(auto schema, metadata.Schema());
ICEBERG_ASSIGN_OR_RAISE(auto spec, metadata.PartitionSpecById(spec_->spec_id()));
ICEBERG_ASSIGN_OR_RAISE(
auto writer,
ManifestWriter::MakeWriter(metadata.format_version, snapshot_id, path, file_io_,
spec, schema, ManifestContent::kData));
for (const auto& f : files) {
ICEBERG_RETURN_UNEXPECTED(writer->WriteAddedEntry(f, sequence_number));
}
ICEBERG_RETURN_UNEXPECTED(writer->Close());
return writer->ToManifestFile();
}
Result<ManifestFile> WriteDeleteManifest(
const TableMetadata& metadata, const std::string& path,
const std::vector<std::shared_ptr<DataFile>>& files, int64_t snapshot_id,
int64_t sequence_number) {
ICEBERG_ASSIGN_OR_RAISE(auto schema, metadata.Schema());
ICEBERG_ASSIGN_OR_RAISE(auto spec, metadata.PartitionSpecById(spec_->spec_id()));
ICEBERG_ASSIGN_OR_RAISE(
auto writer,
ManifestWriter::MakeWriter(metadata.format_version, snapshot_id, path, file_io_,
spec, schema, ManifestContent::kDeletes));
for (const auto& f : files) {
ManifestEntry entry;
entry.status = ManifestStatus::kAdded;
entry.snapshot_id = snapshot_id;
entry.sequence_number = sequence_number;
entry.data_file = f;
ICEBERG_RETURN_UNEXPECTED(writer->WriteAddedEntry(entry));
}
ICEBERG_RETURN_UNEXPECTED(writer->Close());
return writer->ToManifestFile();
}
Result<std::shared_ptr<Snapshot>> MakeSyntheticSnapshot(
std::string operation, int64_t snapshot_id,
std::optional<int64_t> parent_snapshot_id, int64_t sequence_number,
const std::vector<ManifestFile>& manifests) {
auto manifest_list_path = table_location_ + "/metadata/manifest-list-" +
std::to_string(snapshot_id) + ".avro";
ICEBERG_ASSIGN_OR_RAISE(
auto writer,
ManifestListWriter::MakeWriter(table_->metadata()->format_version, snapshot_id,
parent_snapshot_id, manifest_list_path, file_io_,
sequence_number));
ICEBERG_RETURN_UNEXPECTED(writer->AddAll(manifests));
ICEBERG_RETURN_UNEXPECTED(writer->Close());
ICEBERG_ASSIGN_OR_RAISE(
auto snapshot,
Snapshot::Make(sequence_number, snapshot_id, parent_snapshot_id, TimePointMs{},
std::move(operation), {}, table_->metadata()->current_schema_id,
manifest_list_path));
return std::shared_ptr<Snapshot>(std::move(snapshot));
}
std::shared_ptr<PartitionSpec> spec_;
std::shared_ptr<Schema> schema_;
std::shared_ptr<DataFile> file_a_;
std::shared_ptr<DataFile> file_b_;
std::shared_ptr<DataFile> file_c_;
};
// -------------------------------------------------------------------------
// State query tests
// -------------------------------------------------------------------------
TEST_F(MergingSnapshotUpdateTest, AddsDataFilesInitiallyFalse) {
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_FALSE(op->HasDataFiles());
EXPECT_FALSE(op->HasDeleteFiles());
EXPECT_FALSE(op->HasDataDeletes());
}
TEST_F(MergingSnapshotUpdateTest, AddsDataFilesTrueAfterAdd) {
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_TRUE(op->HasDataFiles());
EXPECT_FALSE(op->HasDeleteFiles());
}
TEST_F(MergingSnapshotUpdateTest, AddsDeleteFilesTrueAfterAdd) {
auto del_file = MakeEqualityDeleteFile("/delete/del_a.parquet", 1L);
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddDelete(del_file), IsOk());
EXPECT_FALSE(op->HasDataFiles());
EXPECT_TRUE(op->HasDeleteFiles());
}
TEST_F(MergingSnapshotUpdateTest, DeletesDataFilesTrueAfterRegisterDelete) {
CommitFileA();
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->RemoveDataFile(file_a_), IsOk());
EXPECT_TRUE(op->HasDataDeletes());
}
// -------------------------------------------------------------------------
// Apply / Commit tests
// -------------------------------------------------------------------------
TEST_F(MergingSnapshotUpdateTest, CommitNewDataFile) {
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDataFiles), "1");
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedRecords), "100");
}
TEST_F(MergingSnapshotUpdateTest, CommitReportsCreatedSnapshot) {
auto reporter = std::make_shared<MergingSnapshotCapturingReporter>();
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
op->ReportWith(reporter);
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
ASSERT_EQ(reporter->reports.size(), 1U);
ASSERT_TRUE(std::holds_alternative<CommitReport>(reporter->reports.front()));
const auto& report = std::get<CommitReport>(reporter->reports.front());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(report.table_name, table_->full_name());
EXPECT_EQ(report.operation, DataOperation::kAppend);
EXPECT_EQ(report.snapshot_id, snapshot->snapshot_id);
EXPECT_EQ(report.sequence_number, snapshot->sequence_number);
ASSERT_TRUE(report.commit_metrics.added_data_files.has_value());
EXPECT_EQ(report.commit_metrics.added_data_files->value, 1);
ASSERT_TRUE(report.commit_metrics.added_records.has_value());
EXPECT_EQ(report.commit_metrics.added_records->value, 100);
}
TEST_F(MergingSnapshotUpdateTest, CommitV3NewDataFileAssignsRowLineage) {
UpgradeTableToV3();
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
ICEBERG_UNWRAP_OR_FAIL(auto first_row_id, snapshot->FirstRowId());
ICEBERG_UNWRAP_OR_FAIL(auto added_rows, snapshot->AddedRows());
EXPECT_EQ(first_row_id, std::make_optional<int64_t>(0));
EXPECT_EQ(added_rows, std::make_optional<int64_t>(100));
EXPECT_EQ(table_->metadata()->next_row_id, 100);
}
TEST_F(MergingSnapshotUpdateTest, V3MultiFileRowIds) {
UpgradeTableToV3();
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->AddFile(file_b_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
ICEBERG_UNWRAP_OR_FAIL(auto first_row_id, snapshot->FirstRowId());
ICEBERG_UNWRAP_OR_FAIL(auto added_rows, snapshot->AddedRows());
EXPECT_EQ(first_row_id, std::make_optional<int64_t>(0));
EXPECT_EQ(added_rows, std::make_optional<int64_t>(200));
EXPECT_EQ(table_->metadata()->next_row_id, 200);
ICEBERG_UNWRAP_OR_FAIL(auto first_row_ids,
DataFileFirstRowIds(snapshot, *table_->metadata()));
EXPECT_EQ(first_row_ids.at(file_a_->file_path), std::make_optional<int64_t>(0));
EXPECT_EQ(first_row_ids.at(file_b_->file_path), std::make_optional<int64_t>(100));
}
TEST_F(MergingSnapshotUpdateTest, V3BranchRowIds) {
UpgradeTableToV3();
CommitFileA();
ICEBERG_UNWRAP_OR_FAIL(auto starting_snapshot, table_->current_snapshot());
const auto starting_snapshot_id = starting_snapshot->snapshot_id;
const auto starting_next_row_id = table_->metadata()->next_row_id;
ICEBERG_UNWRAP_OR_FAIL(auto manager, table_->NewSnapshotManager());
manager->CreateBranch("audit", starting_snapshot_id);
EXPECT_THAT(manager->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
op->ToBranch("audit");
EXPECT_THAT(op->AddFile(file_b_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
EXPECT_EQ(table_->metadata()->next_row_id,
starting_next_row_id + file_b_->record_count);
ICEBERG_UNWRAP_OR_FAIL(auto current_snapshot, table_->current_snapshot());
EXPECT_EQ(current_snapshot->snapshot_id, starting_snapshot_id);
auto ref_it = table_->metadata()->refs.find("audit");
ASSERT_NE(ref_it, table_->metadata()->refs.end());
ICEBERG_UNWRAP_OR_FAIL(auto branch_snapshot,
table_->metadata()->SnapshotById(ref_it->second->snapshot_id));
ICEBERG_UNWRAP_OR_FAIL(auto first_row_id, branch_snapshot->FirstRowId());
ICEBERG_UNWRAP_OR_FAIL(auto added_rows, branch_snapshot->AddedRows());
EXPECT_EQ(first_row_id, std::make_optional(starting_next_row_id));
EXPECT_EQ(added_rows, std::make_optional(file_b_->record_count));
ICEBERG_UNWRAP_OR_FAIL(auto first_row_ids,
DataFileFirstRowIds(branch_snapshot, *table_->metadata()));
EXPECT_EQ(first_row_ids.at(file_a_->file_path), std::make_optional<int64_t>(0));
EXPECT_EQ(first_row_ids.at(file_b_->file_path),
std::make_optional(starting_next_row_id));
}
// A staged v3 append must reassign row IDs when a concurrent commit advances next_row_id.
TEST_F(MergingSnapshotUpdateTest, V3RetryRowIds) {
UpgradeTableToV3();
test::FakeRetryEnvironment fake_retry;
ScopedRetryTestHooks retry_hooks(fake_retry.hooks());
// Stage file_a_ with first_row_id = 0, but do not commit the transaction yet.
ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction());
ICEBERG_UNWRAP_OR_FAIL(auto append, txn->NewMergeAppend());
append->AppendFile(file_a_);
EXPECT_THAT(append->Commit(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto staged_snapshot, txn->current().Snapshot());
ICEBERG_UNWRAP_OR_FAIL(auto staged_first_row_id, staged_snapshot->FirstRowId());
EXPECT_EQ(staged_first_row_id, std::make_optional<int64_t>(0));
// Commit file_b_ first, advancing table next_row_id and making file_a_'s row IDs stale.
ICEBERG_UNWRAP_OR_FAIL(auto concurrent, table_->NewMergeAppend());
concurrent->AppendFile(file_b_);
EXPECT_THAT(concurrent->Commit(), IsOk());
auto after_concurrent = ReloadMetadata();
EXPECT_EQ(after_concurrent->next_row_id, file_b_->record_count);
// The original transaction must retry and reassign file_a_ after file_b_'s row IDs.
ICEBERG_UNWRAP_OR_FAIL(auto committed_table, txn->Commit());
EXPECT_FALSE(fake_retry.sleep_durations().empty());
EXPECT_EQ(committed_table->metadata()->next_row_id,
file_b_->record_count + file_a_->record_count);
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, committed_table->current_snapshot());
ICEBERG_UNWRAP_OR_FAIL(auto first_row_id, snapshot->FirstRowId());
ICEBERG_UNWRAP_OR_FAIL(auto added_rows, snapshot->AddedRows());
EXPECT_EQ(first_row_id, std::make_optional(file_b_->record_count));
EXPECT_EQ(added_rows, std::make_optional(file_a_->record_count));
ICEBERG_UNWRAP_OR_FAIL(auto first_row_ids,
DataFileFirstRowIds(snapshot, *committed_table->metadata()));
EXPECT_EQ(first_row_ids.at(file_b_->file_path), std::make_optional<int64_t>(0));
EXPECT_EQ(first_row_ids.at(file_a_->file_path),
std::make_optional(file_b_->record_count));
}
TEST_F(MergingSnapshotUpdateTest, V3UpgradeLeavesExistingRowsUnassigned) {
CommitV2FilesBeforeUpgrade();
const auto v2_snapshot_id = table_->metadata()->current_snapshot_id;
UpgradeTableToV3();
EXPECT_EQ(table_->metadata()->next_row_id, 0);
for (const auto& snapshot : table_->metadata()->snapshots) {
EXPECT_EQ(snapshot->first_row_id, std::nullopt);
EXPECT_EQ(snapshot->added_rows, std::nullopt);
}
ICEBERG_UNWRAP_OR_FAIL(auto current, table_->current_snapshot());
EXPECT_EQ(current->snapshot_id, v2_snapshot_id);
EXPECT_EQ(current->first_row_id, std::nullopt);
EXPECT_EQ(current->added_rows, std::nullopt);
SnapshotCache snapshot_cache(current.get());
ICEBERG_UNWRAP_OR_FAIL(auto data_manifest_range,
snapshot_cache.DataManifests(file_io_));
std::vector<ManifestFile> data_manifests(data_manifest_range.begin(),
data_manifest_range.end());
ASSERT_FALSE(data_manifests.empty());
for (const auto& manifest : data_manifests) {
EXPECT_EQ(manifest.first_row_id, std::nullopt);
}
ICEBERG_UNWRAP_OR_FAIL(auto first_row_ids,
DataFileFirstRowIds(current, *table_->metadata()));
EXPECT_EQ(first_row_ids.at(file_a_->file_path), std::nullopt);
EXPECT_EQ(first_row_ids.at(file_b_->file_path), std::nullopt);
EXPECT_EQ(first_row_ids.at(file_c_->file_path), std::nullopt);
ICEBERG_UNWRAP_OR_FAIL(auto delete_manifest_range,
snapshot_cache.DeleteManifests(file_io_));
ASSERT_FALSE(delete_manifest_range.empty());
for (const auto& manifest : delete_manifest_range) {
EXPECT_EQ(manifest.first_row_id, std::nullopt);
}
}
TEST_F(MergingSnapshotUpdateTest, V3FirstCommitAssignsExistingRowsAfterUpgrade) {
CommitV2FilesBeforeUpgrade();
UpgradeTableToV3();
ICEBERG_UNWRAP_OR_FAIL(auto append, table_->NewFastAppend());
ASSERT_THAT(append->Commit(), IsOk());
ASSERT_THAT(table_->Refresh(), IsOk());
const auto expected_rows = file_a_->record_count + file_c_->record_count;
ICEBERG_UNWRAP_OR_FAIL(auto assigned, table_->current_snapshot());
EXPECT_EQ(assigned->first_row_id, 0);
EXPECT_EQ(assigned->added_rows, expected_rows);
EXPECT_EQ(table_->metadata()->next_row_id, expected_rows);
ICEBERG_UNWRAP_OR_FAIL(auto first_row_ids,
DataFileFirstRowIds(assigned, *table_->metadata()));
ASSERT_TRUE(first_row_ids.at(file_a_->file_path).has_value());
ASSERT_TRUE(first_row_ids.at(file_c_->file_path).has_value());
EXPECT_EQ(first_row_ids.at(file_b_->file_path), std::nullopt);
EXPECT_NE(first_row_ids.at(file_a_->file_path), first_row_ids.at(file_c_->file_path));
EXPECT_THAT((std::vector<int64_t>{*first_row_ids.at(file_a_->file_path),
*first_row_ids.at(file_c_->file_path)}),
::testing::UnorderedElementsAre(0, file_a_->record_count));
SnapshotCache snapshot_cache(assigned.get());
ICEBERG_UNWRAP_OR_FAIL(auto delete_manifests, snapshot_cache.DeleteManifests(file_io_));
ASSERT_FALSE(delete_manifests.empty());
for (const auto& manifest : delete_manifests) {
EXPECT_EQ(manifest.first_row_id, std::nullopt);
}
}
TEST_F(MergingSnapshotUpdateTest, CommitMultipleDataFiles) {
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->AddFile(file_b_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDataFiles), "2");
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedRecords), "200");
}
TEST_F(MergingSnapshotUpdateTest, CommitDataFileAndDeleteFile) {
auto del_file = MakeEqualityDeleteFile("/delete/del_a.parquet", 1L);
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->AddDelete(del_file), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
// Data file summary
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDataFiles), "1");
}
TEST_F(MergingSnapshotUpdateTest, CommitPreservesExistingManifests) {
// First append: file_a
CommitFileA();
// Second merge append: file_b
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file_b_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kTotalDataFiles), "2");
}
TEST_F(MergingSnapshotUpdateTest, CommitDeletesDataFile) {
CommitFileA();
// Remove file_a via merging snapshot update
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->RemoveDataFile(file_a_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kTotalDataFiles), "0");
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kDeletedDataFiles), "1");
}
TEST_F(MergingSnapshotUpdateTest, SetNewDataFilesDataSequenceNumber) {
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
op->SetDataSeqNumber(42);
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDataFiles), "1");
auto snapshot_cache = SnapshotCache(snapshot.get());
ICEBERG_UNWRAP_OR_FAIL(auto data_manifests, snapshot_cache.DataManifests(table_->io()));
std::vector<ManifestFile> manifests(data_manifests.begin(), data_manifests.end());
ICEBERG_UNWRAP_OR_FAIL(auto entries, ReadAllEntries(manifests, *table_->metadata()));
ASSERT_EQ(entries.size(), 1U);
EXPECT_EQ(entries[0].sequence_number, 42);
}
TEST_F(MergingSnapshotUpdateTest, AddDataFileDoesNotMutateCallerFile) {
auto file = MakeDataFile("/data/with-row-id.parquet", 1L);
file->first_row_id = 42;
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file), IsOk());
EXPECT_EQ(file->first_row_id, 42);
}
TEST_F(MergingSnapshotUpdateTest, CustomSummaryPropertySurvivesApplyRebuild) {
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
op->Set("custom-prop", "custom-value");
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at("custom-prop"), "custom-value");
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDataFiles), "1");
}
TEST_F(MergingSnapshotUpdateTest, BaseSetCustomSummaryPropertySurvivesApplyRebuild) {
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
SnapshotUpdate& base_update = *op;
base_update.Set("custom-prop", "custom-value");
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at("custom-prop"), "custom-value");
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDataFiles), "1");
}
// -------------------------------------------------------------------------
// CleanUncommitted test
// -------------------------------------------------------------------------
TEST_F(MergingSnapshotUpdateTest, CleanUncommittedAfterSuccessfulCommitDoesNotCrash) {
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddFile(file_a_), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
// Cleanup may run from an error handler even after commit success.
EXPECT_THAT(op->CleanUncommitted({}), IsOk());
}
TEST_F(MergingSnapshotUpdateTest,
CleanUncommittedDeletesManagerOutputsWithDeleteCallback) {
ICEBERG_UNWRAP_OR_FAIL(auto initial, NewMergeAppend());
EXPECT_THAT(initial->AddFile(file_a_), IsOk());
EXPECT_THAT(initial->AddFile(file_b_), IsOk());
EXPECT_THAT(initial->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
std::vector<std::string> deleted_paths;
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
op->DeleteWith([&deleted_paths](const std::string& path) {
deleted_paths.push_back(path);
return Status{};
});
EXPECT_THAT(op->RemoveDataFile(file_a_), IsOk());
ICEBERG_UNWRAP_OR_FAIL(
auto manifests, op->Apply(*table_->metadata(), table_->current_snapshot().value()));
EXPECT_THAT(manifests, ::testing::SizeIs(1));
EXPECT_THAT(op->CleanUncommitted({}), IsOk());
EXPECT_THAT(deleted_paths, ::testing::Contains(::testing::HasSubstr("/metadata/")));
}
// -------------------------------------------------------------------------
// Delete file summary tests
// -------------------------------------------------------------------------
TEST_F(MergingSnapshotUpdateTest, CommitDeleteFileSummaryHasAddedDeleteFiles) {
auto del_file = MakeDeleteFile("/delete/del_a.parquet", 1L);
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddDelete(del_file), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDeleteFiles), "1");
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedPosDeleteFiles), "1");
EXPECT_EQ(snapshot->summary.count(SnapshotSummaryFields::kRemovedDeleteFiles), 0);
}
TEST_F(MergingSnapshotUpdateTest, CommitDeduplicatesStagedDeleteFiles) {
auto del_file = MakeDeleteFile("/delete/del_a.parquet", 1L);
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddDelete(del_file), IsOk());
EXPECT_THAT(op->AddDelete(del_file), IsOk());
EXPECT_THAT(op->Commit(), IsOk());
EXPECT_THAT(table_->Refresh(), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot());
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDeleteFiles), "1");
EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedPosDeleteFiles), "1");
}
TEST_F(MergingSnapshotUpdateTest, AddDeleteFileWithExplicitSequenceWritesSequenceNumber) {
auto del_file = MakeEqualityDeleteFile("/delete/del_a.parquet", 1L);
ICEBERG_UNWRAP_OR_FAIL(auto op, NewMergeAppend());
EXPECT_THAT(op->AddDelete(del_file, 17), IsOk());
ICEBERG_UNWRAP_OR_FAIL(auto manifests, op->Apply(*table_->metadata(), nullptr));
auto delete_manifest_it =
std::ranges::find_if(manifests, [](const ManifestFile& manifest) {
return manifest.content == ManifestContent::kDeletes;
});
ASSERT_NE(delete_manifest_it, manifests.end());
ICEBERG_UNWRAP_OR_FAIL(auto entries,
ReadAllEntries(std::vector<ManifestFile>{*delete_manifest_it},
*table_->metadata()));
ASSERT_EQ(entries.size(), 1U);
ASSERT_TRUE(entries[0].sequence_number.has_value());
EXPECT_EQ(entries[0].sequence_number.value(), 17);
}