Skip to content

Fix exception when reading a dynamic subcolumn through a Buffer table - #111948

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix/storage-buffer-dynamic-subcolumn-crash
Jul 28, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix/storage-buffer-dynamic-subcolumn-crash

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Reading a dynamic subcolumn - a path inside a JSON or a Dynamic column - through a Buffer table led to an exception, because StorageBuffer::read dereferenced the result of NamesAndTypesList::tryGetByName without checking it: such subcolumns cannot be enumerated, so they are absent from the enumerated lists of columns of the Buffer table and of its destination table.

CREATE TABLE dst (c0 JSON(a UInt32)) ENGINE = MergeTree ORDER BY tuple();
CREATE TABLE buf (c0 JSON) ENGINE = Buffer(currentDatabase(), dst, 1, 1000, 1000, 1000, 1000, 1000000, 1000000);
SELECT c0.a FROM buf;
contrib/llvm-project/libcxx/include/optional:1119: libc++ Hardening assertion this->has_value() failed: optional operator-> called on a disengaged value

For the same reason, a dynamic subcolumn was considered absent in the destination table, and the data of the destination table was silently skipped, so the query returned only the rows still kept in the buffers.

Such columns are now resolved by name in the corresponding storage snapshot, which handles dynamic subcolumns, and the requested dynamic subcolumns are added to the header used for the conversion between the structures of the two tables.

Found by BuzzHouse in the CI of an unrelated pull request: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100391&sha=e252b917d11278bbbaf787c0d2214f0bbfec2767&name_0=PR&name_1=BuzzHouse%20%28amd_tsan%29

Related: #100391

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 to CHANGELOG.md):

Fix an exception and silently skipped data when reading a dynamic subcolumn (a path inside a JSON or a Dynamic column) through a Buffer table.

`StorageBuffer::read` compared the structure of the destination table with its
own structure by looking the requested columns up in the enumerated lists of
columns of both tables, and dereferenced the result of
`NamesAndTypesList::tryGetByName` without checking it.

Dynamic subcolumns - paths inside a `JSON` or a `Dynamic` column - cannot be
enumerated, so they are absent from these lists, and reading such a subcolumn
through a `Buffer` table aborted the server on the disengaged `std::optional`:
`libc++ Hardening assertion this->has_value() failed: optional operator-> called
on a disengaged value`.

For the same reason, when a dynamic subcolumn was missing in the enumerated
columns of the destination table, it was considered absent there, and the data
of the destination table was silently skipped, so the query returned only the
rows still kept in the buffers.

Resolve such columns by name in the corresponding storage snapshot, which
handles dynamic subcolumns, and add the requested dynamic subcolumns to the
header used for the conversion between the structures.

Found by BuzzHouse:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100391&sha=e252b917d11278bbbaf787c0d2214f0bbfec2767&name_0=PR&name_1=BuzzHouse%20%28amd_tsan%29
Related: #100391
@clickhouse-gh

clickhouse-gh Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [be8b42d]

Summary: ✅


AI Review

Summary

This PR fixes StorageBuffer::read so requested dynamic subcolumns are resolved through the relevant StorageSnapshot instead of only through the enumerated column lists, and so the conversion header includes those subcolumns when the Buffer table and destination table have different structures. I did not find any new correctness or contract issues in the current diff: the fix matches the existing BufferSource / StorageSnapshot subcolumn resolution path, the new stateless test covers both the exception and the previously skipped-destination-data case, and the current CI for PR #111948 is all green.

Final Verdict

✅ No blocking or major findings.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 26, 2026
… the test

The first half of `04647_buffer_dynamic_subcolumn` deliberately gives the
`Buffer` table a structure that differs from its destination table, and
`StorageBuffer::read` reports every such difference with a warning:
`Destination table ... has different type of column ...`.

`clickhouse-test` runs the client with `--send_logs_level=warning`, so these
warnings ended up on the client's stderr and the test failed in `Fast test`
with `Reason: having stderror`.

Suppress them with `SET send_logs_level = 'fatal'`, the same way
`00158_buffer_and_nonexistent_table` suppresses the analogous warning.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111948&sha=36424d5aaee96649efe19e434922d6ff5c764a72&name_0=PR&name_1=Fast%20test
@alexey-milovidov

alexey-milovidov commented Jul 27, 2026 •

Copy link
Copy Markdown
Member Author

Both CI failures on e99a507a are unrelated to this PR (it only touches StorageBuffer::read and adds one stateless test) and are already tracked:

@groeneai, please investigate #110518 (report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111948&sha=e99a507ad694189837906329d3f49d1e698b772c&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29) and provide a fix in a separate PR; if a fix is already in progress, link it there.

Merged master to refresh the merge base.

@groeneai

Copy link
Copy Markdown
Collaborator

There is a fix in progress for #110518: #111689 (Closes #110518), open since 2026-07-23 and awaiting review. So no separate PR is needed, and the "no fix yet" note on this failure is out of date.

The logs of this job confirm it is the same defect. In clickhouse-server.log the aborting query is SELECT count() FROM mv WHERE id = 999999999 (query id 2d95e04d-f6ed-4881-b8fd-90b2c61a3340), and the relevant sequence is:

optimizeUseAggregateProjections: Exact count optimization is applied: 1 ranges with exact row count 1. Filtered parts: 1
ParallelReplicasReadingCoordinator: Ignoring announcement from non-snapshot replica 1 for unknown stream test_7l54xp9u.`.inner_id.a8004b14-...`
Logical error: 'Got read request from replica 1 for unknown stream test_7l54xp9u.`.inner_id.a8004b14-...`.'

The coordinator logs exactly three lines for that query: coordinator created, replica 0 set as the snapshot replica, and the ignored announcement from replica 1. The initiator never announced, because the exact-count short-circuit replaced its ReadFromMergeTree with a ReadFromPreparedSource, so initializePipeline never ran and neither did the empty-ranges announcement it guards with result.parts_with_ranges.empty() && isParallelReplicasLocalPlanForInitiator(), which exists precisely to stop a follower from initializing the coordinator with a divergent plan. Follower 1 did not take the short-circuit, announced, was dropped by the non-snapshot-replica guard in handleInitialAllRangesAnnouncement, and its subsequent read request hit the throw in handleRequest. That asymmetry (announcements for an unknown stream are dropped, read requests abort) is what turns the missing announcement into a LOGICAL_ERROR.

#111689 extracts the announcement into announceEmptyReadRangesToCoordinatorIfInitiator() and calls it from the four paths that fully replace the read: exact-count, minmax-count, and a stored aggregate or normal projection whose read selects zero ranges. It does not touch or weaken the throw.

One detail worth recording on the issue: no declared projection is required. The inner table of mv has none, and the carrier here is the implicit exact-count projection, which is why this reproduces on plain count() queries with a selective WHERE.

Rate over the last 7 days for the message signature: 92 hits across 82 distinct pull requests, plus 4 on master, so it is unrelated to this branch. Your attribution of the toStartOfInterval UBSan failure matches what I have as well.

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

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

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Thanks — noted, my "no fix yet" was out of date: #111689 (Closes #110518) is open and awaiting review, so no separate PR is needed here.

Current state of this PR: CI on be8b42d2 is green (the only reds are Performance Comparison (arm_release, master_head) number_formatting_formats entries, flagged unstable, and those are master_head baseline runs, not this branch). The AI Review verdict is ✅ with no blocking or major findings, and there are no unresolved review threads. Ready for review/merge.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is great!

@alexey-milovidov alexey-milovidov self-assigned this Jul 28, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 28, 2026
Merged via the queue into master with commit 049c83a Jul 28, 2026
178 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/storage-buffer-dynamic-subcolumn-crash branch July 28, 2026 23:52
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants