Metadata log entries lose snapshot details after snapshot expiration
- Dominant language
- Java
- Stars
- 9.2k
- Forks
- 3.5k
- Avg merge
- 2d 16h
- Merged PRs (30d)
- 129
Description
### Apache Iceberg version
1.11.0 (latest release)
### Query engine
None
### Please describe the bug 🐞
## Description
The `metadata_log_entries` table derives `latest_*` fields from the current table history. After snapshots are expired, the current snapshot log may be truncated, causing historical metadata entries to incorrectly return `null` for snapshot details.
## Reproduction
```sql
CREATE TABLE local.demo.metadata_log_bug (id BIGINT)
USING iceberg;
INSERT INTO local.demo.metadata_log_bug VALUES (1);
INSERT INTO local.demo.metadata_log_bug VALUES (2);
INSERT INTO local.demo.metadata_log_bug VALUES (3);
CREATE TABLE local.demo.metadata_log_bug_before
USING parquet
AS SELECT * FROM local.demo.metadata_log_bug.metadata_log_entries;
CALL local.system.expire_snapshots(
table => 'local.demo.metadata_log_bug',
snapshot_ids => ARRAY()
);
SELECT
before.file,
before.latest_snapshot_id AS snapshot_id_before,
after.latest_snapshot_id AS snapshot_id_after
FROM local.demo.metadata_log_bug_before before
JOIN local.demo.metadata_log_bug.metadata_log_entries after
ON before.file = after.file
WHERE NOT (
before.latest_snapshot_id <=> after.latest_snapshot_id
);
```
## Current behavior
Historical entries corresponding to expired or truncated snapshots return `null` for `latest_snapshot_id`, `latest_schema_id`, and `latest_sequence_number`.
## Expected behavior
The `latest_*` fields should remain stable and be derived from the metadata file referenced by each entry.
### Willingness to contribute
- [x] I can contribute a fix for this bug independently
- [ ] I would be willing to contribute a fix for this bug with guidance from the Iceberg community
- [ ] I cannot contribute a fix for this bug at this time
Contributor guide
Research direction
Start by running the SQL reproduction for metadata_log_entries, including the expire_snapshots call, and trace how latest_snapshot_id, latest_schema_id, and latest_sequence_number are derived for each metadata file. The fix is done when those fields remain stable after the referenced snapshots are expired or the current snapshot log is truncated, matching the before-and-after query.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100