Skip to content

Fix exception inserting into Iceberg tables that omit optional metadata arrays - #107473

Merged
tiandiwonder merged 5 commits into
ClickHouse:masterfrom
tiandiwonder:fix-iceberg-write-sibling-optional-fields
Jul 7, 2026
Merged

tiandiwonder merged 5 commits into
ClickHouse:masterfrom
tiandiwonder:fix-iceberg-write-sibling-optional-fields

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Related: #106896

Problem

Inserting into an Iceberg table whose metadata was produced by an external engine (e.g. BigLake or another lakehouse) can throw an exception instead of completing. The Iceberg spec marks snapshots, metadata-log and snapshot-log as optional, so empty-table metadata created elsewhere may omit any of them. When ClickHouse then appends a snapshot, the INSERT aborts:

  • a missing snapshots makes getParentSnapshot extract an empty Poco::Dynamic::Var → Poco::InvalidAccessException ("Invalid access");
  • a missing metadata-log or snapshot-log makes getArray(...) return a null Array::Ptr whose following ->add(...) dereferences null → Poco::NullPointerException ("Null pointer").

This is reachable for an empty externally-created table: the metadata read path returns the schema from parseTableSchema and, because current-snapshot-id is absent or -1, returns early before the snapshot-traversal guard that would otherwise reject a missing snapshots, so the write path is reached.

Root cause

MetadataGenerator::generateNextMetadata (src/Storages/ObjectStorage/DataLakes/Iceberg/MetadataGenerator.cpp) reads these three optional arrays without a presence check: getMaxSequenceNumber/getParentSnapshot do get(f_snapshots).extract<Array::Ptr>(), and the function later calls getArray(f_snapshots/f_metadata_log/f_snapshot_log)->add(...). createEmptyMetadataFile always seeds these arrays for ClickHouse-created tables, so the write path implicitly assumes their presence.

Fix

Seed each missing array once at the top of generateNextMetadata, stored as a Poco::JSON::Array::Ptr rather than a raw Poco::JSON::Array* — the latter would reproduce the same type-tag trap that getObject/getArray hit (the bug PR #106896 fixed for refs).

This is a follow-up to PR #106896, which fixes the same bug class for the optional refs object. The refs line is intentionally not touched here to avoid conflicting with that one-line change, so the bug class is fully closed only once both PRs land.

A new gtest (gtest_iceberg_metadata_generator.cpp) drives generateNextMetadata directly on minimal V2 metadata with each array removed (individually and all three); each case aborts on the unmodified code and passes with the guard. A parametrized integration test (test_insert_into_table_without_optional_metadata_arrays) strips the field(s) from real metadata in MinIO, inserts, and reads the row back end-to-end.

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 an exception when inserting into an Iceberg table whose metadata was created by an external engine and omits the optional snapshots, metadata-log, or snapshot-log arrays. Such inserts now succeed instead of failing.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.594 (included in 26.7 and later)

… arrays

generateNextMetadata (the Iceberg INSERT write path) reads three arrays the
Iceberg spec marks optional - snapshots, metadata-log, snapshot-log - without
checking they exist. Metadata produced by an external engine may omit any of
them, so the INSERT aborts: a missing snapshots makes getParentSnapshot extract
an empty Var (Poco::InvalidAccessException), and a missing log array makes
getArray return a null Array::Ptr that the following ->add dereferences
(Poco::NullPointerException).

This is reachable for an empty externally-created table: the read path returns
the schema from parseTableSchema and, because current-snapshot-id is absent/-1,
returns early before the snapshot-traversal guard that would otherwise reject a
missing snapshots, so the write path is reached.

Seed each missing array as a Poco::JSON::Array::Ptr (not a raw Array*, which
would reproduce the same type-tag trap getObject/getArray hit) before use.

This is a follow-up to PR ClickHouse#106896, which fixes the same bug class for the
optional refs object; the refs line is intentionally not touched here to avoid
conflicting with that one-line change, so the bug class is fully closed only
once both land.

Related: ClickHouse#106896

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

clickhouse-gh Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5e5dfa7]

Summary: ✅


AI Review

Summary

This PR makes Iceberg INSERT tolerate missing optional metadata-log / snapshot-log arrays and, for empty tables, a missing snapshots array, with focused unit and integration coverage. The July 7 follow-up fixed the earlier data-loss path for a completely absent snapshots field, but one unsafe variant remains: a live table whose snapshots array exists yet does not contain the current parent snapshot still commits a new snapshot and drops the previously visible data from reads.

Findings

❌ Blockers

  • [src/Storages/ObjectStorage/DataLakes/Iceberg/MetadataGenerator.cpp:138] The new guard only rejects a missing snapshots field. If the array is present but empty or missing parent_snapshot_id, getParentSnapshot returns nullptr, the summary totals are recomputed from zero, and generateManifestList copies no parent manifests. The commit then advances current-snapshot-id while orphaning all existing data. Reject this case with ICEBERG_SPECIFICATION_VIOLATION before building the new snapshot, and add a regression test for a live current-snapshot-id with an empty / parent-missing snapshots array.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions: reject writes when parent_snapshot_id >= 0 but the parent snapshot entry cannot be found in snapshots, and cover that case with a focused regression test.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 15, 2026
# Conflicts:
#	tests/integration/test_database_iceberg/test.py
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Jun 22, 2026
@tiandiwonder
tiandiwonder requested a review from scanhex12 June 22, 2026 07:58

@scanhex12 scanhex12 left a comment

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.

Good catch, thanks!

The new test calls get_file_contents (from helpers.s3_tools) to read the
table's metadata.json before stripping the optional arrays, but never imported
it, so the Style check failed with ruff F821 'Undefined name get_file_contents'.
tiandiwonder and others added 2 commits June 26, 2026 01:09
Bring the branch up to date with master (it was ~2029 commits behind). This
picks up d7f3d44 'Make 04330_rocksdb_truncate_drop_null_ptr stress-restart
safe', which fixes the unrelated stress failure on the sync PR
(clickhouse-private ClickHouse#61740): the stale branch was running the old
04330_rocksdb test, whose read-only EmbeddedRocksDB table could not be reopened
after the stress server restart, so the server failed to start
(Cannot start clickhouse-server / ROCKSDB_ERROR). Not related to the Iceberg
change in this PR.
Seeding an empty `snapshots` list for metadata that carries a live
`current-snapshot-id` would make the commit silently drop all previously
committed data (the manifest-list copy loop finds no parent). Keep seeding
only for the no-current-snapshot case and throw
`ICEBERG_SPECIFICATION_VIOLATION` otherwise, matching the read path.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.70% 85.70% +0.00%
Functions 92.60% 92.70% +0.10%
Branches 77.90% 77.90% +0.00%

Changed lines: Changed C/C++ lines covered: 70/70 (100.00%) · Uncovered code

Full report · Diff report

@tiandiwonder
tiandiwonder added this pull request to the merge queue Jul 7, 2026
Merged via the queue into ClickHouse:master with commit 29c27b2 Jul 7, 2026
175 checks passed
@tiandiwonder
tiandiwonder deleted the fix-iceberg-write-sibling-optional-fields branch July 7, 2026 12:43
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 7, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @tiandiwonder @scanhex12 — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

@tiandiwonder

Copy link
Copy Markdown
Contributor Author

@clickgapai Correct and known — the refs line is the same bug family but was deliberately left out of this PR to avoid conflicting one-line changes: it is fixed by #106896 (open, awaiting review). Not addressed here or in #109739.

@clickgapai

Copy link
Copy Markdown
Contributor

Thanks for the clarification, @tiandiwonder.

I understand now — you deliberately scoped #107473 to avoid the one-line refs fix conflicting with #106896, which is already open and handles that same bug family. That's a sensible separation to avoid merge conflicts and keep review scope tight.

I filed #109732 based on the refs issue I found in generateNextMetadata, but I should have checked the related PRs first — that would have surfaced that it's already known and in-flight. My apologies for the duplicate; it looks superseded by #106896. Thanks for the context — a good reminder to map the dependency graph before filing.

@clickgapai

Copy link
Copy Markdown
Contributor

Update on the earlier review: CI on the proof PR #109732 confirmed the suspected bug via Integration tests (amd_msan, 4/8). Filed as a separate Issue so it isn't lost when the proof PR closes:

(Automated follow-up. The proof PR has been closed; please track the bug on the Issue.)

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ite-sibling-optional-fields

Fix exception inserting into Iceberg tables that omit optional metadata arrays
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 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.

4 participants