Skip to content

Fix Iceberg snapshot summary totals handling - #109288

Open
Diskein wants to merge 15 commits into
masterfrom
dk-iceberg-snapshot-summary-totals
Open

Diskein wants to merge 15 commits into
masterfrom
dk-iceberg-snapshot-summary-totals

Conversation

@Diskein

@Diskein Diskein commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Related: #105198
Related: #108634

  • make all SnapshotSummaryTotals optionals, so if a member wasn't find in the initial snapshot it won't be update to some wrong values (it seems that Spark does the same)
  • MetadataGenerator::generateNextMetadata uses Iceberg::SnapshotSummaryUpdate
  • The OPTIMIZE metadata rebuild chains rebuilt snapshots to each other and attributes each data file only to the snapshot that introduced its manifest, fixing inflated total-records (and SELECT count()) after OPTIMIZE.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Iceberg snapshot summary total-* fields now propagate unknown totals instead of treating them as 0, and OPTIMIZE no longer inflates total-records, which made SELECT count() wrong after OPTIMIZE.


Workflow [PR]
Sync PR [sync-upstream/pr/109288]

@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c8afacb]

Summary: ✅


AI Review

Summary

This PR switches Iceberg snapshot-summary arithmetic to typed SnapshotSummaryUpdate objects and allows total-* fields to stay unknown instead of being coerced to 0, while also trying to rebuild OPTIMIZE TABLE history from those typed summaries. The optional-total changes look directionally right, but the full-table OPTIMIZE TABLE rewrite still misattributes files across manifest-rewriting appends and keeps stale byte totals, and mutation summaries still underdescribe multi-file position-delete overwrites. I do not think this is ready to merge.

Findings

❌ Blockers

  • [src/Storages/ObjectStorage/DataLakes/Iceberg/Compaction.cpp:258-290] and [src/Storages/ObjectStorage/DataLakes/Iceberg/Compaction.cpp:1113-1260] OPTIMIZE TABLE still assigns file ownership by manifest, not by manifest-entry lineage. [dismissed by author -- https://github.com/Fix Iceberg snapshot summary totals handling #109288#discussion_r3521072079] getPlan only visits a manifest on its first reference (manifest_file_to_first_snapshot) and permanently attaches each DataFilePlan to that first-seen ManifestFilePlan. When a later append rewrites or merges manifests and carries older files as EXISTING entries, those older files are either re-counted in the later snapshot's added-records or disappear from the later rewritten manifest list entirely because they stay grouped under the earlier manifest. The rebuilt current snapshot can therefore lose live rows after OPTIMIZE TABLE, and historical snapshot / time-travel results diverge even though the source table was valid.
    Suggested fix: drive snapshot_id_to_data_files and rewritten manifest grouping from each entry's resolved lineage (parsed_snapshot_id / inherited added_snapshot_id, plus the preserved sequence fields) instead of manifest_file_to_first_snapshot, so a carried-forward entry remains visible in every snapshot that referenced it while only the snapshot that originally added it claims it in summary deltas.

⚠️ Majors

  • [src/Storages/ObjectStorage/DataLakes/Iceberg/Mutations.cpp:416-423] and [src/Storages/ObjectStorage/DataLakes/Iceberg/Compaction.cpp:987-995] A delete or update that writes more than one position-delete file still records only added_delete_files, not added_position_delete_files. overwriteIsPositionDeleteOnly treats the missing breakdown as acceptable only when added_delete_files == 1, so a multi-partition mutation written by ClickHouse will later make OPTIMIZE TABLE reject the history as an unsupported overwrite even though it only added position deletes.
    Suggested fix: set added_position_delete_files = delete_files_count in the overwrite update here and add a regression that mutates multiple partitions before OPTIMIZE TABLE.

  • [src/Storages/ObjectStorage/DataLakes/Iceberg/Compaction.cpp:1083-1095] OPTIMIZE TABLE now recomputes added-records from the rewritten files, but added-files-size is still copied from the pre-compaction append summary. [dismissed by author -- https://github.com/Fix Iceberg snapshot summary totals handling #109288#discussion_r3621625444] writeDataFiles measures the new bytes into new_bytes_count and the regenerated manifests publish those sizes, yet the rebuilt snapshot summary keeps the old byte delta. Whenever compaction changes the Parquet size, total-files-size becomes stale, and IcebergMetadata::totalBytes returns that stale summary hint directly.
    Suggested fix: sum new_bytes_count for each rebuilt snapshot when creating the SnapshotSummaryUpdateAppend, and add an OPTIMIZE TABLE regression that asserts total-files-size matches the rewritten manifests after a size-changing rewrite.

Final Verdict

❌ Changes requested — the optional-total plumbing is on the right track, but the full-table OPTIMIZE TABLE rebuild is still incorrect for manifest-rewriting histories and multi-file mutation overwrites, and it still leaves stale byte totals behind.

LLVM Coverage Report

Measured on commit c8afacb.

Metric Baseline Current Δ
Lines 88.50% 88.50% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 80.70% 80.70% +0.00%

Changed lines: Changed C/C++ lines covered: 277/301 (92.03%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 3, 2026
@Diskein
Diskein marked this pull request as ready for review July 3, 2026 16:29
/// them only for the snapshot that introduced the manifest; otherwise a carried-forward
/// file is attributed to several rebuilt snapshots and its rows are counted multiple
/// times in the summary totals (`total-records`), inflating a trivial `SELECT count()`.
if (plan.manifest_file_to_first_snapshot[manifest_file.manifest_file_path] != snapshot.snapshot_id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This still keys row ownership by manifest, not by file entry. ManifestFileIterator::processRow already resolves a per-entry snapshot_id from the manifest row (parsed_snapshot_id or the inherited manifest-list id), because a manifest may contain EXISTING files from older snapshots. With the new manifest_file_to_first_snapshot filter, a manifest that was rewritten or merged in a later APPEND will move all of those older files into that later snapshot during OPTIMIZE.

That breaks rebuilt history: earlier snapshots lose data they still owned, and the later snapshot's total-records / time-travel results become wrong. The fix needs to group data files by each entry's resolved snapshot_id (or sequence), not by the first snapshot that referenced the manifest file.

@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

This PR is confined to Iceberg snapshot-summary metadata generation (append/overwrite/replace total accounting, unknown-total propagation) and its tests; it does not touch any query-execution hot path that ClickBench exercises. The two flagged ClickBench deltas (Q4 -18.3%, Q15 -20.9%) are both apparent improvements that the deterministic gate correctly held at no_change as within-envelope variance, and that matches the diff — the changed code never runs for these MergeTree SELECTs. No real per-query effect; the suppressions look consistent with the change.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: bdb5225f-07aa-4695-8e22-bb339a3baf15
  • MIRAI run: 550ed447-8a02-4b40-94ff-10685a79be8e
  • PR check IDs:
    • clickbench_81587_1784746955
    • clickbench_81593_1784746955
    • clickbench_81609_1784746955
    • tpch_adapted_1_official_81618_1784746955
    • tpch_adapted_1_official_81641_1784746955
    • tpch_adapted_1_official_81655_1784746955

@Diskein
Diskein requested a review from scanhex12 July 22, 2026 17:08
@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.10% 86.20% +0.10%
Functions 92.00% 92.00% +0.00%
Branches 78.20% 78.30% +0.10%

Changed lines: Changed C/C++ lines covered: 234/269 (86.99%) · Uncovered code

Full report · Diff report

Diskein added a commit that referenced this pull request Jul 29, 2026
Add experimental `ALTER TABLE ... DROP PARTITION` support for local and object-storage Iceberg tables when every affected manifest is fully matched. Reject catalog-backed tables, evolved specs, and partially matched manifests before metadata writes.

Preserve history with a `DELETE` snapshot, carry unaffected manifests forward, and handle an empty manifest list safely.

Related: #109288
Diskein added a commit that referenced this pull request Jul 29, 2026
Add experimental `ALTER TABLE ... DROP PARTITION` support for local and object-storage Iceberg tables when every affected manifest is fully matched. Reject catalog-backed tables, evolved specs, and partially matched manifests before metadata writes.

Preserve history with a `DELETE` snapshot, carry unaffected manifests forward, and handle an empty manifest list safely.

Related: #109288
Diskein added a commit that referenced this pull request Aug 5, 2026
Add experimental `ALTER TABLE ... DROP PARTITION` support for local and object-storage Iceberg tables when every affected manifest is fully matched. Reject catalog-backed tables, evolved specs, and partially matched manifests before metadata writes.

Preserve history with a `DELETE` snapshot, carry unaffected manifests forward, and handle an empty manifest list safely.

Related: #109288
@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Sep 4, 2026
Diskein added a commit that referenced this pull request Sep 14, 2026
Add experimental `ALTER TABLE ... DROP PARTITION` support for local and object-storage Iceberg tables when every affected manifest is fully matched. Reject catalog-backed tables, evolved specs, and partially matched manifests before metadata writes.

Preserve history with a `DELETE` snapshot, carry unaffected manifests forward, and handle an empty manifest list safely.

Related: #109288
Diskein added a commit that referenced this pull request Sep 16, 2026
Add experimental `ALTER TABLE ... DROP PARTITION` support for local and object-storage Iceberg tables when every affected manifest is fully matched. Reject catalog-backed tables, evolved specs, and partially matched manifests before metadata writes.

Preserve history with a `DELETE` snapshot, carry unaffected manifests forward, and handle an empty manifest list safely.

Related: #109288
@Diskein
Diskein force-pushed the dk-iceberg-snapshot-summary-totals branch from e0c1401 to ff16f36 Compare September 16, 2026 13:35
.added_files = static_cast<UInt64>(data_files_count),
.added_records = static_cast<UInt64>(data_rows),
.added_files_size = static_cast<UInt64>(delete_bytes + data_bytes),
.added_delete_files = static_cast<UInt64>(delete_files_count),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

delete_files in this path are always position-delete files, but the typed SnapshotSummaryUpdateOverwrite still leaves added_position_delete_files at 0. overwriteIsPositionDeleteOnly later uses that field to decide whether an OVERWRITE is a delete-only snapshot it can compact (Compaction.cpp:992-995), and it only tolerates the field being absent when there is exactly one delete file. A mutation that touches two partitions will therefore write added_delete_files = 2, fail that predicate, and make OPTIMIZE TABLE reject the history as unsupported even though nothing but position deletes was added.

Please set added_position_delete_files = delete_files_count here and add a multi-partition regression test for OPTIMIZE after a mutation.

Diskein added a commit that referenced this pull request Sep 16, 2026
Add experimental `ALTER TABLE ... DROP PARTITION` support for local and object-storage Iceberg tables when every affected manifest is fully matched. Reject catalog-backed tables, evolved specs, and partially matched manifests before metadata writes.

Preserve history with a `DELETE` snapshot, carry unaffected manifests forward, and handle an empty manifest list safely.

Related: #109288
Diskein added a commit that referenced this pull request Sep 16, 2026
`SnapshotSummary` derives the `total-*` fields of an overwrite, delete or
replace from the parent snapshot and throws a `LOGICAL_ERROR` when there is
none, which aborts the sanitizer unit test binaries. Production writers
only emit such updates against a table with a current snapshot, so
`OverwriteOperationForDelete` and `ReplaceOperationIsPreserved` now append
a first snapshot and pass its id as the parent.

CI: https://s3.amazonaws.com/clickhouse-test-reports/PRs/109288/b52a5fc3cc489c88643939083faa075160d03481/pr/unit_tests_msan/job.log
PR: #109288

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Diskein and others added 15 commits September 17, 2026 11:58
…ator

MetadataGenerator::generateNextMetadata now takes a typed
Iceberg::SnapshotSummaryUpdate instead of six loose Int64 counters and
builds the snapshot summary through SnapshotSummary, so the totals
follow the optional-propagation rules instead of ad-hoc string sums.

It also fails close when a positive parent snapshot id does not resolve
to a snapshot in the metadata or its summary cannot be read (except v1,
where summary fields are optional), instead of silently treating the
parent totals as zero.

Extracted from the Iceberg ALTER TABLE DROP PARTITION branch
(dk-iceberg-alter-drop-partition).

Co-Authored-By: Claude Fable 5 <[email protected]>
Relink each rebuilt snapshot to the previously generated one (not the
original parent, which may be a skipped DELETE) so the fail-close
parent-totals check holds, and attribute each data file only to the
snapshot that introduced its manifest so a carried-forward file is not
counted in every later snapshot's summary totals (which inflated
SELECT count()).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ignature

The test was added on master after this branch changed
`generateNextMetadata` to take `Iceberg::SnapshotSummaryUpdate`, so the
rebase left it calling the removed six-counter overload and the
`Build (arm_tidy)` check failed to compile.

Co-Authored-By: Claude Fable 5 <[email protected]>
`SnapshotSummary` derives the `total-*` fields of an overwrite, delete or
replace from the parent snapshot and throws a `LOGICAL_ERROR` when there is
none, which aborts the sanitizer unit test binaries. Production writers
only emit such updates against a table with a current snapshot, so
`OverwriteOperationForDelete` and `ReplaceOperationIsPreserved` now append
a first snapshot and pass its id as the parent.

CI: https://s3.amazonaws.com/clickhouse-test-reports/PRs/109288/b52a5fc3cc489c88643939083faa075160d03481/pr/unit_tests_msan/job.log
PR: #109288

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…NIFEST`

`test_optimize_manifest_parent_summary_missing_totals` still asserted the old
behaviour, where a `total-*` counter missing from the parent snapshot summary
was written as "0" on the manifest-only snapshot. Snapshot summaries now carry
an unknown total forward as unknown, like Spark, so the stripped fields must
stay absent while the totals the parent does know are carried over unchanged.

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109288&sha=7f2d2b41f12392abaac72c548b8d4464f6c3119a&name_0=PR&name_1=Integration%20tests%20%28amd_msan%2C%208%2F8%29
PR: #109288

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Diskein
Diskein force-pushed the dk-iceberg-snapshot-summary-totals branch from 7f2d2b4 to c8afacb Compare September 17, 2026 11:59
@clickhouse-gh

clickhouse-gh Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c8afacb46 with master 88e85261a (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

39 translation units recompiled, 392 s compile time in total, 39 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×0.92 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -31.6 s (-7%) in total before that adjustment.

Job report

Diskein added a commit that referenced this pull request Sep 20, 2026
Add experimental `ALTER TABLE ... DROP PARTITION` support for local and object-storage Iceberg tables when every affected manifest is fully matched. Reject catalog-backed tables, evolved specs, and partially matched manifests before metadata writes.

Preserve history with a `DELETE` snapshot, carry unaffected manifests forward, and handle an empty manifest list safely.

Related: #109288
@clickhouse-gh clickhouse-gh Bot added comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... and removed comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants