Repository navigation
Conversation
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]>
| return *iter; | ||
| } | ||
|
|
||
| Status TableMetadata::Validate() const { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
For example - current-snapshot-id must exist, main branch must match current-snapshot-id, etc. but can be done in a follow up.
|
|
||
| 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)); |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| Result<nlohmann::json> ToJson(const TableMetadata& table_metadata) { | ||
| if (auto status = table_metadata.Validate(); !status) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I'd keep it to enforce checks at writer side.
timothyw553
left a comment
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Thanks for raising the issue. I've opened #1199 to track and would like to fix it after a more thorough discussion.
| } | ||
| SCOPED_TRACE(field); | ||
| auto invalid = json; | ||
| invalid[field] = 0; |
There was a problem hiding this comment.
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()?
|
|
||
| ICEBERG_ASSIGN_OR_RAISE(table_metadata->snapshots, | ||
| FromJsonList<Snapshot>(json, kSnapshots, SnapshotFromJson)); | ||
| if (auto status = table_metadata->ValidateSequenceAndRowIdBounds(); !status) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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
last-column-id,last-partition-id,default-sort-order-id,current-snapshot-id, andlast-updated-ms. A missing or nullcurrent-snapshot-idstill represents a table without a current snapshot.sequence-numberto be between zero andlast-sequence-number, inclusive. In V3, require each retainedfirst-row-idto be at mostnext-row-id. Allow bounds above retained values after expiration and accept conservative V3added-rowsestimates.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 missingadded_rowsand negative row-lineage fields, and acceptsfirst_row_id == next_row_idandsequence_number == last_sequence_number.Follow-up
#1200 tracks checking that the current snapshot ID identifies a retained snapshot and that the
mainbranch points to it.