Skip to content

Fix manifest compaction delete all - #111241

Merged
alexey-milovidov merged 11 commits into
masterfrom
Fix_manifest_compaction_delete_all
Sep 13, 2026
Merged

alexey-milovidov merged 11 commits into
masterfrom
Fix_manifest_compaction_delete_all

Conversation

@SmitaRKulkarni

@SmitaRKulkarni SmitaRKulkarni commented Jul 21, 2026 •

Copy link
Copy Markdown
Member

OPTIMIZE TABLE ... MANIFEST crashed the server with a SIGSEGV when the current snapshot of an Iceberg table had no live data files — for example after a copy-on-write DELETE that removed every row. Every DATA manifest still listed by that snapshot yields zero live files, so the rewrite collected nothing and then asked the Avro writer to close a manifest list it had never written to, dereferencing a null stream.

Such a snapshot now takes the same "nothing to consolidate" path as an already optimally consolidated one, so the table is left alone and OPTIMIZE reports success. The condition is expressed as total_data_files == 0, which is what the check really means: an unpartitioned table is not affected, because the grouping key always carries the source partition-spec id and schema id and so still forms one group as soon as the table holds live files.

Closes: #111216

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixes a crash when running OPTIMIZE TABLE ... MANIFEST on an experimental Iceberg table whose rows had all been deleted.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.9.1.1412 (included in 26.9 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [52c641c]

Summary: ✅


AI Review

Summary

This PR makes OPTIMIZE TABLE ... MANIFEST treat an Iceberg snapshot with zero live data files as a no-op instead of descending into an empty manifest-list rewrite that dereferences the Avro writer's null stream. The current diff also correctly moves the key_metadata rejection behind the two no-op exits, and the updated integration test now proves both the all-deleted shape and that an unpartitioned table with live files still compacts.

Missing context / blind spots
  • ⚠️ I could not validate the data_file.key_metadata path end-to-end because the current integration suite cannot build an encrypted Iceberg table with non-null key_metadata. A focused fixture or synthetic manifest would be needed to prove that the moved rejection still fires on a table that genuinely needs a rewrite.
Final Verdict

✅ No blocking issues found in the current diff.

LLVM Coverage Report

Measured on commit 52c641c.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.70% -0.10%
Branches 81.30% 81.40% +0.10%

Changed lines: Changed C/C++ lines covered: 27/34 (79.41%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 21, 2026
@SmitaRKulkarni
SmitaRKulkarni requested a review from scanhex12 July 22, 2026 12:51
@SmitaRKulkarni
SmitaRKulkarni requested a review from scanhex12 July 23, 2026 09:59
@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

Changed lines: Changed C/C++ lines covered: 1/4 (25.00%) · Uncovered code

Full report · Diff report

if (partitions_map.size() >= num_data_manifests)
/// Nothing to consolidate: either no live data files remain (empty table; writing an empty manifest list would also crash the Avro writer),
/// or the data manifests are already optimally consolidated (at most one per partition) so a rewrite cannot reduce the count.
if (partitions_map.empty() || partitions_map.size() >= num_data_manifests)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if partitions_map.empty() means that table is unpartitioned, I think there could be manifests to compact. It means that the early exit is wrong, isn't it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🕵 An empty partitions_map does not mean "the table is unpartitioned" - the group key always starts with the source partition-spec id and schema id:

String partition_key = std::to_string(source_partition_spec_id) + "|" + std::to_string(source_schema_id) + "|";
FieldVisitorDump dump_visitor;
for (const auto & val : data_file->parsed_entry->partition_key_value)
    partition_key += applyVisitor(dump_visitor, val) + "|";

An unpartitioned table has an empty partition_key_value, so all of its live files land in one group keyed "0|0|": partitions_map.size() == 1, not 0. With N data manifests that makes partitions_map.size() >= num_data_manifests false for any N > 1, so such a table is consolidated exactly as before. The map is empty only when not a single live data file was collected from any DATA manifest of the current snapshot.

To make that impossible to misread I replaced the condition with the equivalent total_data_files == 0 and split it into its own branch with its own log message (6b482a4 - every group is created by the same code path that increments total_data_files, so the two conditions always hold together).

And because the argument should not live only in a comment, test_optimize_manifest_files_all_deleted now has a second phase (0368e1e): it refills the same unpartitioned table so the current snapshot lists several DATA manifests again, runs OPTIMIZE TABLE ... MANIFEST, and asserts they are consolidated into exactly one manifest with every live data-file entry preserved. test_optimize_manifest_files is the same shape (unpartitioned, seven inserts) and also still compacts.

@clickhouse-gh

clickhouse-gh Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Dear @scanhex12, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Sep 4, 2026
@alexey-milovidov alexey-milovidov self-assigned this Sep 12, 2026
alexey-milovidov and others added 3 commits September 12, 2026 20:42
`partitions_map` is keyed by `"<spec-id>|<schema-id>|"` followed by the partition
values, so it is never empty for an unpartitioned table that still has live data
files: such a table forms a single group keyed `"0|0|"`. The map is empty only when
not a single live data file was collected from the current snapshot's `DATA`
manifests, which the equivalent `total_data_files == 0` states outright.

Review feedback read `partitions_map.empty()` as "the table is unpartitioned", so
express the condition the unambiguous way, split it off into its own branch, and give
the two cases separate log messages. There is no behaviour change: every group is
created by the same code path that increments `total_data_files`, so
`partitions_map.empty()` and `total_data_files == 0` always hold together.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…a again

Extend `test_optimize_manifest_files_all_deleted` with a second phase: refill the
(unpartitioned) table so the current snapshot references several `DATA` manifests
again, run `OPTIMIZE TABLE ... MANIFEST`, and assert that they are consolidated into
exactly one manifest with every live data-file entry preserved.

This pins down that the early exit for a table with no live data files does not
swallow a table that genuinely needs compaction - previously that could only be argued
from the code. The manifest-list walk used by the precondition checks is factored into
`current_snapshot_data_manifests` so both phases share it, and it now also reports how
many `DATA` manifests the current snapshot lists.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`test_optimize_manifest_files_all_deleted` asserted that the post-`OPTIMIZE` snapshot
lists a single `DATA` manifest and saw four instead. The compaction itself was correct -
the server log shows `Creating manifest file for partition '0|0|' (4 data files)`,
`Creating manifest list with 1 partition manifest(s)` and `Committing metadata file:
.../v11.metadata.json` - but the test was looking at the wrong snapshot.

`default_upload_directory` uses `LocalUploader`, which copies Spark's table *into* the
instance's container with `copy_file_to_container`. The host-side
`/var/lib/clickhouse/user_files` and the container's are therefore different file
systems that only share a path prefix: the host holds what Spark wrote, while
`OPTIMIZE TABLE ... MANIFEST` commits its metadata inside the container. So the
host-side `get_last_snapshot` can never see a ClickHouse-side commit and kept returning
Spark's snapshot, whose manifest list still had the four appended manifests.

Resolve the current snapshot inside the instance instead, the way ClickHouse itself
resolves it: from the `v<N>.metadata.json` with the highest version. This also removes
the host/container mixing from the pre-`OPTIMIZE` precondition checks, which happened to
agree only because nothing had been committed on the ClickHouse side yet.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 52c641ca7 with master e5dd9d865 (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, 17 s compile time in total, 7 of them have a recent master baseline.

Job report

…ppens

The rejection of Iceberg tables with per-file `key_metadata` protects a *rewrite*: a
manifest-only rewrite cannot round-trip the data-file encryption keys, so dropping them
would make the table unreadable. It was checked while collecting the current snapshot's
manifests, i.e. before either "nothing to consolidate" early return was reachable.

That contradicts the contract this pull request establishes. An emptied table - whose
`DATA` manifests hold only deleted entries and are never read again - got
`NOT_IMPLEMENTED` instead of the no-op it is now supposed to get, and the same held for
a table whose manifests were already optimally consolidated. In both cases every
manifest is left exactly as it is, so there is no encryption metadata to lose and no
reason to turn reporting success into a hard error.

Move the check past both early returns, onto the path that actually writes new
manifests. The data manifests are collected into `data_manifest_paths` during the pass
that was already walking them, which also removes the separate `num_data_manifests`
counter that had to be kept in sync with them.

Addresses the AI review finding on `Compaction.cpp:642-654`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 13, 2026
Merged via the queue into master with commit 17e4013 Sep 13, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the Fix_manifest_compaction_delete_all branch September 13, 2026 17:24
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 13, 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

comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... pr-bugfix Pull request with bugfix, not backported by default 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.

OPTIMIZE ... MANIFEST SIGSEGV on an all-deleted Iceberg table

4 participants