Skip to content

Fix reading a subcolumn of a Nested member added by ALTER ADD COLUMN from older parts - #123690

Merged
asya-ch merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-fill-missing-nested-member-subcolumns
Nov 4, 2026
Merged

asya-ch merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-fill-missing-nested-member-subcolumns

Conversation

@groeneai

@groeneai groeneai commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #116553
Related: #122991

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 reading a subcolumn of a Nested member added by ALTER TABLE ... ADD COLUMN (sizeN, or a JSON path below the member's array) from parts written before the ALTER. It failed with There is no subcolumn, or produced a column of the wrong type, which returned garbage values or crashed the server, also in a FINAL merge.

Description

On 26.8 (26.8.10.6), SELECT n.c.size1 of a Nested member added by ALTER TABLE ... ADD COLUMN fails with There is no subcolumn size1 on parts written before the ALTER, n.j.x.size1 of an Array(JSON(x Array(UInt8))) member returns garbage, and n.j.x.size0 crashes the server (SIGSEGV; also on 26.3):

CREATE TABLE t (k UInt64, n Nested(a UInt64)) ENGINE = MergeTree ORDER BY k;
INSERT INTO t VALUES (1, [10, 20]);
ALTER TABLE t ADD COLUMN `n.c` Array(Array(UInt64)), ADD COLUMN `n.j` Array(JSON(x Array(UInt8)));
SELECT n.c.size1 FROM t;   -- Code: 44 There is no subcolumn size1 in type UInt64; expected [0,0]
SELECT n.j.x.size1 FROM t; -- [57064768,131666517426177]; expected [0,0]

A typed read of a JSON path of an Array(JSON) member with FINAL on a ReplacingMergeTree also crashes (SIGSEGV in ColumnDynamic::chooseDynamicStructureForMerge on 26.6.8.7 and 26.10.2.103): the column filled for the old part has an extra Array level.

fillMissingColumns built such a subcolumn by stripping the member's type to its scalar element and resolving the rest of the subcolumn name against a default of that element. Subcolumn names count from the root of the column, so the name either did not exist (Map, Tuple, Variant, Dynamic, LowCardinality, deeper arrays) or named a different substream.

When the shared offsets are the member's own outer arrays, the subcolumn is now taken by the storage type's getSubcolumn from one default element wrapped in the member's arrays and replicated over the known offsets, so it is resolved like stored data and costs no more than the subcolumn. This also covers the shapes of #116553. Memory reads without another member of the same Nested were fixed separately by #122991.

The new test reads 16 subcolumns of 9 member shapes from Wide, Compact and Memory and checks every row of the pre-ALTER part against stored defaults, plus the FINAL case.


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

Version info

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

A Nested member added by `ALTER TABLE ... ADD COLUMN` is absent from parts
written before the ALTER. `fillMissingColumns` built a requested subcolumn of
such a member by stripping the storage type to its scalar element, re-adding
the subcolumn's own array levels and resolving the remaining subcolumn name
against a default of that element. Subcolumn names are counted from the root
of the column (`sizeN` counts arrays from the root, a JSON path below an array
is resolved at that array level), so the remainder was resolved at the wrong
level: `n.c.size1` of `Array(Array(UInt64))` failed with `There is no
subcolumn size1 in type UInt64`, and `n.j.x.size1` of
`Array(JSON(x Array(UInt8)))` produced a column of the wrong type (garbage
values or a segfault in release builds, a bad cast in debug builds).

When the shared offsets are the outer arrays of the member in storage, take
the subcolumn with `IDataType::getSubcolumn` on the storage type from one
default element wrapped in one-element arrays of the same depth, then
replicate that element's value and wrap it in the known offsets, so the
subcolumn is resolved by the same rule as for stored data and costs no more
than the subcolumn itself. Other cases keep the existing code.

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 3, 2026
@groeneai

groeneai commented Oct 3, 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; 3 rounds, 6 findings; the last round found nothing) plus my own review (3 rounds, 4 findings).

# Sev Finding Verdict Evidence / action
1 ⚠️ The new branch built every default element of the member to extract one subcolumn (~30x memory for one field of a 32-field Tuple member) AGREE, fix round 1 subcolumn taken from one default element, then replicated
2 ⚠️ The test did not cover n.j.x.size0, the shape that crashes on 26.8 AGREE, fix round 1 added for Wide, Compact and Memory
3 ❌ Memory reads without another member of the same Nested still return [] DISAGREE untouched no-offsets path, identical on master; fixed by #122991, which rewrites those fill sites
4 💡 No guard for shapes that worked before and now take the new branch AGREE, fix round 1 n.t.q, n.m.keys, n.nt.null added to the same oracle
5 💡 Use Critical Bug Fix for a crash DISAGREE that category is set only on a maintainer's request
6 💡 Link related PRs with full URLs, visibly AGREE, fix round 1 done in the description
7 ⚠️ The test declares Nullable(Tuple) members without enabling the type, which is off by default on 26.8, so a backport of the test fails before any assertion AGREE, fix round 2 SET enable_nullable_tuple_type = 1
8 💡 Dynamic and LowCardinality, named in the description, were not in the test AGREE, fix round 2 both added to the same oracle

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

Session id: cron:clickhouse-review-self-slot-58:20261003-131200

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The 4-statement repro in the description fails on every run on the base build (Code: 44 There is no subcolumn size1 in type UInt64); the new test fails at its first data query on the base build.
b Root cause explained? fillMissingColumns resolved the root-relative remainder of the subcolumn name (sizeN, a JSON path below an array) against a default of the member's stripped scalar element, at level 0, so the name did not exist or named a different substream.
c Fix matches root cause? Yes. The subcolumn is taken by the storage type's getSubcolumn from one default element wrapped in the member's own arrays, the same resolution that produced the requested type, then replicated over the known outer offsets. No guard and no per-shape case.
d Test intent preserved / new tests added? New test 05320_nested_member_added_by_alter_subcolumns: 16 subcolumns of 9 member shapes on Wide, Compact and Memory, including the crash shape n.j.x.size0 and shapes that already worked (n.t.q, n.m.keys, n.nt.null); every pre-ALTER row is compared with a row that stores the defaults; system.parts_columns proves the old part lacks the members.
e Both directions demonstrated? Yes. Base: FAIL (Code 44 at the first data query), debug abort on n.tt.size1. Fix: OK, every old-part row equals its stored-default row (87/87 pairs, checked by a script with a negative control).
f Fix is general across code paths? Wide and Compact readers and Memory all reach the changed branch and are tested; the no-offsets branch and the subcolumn-next-to-parent path already build the whole default. The extended 04329/04770 arms of #116553 pass without its source change.
g Fix generalizes across inputs (params/datatypes/wrappers)? 2- and 3-level arrays, Map, Tuple element path, Nullable(Tuple), Variant, Dynamic, LowCardinality, JSON typed path and its size0 dynamic path, a tuple element named size1; 0, 1 and 2 elements per row; 50 randomized runs and forced parallel replicas, all green.
h Backward compatible? (maintainer-approved exception only) Yes. Read path only, no setting and no format change. Whole-member reads are byte-identical to the base build, and a 488-test sweep of nested/subcolumn tests has the same result set as the base build.
i Invariants and contracts preserved? The result has num_rows rows (the level-0 offsets) and the requested type (checked with toTypeName and materialize). The new branch has the same precondition on the offsets as the existing one, and every other case keeps the existing code.

Session id: cron:clickhouse-impl-self-slot-57:20261003-124000

@groeneai
groeneai requested review from Avogar and CurtizJ October 3, 2026 13:36
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8485427]

Summary: ✅


AI Review

Summary

Fixes default reconstruction for subcolumns of a Nested member added by ALTER TABLE ... ADD COLUMN, preserving root-relative subcolumn resolution and shared array offsets. The regression coverage includes Wide, Compact and Memory reads and the reported FINAL failure; 05320_nested_member_added_by_alter_subcolumns passed in Fast test at head 8485427a63fdd64f0e240ed916ef1af50172f32b. No actionable findings remain.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Oct 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8485427a6 with master 7f11488e0 (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, 11 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 2b0d93f

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build (amd_fuzzers) / Build ClickHouse not from this PR: the same job is red on master and on 97 PRs in 24h; the clickhouse_fuzzer link outgrows the 2 GiB reach of the small code model #122521 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 05257_sql_security_view_sealed not from this PR: a parallel replica reads the row-policy table of a SQL SECURITY DEFINER view as default, not as the definer (ACCESS_DENIED); 52 PRs in 24h on this new job #123729 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 04825_quantile_deterministic_alter_add_modify_column not from this PR: a -Merge state of quantileDeterministic crosses the replica wire at state version 0, so the merge returns 506014 instead of 492708; 15 PRs in 24h #123070 (mine, open)
Stateless tests (arm_binary, s3 storage, DBReplicated, parallel, 1/2) / 05237_with_expression_alias_scope_descendant deterministic under a Replicated database: the replicated CREATE VIEW stores a nested WITH alias qualified with the database, so the view reads the same-named table; 109 PRs in 24h a fix task is created (fix in progress, fixing-PR link to follow here)
Mergeable Check, PR rollups, red only because of the rows above #122521, #123729, #123070 and the fix task above (a fix task is created)

None of these touches the missing-column fill this PR changes, and 05320 passed in every lane. All 160 checks completed; CIDB has no other FAIL or ERROR on this head.

Session id: cron:our-pr-ci-monitor:20261003-183140

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for 05237_with_expression_alias_scope_descendant (DBReplicated): #123774

@asya-ch

asya-ch commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

@groeneai, merge master.

…sted-member-subcolumns

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

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

Merged master in 512a6b5 (no conflicts, the fix itself is unchanged). On current master the test still fails with Code: 44 ... There is no subcolumn size1 in type UInt64; with this branch it passes, 50/50 randomized runs.

@asya-ch asya-ch self-assigned this Nov 4, 2026
@asya-ch

asya-ch commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

@groeneai this also fixes a segfault on 26.6: a FINAL merge over a typed read of a dynamic JSON path (.:Array(Dynamic)) in an Array(JSON) member added by ALTER ADD COLUMN. Before the fix the placeholder has one Array level too many and the merge aborts in ColumnDynamic::chooseDynamicStructureForMerge via MergedData::initialize.

The current test arms cover a typed path and no FINAL. Please add a case along these lines and confirm it fails on master before the fix and passes on this branch:

CREATE TABLE t_nested_add_final (id UInt64, v UInt64, `p.x` Array(Int64))
ENGINE = ReplacingMergeTree(v) ORDER BY id;
SYSTEM STOP MERGES t_nested_add_final;
INSERT INTO t_nested_add_final VALUES (1, 1, [1, 2]), (2, 1, [3]);
ALTER TABLE t_nested_add_final ADD COLUMN `p.j` Array(JSON);
INSERT INTO t_nested_add_final VALUES (1, 2, [7, 8, 9], ['{"r":["a","b"]}', '{"r":["c",1]}', '{"r":5}']), (2, 2, [10, 11], ['{"r":[1,2]}', '{"r":[2.5,"d",null]}']);
SELECT id, v, p.x, p.j.r.:`Array(Dynamic)`, p.j.r.:`Array(Nullable(String))` FROM t_nested_add_final FINAL ORDER BY id;
DROP TABLE t_nested_add_final;

On this branch it returns:

1       2       [7,8,9] [[],['c',1],[]] [['a','b'],[],[]]
2       2       [10,11] [[1,2],[2.5,'d',NULL]]  [[],[]]

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]>
@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

Added your case to 05320_nested_member_added_by_alter_subcolumns in 8485427.

  • Master debug build (e6c0a8e): aborts on a bad assert_cast to ColumnDynamic in ColumnDynamic::chooseDynamicStructureForMerge, via MergedData::initialize from ReplacingSortedAlgorithm. The official 26.6.8.7 and 26.10.2.103 images crash with SIGSEGV on the same stack.
  • This branch: returns your expected rows with enable_vertical_final 0 and 1, on Wide and Compact parts, and the whole test passes 50/50 with randomized settings.

The same commit also removes a Memory comment that went stale after #122991. I added the FINAL crash to the PR description.

@asya-ch
asya-ch added this pull request to the merge queue Nov 4, 2026
Merged via the queue into ClickHouse:master with commit b288901 Nov 4, 2026
185 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 4, 2026
Avogar added a commit that referenced this pull request Nov 11, 2026
The wrong structure of a missing `Nested` subcolumn is already fixed in
master by #123690, which
takes the subcolumn from the whole storage column. The dimension
correction of this branch is not reached anymore, and it did not handle
`sizeN` subcolumns, so remove it together with its test (covered by
`05320_nested_member_added_by_alter_subcolumns`) and the structure check
of filled columns.

Keep the checked casts of source columns in
`chooseDynamicStructureForMerge`, `takeOrCalculateStatisticsFrom`,
`takeExactDynamicStructureFrom` and `prepareForSquashing`, so a source
column of an unexpected structure becomes a `LOGICAL_ERROR` instead of
corrupting memory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 11, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 11, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 11, 2026
Backport #123690 to 26.9: Fix reading a subcolumn of a `Nested` member added by `ALTER ADD COLUMN` from older parts
clickhouse-gh Bot added a commit that referenced this pull request Nov 11, 2026
Backport #123690 to 26.10: Fix reading a subcolumn of a `Nested` member added by `ALTER ADD COLUMN` from older parts
groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…ted` member added by `ALTER ADD COLUMN` from older parts
groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…sted` member added by `ALTER ADD COLUMN` from older parts
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…r added by `ALTER ADD COLUMN` from older parts
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…er added by `ALTER ADD COLUMN` from older parts
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-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. groeneai-origin-self PR origin: self-found defect (impact-bar validated) pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants