Repository navigation
Do not compact an Iceberg history that retains snapshots off the current chain - #118951
tiandiwonder wants to merge 4 commits into
Conversation
A rollback makes an older snapshot current again and leaves the newer, abandoned snapshots retained in
`snapshots`. On such a table a plain `OPTIMIZE TABLE` succeeded quietly and afterwards the table read
as the abandoned branch tip again - the rollback was silently undone.
`getPlan` iterates every record of the history it is handed with no `is_current_ancestor` filter,
collecting each snapshot's manifests, data files and position deletes, and `writeMetadataFiles` then
replays the snapshots in order, publishing each one and moving `current-snapshot-id` and `refs.main` as
it goes. So a snapshot that is not on the current chain is folded into the rewrite and the last one
replayed becomes current. `getHistory` already computes the missing fact - it walks the parent chain
from `current-snapshot-id` and marks each record `is_current_ancestor` - and nothing in the compaction
path looked at it.
Refuse the rewrite instead, in `checkIfIcebergHistorySupported`, where compaction already forces the
history shapes its experimental implementation supports. Dropping the non-current snapshots and
compacting the rest is not a safe alternative: their data files can still be referenced from another
branch or tag, and compaction deletes the files it did not carry over, so filtering would trade a
wrong-result bug for a data-loss bug.
Measured on a local build, with history append `s1`, position-delete `s2`, append `s3`, rolled back to
`s1`:
after the rollback current-snapshot-id=s1 refs.main=s1 SELECT v = 1,2,3
after plain OPTIMIZE, before current-snapshot-id=s3 refs.main=s3 SELECT v = 1,3,4
after plain OPTIMIZE, after refused, table untouched, SELECT v = 1,2,3
The row count is 3 in both states, so the new test asserts the values rather than the count. With the
check reverted and rebuilt, both of its assertions flip, and the four existing compaction tests
(`04230`, `04513`, `04614`, `04815`) pass either way.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [a503b5b] Summary: ✅
AI ReviewSummaryThis PR refuses Findings
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit a503b5b.
Changed lines: Changed C/C++ lines covered: 39/41 (95.12%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
…e cannot reproduce
Review found the guard of this pull request incomplete, in three related ways, each measured on this
branch with a plain `OPTIMIZE` returning `rc=0` and saying nothing. Compaction rebuilds the metadata
from `createEmptyMetadataFile`, and `MetadataGenerator` then writes a `refs` map holding one `main`
branch with a snapshot id and a type - so anything else a table kept is simply not in the result:
before refs = {main, audit_tag, audit_branch} after refs = {main}
before refs.main = {snapshot-id, type, min-snapshots-to-keep, max-snapshot-age-ms}
after refs.main = {snapshot-id, type}
before properties = {..., history.expire.min-snapshots-to-keep: 5}
after properties = <absent>
A branch or tag that names an ancestor of the current snapshot passes the current-ancestor check, so
none of these shapes was refused. All three weaken retention the same way: `ALTER TABLE ... EXECUTE
expire_snapshots` reads `min-snapshots-to-keep`, `max-snapshot-age-ms` and `max-ref-age-ms` off both
the references and the table properties (`ExpireSnapshotsExecute.cpp`), so history the table asked to
keep can be expired after a compaction that never mentioned dropping anything.
Carrying them over is not a fix by itself: the rewrite does not retain every source snapshot - the
same run drops the count from 3 to 2 - so a reference or a retention floor can outlive the snapshots
it protects and there is nothing correct to remap it to. Refuse instead, next to the
`format_version` check in `getPlan` that already refuses for the same rebuild-from-scratch reason,
and leave the table as it is.
The rewrite drops the *whole* `properties` map, not only the retention keys - a ClickHouse-written
table always carries `write.delete.mode` and friends, so refusing on any property would refuse every
such table. Only the retention floor is refused here; the rest is a defect of the rewrite itself,
which has to carry `properties` across, and is tracked separately.
`05153_iceberg_optimize_non_main_ref` covers the three shapes and asserts both the refusal and that
the references, the retention fields and the rows survive. With the check reverted and rebuilt, the
tag is gone after `OPTIMIZE` and the test fails on both of its first-scenario lines; `04230`,
`04513`, `04614`, `04815` and `05153_iceberg_optimize_rollback_replay` pass either way, because a
table ClickHouse wrote carries only a bare `main` and no retention floor.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
| /// dropped by the rewrite too, which is a defect of its own and not something a guard here can | ||
| /// fix - refusing on any property would refuse every table ClickHouse writes, since it always | ||
| /// sets `write.*.mode` - so only the retention floor is refused. | ||
| if (initial_metadata_object->has(Iceberg::f_properties) && !initial_metadata_object->isNull(Iceberg::f_properties)) |
There was a problem hiding this comment.
properties is still an incomplete allow-list here. writeMetadataFiles rebuilds the table from createEmptyMetadataFile(...), and plan.generator never inherits write.data.path, even though IcebergStorageSink uses that property to decide where data files belong. A table that sets write.data.path therefore still passes this check, then OPTIMIZE writes the compacted files under the default ${table_location}/data/ prefix and drops the property from metadata, so later writes switch prefixes too.
Please either preserve write.data.path in both plan.generator and the rewritten metadata, or fail closed on tables that set it until the rewrite can round-trip it.
| /// Carrying the refs over instead is not a fix on its own: the rewrite does not retain every | ||
| /// source snapshot, so a ref can name one the new metadata no longer has. Refuse the rewrite | ||
| /// and leave the table as it is. | ||
| if (initial_metadata_object->has(Iceberg::f_refs) && !initial_metadata_object->isNull(Iceberg::f_refs)) |
There was a problem hiding this comment.
These new unsupported-shape checks run before we know whether this OPTIMIZE will rewrite anything. plan.need_optimize is only set after scanning the manifests and finding at least one position delete (!all_positional_delete_files.empty() at line 368), and when it stays false we exit without touching metadata.
That means an append-only table with a rollbacked history, an extra ref, or one of these retention settings now starts throwing BAD_ARGUMENTS even though the command would have been a pure no-op. The same regression applies to the later is_current_ancestor guard. Please gate these refusals on actually needing a rewrite, or add an earlier cheap "has position deletes" pre-check, otherwise we turn harmless no-op OPTIMIZEs into hard failures.
Related: #114465
Problem
A rollback makes an older snapshot current again and leaves the newer, abandoned snapshots retained in
snapshots. On such a table a plainOPTIMIZE TABLEsucceeded quietly, and afterwards the table readas the abandoned branch tip again — the rollback was silently undone. Measured on a local build, with
history append
s1, position-deletes2, appends3, rolled back tos1:current-snapshot-idrefs.mainSELECT vs3s31,3,4s1s11,2,3OPTIMIZEs3s31,3,4Row
v=2disappears (the abandoned delete is applied) andv=4appears (the abandoned append isrepublished). The row count is 3 in both states, so a count-only check cannot see it.
Root cause
getPlaniterates every record of the history it is handed with nois_current_ancestorfilter,collecting each snapshot's manifests, data files and position deletes;
writeMetadataFilesthenreplays the snapshots in order, publishing each one and moving
current-snapshot-idandrefs.mainasit goes. So a snapshot that is not on the current chain is folded into the rewrite and the last one
replayed becomes current.
IcebergMetadata::getHistoryalready computes the fact that was missing: it walks the parent chainfrom
current-snapshot-idand marks each recordis_current_ancestor. Nothing in the compaction pathconsulted it.
Solution
Refuse the rewrite, in
checkIfIcebergHistorySupported— where compaction already forces the historyshapes its experimental implementation supports — naming the offending snapshot and leaving the table
untouched.
Dropping the non-current snapshots and compacting the rest is not a safe alternative: their data files
can still be referenced from another branch or tag, and compaction deletes the files it did not carry
over, so filtering would trade a wrong-result bug for a data-loss bug.
New test
05153_iceberg_optimize_rollback_replayasserts the row values before and afterOPTIMIZE,plus the refusal itself on an open-source build (a cloud build gates
OPTIMIZEonIcebergCompactionMetadataGeneratorand throws before reaching this check, so only the row assertionsapply there). With the check reverted and rebuilt, both of its assertions flip. The four existing
compaction tests —
04230,04513,04614,04815— pass either way.Compaction is behind the off-by-default
allow_experimental_iceberg_compaction, so no backport.#114465 guards the degenerate case of this defect — a table with no current snapshot at all — in
IcebergMetadata::optimize, beforecompactIcebergTable. The two changes do not collide: with bothin, a snapshotless table stays a quiet no-op and a rollbacked table is refused.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
OPTIMIZE TABLEon an Iceberg table silently moving the table off the snapshot it had been rolled back to, republishing the abandoned snapshots and changing the rows aSELECTreturns.Workflow [PR]
Sync PR [sync-upstream/pr/118951]