Skip to content

Iceberg OPTIMIZE: write correct snapshot totals and read each position delete file once - #128759

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:iceberg-optimize-carried-forward-files-once
Nov 3, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:iceberg-optimize-carried-forward-files-once

Conversation

@groeneai

@groeneai groeneai commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #128265

Changelog category (leave one):

  • Experimental Feature

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

OPTIMIZE of Iceberg tables (allow_experimental_iceberg_compaction) now writes the total-records, total-data-files and total-files-size of every rewritten snapshot from its live rewritten files, so a trivial count() after compaction matches a full scan, and reads each position delete file once per data file it applies to instead of once for every snapshot that lists the two files, which makes compaction of long histories much cheaper. Closes #128265.

Description

getPlan reads the manifest list of every snapshot, and a manifest list also lists the manifests of earlier snapshots, so each data file and position delete file was collected once per snapshot listing it:

  • every copy of a delete file was applied to every copy of a data file, so each delete file was opened many times;
  • added_records was the row count of the snapshot's whole live set, and setSnapshotTotals adds it to the parent's total-records (in the issue: trivial count() 3, full scan 2).

Now each file is collected once, and a rewritten snapshot's added_records, added_files and added_files_size are its live rows, data files and bytes minus its parent's when the parent is rewritten too, so each total equals the live value. Counting a data file only under its first snapshot is not enough: a delete-only snapshot is not rewritten, so its child would get only its own rows (INSERT (1),(2); DELETE x = 1; INSERT (3) gives 1, the test's delete_parent case). total-data-files and total-files-size restarted there too (1 of 2 files, 551 of 1102 bytes, e.g. system.tables.total_bytes of a Merge table over it).

Other multi-snapshot walks already read each manifest once.

Validated by the new test (before: 3 2, delete-file reads 24 10; after: 2 2, 2 2), 50/50 randomized runs, and the Iceberg OPTIMIZE stateless and integration tests. OPTIMIZE of IcebergS3 on MinIO, N single-row inserts then M deletes (master already prunes by delete-file bounds):

N/M S3GetObject before -> after trivial count() before -> after (full scan)
10/2 84 -> 18 36 -> 8 (8)
20/4 484 -> 32 136 -> 16 (16)
40/8 3,332 -> 60 528 -> 32 (32)

Caused by #85250.


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

Version info

  • Merged into: 26.11.1.1207-master (included in 26.11 and later)

groeneai and others added 3 commits November 2, 2026 18:32
… once

The manifest list of every snapshot also lists the manifests of the earlier
snapshots, so getPlan collected each data file and position delete file once
per snapshot that lists it.

- Every copy of a delete file was applied to every copy of a data file, so
  IcebergPositionDeleteTransform opened the same delete file many times. The
  bitmap is idempotent, so only the cost grew: on IcebergS3 with 40 single-row
  inserts and 8 deletes, OPTIMIZE made 3,332 S3 GETs instead of 60.
- snapshot_id_to_data_files[S] is the whole live set of S, and its row count
  was passed as added_records, which setSnapshotTotals adds to the rewritten
  parent's total-records. INSERT (1),(2); INSERT (3); DELETE x = 1; OPTIMIZE
  left a trivial count() of 3 against a full scan of 2.

A data file now enters its partition bucket, and a delete file the delete
list, only the first time it is seen. added_records is the live rows of the
snapshot minus those of its parent when the parent was rewritten in the same
pass (the parent is then found by getParentSnapshot), else the live rows.

Attributing each data file only to the first snapshot that lists it would not
be enough for the totals: a delete-only snapshot is not rewritten, so an
append whose parent is one gets total-records = added_records alone, and
INSERT (1),(2); DELETE x = 1; INSERT (3) would report 1 instead of 2.

The accumulator is widened from Int32 to Int64: it sums a whole snapshot.

Closes ClickHouse#128265

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The trivial count falls back to the manifest row counts when it cannot use
total-records, so it alone cannot see total-records, and it never sees a
snapshot other than the current one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…shots from their live files

They were chained from the original deltas, so they restarted after a
delete-only parent and kept the pre-rewrite sizes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 rounds, 2 findings; last round clean) plus my own review.

# Sev Finding Verdict Evidence / action
1 ⚠️ The test read total-records only through the trivial count(), which falls back to manifest row counts when the snapshot totals are unusable AGREE, fixed the test asserts summary['total-records'] of every snapshot in system.iceberg_history
2 ⚠️ total-data-files and total-files-size were still chained from the original snapshot deltas: they restarted after a delete-only snapshot and kept the pre-rewrite sizes AGREE, fixed all three data totals come from the live rewritten files; the test asserts total-data-files of every snapshot and the current snapshot's total-files-size
3 💡 getPlan still parses each carried-forward manifest once per snapshot DISAGREE the per-snapshot live set needs it; the reads are metadata-cache hits (S3 GETs 3,332 -> 60 at 40/8)
4 💡 A rewritten snapshot whose parent is not rewritten reports its whole live set as added-* noted informational fields; the parent is absent from the rewritten metadata, so total-* = added-* is the base-snapshot reading

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-9:20261102-204600

@groeneai

groeneai commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new test fails on master every run: appended_parent 3 2, delete_file_reads 3 2, total-records ['1','3'] / ['0','0','1','3'], delete_parent total-data-files ['1','1'], the current snapshot's total-files-size differs from the data files' size in all 3 cases, delete-file reads 24 10.
b Root cause explained? Every snapshot's manifest list also lists the earlier manifests, so getPlan collected each data file and delete file once per listing snapshot. That made the delete loop apply each delete file once per (delete copy, data copy) pair, and passed each snapshot's whole live row count as added_records, which setSnapshotTotals adds to the rewritten parent's total.
c Fix matches root cause? Yes. Files are collected once where they are collected, and added_records is the live-row delta against the rewritten parent, so total-records equals the live rows.
d Test intent preserved / new tests added? New stateless test, 3 cases: the issue's shape, a delete-only parent, and per-delete-file reads. Each case checks the trivial count, the total-records and total-data-files of every snapshot in system.iceberg_history, and that the current snapshot's total-files-size equals the size of the data files; each has a mutation arm that turns it red.
e Both directions demonstrated? Yes. Master: 3 2, ['1','3'], size check 0, 24 10; fix: 2 2, ['1','2'], size check 1, 2 2; a build that fixes only total-records still fails (delete_parent total-data-files ['1','1'], size check 0 in all 3 cases); 50/50 randomized runs green.
f Fix is general across code paths? getPlan is the only walk over many snapshots that did not deduplicate. expire_snapshots and remove_orphan_files read each manifest once; manifest compaction, mutations and reads use one snapshot.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked 30 random partitioned INSERT/DELETE histories (0 mismatches vs 18/30 on master), a second OPTIMIZE, and the Spark-written histories of the integration tests. The change is about file lists, so there is no column-type axis.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or schema change; the next OPTIMIZE corrects totals that were already written wrong.
i Invariants and contracts preserved? total-records(S) equals the live rows of S. Only APPEND snapshots are rewritten, so the delta is >= 0. "Parent in the map" is the same predicate as getParentSnapshot finding it. Partition buckets hold each data file once.

Session id: cron:clickhouse-impl-slot-40:20261102-200700

@groeneai

groeneai commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author

cc @PedroTadim @SmitaRKulkarni, could you review this and add the can be tested label so CI runs? getPlan collected every carried-forward data file and position delete file once per snapshot that lists it; it now takes each file once, and a rewritten snapshot's total-* counters come from its live files minus those of its rewritten parent.

@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6d52681]

Summary: ✅


AI Review

Summary

This PR fixes Iceberg OPTIMIZE snapshot totals and repeated position-delete reads by collecting each physical file once and deriving rewritten snapshot deltas from the live files of the snapshot and its rewritten parent. Reviewed head 6d5268199f276a6547819ea83e0576b8aa56175a, the complete diff, surrounding compaction and metadata-generation code, the regression test, and all prior discussion. No actionable findings remain; approve.

Missing context / blind spots
  • ⚠️ Independent runtime validation is limited: the fetched Praktika reports contain Fast test results but no executed regular stateless test results. The new 05483_iceberg_optimize_counts_carried_forward_files_once_128265 test requires Avro and is excluded from Fast test. No local ClickHouse binary is available. Runtime evidence therefore comes from the author's recorded before/after measurements and randomized runs; running this test on an Avro-enabled OSS build would provide independent confirmation.
Final Verdict

✅ Approve — no new inline findings to post.

@clickhouse-gh clickhouse-gh Bot added pr-experimental Experimental Feature can be tested Allows running workflows for external contributors comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6d5268199 with master 4944813cc (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

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

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 3, 2026
Merged via the queue into ClickHouse:master with commit 3b3f22d Nov 3, 2026
529 of 531 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 3, 2026
@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 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... pr-experimental Experimental Feature pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Iceberg OPTIMIZE sums all live files into added-records: count() inflated

3 participants