Skip to content

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

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

robot-clickhouse-ci-2 merged 74 commits into
backport/26.3/100407from
cherrypick/26.3/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 21 commits May 12, 2026 18:47
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 against 26.3.

* `ManifestFileIterator::create` has a different signature in 26.3 (it
  takes `manifest_file_name` / `common_path` instead of the master-only
  `IcebergPathFromMetadata` / `IcebergPathResolver`). The `format_version`
  parameter is dropped and the version is derived from the manifest file's
  own Avro metadata, as in the original pull request; call sites in
  `IcebergIterator.cpp` and `StatelessMetadataFileGetter.cpp` are updated
  accordingly.
* `RemoveOrphanFilesExecute.cpp` does not exist in 26.3 - the
  `remove_orphan_files` command was introduced later - so its change and
  the `test_remove_orphan_files_after_external_upgrade` integration test
  are dropped from the backport.

Original pull-request: #100407
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 merged commit f1fd46c into backport/26.3/100407 Aug 1, 2026
2 checks passed
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 deleted the cherrypick/26.3/100407 branch August 1, 2026 08:55
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