Repository navigation
Fix crash when selecting subcolumns from Buffer table - #102470
Conversation
When selecting subcolumns (like `.null` for Nullable, `.size0` for Array,
tuple elements, or map keys/values) from a Buffer table, the server
crashed with:
Logical error: Unknown virtual column: 'c0.null'
The issue was in BufferSource::generate(). It checked
`buffer.data.has(name)` using the full subcolumn name (e.g., "c0.null"),
but the in-memory buffer block only stores parent columns (e.g., "c0").
When the column wasn't found, it fell through to fillVirtualColumn()
which only handles the "_table" virtual column and threw a LOGICAL_ERROR.
The fix resolves column names through the storage metadata's
ColumnsDescription, which properly identifies subcolumns and provides the
parent column name. This allows getColumnFromBlock() to find the parent
column in the buffer data and extract the subcolumn.
Closes ClickHouse#102104
Pre-PR Validation Gate (session: cron:clickhouse-ci-task-worker:20260411-174500)a) Deterministic repro? ✅ Yes — b) Root cause explained? ✅ c) Fix matches root cause? ✅ The fix resolves column names through d) New test added? ✅ e) Both directions demonstrated? ✅ Without fix: crashes with f) Fix is general? ✅ Covers ALL subcolumn types via metadata resolution — not hardcoded to specific subcolumn names. Preserves existing behavior for regular columns and virtual columns. Other storages (StorageLog, StorageStripeLog) use different read patterns (file-based serialization) and are not affected. |
|
Workflow [PR], commit [6d8e6a5] Summary: ✅ AI ReviewSummaryThis PR fixes ClickHouse Rules
Final Verdict
|
| @@ -0,0 +1,42 @@ | |||
| -- Tags: no-parallel | |||
There was a problem hiding this comment.
-- Tags: no-parallel looks unnecessary for this stateless regression test and will reduce parallel test coverage/perf in CI. The test only creates/drops local tables and does not rely on global state, so it should be safe to run in parallel.
Please remove the no-parallel tag unless there is a specific known race/isolation issue that requires serialization.
There was a problem hiding this comment.
Removed in 0bce791 — thanks for the catch.
The test only creates/drops two local tables in the current database (t_buf_sub, t_buf_sub_dst) and does not touch any shared/global state, so it is safe to run in parallel. The no-parallel tag was an unneeded carryover.
…n-crash Resolve conflict in src/Storages/StorageBuffer.cpp: - Master refactored the virtuals API (storage_snapshot->virtual_columns was removed, virtuals now live in metadata->virtuals). - The unused 'virtual_columns' member I introduced in PR ClickHouse#102470 was dead code; drop it. - Keep the 'metadata' member — used in generate() via columns_desc = metadata->getColumns() to resolve subcolumn names. Pulls in trunk fix ClickHouse#103489 (yakov-olkhovskiy) for the unrelated Unit tests (msan, function_prop_fuzzer) failures.
|
Rebased onto current Conflict resolutionThe only merge conflict was in Master refactored the virtuals API (#102304,
My original fix added two members to
Verification
cc @novikd @azat — could you take another look? CI will re-run on the rebased HEAD. |
Address review feedback from `clickhouse-gh[bot]` on PR ClickHouse#102470: the test only creates and drops local tables in the current database and does not depend on global state, so it can run in parallel safely.
|
@Michicosun — could you please review this PR? This fixes the chronic BuzzHouse trunk bug STID 3381-2a5e — Root cause: Fix: Resolve names through CI is clean on the rebased HEAD (
Regression test ( Per @alexey-milovidov's directive on #100949 (2026-04-25): this is the separate fix he asked for. cc @alexey-milovidov @azat @novikd Session: cron:clickhouse-ci-task-worker:20260427-151500 |
| /// Resolve the column through metadata to get proper subcolumn info | ||
| /// (e.g., "c0.null" for Nullable, "arr.size0" for Array). | ||
| /// The header's NameAndTypePair lacks subcolumn_delimiter_position, | ||
| /// so getNameInStorage() would return the full name instead of the parent. | ||
| auto resolved = columns_desc.tryGetColumnOrSubcolumn(GetColumnsOptions::All, name); | ||
| if (resolved && buffer.data.has(resolved->getNameInStorage())) | ||
| columns.emplace_back(getColumnFromBlock(buffer.data, *resolved)); | ||
| else if (buffer.data.has(name)) |
There was a problem hiding this comment.
Remove this comment completely.
change logic to the following:
- Use the metadata isVirtualColumn method to check if the column is virtual
- If it is virtual, use fillVirtualColumns
- If it is not virtual, use tryGetColumnFromBlock
- If the column is not found, throw a logical error
There was a problem hiding this comment.
Done in 2cbf5702041 — refactored to the 4-step shape you specified:
if (metadata->isVirtualColumn(name))
{
columns.emplace_back(fillVirtualColumn(name, type, buffer.data.rows()));
continue;
}
auto resolved = columns_desc.tryGetColumnOrSubcolumn(GetColumnsOptions::All, name);
auto col = resolved ? tryGetColumnFromBlock(buffer.data, *resolved) : nullptr;
if (!col)
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Column or subcolumn '{}' not found in Buffer table", name);
columns.emplace_back(std::move(col));Notes:
- The intermediate
tryGetColumnOrSubcolumnlookup is needed because the header pair fromgetPort().getHeader().getNamesAndTypes()is built via the defaultNameAndTypePair(name, type)constructor, which leavessubcolumn_delimiter_positionunset.tryGetColumnFromBlockrelies onrequested_column.isSubcolumn()to extract subcolumns, so without resolving againstmetadata->getColumns()first it would treatc0.nullas a top-level column and fail. Same pattern asReadFromMemoryStorageStep::fillPhysicalColumns. fillVirtualColumn(singular) — kept the name unchanged, since this helper returns one column per call.- Explanatory comment removed as requested.
Verified locally on debug build:
04097_buffer_subcolumn_virtual_column_crash— 50/50 PASS with full randomization- Manual repro from issue Logical error: Unknown virtual column: 'A (STID: 3381-2a5e) #102104 (
SELECT c0.null FROM buffer_table) returns correct values, noLOGICAL_ERROR _tablevirtual column still works- Adjacent Buffer/virtual-column tests (00126, 00158, 00168, 02890, 03094) all pass
PR HEAD: 2cbf5702041 on fork:fix/buffer-subcolumn-crash.
…ispatch Per review feedback on ClickHouse#102470, restructure the per-column dispatch in `BufferSource::generate` into a clear 4-step shape: 1. `metadata->isVirtualColumn(name)` — check whether the column is virtual 2. If virtual, use `fillVirtualColumn` 3. Otherwise, resolve the name through `tryGetColumnOrSubcolumn` and fetch via `tryGetColumnFromBlock`, which natively handles subcolumn extraction 4. If the column is not found, throw a `LOGICAL_ERROR` This removes the awkward fall-through chain that conflated "subcolumn lookup failed" with "column might be virtual" and the dead-code path that called `fillVirtualColumn` for non-virtual misses (which was the original crash vector fixed by ClickHouse#102104). The explicit `isVirtualColumn` check makes the dispatch obvious and keeps the unknown-column case as a pure invariant guard. Behaviour: - `_table` (virtual) → `fillVirtualColumn` returns the constant column - `c0`, `c0.null`, `arr.size0`, `tup.a`, `m.keys`, ... → `tryGetColumnFromBlock` via the resolved `NameAndTypePair` extracts the parent column from the buffer's in-memory block and returns the requested subcolumn - Anything else (must not happen — `getSampleBlockForColumns` validates the column list upstream) → `LOGICAL_ERROR` The existing regression test `tests/queries/0_stateless/04097_buffer_subcolumn_virtual_column_crash.sql` covers all the subcolumn shapes (`.null`, `.size0`, tuple elements, map keys/values) plus the `_table` virtual column path. It passes 50/50 with full randomization on this commit.
|
Refactor pushed in Applied @Michicosun's 4-step refactor request:
The intermediate Verified on debug build: 50/50 PASS for Inline reply: #102470 (comment) |
LLVM Coverage Report
Changed lines: 100.00% (12/12) · Uncovered code |
|
Hi @groeneai @Michicosun — this PR fixes a I verified this by running the test queries which produce: Could you update the changelog category? Feel free to ignore if there's a specific reason for the current categorization. |
|
Hi — this PR may need backporting to Affected code: Why: If this should be backported, consider adding |
Backport #102470 to 26.4: Fix crash when selecting subcolumns from Buffer table
…ns from Buffer table
Linked issues
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed a crash (
LOGICAL_ERROR: Unknown virtual column) when selecting subcolumns (like.nullfor Nullable,.size0for Array, tuple elements, or map keys/values) from a Buffer table engine.What changed and why:
When selecting subcolumns from a
Buffertable, the server crashed with:Root cause:
BufferSource::generate()iterates over the output header columns and checksbuffer.data.has(name)to decide whether to read from the in-memory buffer or fill a virtual column. For subcolumn accesses likec0.null, the name is the full qualified subcolumn name (e.g.,"c0.null"), but the buffer's data block only stores parent columns (e.g.,"c0"). Sohas()returned false, and the code fell through tofillVirtualColumn(), which only handles the_tablevirtual column and threw aLOGICAL_ERROR.Fix: Resolve column names through the storage metadata (
ColumnsDescription::tryGetColumnOrSubcolumn), which properly identifies subcolumns and returns aNameAndTypePairwith the correct parent column name and subcolumn extraction info. This allows the existinggetColumnFromBlock()function to find the parent column in the buffer data and extract the subcolumn correctly.This is the same approach used by
ReadFromMemoryStorageStep, which correctly handles subcolumns from in-memory data blocks.Reproduction (from #102104):
Closes #102104
Version info
26.5.1.146(included in26.5and later)26.4.5.98