Skip to content

Fix subcolumns of a Merge table column that a child table does not have - #123633

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:merge-subcolumn-of-missing-column
Oct 4, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:merge-subcolumn-of-missing-column

Conversation

@groeneai

@groeneai groeneai commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #122523

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 wrong results when a Merge table (or the merge table function) reads a subcolumn of a column that one of its child tables does not have: for that child, x.null of a missing Nullable column x was read as 0, so x IS NULL returned no rows from it. Element subcolumns of Tuple, Variant and JSON columns and the sizes of a missing flattened Nested member were affected the same way. Closes #122523.

Description

On 26.8.10.6 (and 26.3.33.24), given CREATE TABLE child (k UInt8) ENGINE = MergeTree ORDER BY k with one row and
CREATE TABLE m (k UInt8, c0 Nullable(UInt8)) ENGINE = Merge(currentDatabase(), '^child$'),
SELECT count() FROM m WHERE c0 IS NULL returns 0 instead of 1 (1 with optimize_functions_to_subcolumns = 0), and
SELECT c0, c0.null FROM m returns \N 0.

Root cause: a subcolumn of a column the child lacks is dropped from the child's read, and the missing names are then
filled independently, each with its own type's default: c0 with NULL, c0.null with 0. A Distributed child
evaluates the rewritten query itself and got the same wrong constant.

Change (StorageMerge.cpp): fill the missing column itself and extract the requested subcolumns from it with
createSubcolumnsExtractionActions; for a child that runs the query, substitute the subcolumn of the column's
default. This is what a MergeTree part that lacks the column already returns.

Validation: the new test fails on master and passes with the fix (MergeTree, ALIAS-column and Distributed
children, merge(), Nullable, Tuple, Variant, JSON and flattened Nested); reverting either half reddens
only its own cases; 50 randomized runs pass.

Not changed here: when a child that runs the query above FetchColumns returns fewer columns than the Merge table
expects (equal constant aggregates merged into one, constant columns not sent), the lost columns are still filled
with type defaults, e.g. min(a), min(b) over a MergeTree child and a Distributed child lacking both. This is
pre-existing and not specific to subcolumns.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1471-master (included in 26.10 and later)

groeneai and others added 2 commits October 2, 2026 21:38
…lumn's default

A Merge table column that a child table lacks is filled with its default,
but a subcolumn of it was filled independently with the default of the
subcolumn's own type. So `x.null` of a missing `Nullable` column read 0
while `x` read NULL, and `x IS NULL` (rewritten to `x.null` by
optimize_functions_to_subcolumns) returned no rows from that child. The same
held for `Tuple`/`Variant`/`JSON` element subcolumns and for the sizes of a
missing flattened `Nested` member.

Stream path (convertAndFilterSourceStream): fill the missing column itself,
then extract the requested subcolumns from the filled column with
createSubcolumnsExtractionActions, so a `Nested` member filled with its read
sibling's sizes also gets matching subcolumns. Query-tree path
(getModifiedQueryInfo, used when a child such as Distributed runs the query
itself): substitute the subcolumn of the column's default. This is what a
MergeTree part that lacks the column already returns.

Not changed: above FetchColumns a child's result header can be narrower than
the Merge header (equal constant aggregates merged, constant columns not
sent); the lost columns are still filled with type defaults. That is
pre-existing and also affects ordinary columns.

Closes ClickHouse#122523

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the subcolumn-extraction comment in convertAndFilterSourceStream to one line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-self PR origin: self-found defect (impact-bar validated) labels Oct 2, 2026
@groeneai

groeneai commented Oct 2, 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; 2 findings).

# Sev Finding Verdict Evidence / action
1 ❌ A child column literally named `x.null` is read instead of deriving x.null from the missing x (StorageMerge.cpp:1698, :2514) DISAGREE A column named like a subcolumn shadows it by design (ColumnsDescription.cpp:697-701, StorageMerge.cpp:584-587); the new code only replaces names the child does not produce, so the result is the same as on master.
2 ❌ A missing flattened Nested member still reads size 0 when its sibling is not selected, and a Distributed child substitutes [] DISAGREE The member fill is unchanged: addMissingDefaults copies offsets only from a sibling in the block, so SELECT n.b alone is [] on master too; the subcolumns now follow the member either way (both cases in the test).

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

Session id: cron:clickhouse-review-self-slot-58:20261002-214503

@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e97f90c]

Summary: ✅


AI Review

Summary

This PR fixes the main Merge-table bug by filling missing parent columns before extracting subcolumns on the FetchColumns path, and the new stateless coverage exercises the plain-type-default cases well. I do not think the fix is complete, though: the query-tree rewrite used for Distributed / above-FetchColumns children still models a missing parent as a plain type default, so it can return wrong results for missing subcolumns whenever the parent would actually be synthesized via addMissingDefaults.

Findings

❌ Blockers

  • [src/Storages/StorageMerge.cpp:1738] The above-FetchColumns rewrite still substitutes a missing subcolumn with getSubcolumn(default(type_in_storage)), which is not the same contract as the addMissingDefaults fill used on the FetchColumns path. That stays wrong for missing parents with DEFAULT / MATERIALIZED expressions and for flattened Nested members that should inherit sibling offsets. A Distributed child can therefore still return wrong results; e.g. x Nullable(UInt8) DEFAULT 5 makes x.null become 1, and SELECT n.a, length(n.b) keeps n.b.size0 = 0 even though the stream path would synthesize n.b from n.a when share_nested_offsets = 1.
Tests
  • ⚠️ Add a focused Distributed-child regression for both remaining cases above: one where the missing parent has a non-type-default DEFAULT / MATERIALIZED expression, and one where length / size0 of a missing flattened Nested member is read together with a sibling member. The new test only covers plain type defaults on the Distributed path.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions: make the above-FetchColumns path mirror the parent-fill semantics of addMissingDefaults, then cover the DEFAULT / MATERIALIZED and flattened-Nested Distributed cases with a focused regression test.

LLVM Coverage Report

Measured on commit e97f90c.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.50% 79.50% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 2, 2026
{
const auto & type_in_storage = merge_column->getTypeInStorage();
auto subcolumn = type_in_storage->getSubcolumn(
merge_column->getSubcolumnName(), type_in_storage->createColumnConstWithDefaultValue(1));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This still diverges from the parent-column fill we do in the FetchColumns path below.

Here the Distributed / above-FetchColumns rewrite turns every missing subcolumn into getSubcolumn(default(type_in_storage)). That is only equivalent when the parent really falls back to a plain type default. It is not equivalent when the Merge schema gives the missing parent a DEFAULT / MATERIALIZED expression, or when a flattened Nested member should borrow sibling offsets.

Concrete cases that stay wrong here:

  • x Nullable(UInt8) DEFAULT 5: x.null becomes 1 in this branch, while the stream path fills x with 5 and then derives x.null = 0.
  • SELECT n.a, length(n.b) over a Distributed child that has n.a but lacks n.b: this branch still substitutes n.b.size0 with 0, instead of matching the [NULL, NULL] fill that addMissingDefaults produces from n.a's offsets when share_nested_offsets = 1.

Could we make the above-FetchColumns branch derive the full missing parent column with the same logic as addMissingDefaults, and only then extract the requested subcolumn?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🕵 This branch keeps a subcolumn consistent with how its parent column is filled in the same path, which is the goal of this PR. In the above-FetchColumns path, a missing parent column is already (before this PR) replaced with ConstantNode(merge_column->type->getDefault()). So for x Nullable(UInt8) DEFAULT 5, a Distributed child returns x = NULL, and now x.null = 1 matches it. If only the subcolumn followed the DEFAULT expression, we would get x = NULL, x.null = 0, which is the same inconsistency this PR fixes. The same applies to n.b: in this path the parent n.b is filled with [], so n.b.size0 = 0 agrees with it.

The broader difference, where the above-FetchColumns path uses type defaults while addMissingDefaults evaluates DEFAULT/MATERIALIZED expressions and shares Nested offsets, already exists for the parent columns themselves and isn't specific to subcolumns. Fixing it means translating column-default expressions and sibling offsets into the query tree sent to the child. That is a separate change to how missing columns are handled, so it belongs in its own follow-up PR rather than this subcolumn fix (the PR description already notes this path's remaining type-default limitation).

@clickhouse-gh clickhouse-gh Bot added the comp-storage-merge Merge table engine (logical union over tables) and related mechanics. label Oct 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e97f90c52 with master b1f44e396 (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, 22 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
Merged via the queue into ClickHouse:master with commit 4fccb89 Oct 4, 2026
149 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 4, 2026
CurtizJ pushed a commit to CurtizJ/ClickHouse that referenced this pull request Oct 5, 2026
…child lacks the column

ClickHouse#123633 made `Merge` derive a subcolumn of a column a child table does not have from the
column's default value. That holds for subcolumns of the declared type, but the `quantized`
and `product_quantization_codebook` subcolumns of a `Quantized` codec column come from the
codec's custom serialization and cannot be computed from an in-memory value: for `product`
there is no column at all, so `IDataType::getSubcolumn` returned null and
`getModifiedQueryInfo` built a `ColumnConst` over it (SIGSEGV); for the other methods the
codes of the default `[]` were rejected with SIZES_OF_ARRAYS_DONT_MATCH.

Both places that fill a missing column (the query tree substitution and the stream path)
now derive the subcolumn from the default only when the declared type has it, and give
other subcolumns the default of their own type, which is what releases before ClickHouse#123633 did.

`IDataType::getSubcolumn` now throws NOT_IMPLEMENTED instead of returning a null column,
which every caller dereferenced. This also turns `getSubcolumn(materialize(vec), 'quantized')`
(LOGICAL_ERROR "Empty column was returned") and reading `vec.quantized` from a Memory table
with a `product` codec (null dereference) into an error. `tryGetSubcolumn` is unchanged: its
callers use null as "not available".

Closes ClickHouse#123866

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nickitat pushed a commit to egbaydarov/ClickHouse that referenced this pull request Nov 20, 2026
… twice

Above `FetchColumns` the `Merge` header repeats a column the projection
selects twice (`SELECT k, k ...` -> `[__table1.k, __table1.k]`).
`ReadFromMerge::convertAndFilterSourceStream` builds `columns_to_fill` with
one entry per name and `addMissingDefaults` projects onto those names, so
the child stream lost the repeated column. Only the subcolumn branch
re-projected onto `header.getNames()`. Since ClickHouse#123633 (26.10), a `Merge` or
`merge()` read over a `Distributed` child therefore returns one column `k`
for `SELECT k, k`, and with `SHUFFLE` the `ShuffleTransform` built for the
plan header indexes past the narrower stream and aborts (libc++ hardening,
`vector[] index out of bounds`, STID 2882-464f).

Run the re-projection for every child, so the stream has the header's
columns with their multiplicity.

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-storage-merge Merge table engine (logical union over tables) and related mechanics. groeneai-origin-self PR origin: self-found defect (impact-bar validated) 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.

x.null of a Nullable column missing from a Merge child reads 0 instead of 1

3 participants