Skip to content

Commit 94b25e1

Browse files
committed
Return only published main branch snapshots in history metadata table
1 parent 6abf6a9 commit 94b25e1

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/HistoryTable.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import io.trino.spi.connector.SystemTable;
2626
import io.trino.spi.predicate.TupleDomain;
2727
import io.trino.spi.type.TimeZoneKey;
28+
import org.apache.iceberg.HistoryEntry;
2829
import org.apache.iceberg.Snapshot;
2930
import org.apache.iceberg.Table;
3031
import org.apache.iceberg.util.SnapshotUtil;
3132

33+
import java.util.HashMap;
3234
import java.util.List;
35+
import java.util.Map;
3336
import java.util.Set;
3437

3538
import static io.trino.spi.type.BigintType.BIGINT;
@@ -76,8 +79,13 @@ public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, Connect
7679

7780
Set<Long> ancestorIds = ImmutableSet.copyOf(SnapshotUtil.currentAncestorIds(icebergTable));
7881
TimeZoneKey timeZoneKey = session.getTimeZoneKey();
82+
Map<Long, Snapshot> snapshots = new HashMap<>();
7983
for (Snapshot snapshot : icebergTable.snapshots()) {
80-
long snapshotId = snapshot.snapshotId();
84+
snapshots.put(snapshot.snapshotId(), snapshot);
85+
}
86+
for (HistoryEntry historyEntry : icebergTable.history()) {
87+
Long snapshotId = historyEntry.snapshotId();
88+
Snapshot snapshot = snapshots.get(snapshotId);
8189

8290
table.addRow(
8391
packDateTimeWithZone(snapshot.timestampMillis(), timeZoneKey),

plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/BaseIcebergSystemTables.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ public void testHistoryTable()
241241

242242
// Test the number of history entries
243243
assertQuery("SELECT count(*) FROM test_schema.\"test_table$history\"", "VALUES 3");
244+
245+
try (TestTable table = newTrinoTable("test_history", "AS SELECT 1 id")) {
246+
assertQuery("SELECT count(*) FROM \"" + table.getName() + "$history\"", "VALUES 1");
247+
assertUpdate("INSERT INTO " + table.getName() + " VALUES (2)", 1);
248+
assertQuery("SELECT count(*) FROM \"" + table.getName() + "$history\"", "VALUES 2");
249+
250+
// Perform a stage only commit
251+
Table icebergTable = this.loadTable(table.getName());
252+
icebergTable.newAppend().set("table_id", "1").stageOnly().commit();
253+
254+
// Check if history table doesn't contain the stage commit snapshot
255+
assertQuery("SELECT count(*) FROM \"" + table.getName() + "$history\"", "VALUES 2");
256+
assertQuery("SELECT count(*) FROM \"" + table.getName() + "$snapshots\"", "VALUES 3");
257+
}
244258
}
245259

246260
@Test

plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/catalog/rest/TestIcebergVendingRestCatalogConnectorSmokeTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ public void testDropTableWithNonExistentTableLocation()
312312
.hasMessageMatching("Failed to load table: (.*)");
313313
}
314314

315+
@Test
316+
@Override
317+
public void testCreateOrReplaceTable()
318+
{
319+
// TODO: Enable this test once tabulario/iceberg-rest image is updated to 1.8.0+
320+
// Without this change - https://github.com/apache/iceberg/pull/11779 CREATE OR REPLACE clears snapshotLog resulting in incorrect results in history metadata table
321+
}
322+
315323
@Override
316324
protected boolean isFileSorted(Location path, String sortColumnName)
317325
{

0 commit comments

Comments
 (0)