Repository navigation
Fix exception inserting into Iceberg tables that omit optional metadata arrays - #107473
Conversation
… 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]>
|
Workflow [PR], commit [5e5dfa7] Summary: ✅
AI ReviewSummaryThis PR makes Iceberg Findings❌ Blockers
Final Verdict
|
# Conflicts: # tests/integration/test_database_iceberg/test.py
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'.
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]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 70/70 (100.00%) · Uncovered code |
|
Hi @tiandiwonder @scanhex12 — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
|
@clickgapai Correct and known — the |
|
Thanks for the clarification, @tiandiwonder. I understand now — you deliberately scoped #107473 to avoid the one-line I filed #109732 based on the |
|
Update on the earlier review: CI on the proof PR #109732 confirmed the suspected bug via
(Automated follow-up. The proof PR has been closed; please track the bug on the Issue.) |
…ite-sibling-optional-fields Fix exception inserting into Iceberg tables that omit optional metadata arrays
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-logandsnapshot-logas optional, so empty-table metadata created elsewhere may omit any of them. When ClickHouse then appends a snapshot, the INSERT aborts:snapshotsmakesgetParentSnapshotextract an emptyPoco::Dynamic::Var→Poco::InvalidAccessException("Invalid access");metadata-logorsnapshot-logmakesgetArray(...)return a nullArray::Ptrwhose 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
parseTableSchemaand, becausecurrent-snapshot-idis absent or-1, returns early before the snapshot-traversal guard that would otherwise reject a missingsnapshots, 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/getParentSnapshotdoget(f_snapshots).extract<Array::Ptr>(), and the function later callsgetArray(f_snapshots/f_metadata_log/f_snapshot_log)->add(...).createEmptyMetadataFilealways 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 aPoco::JSON::Array::Ptrrather than a rawPoco::JSON::Array*— the latter would reproduce the same type-tag trap thatgetObject/getArrayhit (the bug PR #106896 fixed forrefs).This is a follow-up to PR #106896, which fixes the same bug class for the optional
refsobject. Therefsline 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) drivesgenerateNextMetadatadirectly 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):
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, orsnapshot-logarrays. Such inserts now succeed instead of failing.Documentation entry for user-facing changes
Version info
26.7.1.594(included in26.7and later)