Skip to content

Fix a Nested member added to a Memory table by ALTER ADD COLUMN reading as empty arrays - #122991

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:groeneai-memory-nested-member-added-by-alter
Oct 11, 2026
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:groeneai-memory-nested-member-added-by-alter

Conversation

@groeneai

@groeneai groeneai commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #122497

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 a member of a Nested added by ALTER TABLE ... ADD COLUMN to a Memory table being read as empty arrays for the rows inserted before the ALTER, unless another member of the same Nested was read at the same time. It now gets the array sizes of the other members, like in MergeTree. This fixes wrong results of SELECT, WHERE, PREWHERE, ARRAY JOIN and row policies over such a column, and SIZES_OF_ARRAYS_DONT_MATCH errors.

Description

On 26.3 and 26.9, after ALTER TABLE t ADD COLUMN n.b Array(UInt64) on a Memory table with n Nested(a UInt64), SELECT n.b FROM t returns [] for a row inserted with n.a = [10, 20] before the ALTER; MergeTree returns [0, 0]. The same row reads [0, 0] when n.a is selected too, n.b.size0 reads 0 either way, and a row policy USING empty(n.b) shows rows that MergeTree hides. On master, with PREWHERE on n.a, arrayMap over n.a and n.b throws SIZES_OF_ARRAYS_DONT_MATCH (#122497).

Root cause. MemorySource fills a column the stored block lacks with fillMissingColumns over the columns of the current read only, so a missing array gets sizes only from a member of its Nested read in the same call. The stored block has them: its arrays of one Nested have equal sizes, checked on insert and by mutations.

Change. One helper for the three fill sites of ReadFromMemoryStorageStep.cpp fills only the columns the block lacks, from the block alone: a missing member of a Nested gets the sizes of the first member of that Nested that the block stores and the table still has, so never a dropped column (Memory keeps those in its blocks). An array without a dot in its name is not a member. A missing subcolumn is taken from the filled array, which also removes an ILLEGAL_COLUMN for size1 of a missing Array(Array). A member dropped and added again under the same name still reads its old data until #122954. The open #122604 also changes this function.

Validation. The new test fails on master and passes with the fix, 50/50 with randomized settings; mutation arms cover each fill site and the choice of the sizes source; 417 related Memory/Nested tests show no fix-only failure.

🤖 Generated with Claude Code


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

Version info

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

groeneai and others added 5 commits September 29, 2026 18:44
…ts siblings

For the blocks inserted into a `Memory` table before `ALTER TABLE ... ADD
COLUMN` of a `Nested` member, the member was read as empty arrays, and its
`size0` as 0, unless another member of the `Nested` was filled in the same
`fillMissingColumns` call. So one row changed value with the query shape
(`SELECT n.b` vs `SELECT n.a, n.b`), `PREWHERE` over one member and a read of
another gave `SIZES_OF_ARRAYS_DONT_MATCH` (ClickHouse#122497), and a row policy on the
member showed rows that `MergeTree` hides.

The stored block holds the sizes: its arrays of one `Nested` have equal sizes,
which inserts and mutations check. `MemorySource` now fills a missing array
from the offsets of the first array of the same `Nested` that the block
stores, taken from the arrays the table currently has (so never from a column
dropped since, which `Memory` still keeps in its blocks). A missing subcolumn
of such an array is taken from the filled array, which also avoids the
`ILLEGAL_COLUMN` exception for `sizeK` (K >= 1) of a missing multidimensional
array. The same helper serves the unfiltered read, the in-source filter
inputs and the columns read after the filter, and a block that has all
requested columns takes the old path unchanged.

Closes ClickHouse#122497

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… `Nested`

An array whose name has no dot is not a member of a `Nested`: inserts do not
check its sizes against the members (`Nested::validateArraySizes` skips it),
and `MergeTree` rejects such a table as a stream name collision. It shares the
`n.size0` offsets key with the members only through the stream file name, so it
could still lend its sizes to a missing member, both as a sizes source and, as
on master, as another column of the same read: `SELECT n.b` and
`SELECT n, n.b` returned different arrays for the same row.

`findSizesSource` and `getArraysOfRequestedNested` now count only dotted names,
and the missing entries are filled together with their sources only, so no
other column of the read lends its offsets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…embers

An array whose name has no dot (`n`) shares the `n.size0` offsets key with
the members of `Nested` `n`, so filling it in the same `fillMissingColumns`
call as the source chosen for a missing member gave it that member's sizes.
After `n.a` is dropped, `n` is added and `n.a` is added again (its old data
is still in the `Memory` blocks), `SELECT n` returned `[]` but
`SELECT n, n.b` returned `[0,0]` for the same row.

The missing entries without a sizes source are now filled in a separate call
that is given no stored column, so no stored array lends them its offsets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sizes of a missing member were taken by passing its source to
`fillMissingColumns`, which matches every array level by stream name. A tuple
element is escaped in stream names, so for a source
`n.a Array(Tuple(x Array(UInt8)))` the inner sizes stream of `x` has the name
of level 1 of a member `n.a.x Array(Array(UInt8))` added later, and that member
read the inner sizes of the tuple element (`[[0,0],[0]]`) instead of empty
inner arrays (`[[],[]]`).

A missing member that has a sizes source is now built directly as an array of
default elements of its type with the offsets of the source, and a missing
subcolumn of it is taken from that array. Only the entries without a sizes
source still go through `fillMissingColumns`, given no stored column.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the comments that narrate the new helpers; keep the Nested membership and equal-sizes invariants, and the outer-sizes note.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-issue PR origin: picked up an external GitHub issue labels Sep 30, 2026
@groeneai

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

Pre-publication review by an independent model (engine: codex; 4 rounds: one finding in each of the first three, none in the fourth) plus my own cold review in every round.

# Sev Finding Verdict Evidence / action
1 ❌ An array whose name has no dot (n) gave its sizes to a missing n.a/n.b, although it is not a member of Nested n AGREE, fixed @ 9f4212a Insert validation and Nested::collect count only dotted names as members, and MergeTree rejects this layout. Only dotted members give sizes now.
2 ❌ The reverse: a missing undotted n took the sizes of the member picked for a missing n.b in the same fill (SELECT n vs SELECT n, n.b) AGREE, fixed @ 5f0ea46 Reachable after dropping and re-adding n.a (Memory keeps dropped columns in its blocks, #122954). Entries without a sizes source are filled apart.
3 ❌ A missing member named like a tuple element of its sibling (n.a.x Array(Array(UInt8)) next to n.a Array(Tuple(x Array(UInt8)))) got that element's inner sizes, [[0,0],[0]] instead of [[],[]] AGREE, fixed @ 0c77a3f Offsets were matched by stream name. A missing member is now built from the outer sizes of its source only; test lines 52-53.
4 💡 A sizes source that is also read in the same call is decompressed twice (compress = 1, blocks written before the ALTER) AGREE, not changed Limited to old blocks.
5 💡 The check that a decompressed source is a ColumnArray cannot fail (stored array columns always are one) AGREE, not changed Could be an assert_cast.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-10:20260929-235400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: Memory table (k, n Nested(a)), two rows, ADD COLUMN n.b Array(UInt64), SELECT k, n.b, n.b.size0 prints [] 0 on every run of master (and of 26.3.33.24, 26.9.1.1629); MergeTree prints [0,0] 2.
b Root cause explained? The Memory source fills a column missing from a stored block with fillMissingColumns over the columns of the current read only, so the shared Nested offsets are found only when a read column has a matching stream name (a member, an undotted n, or at a deeper level a tuple element of a sibling), and a subcolumn of the missing member is filled as a scalar default.
c Fix matches root cause? Yes: a missing member takes the outer sizes of a live member of the same Nested stored in the block (members of one Nested have equal sizes, checked on insert) and is built from them with default elements, so its deeper levels are its own; subcolumns are extracted from that array; a missing column that is not such a member gets its type's default, apart from any stored array. No guard, no setting.
d Test intent preserved / new tests added? New test 05293_memory_nested_member_added_by_alter (unfiltered, subcolumns, WHERE/PREWHERE incl. #122497, ARRAY JOIN, row policy, compress = 1, dropped members, an undotted array that is not a member, a member named like a tuple element of its sibling); existing tests unchanged.
e Both directions demonstrated? Yes: the new test fails on master (runner and clickhouse local) and passes with the fix, same commit, Build-IDs checked; 50/50 randomized runs green; each mechanism is covered by a mutation that turns its lines red.
f Fix is general across code paths? One helper at all three fill sites (unfiltered read, PREWHERE/row-policy inputs, columns read after the filter), each proven necessary by a mutation; the delayed GLOBAL IN read and mutations use the same source.
g Fix generalizes across inputs (params/datatypes/wrappers)? Members of type Array(UInt64), Array(Array(UInt64)), Array(Nullable(String)), Array(Tuple(...)), Array(JSON), and an Array(Array(UInt8)) named like a tuple element of its sibling; subcolumns size0, size1, null, tuple element, JSON path; empty and multi-element rows; a second Nested with other sizes; a Nested with no stored member; an undotted array with the same prefix, alone and co-read.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or persisted state changes; only results that were wrong, or that depended on the other columns of the query, change.
i Invariants and contracts preserved? A missing member is a ColumnArray of default elements, as many as the source's last offset, over the source's offsets (the ColumnArray invariant); a source is decompressed once and used only if it is a ColumnArray; the one remaining fillMissingColumns call gets the entries without a source, parallel lists, no stored column; every missing entry is non-null afterwards, read entries are not touched, and the filter mask is still applied after the fill.

Session id: cron:clickhouse-impl-slot-6:20260929-231800

@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [41dffe8]

Summary: ✅


AI Review

Summary

This PR teaches ReadFromMemoryStorageStep to synthesize a missing Nested member in Memory blocks from a live sibling's outer offsets instead of letting the result depend on which other columns the query reads. The fix is wired through the plain read path, the in-source filter inputs, and the deferred columns path, and the new stateless test covers the previously reported edge cases around PREWHERE, row policies, compressed blocks, dropped members, non-members with the same prefix, and tuple-element shadowing. I did not find a new correctness, compatibility, or performance issue in the current PR head.

Final Verdict

✅ No new findings.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 30, 2026
@groeneai
groeneai requested review from Avogar and novikd September 30, 2026 00:33
@clickhouse-gh clickhouse-gh Bot added the comp-simple-engines Lightweight single-node table engines: Log/StripeLog (append-only logs), Buffer (async batching),... label Sep 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 41dffe80ced93cd06e05943fbd7ef5a94d980b13 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

@alexey-milovidov alexey-milovidov left a comment

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.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Oct 10, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 10, 2026
Merged via the queue into ClickHouse:master with commit 87f943a Oct 11, 2026
163 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 11, 2026
…ed-column-default-expression

Conflict in `ReadFromMemoryStorageStep.cpp` with ClickHouse#122991, which fills a
missing `Nested` member from the sizes of a stored sibling. Its
`fillColumnsMissingFromBlock` now also serves `readColumns`: it builds the
sized member over the whole block and cuts it by the read's mask, and with
`defaults` it leaves an entry that has a default expression (a `Nested`
member included) null for `evaluateDefaults`, as `fillMissingColumns` does
with a snapshot. The entries without a sizes source are still filled apart
from the read columns, so no stored array lends them its offsets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 4, 2026
Before the fix, the placeholder filled for a typed JSON path of an
Array(JSON) member had one Array level more than its type, so the FINAL
merge of a ReplacingMergeTree aborted in
ColumnDynamic::chooseDynamicStructureForMerge (SIGSEGV on 26.6.8.7 and
26.10.2.103). Case provided by the PR assignee.

Also drop the Memory comment that predates ClickHouse#122991: Memory now finds the
shared offsets in the stored block without a sibling read.

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),... groeneai-origin-issue PR origin: picked up an external GitHub issue 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.

Memory table PREWHERE returns empty Nested sibling arrays after ALTER ADD COLUMN

3 participants