Repository navigation
Fix wrong results reading a re-added Nested member subcolumn - #113225
PedroTadim merged 2 commits into
Conversation
On a MergeTree table with share_nested_offsets = 1, a Nested member that was dropped and re-added is missing from the part. Reading a subcolumn of that member together with a present member of the same group returned wrong values for the present members whenever the range needed more than one readRows call, and aborted on 'column->size() == num_rows' in debug and sanitizer builds when the short read left a non-empty residue. MergeTreeReaderWide::readRows keys the substreams cache by getNameInStorage so a stream shared by a Nested group is read once per call and the other consumers are served from the cache. getSubcolumnsOfNested skipped subcolumn entries, so when the missing member was requested only as a subcolumn nothing contributed it to the group, tryGetSubcolumnType failed, the conditional remap in Nested::convertToSubcolumns was skipped, and the subcolumn kept a cache bucket of its own. The shared offsets stream was then read twice per call, and on a continuation call the mark seek is suppressed because it would rewind to the granule start, so the second reader got fewer rows than it asked for. Make getSubcolumnsOfNested contribute the member of a Nested group for a subcolumn entry, using the member's name and storage type and never the subcolumn's own, which would rebuild the invalid Nested type that 16d7bab removed. The contribution is de-duped so a co-requested parent cannot produce a duplicate element. That alone exposed a latent second defect. fillMissingColumns satisfies a column absent from the part either by deferring to evaluateMissingDefaults or by synthesizing a default from a Nested sibling's shared offsets, and the offsets branch derived the slot's type and residual subcolumn name from getTypeInStorage and getSubcolumnName. Those are exactly the accessors the remap rewrites, so the branch walked the group type instead of the member type, getBaseTypeOfArray silently stopped descending, and the result was a Tuple under a declared element type or a residual path the truncated type could not resolve. 9f656f3 documents this coupling and fixed the branch-selection consumer through the storage snapshot; this fixes the type-computation one the same way. It was latent only because the first defect suppressed the remap for these shapes, so the branch got correct types by accident. Resolve the metadata shape from the storage snapshot by full name, which the remap preserves, and drive tuple_elements, getBaseTypeOfArray and removeTupleElementsFromSubcolumn off it. num_dimensions and the collected offsets columns are deliberately left alone: they must keep matching the reader's stream names, which the remap normalizes on purpose. Without a snapshot the code falls back to the requested pair, which the Memory engine caller relies on. Beyond the reported shape this also repairs previously aborting member subcolumn reads over Array, Map, Nullable and Tuple value types and at subcolumn depth 2 and 3.
Internal second-model review (independent cold review + gate, all findings adjudicated)Two independent reviews of this diff: a cold read of the resulting code (no access to the
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-45:20260804-004100 |
|
cc @KochetovNicolai @alesapin, could you review this? Under share_nested_offsets a Nested member requested only as a subcolumn never reached its group, so it got a different substreams-cache bucket from its siblings and the one shared offsets stream was read twice per readRows; on a continuation call the mark seek is correctly suppressed, so the second reader short-reads and the present siblings come back with right lengths but wrong values. |
|
No, they are different defects, and the two diffs have no file in common. #112997 changes prefetch What separates them is On this PR's base The split was agreed on #112997 itself: the automated reviewer re-derived this mechanism from that |
|
Workflow [PR], commit [99c9554] Summary: ✅
AI ReviewSummaryThis PR fixes a real Final Verdict✅ No findings. No new inline review comments to post. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 48/49 (97.96%) · Uncovered code |
| GTEST_TEST(NestedUtils, subcolumnOfNestedMember) | ||
| { | ||
| DataTypePtr uint_type = std::make_shared<DataTypeUInt32>(); | ||
| DataTypePtr array_type = std::make_shared<DataTypeArray>(uint_type); |
There was a problem hiding this comment.
Removed, in 99c9554.
Before deleting it I checked what it was pinning, since the PR description claimed reverting either
half reddens a test. It was covering only the getSubcolumnsOfNested change. Reverting just that
hunk and keeping everything else, 04770 alone aborts on column->size() == num_rows, so the SQL
test carries that half on its own and no regression power is lost.
The same commit also fixes a Map path in getBaseTypeOfArray, which the AST fuzzer found on this
branch by wrapping the test's own query in materialize. The descent walked Array and Tuple but not
Map, so arr.m.keys on a missing member was synthesized as
Array(Array(Map(String, UInt64))) under a declared Array(Array(String)). The old assertions used
sum(length(...)), which passes on a wrongly typed column, so they are now wrapped in
materialize and a Map reached through a Tuple element is covered too.
getBaseTypeOfArray descended only Array and Tuple, so a path through a Map stopped at the Map itself and never consumed its keys/values element. For a missing Nested member `arr.m Array(Map(String, UInt64))` the synthesized `arr.m.keys` column therefore carried Array(Array(Map(String, UInt64))) under a declared Array(Array(String)). A length aggregate does not force the check, so the mismatch stayed latent until an action re-materialized the column, where ExpressionActions reports the declared and actual types disagree. The AST fuzzer found it by wrapping the new test's own query in materialize. Map keys and values are already enumerated as Substream::TupleElement by DataTypeMap::getSerialization, so they belong on the tuple_elements path like any other tuple element; teaching the descent to walk into the Map's nested tuple is all that is needed. getBaseTypeOfArray has a single caller, the missing-column synthesis branch in fillMissingColumns. The test asserted these paths with sum(length(...)) alone, which passes on a wrongly typed column. Every wrapped path is now asserted through materialize as well, and a Map reached through a Tuple element of the member is covered. Remove the NestedUtils gtest per review request: the SQL test alone reddens for the getSubcolumnsOfNested change, verified by reverting only that hunk (04770 aborts on 'column->size() == num_rows'), so no regression power is lost. Co-Authored-By: Claude Fable 5 <[email protected]>
CI finish ledger - 99c9554CI is fully finished on this head with zero failures to own: 174 check-runs, 157 success, The two Session id: cron:our-pr-ci-monitor:20260804-190000 |
Systematic stateless tests sweeping {column spelling} x {wrapper evolution:
Nullable, Tuple, Dynamic, widening, LowCardinality} x {part state after the
ALTER: compact/wide unrewritten, rewritten, no shared offsets, mixed parts,
multi-member} x {projection shape of the read}. A part written before a
MODIFY COLUMN keeps its own older type until the mutation rewrites it, and
bugs in this family are order- and state-sensitive: the regression fixed by
ClickHouse#113925 lived in exactly one such cell (unrewritten part x dotted Nested
member x subcolumn-first read) that no hand-written test covered. Split
into four test files so no single one risks a timeout.
The suite passes on a binary with ClickHouse#113925 applied and the first witness
cell reproduces the abort (STID 4256-3fc1) on current master without it,
so this must be merged after ClickHouse#113925.
Related: ClickHouse#113925
Related: ClickHouse#113225
Co-Authored-By: Claude Fable 5 <[email protected]>
A part written before an ALTER keeps its own older type (or misses the column) until the mutation rewrites it. Tests pin their results with mutations_sync = 2, so the corpus systematically avoids the pending- mutation state, and background mutations normally close the window in milliseconds - which is why bugs living in that state (e.g. the regression fixed by ClickHouse#113925, which the full pre-merge CI of ClickHouse#113225 missed) reach master untested. The new failpoint mutate_task_random_sleep_in_prepare sleeps a random 0-3 s in MutateTask::prepare, covering plain, replicated and shared MergeTree mutations. The stress test enables it right after the smoke check (never in upgrade check: the old binary may not know it) and disables it in prepare_for_hung_check, next to stopping the thread fuzzer, so pending mutations drain at full speed before the hung check. Disabling a registered failpoint that was never enabled is a no-op. With the failpoint on, every test that ALTERs without waiting reads parts the mutation has not rewritten yet, under the stress runner's random settings and the server-side AST fuzzer's query mutations. Measured on a RelWithDebInfo build: a mutations_sync = 2 UPDATE takes 0.12 s with the failpoint off and 0.65-1.67 s with it on. Related: ClickHouse#113925 Related: ClickHouse#113225 Co-Authored-By: Claude Fable 5 <[email protected]>
…umns The type-directed enumerateStreams walk in collectOffsetsColumns pairs each available column's declared type with its data. When an entry's type was resolved from the table's metadata while the column was read from a data part with an older type (the mixed type provenance behind ClickHouse#113925), the walk casts the column to the wrong class at the first diverging wrapper: in debug and sanitizer builds that aborted with a bare 'Bad cast from type A to B' naming no column, and in release builds assert_cast does not check at all, so the walk silently misbehaved on memory-unsafe reinterpretation. The new check descends exactly the wrapper levels whose serializations assert_cast the column (Array, Nullable, Tuple, Map, and class-only Dynamic, Variant, Object), accepts every leaf it does not know (leaf divergence such as a part storing UInt32 for a column widened to UInt64 is legitimate), and reports a LOGICAL_ERROR naming the column, the declared type and the column structure. Debug builds get a one-frame diagnosis instead of a cross-file hunt; release builds now fail close instead of walking mismatched memory. Related: ClickHouse#113925 Related: ClickHouse#113225 Related: ClickHouse#113891 Co-Authored-By: Claude Fable 5 <[email protected]>
Related: #112769
Closes: #113411
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong results and a
LOGICAL_ERRORwhen reading a subcolumn of a dropped-and-re-addedNestedmember together with a present member of the same group, on aMergeTreetable withshare_nested_offsets = 1. The present members returned values from a later block whenever the read spanned more than one block.Description
No open issue. I hit this while validating #112997; it is separate and pre-existing - it reproduces at
local_filesystem_read_prefetch = 0, where that PR is inert.What breaks.
MergeTree,share_nested_offsets = 1(the default), ordinary settings. ANestedmember was dropped and re-added, so it is missing from the part, and a query reads a subcolumn of it (arr.nested.b) beside a present member (arr.id) - both read one shared offsets stream. Once the range spans two blocks the present members come back with right lengths but wrong values; debug and sanitizer builds abort oncolumn->size() == num_rows.Root cause.
MergeTreeReaderWide::readRowskeys the substreams cache bygetNameInStorage, so a shared stream is read once per call.getSubcolumnsOfNestedskipped subcolumn entries, so a member requested only as a subcolumn never reached the group,Nested::convertToSubcolumnsskipped its conditional remap, and the subcolumn landed in a different cache bucket. The stream was then read twice, and a continuation call suppresses the mark seek (correctly - it cannot rewind), so the second reader short-reads. That exposed a latent second half:fillMissingColumns' offsets branch derives its type from the same accessors.The change.
getSubcolumnsOfNestedcontributes a subcolumn's member (never the subcolumn's own name, which would build an invalidNestedtype), de-duped.fillMissingColumnsresolves the metadata shape fromstorage_snapshotby full name, as the neighbouring guard from #112769 does, falling back to today's behavior without a snapshot. This also fixes previously-aborting reads at depth 3 and over nestedArray,Map,NullableandTuplevalue types.Validation. New
04770_nested_subcolumn_shared_offsets_continuation, verified to fail without the fix; reverting any one of the three hunks reddens it.03742_test_flattened_crashpasses byte-identically. 50/50 randomized runs at jobs 8 and 1. Compact parts, merges, mutations and object storage also covered.26.4-26.7look exposed, but a backport needs #112769 first.Version info
26.8.1.800(included in26.8and later)