Skip to content

Fix count() and other queries reading no column from a Buffer table over a Distributed table - #129143

Merged
azat merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-buffer-over-distributed-column-kinds
Nov 13, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-buffer-over-distributed-column-kinds

Conversation

@groeneai

@groeneai groeneai commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

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):

Fixed NOT_FOUND_COLUMN_IN_BLOCK for a query that reads no column, such as SELECT count(), from a Buffer table whose destination is a Distributed table, when the Buffer table has an ALIAS or EPHEMERAL column or only columns larger than its _table virtual column.

Description

On 26.10.2.103 (also 26.9 and 26.3), SELECT count() FROM buf over a Buffer table with a Distributed destination and an ALIAS or EPHEMERAL column is rejected with Not found column a in block. There are only columns: s. (NOT_FOUND_COLUMN_IN_BLOCK). On master (since #126476) the same path is a LOGICAL_ERROR (Column a is not found in the data of table ...), found by the AST fuzzer, STID 2127-25d8 (AST fuzzer (amd_debug, targeted, old_compatibility) report 1, AST fuzzer (amd_debug, targeted) report 2). No related open issue found.

Root cause: when the destination is processed past FetchColumns, StorageBuffer::read re-analyzes the query against a StorageValues holding the buffered rows. That storage was built from getAllColumnsDescription(), which turns ALIAS/EPHEMERAL columns and _table into ordinary columns. For a query that reads no column, the inner analysis then picks a different "smallest column" than the outer one, one the buffers never provide.

Change: build that StorageValues from the table's own ColumnsDescription (column kinds preserved, virtual columns passed separately), as InsertDependenciesBuilder and INSERT ... RETURNING already do.

Validation: the new test fails on master (debug server aborts) and passes with the fix, 50/50 with randomized settings; the 159 Buffer tests have identical results with and without the fix.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/129143]

Version info

  • Merged into: 26.11.1.2233-master (included in 26.11 and later)

When the destination of a `Buffer` table is processed past `FetchColumns`
(a `Distributed` destination), `StorageBuffer::read` re-analyzes the query
against a `StorageValues` that holds the rows still in the buffers. That
storage was built from `StorageSnapshot::getAllColumnsDescription`, which
lists ALIAS and EPHEMERAL columns and the `_table` virtual column as
ordinary physical columns. The inner analysis then saw a different table
than the outer one: for a query that reads no column (`count()`) it picked
the smallest of a different set of physical columns, an ALIAS or EPHEMERAL
column or `_table`, which the buffers pipe does not contain. Debug builds
abort with `Column <name> is not found in the data of table`; releases
reject the query with `NOT_FOUND_COLUMN_IN_BLOCK` (26.3, 26.9, 26.10).

Build the `StorageValues` from the table's own `ColumnsDescription` with
the virtual columns passed separately, so both analyses read the same
columns. The same is done for the materialized view source in
`InsertDependenciesBuilder` and for `INSERT ... RETURNING`.

Found by the AST fuzzer (STID 2127-25d8).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (3 nits).

# Sev Finding Verdict Evidence / action
1 💡 optimize_trivial_count_query = 0 in the test is redundant: Buffer does not support the trivial count optimization AGREE, noted harmless, keeps the count() arms explicit
2 💡 A non-constant ALIAS column read through a Buffer over Distributed still fails with THERE_IS_NO_COLUMN (StorageBuffer.cpp:722) DISAGREE (out of scope: separate PR) fails with and without this change, also with empty buffers: a header-naming mismatch between the two plans, not the column-set mismatch fixed here

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-11:20261104-010200

@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT count() FROM buf over Buffer -> Distributed(test_shard_localhost, ...) with an ALIAS column, an EPHEMERAL column, or only a FixedString(200) column: a master debug server throws LOGICAL_ERROR Column a / e / _table is not found in the data of table and aborts on every run. The fuzzer's own auto-generated repro (01584 schema, EXPLAIN ESTIMATE ...) reproduces the same way.
b Root cause explained? StorageBuffer::read re-analyzes the query against a StorageValues built from getAllColumnsDescription(), which lists ALIAS/EPHEMERAL columns and _table as ordinary columns. For a query reading no column, the inner analysis picks a different smallest column than the outer read, one the buffers pipe does not contain.
c Fix matches root cause? Yes. The StorageValues is built from the table's own ColumnsDescription (kinds preserved, virtuals separate), so both analyses see the same table. No guard or default-filling in StorageValues::read.
d Test intent preserved / new tests added? New 05497_buffer_over_distributed_alias_ephemeral_columns: three count() arms that fail on master, plus guards for a referenced constant ALIAS and for _table.
e Both directions demonstrated? Yes, same HEAD, two builds: master debug server aborts on each arm (runner: server died), fixed build passes; 50/50 with randomized settings.
f Fix is general across code paths? This is the only StorageValues re-analysis built from the flattened description; the materialized view source (InsertDependenciesBuilder) and INSERT ... RETURNING already use getColumns(). Chained Buffer tables go through the same line per level.
g Fix generalizes across inputs (params/datatypes/wrappers)? ALIAS, EPHEMERAL, _table, wide FixedString in the test; a matrix over Array .size0, Nullable .null, Tuple element, JSON subcolumn, LowCardinality(Nullable), MATERIALIZED, DEFAULT, *, filtered count(), GROUP BY _table returns identical output before and after.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; the 159 Buffer tests have the same results with and without the fix.
i Invariants and contracts preserved? The inner analysis now requests only columns the buffers pipe provides (or their subcolumns); the StorageValues metadata stays valid (a Buffer always has a physical column); no locking or lifetime change.

Session id: cron:clickhouse-impl-slot-40:20261104-000600

@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

cc @azat @KochetovNicolai, could you review this and add the can be tested label so CI runs? StorageBuffer::read re-analyzes the query against a StorageValues built from getAllColumnsDescription(), which made ALIAS/EPHEMERAL columns and _table physical, so a query reading no column (e.g. count()) picked a column the buffers never provide; the StorageValues now gets the table's own ColumnsDescription.

@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7180e71]

Summary: ✅


AI Review

Summary

PR #129143 preserves the original column kinds when StorageBuffer::read builds the StorageValues used to plan buffered rows. This fixes column-free queries choosing an unavailable ALIAS, EPHEMERAL, or _table column. No actionable findings in head 7180e710c46d1d4306d6314d70e93ffec8a33be1; the focused regression test and reported before-and-after validation support the fix.

Missing context / blind spots
  • ⚠️ Full stateless CI and functional bugfix validation are still pending in the head CI report. No local server binary is available, so the reported runtime validation was not independently repeated. Completed CI results for 05497_buffer_over_distributed_alias_ephemeral_columns would close this gap.
Final Verdict

Status: ✅ Approve. No code changes requested; normal CI completion remains pending.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-simple-engines Lightweight single-node table engines: Log/StripeLog (append-only logs), Buffer (async batching),... can be tested Allows running workflows for external contributors labels Nov 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7180e710c with master 64576f956 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 15 s compile time in total, 7 of them have a recent master baseline.

Job report

@azat
azat added this pull request to the merge queue Nov 13, 2026
Merged via the queue into ClickHouse:master with commit 2305001 Nov 13, 2026
546 of 550 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 13, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 14, 2026
…s Distributed

A `Distributed` destination plans the query it ships: after inlining `ALIAS`
columns, `buildQueryTreeForShard` replaces every constant whose name is longer
than `optimize_const_name_size` (256 by default) with `__getScalar('<hash>')`.
So `SELECT countIf(s = repeat('x', 1000)), count() FROM buf` named the column
`countIf(equals(__table1.s, __getScalar('...')))` on the destination side and
`countIf(equals(__table1.s, _CAST(<value>, 'String')))` on the buffers side, and
the by-name union conversion in `StorageBuffer::read` threw THERE_IS_NO_COLUMN.

The buffers replan, which already mirrors `inlineAliasColumns`, now also mirrors
the scalar rewrite: it is exposed from `buildQueryTreeForShard.cpp` as
`replaceLongConstantsWithScalars` and used by both callers, so the guard and the
threshold stay in one place. The replanned shipped query is analyzed with
`ignoreASTOptimizations`, as `createLocalPlan` analyzes the local shard of the
same query: an initiator names a folded `__getScalar` constant by its value,
a shard by its source expression, and the destination header uses the latter.
The gate, the adopt-only-if-covered fallback and the access exemption are unchanged.

Also merged master to resolve the conflict with ClickHouse#129143 and ClickHouse#125990 in
`StorageBuffer::read` (master's column kinds and `view_source_in_subqueries`
are kept inside the `plan_buffers` lambda).

Co-Authored-By: Claude Opus 5.5 <[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 comp-simple-engines Lightweight single-node table engines: Log/StripeLog (append-only logs), Buffer (async batching),... 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