Skip to content

Commit e435bce

Browse files
sririshindravanliu-tx
authored andcommitted
Core: Fix history timestamp for rollbacks (apache#4135)
1 parent 810812d commit e435bce

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

core/src/main/java/org/apache/iceberg/SnapshotProducer.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,18 @@ public void commit() {
282282
.run(taskOps -> {
283283
Snapshot newSnapshot = apply();
284284
newSnapshotId.set(newSnapshot.snapshotId());
285-
TableMetadata updated;
286-
if (stageOnly) {
287-
updated = base.addStagedSnapshot(newSnapshot);
285+
TableMetadata.Builder update = TableMetadata.buildFrom(base);
286+
if (base.snapshot(newSnapshot.snapshotId()) != null) {
287+
// this is a rollback or cherrypick operation
288+
update.setCurrentSnapshot(newSnapshot.snapshotId());
289+
} else if (stageOnly) {
290+
update.addSnapshot(newSnapshot);
288291
} else {
289-
updated = base.replaceCurrentSnapshot(newSnapshot);
292+
update.setCurrentSnapshot(newSnapshot);
290293
}
291294

292-
if (updated == base) {
295+
TableMetadata updated = update.build();
296+
if (updated.changes().isEmpty()) {
293297
// do not commit if the metadata has not changed. for example, this may happen when setting the current
294298
// snapshot to an ID that is already current. note that this check uses identity.
295299
return;

core/src/main/java/org/apache/iceberg/TableMetadata.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ public TableMetadata replaceCurrentSnapshot(Snapshot snapshot) {
501501
return new Builder(this).setCurrentSnapshot(snapshot).build();
502502
}
503503

504+
public TableMetadata replaceCurrentSnapshot(long snapshotId) {
505+
return new Builder(this).setCurrentSnapshot(snapshotId).build();
506+
}
507+
504508
public TableMetadata removeSnapshotsIf(Predicate<Snapshot> removeIf) {
505509
List<Snapshot> toRemove = snapshots.stream().filter(removeIf).collect(Collectors.toList());
506510
return new Builder(this).removeSnapshots(toRemove).build();

0 commit comments

Comments
 (0)