Skip to content

fix(metadata): validate snapshot integers and allocation bounds - #1169

Open
manuzhang wants to merge 6 commits into
apache:mainfrom
manuzhang:fix/json-snapshot-sequence-and-row-id
Open

manuzhang wants to merge 6 commits into
apache:mainfrom
manuzhang:fix/json-snapshot-sequence-and-row-id

Conversation

@manuzhang

@manuzhang manuzhang commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Why

JSON conversion could truncate or wrap integer-valued snapshot and table metadata fields. A table could also carry bounds below values in its retained snapshots, so a later commit could allocate sequence numbers or row IDs from a stale starting point.

What

  • Decode snapshot integer fields and the table's sequence and row ID bounds exactly. Apply the same type and range checks to last-column-id, last-partition-id, default-sort-order-id, current-snapshot-id, and last-updated-ms. A missing or null current-snapshot-id still represents a table without a current snapshot.
  • Require each retained V2+ snapshot’s sequence-number to be between zero and last-sequence-number, inclusive. In V3, require each retained first-row-id to be at most next-row-id. Allow bounds above retained values after expiration and accept conservative V3 added-rows estimates.
  • Call TableMetadata::Validate() after reading all metadata fields, before writing JSON, and when building table metadata.

Validation

Local validation passed for table, JSON, catalog, REST, C++20, and 33 expiration tests, plus pre-commit. REST had one existing skip. All 339 table tests, all 181 JSON tests, and pre-commit passed with the review changes. The five additional fields are each tested with a fractional number, a boolean, and an integer above the field’s signed range. The regression test covers negative snapshot sequence numbers on V2 and V3 metadata reads, writes, and builds. The tests also reject negative table counters without retained snapshots. For directly constructed V3 snapshots, Build() rejects missing added_rows and negative row-lineage fields, and accepts first_row_id == next_row_id and sequence_number == last_sequence_number.

Follow-up

#1200 tracks checking that the current snapshot ID identifies a retained snapshot and that the main branch points to it.

Parse snapshot integer fields and table sequence and row ID bounds exactly. Check the bounds against retained snapshots on read, write, and build, including V3 snapshots with conservative added-rows bounds.

Co-authored-by: Codex <[email protected]>
Comment thread src/iceberg/table_metadata.cc Resolved
Comment thread src/iceberg/table_metadata.h Resolved
return *iter;
}

Status TableMetadata::Validate() const {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many more invariants check we can be added here. We can do it in a separate follow up. Can you please file an issue?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example - current-snapshot-id must exist, main branch must match current-snapshot-id, etc. but can be done in a follow up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #1200 to track.

Comment thread src/iceberg/json_serde.cc

Result<std::unique_ptr<Snapshot>> SnapshotFromJson(const nlohmann::json& json) {
ICEBERG_ASSIGN_OR_RAISE(auto snapshot_id, GetJsonValue<int64_t>(json, kSnapshotId));
ICEBERG_ASSIGN_OR_RAISE(auto snapshot_id, GetJsonInteger<int64_t>(json, kSnapshotId));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls apply the similar fix to other integer fields - last-column-id, last-partition-id, default-sort-order-id, current-snapshot-id and last-updated-ms.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed now

Comment thread src/iceberg/json_serde.cc
}

Result<nlohmann::json> ToJson(const TableMetadata& table_metadata) {
if (auto status = table_metadata.Validate(); !status) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need to call this inside ToJson? I was supposed to call Validate inside FromJson but ask users to call it out of ToJson. Not a strong opinion though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep it to enforce checks at writer side.

@timothyw553 timothyw553 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR, overall the validation changes look good. i left two comments: one on stale parentless snapshots failing instead of retrying, and one on test coverage.

// 1. Validate the format version and metadata consistency.
ICEBERG_PRECHECK(TableMetadata::IsSupportedFormatVersion(metadata_.format_version),
"Unsupported table format version: {}", metadata_.format_version);
ICEBERG_RETURN_UNEXPECTED(metadata_.Validate());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to my understanding, AddSnapshot() skips the RetryableValidationFailed check when parent_snapshot_id is absent and overwrites last_sequence_number with snapshot->sequence_number. if another writer already advanced last_sequence_number, won't Build() now fail permanently with kValidationFailed instead of retrying (for example on StageOnly() or a new branch)? should we check stale sequence numbers in AddSnapshot() for parentless snapshots too, and clarify how sequence == last should behave for the seq 0 init case?

@manuzhang manuzhang Dec 1, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising the issue. I've opened #1199 to track and would like to fix it after a more thorough discussion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sounds good

}
SCOPED_TRACE(field);
auto invalid = json;
invalid[field] = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting this to 0 only hits the retained-snapshot comparison, not the negative-counter checks. could we also test negative last-sequence-number and next-row-id without snapshots (e.g. adding int64_t{-1} in TableSequenceAndRowIdBoundsRejectInvalidJsonIntegers), plus directly constructed snapshots with missing added_rows or negative row-lineage fields? also, do we test the accepted boundaries like first_row_id == next_row_id and sequence_number == last_sequence_number through Build()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in new tests.

Comment thread src/iceberg/json_serde.cc Outdated

ICEBERG_ASSIGN_OR_RAISE(table_metadata->snapshots,
FromJsonList<Snapshot>(json, kSnapshots, SnapshotFromJson));
if (auto status = table_metadata->ValidateSequenceAndRowIdBounds(); !status) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say remove this and do it in a separate CR. We need a thoughtful consideration on how to do validation. We don't want these validation logics spread all over the places. And I don't think this is the right place for validation either. This class should just be doing json parsing. The semantics validation can be done on a higher level.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you agree, we should update the PR title. This PR should scope down to only fixing the potential JSON parsing issue. We open another issue for semantics validation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants