Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Iceberg $history table when using REST Catalog #17470

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.trino.spi.connector.SystemTable;
import io.trino.spi.predicate.TupleDomain;
import io.trino.spi.type.TimeZoneKey;
import org.apache.iceberg.HistoryEntry;
import org.apache.iceberg.Snapshot;
import org.apache.iceberg.Table;
import org.apache.iceberg.util.SnapshotUtil;
Expand Down Expand Up @@ -77,14 +76,13 @@ public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, Connect

Set<Long> ancestorIds = ImmutableSet.copyOf(SnapshotUtil.currentAncestorIds(icebergTable));
TimeZoneKey timeZoneKey = session.getTimeZoneKey();
for (HistoryEntry historyEntry : icebergTable.history()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current version of Iceberg library, when not all the
snapshots are initially loaded (for the REST session catalog),
the snapshotsLog does not get reinitialized, causing to
expose only the current snapshots in the history() method
of the Iceberg table.

is this a bug in the library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a bug in the library?

Yes, please see the discussion https://apache-iceberg.slack.com/archives/C03LG1D563F/p1683871245426989

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@findinpath unfortunately the discussion is no longer visible in slack (thread is a bit old).

We recently hit an issue here because we made the assumption that $history table only contained snapshots that were at some point or another the current snapshot of a table.

I wonder what is the difference now between $snapshots and $history? Since I can get pretty much anything from $history using only $snapshots.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gustavoatt i don't see the replies anymore on the slack thread and lost context :(

long snapshotId = historyEntry.snapshotId();
Snapshot snapshot = icebergTable.snapshot(snapshotId);
for (Snapshot snapshot : icebergTable.snapshots()) {
long snapshotId = snapshot.snapshotId();
findinpath marked this conversation as resolved.
Show resolved Hide resolved

table.addRow(
packDateTimeWithZone(historyEntry.timestampMillis(), timeZoneKey),
packDateTimeWithZone(snapshot.timestampMillis(), timeZoneKey),
snapshotId,
snapshot != null ? snapshot.parentId() : null,
snapshot.parentId(),
ancestorIds.contains(snapshotId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,33 @@ public void testDropTableWithNonExistentTableLocation()
assertFalse(getQueryRunner().tableExists(getSession(), tableName));
}

// Verify the accuracy of Trino metadata tables while retrieving Iceberg table metadata from the underlying `TrinoCatalog` implementation
@Test
public void testMetadataTables()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a code comment why this is in SmokeTest.

{
try (TestTable table = new TestTable(
getQueryRunner()::execute,
"test_metadata_tables",
"(id int, part varchar) WITH (partitioning = ARRAY['part'])",
ImmutableList.of("1, 'p1'", "2, 'p1'", "3, 'p2'"))) {
List<Long> snapshotIds = computeActual("SELECT snapshot_id FROM \"" + table.getName() + "$snapshots\" ORDER BY committed_at DESC")
.getOnlyColumn()
.map(Long.class::cast)
.collect(toImmutableList());
List<Long> historySnapshotIds = computeActual("SELECT snapshot_id FROM \"" + table.getName() + "$history\" ORDER BY made_current_at DESC")
.getOnlyColumn()
.map(Long.class::cast)
.collect(toImmutableList());
long filesCount = (long) computeScalar("SELECT count(*) FROM \"" + table.getName() + "$files\"");
long partitionsCount = (long) computeScalar("SELECT count(*) FROM \"" + table.getName() + "$partitions\"");

assertThat(snapshotIds).hasSize(4);
assertThat(snapshotIds).hasSameElementsAs(historySnapshotIds);
assertThat(filesCount).isEqualTo(3L);
assertThat(partitionsCount).isEqualTo(2L);
}
}

protected abstract boolean isFileSorted(Location path, String sortColumnName);

private String getTableLocation(String tableName)
Expand Down