Skip to content

Commit 62e3d81

Browse files
authored
[fix](iceberg) Refresh table before classifying expired files (#69703)
### What problem does this PR solve? Issue Number: N/A Related PR: #69551 Problem Summary: `expire_snapshots` built its delete-file kind map from a table handle that could predate delete files committed by another writer. Iceberg refreshes table metadata during expiration, so the callback could receive an unmapped position or equality delete file and count it as a data file. Refresh the table before building the file-kind map so classification and expiration operate on the same metadata generation. Add regression coverage for position and equality delete files committed after the table was loaded. Before the fix, both cases returned `[1, 0, 0, 2, 3, 0]`. After the fix, position deletes return `[0, 1, 0, 2, 3, 0]` and equality deletes return `[0, 0, 1, 2, 3, 0]`. ### Release note Fix `expire_snapshots` misreporting position and equality delete files as data files after concurrent metadata commits. ### Check List (For Author) - Test - [ ] Regression test - [x] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No code files have been changed. - [ ] Other reason Validated on latest `apache/master`: `IcebergExpireSnapshotsActionTest`, 15 tests passed. The full `fe-connector-iceberg` suite previously passed 1739 tests with 14 skipped. - Behavior changed: - [ ] No. - [x] Yes. The reported position/equality delete counters are corrected. - Does this need documentation? - [x] No. - [ ] Yes. ### Check List (For Reviewer who merge this PR) - [ ] Confirm the release note - [ ] Confirm test cases - [ ] Confirm document - [ ] Add branch pick label
1 parent a47d461 commit 62e3d81

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎fe/fe-connector/fe-connector-iceberg/src/main/java/org/apache/doris/connector/iceberg/action/IcebergExpireSnapshotsAction.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ protected List<String> executeAction(Table icebergTable, ConnectorSession sessio
189189

190190
ExecutorService deleteExecutor = null;
191191
try {
192+
// expireSnapshots() refreshes the table metadata before planning expiration. Build the file-kind
193+
// map from that same generation so delete files committed since this table handle was loaded are
194+
// not left unmapped and counted as data files by the delete callback.
195+
icebergTable.refresh();
192196
Map<String, FileKind> fileKindByPath = buildFileKindMap(icebergTable);
193197
ExpireSnapshots expireSnapshots = icebergTable.expireSnapshots();
194198

‎fe/fe-connector/fe-connector-iceberg/src/test/java/org/apache/doris/connector/iceberg/action/IcebergExpireSnapshotsActionTest.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import com.google.common.collect.Iterables;
3030
import org.apache.iceberg.DataFile;
3131
import org.apache.iceberg.DataFiles;
32+
import org.apache.iceberg.DeleteFile;
3233
import org.apache.iceberg.FileFormat;
34+
import org.apache.iceberg.FileMetadata;
3335
import org.apache.iceberg.ManifestFile;
3436
import org.apache.iceberg.PartitionSpec;
3537
import org.apache.iceberg.Snapshot;
@@ -330,6 +332,51 @@ public void buildFileKindMapDedupsManifestsSharedAcrossSnapshots() {
330332
"the equality-delete file must stay classified as EQUALITY_DELETES");
331333
}
332334

335+
@Test
336+
public void countsPositionDeletesCommittedAfterTheTableWasLoaded() {
337+
assertLateDeleteClassification(true);
338+
}
339+
340+
@Test
341+
public void countsEqualityDeletesCommittedAfterTheTableWasLoaded() {
342+
assertLateDeleteClassification(false);
343+
}
344+
345+
private static void assertLateDeleteClassification(boolean positionDelete) {
346+
InMemoryCatalog catalog = ActionTestTables.freshCatalog();
347+
TableIdentifier id = ActionTestTables.id("late_delete");
348+
ActionTestTables.createTable(catalog, "late_delete");
349+
ActionTestTables.appendSnapshot(catalog, "late_delete", "base.parquet", 1L);
350+
Table loadedBeforeDelete = catalog.loadTable(id);
351+
352+
FileMetadata.Builder builder = FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned());
353+
if (positionDelete) {
354+
builder.ofPositionDeletes().withReferencedDataFile("s3://b/db1/base.parquet");
355+
} else {
356+
builder.ofEqualityDeletes(1);
357+
}
358+
DeleteFile delete = builder.withPath("s3://b/db1/late-delete.parquet")
359+
.withFormat(FileFormat.PARQUET)
360+
.withFileSizeInBytes(64L)
361+
.withRecordCount(1L)
362+
.build();
363+
364+
Table writer = catalog.loadTable(id);
365+
writer.newRowDelta().addDeletes(delete).commit();
366+
writer.newRowDelta().removeDeletes(delete).commit();
367+
ActionTestTables.appendSnapshot(catalog, "late_delete", "current.parquet", 1L);
368+
369+
IcebergExpireSnapshotsAction action =
370+
action(ImmutableMap.of("older_than", "4102444800000", "retain_last", "1"));
371+
action.validate();
372+
List<String> row = action.execute(loadedBeforeDelete, ActionTestTables.session("UTC")).getRows().get(0);
373+
374+
Assertions.assertEquals(1, Iterables.size(catalog.loadTable(id).snapshots()),
375+
"the delete-file history must actually expire");
376+
Assertions.assertEquals(ImmutableList.of("0", positionDelete ? "1" : "0", positionDelete ? "0" : "1",
377+
"2", "3", "0"), row);
378+
}
379+
333380
@Test
334381
public void rejectsNegativeOlderThanTimestamp() {
335382
DorisConnectorException e = Assertions.assertThrows(DorisConnectorException.class,

0 commit comments

Comments
 (0)