Skip to content

Cherry pick #100407 to 25.8: Fix exception when Iceberg format version is upgraded by external tool - #112748

Merged
robot-clickhouse-ci-2 merged 74 commits into
backport/25.8/100407from
cherrypick/25.8/100407
Aug 1, 2026
Merged

robot-clickhouse-ci-2 merged 74 commits into
backport/25.8/100407from
cherrypick/25.8/100407

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #100407

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 30 commits March 23, 2026 01:58
When external tools like Spark upgrade the Iceberg format version (e.g.
from v1 to v2), the `format_version` cached in `PersistentTableComponents`
becomes stale. The `chassert` in `getState` and `getHistory` would then
trigger a logical error because the cached value no longer matches the
metadata file.

Replace the assertions with updates to the cached `format_version`, so
that downstream code (manifest parsing, etc.) always uses the correct
version. The field is changed from `const` to `mutable` to allow
updating it from const methods.

Fixes: #86776

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
… `std::atomic`

The `format_version` field can be updated when external tools upgrade
the Iceberg format version, but can also be read concurrently from
multiple queries. Using `mutable Int32` without synchronization is a
data race (undefined behavior). Replace with `std::atomic<Int32>`.

Since `std::atomic` is neither copyable nor movable, add explicit
constructors for `PersistentTableComponents`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…version-upgrade

# Conflicts:
#	src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp
#	src/Storages/ObjectStorage/DataLakes/Iceberg/PersistentTableComponents.h
Adds an integration test that verifies ClickHouse can read an Iceberg
table after its `format-version` has been upgraded from v1 to v2 by an
external tool (simulated by directly modifying the metadata JSON).
This is a regression test for #86776

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
When an external tool (e.g. Spark) upgrades the Iceberg format version
from v1 to v2, existing manifest files are left in v1 format (per the
Iceberg specification, their entries are interpreted as having
`sequence_number = 0`).  `ManifestFileIterator::create` was checking the
table's cached `format_version`, so it rejected these legacy manifests
with an `ICEBERG_SPECIFICATION_VIOLATION` exception claiming that the
required `sequence_number` column was missing.

Use the manifest file's own Avro metadata to determine how to parse it,
matching what `AvroForIcebergDeserializer::createParsedManifestFileEntry`
already does for entry-level decoding.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100407&sha=1299abf41df6d00d2744ca5a67931bf136c0aa15&name_0=PR

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Address review feedback: `PersistentTableComponents` is shared across
concurrent queries, so mutating `format_version` (even via `std::atomic`)
introduced a cross-query correctness hazard — query A could observe the
`format-version` written by query B while still parsing A's own metadata
structures.

Revert the field to `const Int32` and drop the assignments in `getState`
and `getHistory`. Parsing now relies on the format version recorded in
each manifest list / manifest file's own Avro metadata, which
`getFormatVersionFromManifestFileMetadata` already returns: the previous
commit already wired this into `ManifestFileIterator::create`, and this
commit extends the same approach to `getManifestList` so that a v1
manifest list left over after a Spark v1 -> v2 upgrade is not read with
v2 rules.
The previous commits moved away from mutating the cached `format_version`
in `PersistentTableComponents` toward reading the format version from each
manifest list / manifest file's own Avro metadata. Update the test's
docstring accordingly so the intent matches the current implementation.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…:create`

After the switch to reading the format version from each manifest file's
own Avro metadata, the `format_version_` argument threaded through from
`PersistentTableComponents` became dead — `create` derives the authoritative
version itself via `getFormatVersionFromManifestFileMetadata`. Drop it from
the signature and from both call sites.

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

`getFormatVersionFromManifestFileMetadata` previously returned 1 whenever the
Avro `format-version` metadata key was absent. This regressed Iceberg tables
written by ClickHouse, because `IcebergWrites.cpp` did not set that key when
generating manifest lists or manifest files. v2-only fields such as
`sequence_number` and `data_file.content` then went unread, and `ALTER TABLE ...
UPDATE` failed with `ICEBERG_SPECIFICATION_VIOLATION` while reading the
position-delete `Parquet` file.

Two fixes:

1. Fall back to schema-based detection when the manifest's Avro metadata is
   missing the key. The `sequence_number` field appears at the top level of v2
   manifest lists and v2 manifest entries, but is absent from their v1
   counterparts, so its presence is a reliable signal.

2. Make ClickHouse's manifest writer set the `format-version` Avro metadata key
   on both manifest lists and manifest files, so freshly written manifests no
   longer rely on the schema-based fallback.

Regression tests: `04061_iceberg_update_partition_key_empty_block` and
`04092_iceberg_update_low_cardinality`.

Reports:
- https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100407&sha=4a25e2f21ab4ffea9c4e7a65d3b2dcd9497927f1&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%201%2F2%29
- https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100407&sha=4a25e2f21ab4ffea9c4e7a65d3b2dcd9497927f1&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%202%2F2%29

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Per the review on #100407, the
`executeRemoveOrphanFiles` version gate was still using
`persistent_components.format_version`, which is captured when the table is
opened and can become stale after an external tool (e.g. Spark) upgrades the
Iceberg `format-version` from `v1` to `v2`. This caused `EXECUTE
remove_orphan_files` to throw `remove_orphan_files requires Iceberg format
version >= 2` even after the table was upgraded, until the table or server was
reloaded.

Now we read the latest metadata file and derive the format version from there
for the command gate.
Older ClickHouse versions wrote manifest lists and manifest files without
the `format-version` Avro metadata key.
`AvroForIcebergDeserializer::getFormatVersionFromManifestFileMetadata` falls
back to schema-based detection in that case (presence of `sequence_number`
at the top level signals v2, its absence signals v1), but that branch was
not exercised by any existing test.

This test covers both branches of the fallback by stripping the
`format-version` Avro metadata key from every manifest list / manifest file
of the table (using the `avro` Python library), dropping the Iceberg
metadata cache, and verifying that reads still succeed for v1 and v2
tables.

Addresses an unresolved review comment on
#100407

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

Bugfix validation runs the new tests against the master release binary
(no sanitizers) and expects them to fail without the fix. The existing
two cases only exercised the `chassert` path that this PR removes; in a
release build that assertion is a no-op, so they passed on master and
the bugfix-validation job reported "Failed to reproduce the bug".

Add `test_remove_orphan_files_after_external_upgrade`: it opens a v1
table, populates `PersistentTableComponents.format_version` via a read,
upgrades the metadata file to v2 out-of-band, and runs
`EXECUTE remove_orphan_files`. Without the fix the version gate uses
the cached `format_version = 1` and throws
`requires Iceberg format version >= 2`; with the fix it consults the
latest metadata and succeeds. This is the release-observable behavior
introduced by the gate change in `RemoveOrphanFilesExecute.cpp`.

CI report: #100407

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
alexey-milovidov and others added 20 commits May 13, 2026 02:46
The previous commit `cc42588f5a5` ("Keep `auto_releases.yml` at the
previous PR tip to avoid push rejection") inadvertently reverted the
master change in `db48967358e` that disabled fail-fast for the
auto-release matrix. Without `fail-fast: false`, a failure on one
release branch cancels the queued matrix entries for the others,
hiding subsequent failures.
…tion

The OAuth token used to push from this environment lacks the `workflow`
scope, so any commit that adds or modifies a file under `.github/workflows/`
is rejected on push. Removing the master-only `optimize_clickhouse.yml` from
the merge commit restores the workflow tree to what was already in the PR.
The prior commit deleted/modified workflow files in this branch as a
workaround for GitHub's workflow-scope check. Restoring them to match
`master` so the PR diff no longer contains unrelated changes.
The `test_format_version_upgrade_concurrent_reads` test submits infinite
reader loops to a `ThreadPoolExecutor`, then sets `stop["flag"]` only after
the main-thread upgrade path succeeds. If `_read_iceberg_metadata`, the
`format-version` assertion, or `_write_iceberg_metadata` raised, the flag was
never set, so the `ThreadPoolExecutor` shutdown on leaving the `with` block
waited forever on the reader loops and the test hung until the global CI
timeout instead of failing fast.

Set `stop["flag"]` in a `finally` so the readers always stop. Addresses the
AI Review finding reported by the `CH Inc sync` job.

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

Fix exception when Iceberg format version is upgraded by external tool
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Jul 31, 2026
Resolve the cherry-pick conflicts of #100407 ("Fix exception when Iceberg
format version is upgraded by external tool") against the 25.8 Iceberg code,
which predates the `Common/AvroForIcebergDeserializer` move, the
`ManifestFileIterator` split and the `remove_orphan_files` command.

Adaptations to 25.8:

- `AvroForIcebergDeserializer` lives under `DataLakes/Iceberg` and has no
  `getFormatVersionFromManifestFileMetadata` yet, so the whole method is added
  (metadata key first, schema-based fallback on `sequence_number` second).
- The manifest-file parser is `ManifestFileContent`, not `ManifestFileIterator`.
  Its `format_version_` constructor parameter is dropped and replaced by the
  manifest file's own version, taken from its Avro metadata.
- `IcebergMetadata::update` carries the `chassert` that master has in
  `IcebergMetadata::getState`; both it and the one in
  `IcebergMetadata::getHistory` are removed.
- `IcebergIterator` still gates the manifest-list content type check on the
  cached `persistent_components.format_version`. The gate is removed, matching
  master: v1 manifest list entries are always `DATA`, so the comparison alone is
  correct and no longer depends on a value that goes stale after an external
  upgrade.
- `remove_orphan_files` does not exist in 25.8, so that part of the change and
  its test are omitted.
- The new integration tests are appended to
  `tests/integration/test_storage_iceberg/test.py` (25.8 has no
  `test_storage_iceberg_with_spark` package and no shared `conftest.py`), use
  the `started_cluster` fixture and the `allow_experimental_insert_into_iceberg`
  setting, which is how 25.8 spells `allow_insert_into_iceberg`.

Original pull request: #100407
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Resolved the cherry-pick conflicts and reworked the change for the 25.8 Iceberg code, which predates several master refactorings. The PR is now MERGEABLE/CLEAN (merge commit d945a97a6f8).

Adaptations to 25.8:

  • AvroForIcebergDeserializer lives under DataLakes/Iceberg (not DataLakes/Common) and has no getFormatVersionFromManifestFileMetadata yet, so the whole method was added — format-version Avro metadata key first, schema-based fallback on sequence_number second.
  • The manifest-file parser is ManifestFileContent, not ManifestFileIterator. Its format_version_ constructor parameter is dropped and replaced by the manifest file's own version.
  • Master's chassert in IcebergMetadata::getState is in IcebergMetadata::update in 25.8; that one and the one in IcebergMetadata::getHistory are removed.
  • 25.8's IcebergIterator still gates the manifest-list content-type check on the cached persistent_components.format_version. The gate is removed, matching master: v1 manifest list entries are always DATA, so the comparison alone is correct and no longer reads a value that goes stale after an external upgrade.
  • remove_orphan_files does not exist in 25.8, so that hunk and its test are omitted.
  • The integration tests were appended to tests/integration/test_storage_iceberg/test.py (25.8 has no test_storage_iceberg_with_spark package and no shared conftest.py), using its started_cluster fixture and the allow_experimental_insert_into_iceberg setting, which is how 25.8 spells allow_insert_into_iceberg.

All six changed translation units pass a syntax check. No full build here — the follow-up backport PR gets the CI.

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

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants