Skip to content

fix: SetSnapshotRef has to produce updates for all branches - #519

Merged
zeroshade merged 5 commits into
apache:mainfrom
twuebi:tp/fix-snapshot-update
Aug 6, 2025
Merged

zeroshade merged 5 commits into
apache:mainfrom
twuebi:tp/fix-snapshot-update

Conversation

@twuebi

@twuebi twuebi commented Aug 1, 2025

Copy link
Copy Markdown
Contributor

Java, PyIceberg and Rust add the update unconditionally:

    public Builder setRef(String name, SnapshotRef ref) {
      SnapshotRef existingRef = refs.get(name);
      if (existingRef != null && existingRef.equals(ref)) {
        return this;
      }
      ...
      refs.put(name, ref);
      MetadataUpdate.SetSnapshotRef refUpdate =
          new MetadataUpdate.SetSnapshotRef(
              name,
              ref.snapshotId(),
              ref.type(),
              ref.minSnapshotsToKeep(),
              ref.maxSnapshotAgeMs(),
              ref.maxRefAgeMs());
      changes.add(refUpdate);
      return this;
    }
@_apply_table_update.register(SetSnapshotRefUpdate)
   ...

    if update.ref_name == MAIN_BRANCH:
        metadata_updates["current_snapshot_id"] = snapshot_ref.snapshot_id
        if "last_updated_ms" not in metadata_updates:
            metadata_updates["last_updated_ms"] = datetime_to_millis(datetime.now().astimezone())

        metadata_updates["snapshot_log"] = base_metadata.snapshot_log + [
            SnapshotLogEntry(
                snapshot_id=snapshot_ref.snapshot_id,
                timestamp_ms=metadata_updates["last_updated_ms"],
            )
        ]

    metadata_updates["refs"] = {**base_metadata.refs, update.ref_name: snapshot_ref}
    context.add_update(update)
    return base_metadata.model_copy(update=metadata_updates)
    /// Set a reference to a snapshot.
    ///
    /// # Errors
    /// - The snapshot id is unknown.
    pub fn set_ref(mut self, ref_name: &str, reference: SnapshotReference) -> Result<Self> {
        if self
            .metadata
            .refs
            .get(ref_name)
            .is_some_and(|snap_ref| snap_ref.eq(&reference))
        {
            return Ok(self);
        }

        ...
        self.changes.push(TableUpdate::SetSnapshotRef {
            ref_name: ref_name.to_string(),
            reference: reference.clone(),
        });
        self.metadata.refs.insert(ref_name.to_string(), reference);

        Ok(self)
    }

@zeroshade

Copy link
Copy Markdown
Member

Any way we can add a test for this?

@twuebi

twuebi commented Aug 5, 2025

Copy link
Copy Markdown
Contributor Author

Any way we can add a test for this?

Found that issue while porting the table-metadata(-builder) test-suite from iceberg-rust to iceberg-go. There's a test on my branch at https://github.com/twuebi/iceberg-go/pull/1/files#diff-3496f0aa7389c50dffe858c9397990ae22d0a0c20c1ee31155d010c02da2c9ad but it depends on a number of other changes, e.g. MetadataBuilderFromPieces, func-opts partition spec builder, that are probably going to take some discussion.

I'll try to add something more self-contained to this pr.

@twuebi

twuebi commented Aug 5, 2025

Copy link
Copy Markdown
Contributor Author

easier than expected - extracted & backported two set-snapshot-ref tests

Comment thread table/transaction_test.go Resolved
@zeroshade
zeroshade merged commit d061b71 into apache:main Aug 6, 2025
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.

2 participants