Skip to content

Fix iceberg write for metadata without refs - #106896

Merged
tiandiwonder merged 5 commits into
ClickHouse:masterfrom
AVMusorin:npe-lakehouse
Jul 27, 2026
Merged

tiandiwonder merged 5 commits into
ClickHouse:masterfrom
AVMusorin:npe-lakehouse

Conversation

@AVMusorin

@AVMusorin AVMusorin commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

It can happen when the table was created outside of ClickHouse (BigLake/Lakehouse in my case).

Code: 1000. DB::Exception: Null pointer. (POCO_EXCEPTION) (version 26.5.1.882 (official build))
stack_trace:
0. DB::MetadataGenerator::generateNextMetadata(DB::FileNamesGenerator&, DB::Iceberg::IcebergPathFromMetadata const&, long, long, long, long, long, long, long, std::optional<long>, std::optional<long>) @ 0x000000001a063a83
1. DB::IcebergStorageSink::initializeMetadata() @ 0x000000001a03939b
2. DB::IcebergStorageSink::onFinish() @ 0x000000001a038da8
3. DB::runStep(std::function<void ()>, std::shared_ptr<DB::ThreadGroup>&) @ 0x000000001db85880
4. DB::ExceptionKeepingTransform::work() @ 0x000000001db853cb
5. DB::ExecutionThreadContext::executeTask() @ 0x000000001d89b033
6. DB::PipelineExecutor::executeStepImpl(unsigned long, DB::IAcquiredSlot*, std::atomic<bool>*) @ 0x000000001d889f04
7. DB::PipelineExecutor::execute(unsigned long, bool) @ 0x000000001d888294
8. void std::__function::__policy_func<void ()>::__call_func[abi:fqe220101]<ThreadFromGlobalPoolImpl<true, true>::ThreadFromGlobalPoolImpl<DB::CompletedPipelineExecutor::execute()::$_0>(DB::CompletedPipelineExecutor::execute()::$_0&&)::'lambda'()>(std::__function::__policy_storage const*) @ 0x000000001d8870c6
9. ThreadPoolImpl<std::thread>::ThreadFromThreadPool::worker() @ 0x00000000173a34c0
10. void* std::__thread_proxy[abi:fqe220101]<std::tuple<std::unique_ptr<std::__thread_struct, std::default_delete<std::__thread_struct>>, void (ThreadPoolImpl<std::thread>::ThreadFromThreadPool::*)(), ThreadPoolImpl<std::thread>::ThreadFromThreadPool*>>(void*) @ 0x00000000173aaeda
11. ? @ 0x0000000000094ac3
12. ? @ 0x00000000001268d0

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 iceberg write for metadata without refs

Version info

  • Merged into: 26.8.1.182 (included in 26.8 and later)

It can happen when the table was created outside of ClickHouse
(BigLake/Lakehouse in my case).

```
Code: 1000. DB::Exception: Null pointer. (POCO_EXCEPTION) (version 26.5.1.882 (official build))
stack_trace:
0. DB::MetadataGenerator::generateNextMetadata(DB::FileNamesGenerator&, DB::Iceberg::IcebergPathFromMetadata const&, long, long, long, long, long, long, long, std::optional<long>, std::optional<long>) @ 0x000000001a063a83
1. DB::IcebergStorageSink::initializeMetadata() @ 0x000000001a03939b
2. DB::IcebergStorageSink::onFinish() @ 0x000000001a038da8
3. DB::runStep(std::function<void ()>, std::shared_ptr<DB::ThreadGroup>&) @ 0x000000001db85880
4. DB::ExceptionKeepingTransform::work() @ 0x000000001db853cb
5. DB::ExecutionThreadContext::executeTask() @ 0x000000001d89b033
6. DB::PipelineExecutor::executeStepImpl(unsigned long, DB::IAcquiredSlot*, std::atomic<bool>*) @ 0x000000001d889f04
7. DB::PipelineExecutor::execute(unsigned long, bool) @ 0x000000001d888294
8. void std::__function::__policy_func<void ()>::__call_func[abi:fqe220101]<ThreadFromGlobalPoolImpl<true, true>::ThreadFromGlobalPoolImpl<DB::CompletedPipelineExecutor::execute()::$_0>(DB::CompletedPipelineExecutor::execute()::$_0&&)::'lambda'()>(std::__function::__policy_storage const*) @ 0x000000001d8870c6
9. ThreadPoolImpl<std::thread>::ThreadFromThreadPool::worker() @ 0x00000000173a34c0
10. void* std::__thread_proxy[abi:fqe220101]<std::tuple<std::unique_ptr<std::__thread_struct, std::default_delete<std::__thread_struct>>, void (ThreadPoolImpl<std::thread>::ThreadFromThreadPool::*)(), ThreadPoolImpl<std::thread>::ThreadFromThreadPool*>>(void*) @ 0x00000000173aaeda
11. ? @ 0x0000000000094ac3
12. ? @ 0x00000000001268d0
```
@tiandiwonder tiandiwonder self-assigned this Jun 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a52c0d0]

Summary: ✅


AI Review

Summary

This PR fixes the Iceberg metadata write path when external empty-table metadata omits the optional refs object, and extends the existing integration coverage to include that case. I did not find any remaining correctness, compatibility, or test-coverage issues in the current diff.

Final Verdict

✅ No actionable findings.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 9, 2026
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Jun 9, 2026
@tiandiwonder

Copy link
Copy Markdown
Contributor

The newly added test_insert_into_empty_table_without_refs calls get_file_contents at test.py:764 but never imports it, so the Style check / ruff job fails with F821 and the test can't run — add from helpers.s3_tools import get_file_contents.

@AVMusorin

Copy link
Copy Markdown
Contributor Author

The newly added test_insert_into_empty_table_without_refs calls get_file_contents at test.py:764 but never imports it, so the Style check / ruff job fails with F821 and the test can't run — add from helpers.s3_tools import get_file_contents.

thank you! fixed

pull Bot pushed a commit to weiyilai/ClickHouse that referenced this pull request Jul 7, 2026
… 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]>
# Conflicts:
#	tests/integration/test_database_iceberg/test.py
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 79.50% 85.80% +6.30%
Functions 91.80% 92.70% +0.90%
Branches 71.60% 77.90% +6.30%

Changed lines: Changed C/C++ lines covered: 3/4 (75.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 08c36adf-b49b-4989-80d0-1e633316860a
  • MIRAI run: 945ed741-df80-496f-a5fb-735d5bc4d195
  • PR check IDs:
    • clickbench_113548_1784689155
    • clickbench_113561_1784689155
    • clickbench_113567_1784689155
    • tpch_adapted_1_official_113575_1784689155
    • tpch_adapted_1_official_113586_1784689155
    • tpch_adapted_1_official_113617_1784689155

@tiandiwonder
tiandiwonder added this pull request to the merge queue Jul 27, 2026
Merged via the queue into ClickHouse:master with commit 08b7e60 Jul 27, 2026
346 of 347 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 27, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

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

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

@clickgapai

Copy link
Copy Markdown
Contributor

Update on the earlier review: CI on the proof PR #112024 confirmed the suspected bug via Stateless tests (amd_debug, parallel). 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.)

rorylshanks pushed a commit to rorylshanks/ClickHouse that referenced this pull request Aug 28, 2026
`refs` is optional in the Iceberg spec, so a metadata file written by another
engine may not contain it. `generateManifestOnlySnapshot` seeded the missing key
with a raw `Poco::JSON::Object *`.

`Poco::JSON::Object::set` records the value's static type in a `Dynamic::Var`, and
`Object::getObject` matches strictly on `typeid(Object::Ptr)` and returns a null
`Ptr` for any other type tag rather than throwing. The `getObject(refs)->has(main)`
call three lines below therefore dereferenced a null `SharedPtr` and threw
`Poco::NullPointerException`, leaving manifest compaction permanently unavailable
on such a table.

The sibling builder `generateNextMetadata`, in the same file, already stores an
`Object::Ptr`: it was corrected in ClickHouse#106896 for the insert path, and this copy was
missed. Store an `Object::Ptr` here too, so `getObject` sees the expected type tag
and the `refs.main` branch is written as intended. The same function already seeds
`metadata_log` and `snapshot_log` as `Array::Ptr` for exactly this reason.

A sweep of `set(..., new Poco::JSON::(Object|Array))` over src, programs and base
returns this one site, so the change closes the class rather than one instance.
Nothing relied on the null return: no caller of the changed function inspects the
pre-seed value, and no setting, serialization format or on-disk state is affected.

The regression test extends the existing IcebergMetadataGenerator suite, which
already covers this family of omitted optional metadata fields. It asserts
`isObject(refs)` rather than only the absence of a throw, because `isObject` uses
the same type check as `getObject` and so also rejects re-introducing the
raw-pointer spelling.

Closes ClickHouse#112082
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
… 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]>
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