Skip to content

[fix](iceberg) Read snapshots without schema-id - #117112

Open
1fanwang wants to merge 8 commits into
ClickHouse:masterfrom
1fanwang:fix/optional-snapshot-schema-id
Open

1fanwang wants to merge 8 commits into
ClickHouse:masterfrom
1fanwang:fix/optional-snapshot-schema-id

Conversation

@1fanwang

@1fanwang 1fanwang commented Aug 30, 2026 •

Copy link
Copy Markdown

Closes: #100304
Related: #114465

Iceberg snapshots may omit the optional schema-id field. ClickHouse currently converts the missing value to an integer unconditionally, so one valid current or historical snapshot can make the table unreadable with Poco::InvalidAccessException.

This change uses the table's current schema as the fallback, matching Iceberg's reference behavior. Explicit snapshot schema IDs still take precedence, and manifest reads retain their existing schema fallback.

Testing

The official master build at 2af4f37af27fba2b561f4984d95ff1703a7e2c00 reproduces both forms:

== snapshot omits schema-id ==
baseline rows: 1
Code: 1001. DB::Exception: Poco::InvalidAccessException: Invalid access. (STD_EXCEPTION)

== snapshot has "schema-id": null ==
baseline rows: 1
Code: 1001. DB::Exception: Poco::InvalidAccessException: Invalid access. (STD_EXCEPTION)

The new stateless test covers absent and null IDs on the selected snapshot, an older historical snapshot without an ID, and time travel to that snapshot. A post-fix ClickHouse binary was not available locally, so runtime green is deferred to CI. The shell and embedded Python parse cleanly; shellcheck and changed-line clang-format pass.

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):

Fix reading Iceberg tables when a current or historical snapshot omits its optional schema ID.


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

@1fanwang
1fanwang marked this pull request as ready for review August 30, 2026 02:13
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Aug 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8bb2278]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, targeted) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_asan_ubsan, flaky check) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_tsan, flaky check) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
13 more test cases not shown
Stateless tests (amd_msan, flaky check) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
2 more test cases not shown
Stateless tests (amd_debug, flaky check) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
2 more test cases not shown
Stateless tests (amd_binary, flaky check) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
3 more test cases not shown
Stateless tests (amd_debug, parallel) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
Stateless tests (arm_binary, parallel) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
Stateless tests (amd_asan_ubsan, db disk, distributed plan, targeted) FAIL
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
05052_iceberg_optional_snapshot_schema_id FAIL cidb
Too many test failures FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL

AI Review

Summary

This PR teaches Iceberg reads to tolerate snapshots that omit schema-id, expands the legacy Spark fixture, and fixes the v1-manifest fallback path. The main read path is much better, but one blocker remains: snapshots that still exist yet omit schema-id are still collapsed into the same fallback path as truly expired snapshots, so a later manifest rewrite can make schema-evolved historical files bypass the old-schema transform entirely.

Findings

❌ Blockers

  • [src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp:429, src/Storages/ObjectStorage/DataLakes/Iceberg/ManifestFileIterator.cpp:500] The new code never records any snapshot→schema mapping when a snapshot exists but omits schema-id; downstream ManifestFileIterator therefore falls back to manifest_schema_id exactly as it does for an actually missing/expired snapshot. That is only safe while the manifest header still carries the original file schema. ClickHouse's metadata-only rewrites and manifest compaction regenerate manifests with the current table schema in the header (src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergWrites.cpp:559, src/Storages/ObjectStorage/DataLakes/Iceberg/Compaction.cpp:1242) while preserving each entry's original snapshot_id, so after a rename or type change a time-travel read of one of these legacy snapshots can silently stop taking the old-schema transform path.
    Suggested fix: preserve a distinct "snapshot exists but omitted schema-id" state instead of merging it into the expired-snapshot fallback, and add a regression that rewrites/compacts manifests after schema evolution on this legacy fixture before reading the historical snapshot again.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Preserve a separate resolution path for snapshots that omit schema-id so rewritten manifests cannot masquerade as the expired-snapshot fallback.
    • Add a regression covering manifest rewrite or compaction after schema evolution on the legacy fixture.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8bb227887 with master 640dc1988 (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.

Object file sizes

5 object files changed (+51.85 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/ObjectStorage/DataLakes/Iceberg/ManifestFileIterator.cpp.o 491.88 KiB 519.57 KiB +27.69 KiB (+5.63%)
src/CMakeFiles/dbms.dir/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp.o 1.25 MiB 1.27 MiB +21.65 KiB (+1.70%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

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

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Sep 4, 2026
@scanhex12 scanhex12 self-assigned this Sep 8, 2026
metadata = json.load(metadata_file)

current_snapshot_id = metadata["current-snapshot-id"]
if mode == "historical":

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.

really strange test, do you have some ready table or could you please generate it explicitly with spark?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Replaced the test with a Spark-generated Iceberg fixture in data_minio/iceberg_optional_snapshot_schema_id, produced by generate.py via iceberg-spark-runtime. Updated 05052_iceberg_optional_snapshot_schema_id.sh to query the table across snapshots.

The Iceberg specification allows snapshots in older versions (e.g. v1 or legacy writers) to omit `schema-id`. When `schema-id` is missing in snapshot metadata, fall back to the current table schema id instead of failing.

Includes stateless test using Spark-generated legacy snapshot fixtures.

Signed-off-by: 1fanwang <[email protected]>
@1fanwang
1fanwang force-pushed the fix/optional-snapshot-schema-id branch from 9202d90 to 99c695f Compare September 30, 2026 00:35
Verify both current and historical snapshot reads when snapshot schema-id
is explicitly JSON null in metadata, exercising the !snapshot->isNull(f_schema_id)
guards in traverseMetadataAndFindNecessarySnapshotObject and
createIcebergDataSnapshotFromSnapshotJSON.

Signed-off-by: 1fanwang <[email protected]>
…ema-id

Allow reading tables using Iceberg format-version 1 metadata where
top-level schema omits schema-id (defaulting to current-schema-id or 0)
and schemas array is absent. Register the top-level schema in
traverseMetadataAndFindNecessarySnapshotObject and support v1 schema
resolution in getSortingKey and getSortingKeyDescriptionFromMetadata.
Add tests querying raw legacy v3 metadata.

Signed-off-by: 1fanwang <[email protected]>
Add column `extra STRING` in `v5.metadata.json` and `generate.py`, and assert
that time-travel queries of historical snapshots without snapshot `schema-id`
expose the evolved column as NULL.

Signed-off-by: 1fanwang <[email protected]>
Iceberg v1 makes the manifest schema ID optional and assigns the initial schema ID when it is absent. Legacy Spark manifests therefore failed before the snapshot fallback could take effect.

Signed-off-by: Stefan Wang <[email protected]>
The PR no longer merged cleanly after manifest schema selection changed on master. Keeping it current lets CI exercise the legacy fallback through the supported path.

Signed-off-by: Stefan Wang <[email protected]>
The parsed schema pointer was held through a const wrapper, which prevented adding the spec-defined fallback ID and stopped arm_tidy before the regression could run.

Signed-off-by: Stefan Wang <[email protected]>
schema_processor->registerSnapshotWithSchemaId(current_snapshot_id, current_schema_id);
if (snapshot->getValue<Int64>(f_metadata_snapshot_id) == snapshot_id)
const auto current_snapshot_id = snapshot->getValue<Int64>(f_metadata_snapshot_id);
if (snapshot->has(f_schema_id) && !snapshot->isNull(f_schema_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.

Skipping registerSnapshotWithSchemaId for legacy snapshots makes them indistinguishable from expired snapshots downstream. ManifestFileIterator::processRow treats !tryGetSchemaIdForSnapshot(...) as "snapshot metadata is gone" and falls back to the manifest header schema instead.

That is not safe once manifests are rewritten. ClickHouse's metadata-only rewrites keep each entry's original snapshot_id (src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergWrites.cpp:587, src/Storages/ObjectStorage/DataLakes/Iceberg/Compaction.cpp:1258), but the new manifest header is written with the current table schema (src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergWrites.cpp:559). For a legacy v1 table that later renames or type-changes a column, old files from a snapshot without schema-id will now be attributed to that rewritten header schema, so the read path stops taking the old-schema transform route that StorageObjectStorage.cpp:349 relies on for schema evolution.

Please keep a distinct mapping for "snapshot exists but omitted schema-id" instead of letting it fall into the same path as an actually missing/expired snapshot, and add a regression that rewrites manifests after a schema evolution on this legacy fixture.

The generated null-schema metadata fixture used a filename that metadata discovery rejects, so targeted CI failed before exercising the intended fallback.

Signed-off-by: 1fanwang <[email protected]>
@1fanwang
1fanwang force-pushed the fix/optional-snapshot-schema-id branch from 9d9bd1b to 8bb2278 Compare October 6, 2026 10:04
@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 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.

@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 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 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

can be tested Allows running workflows for external contributors comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: IcebergS3 Engine Fails to Parse Valid Iceberg v2 Metadata

4 participants