Skip to content

Commit 296d04f

Browse files
committed
handle initial sequence number
1 parent 8847ca8 commit 296d04f

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/iceberg/json_internal.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "iceberg/json_internal.h"
2121

22+
#include <cstdint>
2223
#include <format>
2324
#include <regex>
2425
#include <unordered_set>
@@ -82,6 +83,8 @@ constexpr std::string_view kMinSnapshotsToKeep = "min-snapshots-to-keep";
8283
constexpr std::string_view kMaxSnapshotAgeMs = "max-snapshot-age-ms";
8384
constexpr std::string_view kMaxRefAgeMs = "max-ref-age-ms";
8485

86+
constexpr int64_t kInitialSequenceNumber = 0;
87+
8588
const std::unordered_set<std::string_view> kValidSnapshotSummaryFields = {
8689
SnapshotSummaryFields::kOperation,
8790
SnapshotSummaryFields::kAddedDataFiles,
@@ -324,7 +327,9 @@ nlohmann::json ToJson(const Snapshot& snapshot) {
324327
nlohmann::json json;
325328
json[kSnapshotId] = snapshot.snapshot_id;
326329
SetOptionalField(json, kParentSnapshotId, snapshot.parent_snapshot_id);
327-
json[kSequenceNumber] = snapshot.sequence_number;
330+
if (snapshot.sequence_number > kInitialSequenceNumber) {
331+
json[kSequenceNumber] = snapshot.sequence_number;
332+
}
328333
json[kTimestampMs] = snapshot.timestamp_ms;
329334
json[kManifestList] = snapshot.manifest_list;
330335
// If there is an operation, write the summary map
@@ -552,7 +557,7 @@ Result<std::unique_ptr<SnapshotRef>> SnapshotRefFromJson(const nlohmann::json& j
552557
Result<std::unique_ptr<Snapshot>> SnapshotFromJson(const nlohmann::json& json) {
553558
ICEBERG_ASSIGN_OR_RAISE(auto snapshot_id, GetJsonValue<int64_t>(json, kSnapshotId));
554559
ICEBERG_ASSIGN_OR_RAISE(auto sequence_number,
555-
GetJsonValue<int64_t>(json, kSequenceNumber));
560+
GetJsonValueOptional<int64_t>(json, kSequenceNumber));
556561
ICEBERG_ASSIGN_OR_RAISE(auto timestamp_ms, GetJsonValue<int64_t>(json, kTimestampMs));
557562
ICEBERG_ASSIGN_OR_RAISE(auto manifest_list,
558563
GetJsonValue<std::string>(json, kManifestList));
@@ -591,9 +596,10 @@ Result<std::unique_ptr<Snapshot>> SnapshotFromJson(const nlohmann::json& json) {
591596

592597
ICEBERG_ASSIGN_OR_RAISE(auto schema_id, GetJsonValueOptional<int32_t>(json, kSchemaId));
593598

594-
return std::make_unique<Snapshot>(snapshot_id, parent_snapshot_id, sequence_number,
595-
timestamp_ms, manifest_list, std::move(summary),
596-
schema_id);
599+
return std::make_unique<Snapshot>(
600+
snapshot_id, parent_snapshot_id,
601+
sequence_number.has_value() ? *sequence_number : kInitialSequenceNumber,
602+
timestamp_ms, manifest_list, std::move(summary), schema_id);
597603
}
598604

599605
} // namespace iceberg

0 commit comments

Comments
 (0)