From 3433a90f51945721a674eeaa0b21e517bc761a96 Mon Sep 17 00:00:00 2001 From: Sun Dapeng Date: Tue, 10 Nov 2026 17:19:13 +0800 Subject: [PATCH 1/3] [iceberg] Derive snapshot summary operation from commit kind in metadata rebuild When Iceberg metadata is recreated from scratch (no base metadata file: first snapshot after enabling the compatibility, metadata directory deleted or expired, or an Iceberg format-version change), createMetadataWithoutBase hardcoded the snapshot summary operation to "append" even when the triggering Paimon snapshot was a COMPACT or OVERWRITE commit. The with-base path already derives REPLACE/OVERWRITE from the commit kind, so the two paths disagreed: incremental Iceberg readers that key off the operation replay a full rebuilt snapshot as newly appended data and see duplicated rows. Derive the operation from paimonSnapshot.commitKind() in the rebuild path as well, mirroring createWithDeleteManifestFileMetas. --- .../paimon/iceberg/IcebergCommitCallback.java | 11 ++++- .../iceberg/IcebergCompatibilityTest.java | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/paimon-core/src/main/java/org/apache/paimon/iceberg/IcebergCommitCallback.java b/paimon-core/src/main/java/org/apache/paimon/iceberg/IcebergCommitCallback.java index cf89a26b7aff..d1f923dcb278 100644 --- a/paimon-core/src/main/java/org/apache/paimon/iceberg/IcebergCommitCallback.java +++ b/paimon-core/src/main/java/org/apache/paimon/iceberg/IcebergCommitCallback.java @@ -563,9 +563,16 @@ private void createMetadataWithoutBase( List partitionFields = getPartitionFields(table.schema().partitionKeys(), icebergSchema); + // the with-base path derives the operation from the commit kind; a rebuild exporting + // the full table must do the same, or incremental consumers replay it as new data + String operation = IcebergSnapshotSummary.APPEND.operation(); + if (paimonSnapshot.commitKind() == Snapshot.CommitKind.COMPACT) { + operation = IcebergSnapshotSummary.REPLACE.operation(); + } else if (paimonSnapshot.commitKind() == Snapshot.CommitKind.OVERWRITE) { + operation = IcebergSnapshotSummary.OVERWRITE.operation(); + } IcebergSnapshotSummary snapshotSummary = - computeSnapshotSummary( - IcebergSnapshotSummary.APPEND.operation(), paimonSnapshot, metrics); + computeSnapshotSummary(operation, paimonSnapshot, metrics); IcebergSnapshot snapshot = new IcebergSnapshot( diff --git a/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java b/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java index 8364c4f6a533..65f8c00cfbad 100644 --- a/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java +++ b/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java @@ -179,6 +179,46 @@ public void testFileLevelChange() throws Exception { commit.close(); } + @Test + public void testWithoutBaseRebuildDerivesOperationFromCommitKind() throws Exception { + RowType rowType = + RowType.of( + new DataType[] {DataTypes.INT(), DataTypes.INT()}, new String[] {"k", "v"}); + FileStoreTable table = + createPaimonTable( + rowType, Collections.emptyList(), Collections.singletonList("k"), 1); + + String commitUser = UUID.randomUUID().toString(); + TableWriteImpl write = table.newWrite(commitUser); + TableCommitImpl commit = table.newCommit(commitUser); + + write.write(GenericRow.of(1, 10)); + write.write(GenericRow.of(2, 20)); + commit.commit(1, write.prepareCommit(false, 1)); + assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); + + // wipe the Iceberg metadata directory, so the next commit rebuilds it without a base + table.fileIO().deleteDirectoryQuietly(new Path(table.location(), "metadata")); + + // a pure compaction commit rebuilds the metadata from scratch; the snapshot summary + // must derive the operation from the commit kind instead of hardcoding "append" + write.compact(BinaryRow.EMPTY_ROW, 0, true); + commit.commit(2, write.prepareCommit(true, 2)); + + long snapshotId = table.snapshotManager().latestSnapshotId(); + IcebergMetadata metadata = + IcebergMetadata.fromPath( + table.fileIO(), + new Path(table.location(), "metadata/v" + snapshotId + ".metadata.json")); + assertThat(metadata.currentSnapshot().summary().operation()).isEqualTo("replace"); + + // the rebuilt metadata still exposes the table data + assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); + + write.close(); + commit.close(); + } + @Test public void testDeletedEntryRecordsTheDeletingSnapshot() throws Exception { RowType rowType = From bd2bd6b288c79f4bab597764dafed1ae880af112 Mon Sep 17 00:00:00 2001 From: Sun Dapeng Date: Tue, 24 Nov 2026 20:17:09 +0800 Subject: [PATCH 2/3] Cover overwrite and append operations in without-base rebuild tests The rebuild test only pinned COMPACT -> "replace". Add an overwrite case asserting that an overwrite commit rebuilds the metadata with the "overwrite" operation, an append case pinning the "append" default of the first without-base export, and an existence check that the metadata wipe really removed the directory so the with-base path cannot silently cover the rebuild. --- .../iceberg/IcebergCompatibilityTest.java | 80 ++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java b/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java index 65f8c00cfbad..465d6d0009d0 100644 --- a/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java +++ b/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java @@ -197,8 +197,10 @@ public void testWithoutBaseRebuildDerivesOperationFromCommitKind() throws Except commit.commit(1, write.prepareCommit(false, 1)); assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); - // wipe the Iceberg metadata directory, so the next commit rebuilds it without a base + // wipe the Iceberg metadata directory, so the next commit rebuilds it without a base; + // assert the wipe really happened, otherwise the with-base path would cover the rebuild table.fileIO().deleteDirectoryQuietly(new Path(table.location(), "metadata")); + assertThat(table.fileIO().exists(new Path(table.location(), "metadata"))).isFalse(); // a pure compaction commit rebuilds the metadata from scratch; the snapshot summary // must derive the operation from the commit kind instead of hardcoding "append" @@ -219,6 +221,82 @@ public void testWithoutBaseRebuildDerivesOperationFromCommitKind() throws Except commit.close(); } + @Test + public void testWithoutBaseRebuildOverwriteDerivesOperation() throws Exception { + RowType rowType = + RowType.of( + new DataType[] {DataTypes.INT(), DataTypes.INT()}, new String[] {"k", "v"}); + FileStoreTable table = + createPaimonTable( + rowType, Collections.emptyList(), Collections.singletonList("k"), 1); + + String commitUser = UUID.randomUUID().toString(); + TableWriteImpl write = table.newWrite(commitUser); + TableCommitImpl commit = table.newCommit(commitUser); + + write.write(GenericRow.of(1, 10)); + write.write(GenericRow.of(2, 20)); + commit.commit(1, write.prepareCommit(false, 1)); + assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); + + // wipe the Iceberg metadata directory, so the next commit rebuilds it without a base; + // assert the wipe really happened, otherwise the with-base path would cover the rebuild + table.fileIO().deleteDirectoryQuietly(new Path(table.location(), "metadata")); + assertThat(table.fileIO().exists(new Path(table.location(), "metadata"))).isFalse(); + + // an overwrite commit rebuilds the metadata from scratch; the snapshot summary + // must derive the operation from the commit kind instead of hardcoding "append" + write.write(GenericRow.of(3, 30)); + commit.withOverwriteStaticPartitions(Collections.singletonList(BinaryRow.EMPTY_ROW)) + .commit(2, write.prepareCommit(false, 2)); + + long snapshotId = table.snapshotManager().latestSnapshotId(); + IcebergMetadata metadata = + IcebergMetadata.fromPath( + table.fileIO(), + new Path(table.location(), "metadata/v" + snapshotId + ".metadata.json")); + assertThat(metadata.currentSnapshot().summary().operation()).isEqualTo("overwrite"); + + // the rebuilt metadata exposes the overwritten data + assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(3, 30)"); + + write.close(); + commit.close(); + } + + @Test + public void testWithoutBaseRebuildAppendKeepsAppendOperation() throws Exception { + // the very first commit has no base metadata file, so it is exported through the + // without-base rebuild path; a plain append commit must keep the "append" operation + RowType rowType = + RowType.of( + new DataType[] {DataTypes.INT(), DataTypes.INT()}, new String[] {"k", "v"}); + FileStoreTable table = + createPaimonTable( + rowType, Collections.emptyList(), Collections.singletonList("k"), 1); + + String commitUser = UUID.randomUUID().toString(); + TableWriteImpl write = table.newWrite(commitUser); + TableCommitImpl commit = table.newCommit(commitUser); + + write.write(GenericRow.of(1, 10)); + write.write(GenericRow.of(2, 20)); + commit.commit(1, write.prepareCommit(false, 1)); + + long snapshotId = table.snapshotManager().latestSnapshotId(); + IcebergMetadata metadata = + IcebergMetadata.fromPath( + table.fileIO(), + new Path(table.location(), "metadata/v" + snapshotId + ".metadata.json")); + assertThat(metadata.currentSnapshot().summary().operation()).isEqualTo("append"); + + // the rebuilt metadata exposes all appended data + assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); + + write.close(); + commit.close(); + } + @Test public void testDeletedEntryRecordsTheDeletingSnapshot() throws Exception { RowType rowType = From 863101cc0815ca082bb721f97d518456b41a928d Mon Sep 17 00:00:00 2001 From: Sun Dapeng Date: Wed, 25 Nov 2026 15:08:29 +0800 Subject: [PATCH 3/3] Start a new Iceberg snapshot history on metadata rebuild A full export cannot serve as an incremental delta from a missing parent. Publish it without a parent so a reader with an old checkpoint must resync. Preserve append semantics for an initial export, including a first overwrite, and verify both behaviors with the Iceberg client. --- docs/docs/iceberg/index.md | 3 + .../paimon/iceberg/IcebergCommitCallback.java | 15 +- .../iceberg/IcebergCompatibilityTest.java | 163 ++++++++---------- 3 files changed, 76 insertions(+), 105 deletions(-) diff --git a/docs/docs/iceberg/index.md b/docs/docs/iceberg/index.md index 453f41410bd0..ae3b892eb167 100644 --- a/docs/docs/iceberg/index.md +++ b/docs/docs/iceberg/index.md @@ -73,6 +73,9 @@ Initial publication and metadata rebuilds use snapshot splits that can be read d Paimon's merge logic. The table above describes subsequent incremental publication. Use compaction to establish a predictable visibility boundary for primary key tables. +An Iceberg metadata rebuild starts a new snapshot history. An incremental reader with a checkpoint +from before the rebuild must read a full snapshot and resume from a checkpoint in the new history. + The [primary key guide](./primary-key-table.mdx) explains both modes and their configuration. Disabling an Iceberg catalog's cache can help with interactive verification, but cannot make uncompacted or unpublished changes visible. diff --git a/paimon-core/src/main/java/org/apache/paimon/iceberg/IcebergCommitCallback.java b/paimon-core/src/main/java/org/apache/paimon/iceberg/IcebergCommitCallback.java index d1f923dcb278..cd22cf293d4c 100644 --- a/paimon-core/src/main/java/org/apache/paimon/iceberg/IcebergCommitCallback.java +++ b/paimon-core/src/main/java/org/apache/paimon/iceberg/IcebergCommitCallback.java @@ -563,22 +563,17 @@ private void createMetadataWithoutBase( List partitionFields = getPartitionFields(table.schema().partitionKeys(), icebergSchema); - // the with-base path derives the operation from the commit kind; a rebuild exporting - // the full table must do the same, or incremental consumers replay it as new data - String operation = IcebergSnapshotSummary.APPEND.operation(); - if (paimonSnapshot.commitKind() == Snapshot.CommitKind.COMPACT) { - operation = IcebergSnapshotSummary.REPLACE.operation(); - } else if (paimonSnapshot.commitKind() == Snapshot.CommitKind.OVERWRITE) { - operation = IcebergSnapshotSummary.OVERWRITE.operation(); - } IcebergSnapshotSummary snapshotSummary = - computeSnapshotSummary(operation, paimonSnapshot, metrics); + computeSnapshotSummary( + IcebergSnapshotSummary.APPEND.operation(), paimonSnapshot, metrics); IcebergSnapshot snapshot = new IcebergSnapshot( snapshotId, snapshotId, - snapshotId == Snapshot.FIRST_SNAPSHOT_ID ? null : (Long) (snapshotId - 1), + // A full export starts a new lineage. Readers with old checkpoints must + // resync. + null, // the Paimon snapshot's own commit time, the as-of time readers see paimonSnapshot.timeMillis(), snapshotSummary, diff --git a/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java b/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java index 465d6d0009d0..126c0cd7d767 100644 --- a/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java +++ b/paimon-core/src/test/java/org/apache/paimon/iceberg/IcebergCompatibilityTest.java @@ -81,6 +81,7 @@ import org.apache.avro.generic.GenericDatumReader; import org.apache.avro.generic.GenericRecord; import org.apache.hadoop.conf.Configuration; +import org.apache.iceberg.FileScanTask; import org.apache.iceberg.catalog.TableIdentifier; import org.apache.iceberg.data.IcebergGenerics; import org.apache.iceberg.data.Record; @@ -180,121 +181,93 @@ public void testFileLevelChange() throws Exception { } @Test - public void testWithoutBaseRebuildDerivesOperationFromCommitKind() throws Exception { + public void testWithoutBaseRebuildRequiresIncrementalResync() throws Exception { RowType rowType = RowType.of( new DataType[] {DataTypes.INT(), DataTypes.INT()}, new String[] {"k", "v"}); FileStoreTable table = createPaimonTable( - rowType, Collections.emptyList(), Collections.singletonList("k"), 1); - - String commitUser = UUID.randomUUID().toString(); - TableWriteImpl write = table.newWrite(commitUser); - TableCommitImpl commit = table.newCommit(commitUser); - - write.write(GenericRow.of(1, 10)); - write.write(GenericRow.of(2, 20)); - commit.commit(1, write.prepareCommit(false, 1)); - assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); - - // wipe the Iceberg metadata directory, so the next commit rebuilds it without a base; - // assert the wipe really happened, otherwise the with-base path would cover the rebuild - table.fileIO().deleteDirectoryQuietly(new Path(table.location(), "metadata")); - assertThat(table.fileIO().exists(new Path(table.location(), "metadata"))).isFalse(); - - // a pure compaction commit rebuilds the metadata from scratch; the snapshot summary - // must derive the operation from the commit kind instead of hardcoding "append" - write.compact(BinaryRow.EMPTY_ROW, 0, true); - commit.commit(2, write.prepareCommit(true, 2)); - - long snapshotId = table.snapshotManager().latestSnapshotId(); - IcebergMetadata metadata = - IcebergMetadata.fromPath( - table.fileIO(), - new Path(table.location(), "metadata/v" + snapshotId + ".metadata.json")); - assertThat(metadata.currentSnapshot().summary().operation()).isEqualTo("replace"); - - // the rebuilt metadata still exposes the table data - assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); - - write.close(); - commit.close(); - } - - @Test - public void testWithoutBaseRebuildOverwriteDerivesOperation() throws Exception { - RowType rowType = - RowType.of( - new DataType[] {DataTypes.INT(), DataTypes.INT()}, new String[] {"k", "v"}); - FileStoreTable table = - createPaimonTable( - rowType, Collections.emptyList(), Collections.singletonList("k"), 1); + rowType, + Collections.emptyList(), + Collections.singletonList("k"), + 1, + Collections.singletonMap( + CoreOptions.DELETION_VECTORS_ENABLED.key(), "false")); String commitUser = UUID.randomUUID().toString(); - TableWriteImpl write = table.newWrite(commitUser); - TableCommitImpl commit = table.newCommit(commitUser); - - write.write(GenericRow.of(1, 10)); - write.write(GenericRow.of(2, 20)); - commit.commit(1, write.prepareCommit(false, 1)); - assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); - - // wipe the Iceberg metadata directory, so the next commit rebuilds it without a base; - // assert the wipe really happened, otherwise the with-base path would cover the rebuild - table.fileIO().deleteDirectoryQuietly(new Path(table.location(), "metadata")); - assertThat(table.fileIO().exists(new Path(table.location(), "metadata"))).isFalse(); - - // an overwrite commit rebuilds the metadata from scratch; the snapshot summary - // must derive the operation from the commit kind instead of hardcoding "append" - write.write(GenericRow.of(3, 30)); - commit.withOverwriteStaticPartitions(Collections.singletonList(BinaryRow.EMPTY_ROW)) - .commit(2, write.prepareCommit(false, 2)); + try (TableWriteImpl write = table.newWrite(commitUser); + TableCommitImpl commit = table.newCommit(commitUser)) { + commit.ignoreEmptyCommit(false).commit(1, Collections.emptyList()); + commit.ignoreEmptyCommit(true); + long checkpoint = getIcebergTable().currentSnapshot().snapshotId(); + assertThat(getIcebergResult()).isEmpty(); - long snapshotId = table.snapshotManager().latestSnapshotId(); - IcebergMetadata metadata = - IcebergMetadata.fromPath( - table.fileIO(), - new Path(table.location(), "metadata/v" + snapshotId + ".metadata.json")); - assertThat(metadata.currentSnapshot().summary().operation()).isEqualTo("overwrite"); + write.write(GenericRow.of(1, 10)); + commit.commit(2, write.prepareCommit(false, 2)); + write.write(GenericRow.of(1, 11)); + write.write(GenericRow.of(2, 20)); + commit.commit(3, write.prepareCommit(false, 3)); + assertThat(getIcebergResult()).isEmpty(); + long previousHead = getIcebergTable().currentSnapshot().snapshotId(); - // the rebuilt metadata exposes the overwritten data - assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(3, 30)"); + Path metadataPath = IcebergCommitCallback.catalogTableMetadataPath(table); + table.fileIO().deleteDirectoryQuietly(metadataPath); + assertThat(table.fileIO().exists(metadataPath)).isFalse(); - write.close(); - commit.close(); + write.compact(BinaryRow.EMPTY_ROW, 0, true); + commit.commit(4, write.prepareCommit(true, 4)); + + assertThat(table.snapshotManager().latestSnapshot().commitKind()) + .isEqualTo(Snapshot.CommitKind.COMPACT); + assertThat(getIcebergResult()) + .containsExactlyInAnyOrder("Record(1, 11)", "Record(2, 20)"); + + org.apache.iceberg.Table rebuilt = getIcebergTable(); + assertThat(rebuilt.currentSnapshot().snapshotId()).isEqualTo(previousHead + 1); + for (long oldCheckpoint : new long[] {previousHead, checkpoint}) { + assertThatThrownBy( + () -> + rebuilt.newIncrementalAppendScan() + .fromSnapshotExclusive(oldCheckpoint) + .planFiles()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("not a parent ancestor"); + } + assertThat(rebuilt.currentSnapshot().parentId()).isNull(); + } } - @Test - public void testWithoutBaseRebuildAppendKeepsAppendOperation() throws Exception { - // the very first commit has no base metadata file, so it is exported through the - // without-base rebuild path; a plain append commit must keep the "append" operation + @ParameterizedTest + @ValueSource(booleans = {false, true}) + public void testInitialIcebergSnapshotCanBeReadAsAppend(boolean overwrite) throws Exception { RowType rowType = RowType.of( new DataType[] {DataTypes.INT(), DataTypes.INT()}, new String[] {"k", "v"}); FileStoreTable table = - createPaimonTable( - rowType, Collections.emptyList(), Collections.singletonList("k"), 1); + createPaimonTable(rowType, Collections.emptyList(), Collections.emptyList(), -1); String commitUser = UUID.randomUUID().toString(); - TableWriteImpl write = table.newWrite(commitUser); - TableCommitImpl commit = table.newCommit(commitUser); - - write.write(GenericRow.of(1, 10)); - write.write(GenericRow.of(2, 20)); - commit.commit(1, write.prepareCommit(false, 1)); - - long snapshotId = table.snapshotManager().latestSnapshotId(); - IcebergMetadata metadata = - IcebergMetadata.fromPath( - table.fileIO(), - new Path(table.location(), "metadata/v" + snapshotId + ".metadata.json")); - assertThat(metadata.currentSnapshot().summary().operation()).isEqualTo("append"); + try (TableWriteImpl write = table.newWrite(commitUser); + TableCommitImpl commit = table.newCommit(commitUser)) { + if (overwrite) { + commit.withOverwriteStaticPartitions( + Collections.singletonList(BinaryRow.EMPTY_ROW)); + } + write.write(GenericRow.of(1, 10)); + write.write(GenericRow.of(2, 20)); + commit.commit(1, write.prepareCommit(false, 1)); + } - // the rebuilt metadata exposes all appended data + assertThat(table.snapshotManager().latestSnapshot().commitKind()) + .isEqualTo(overwrite ? Snapshot.CommitKind.OVERWRITE : Snapshot.CommitKind.APPEND); + org.apache.iceberg.Table icebergTable = getIcebergTable(); + assertThat(icebergTable.currentSnapshot().parentId()).isNull(); + try (CloseableIterable files = + icebergTable.newIncrementalAppendScan().planFiles()) { + assertThat(files).extracting(task -> task.file().recordCount()).containsExactly(2L); + } + assertThat(icebergTable.currentSnapshot().operation()).isEqualTo("append"); assertThat(getIcebergResult()).containsExactlyInAnyOrder("Record(1, 10)", "Record(2, 20)"); - - write.close(); - commit.close(); } @Test