Repository navigation
Fix reading a re-added Nested subcolumn with its parent column - #112769
PedroTadim merged 4 commits into
Conversation
A SELECT that reads a Nested element subcolumn together with its own parent
column, after that column was dropped and re-added, put the whole element in
the subcolumn's slot while the block still declared the element type.
fillMissingColumns can satisfy a column absent from the part either by
deferring to evaluateMissingDefaults, which derives a subcolumn from its
parent, or by synthesizing a default from a Nested sibling's shared offsets.
IMergeTreeReader first runs its column lists through
Nested::convertToSubcolumns, which moves the subcolumn delimiter, so
`arr.nested.b` becomes {name_in_storage = "arr", subcolumn = "nested.b"}. The
deferral compared that name_in_storage against lists keyed by full name, so it
never matched and the offsets branch produced a Tuple under a declared element
type.
Resolve the requested column through the storage snapshot, which is
authoritative about where the subcolumn delimiter belongs, require the resolved
pair to be a subcolumn, and match its parent's full name against both
availability sets. Requiring isSubcolumn on the resolved pair is what keeps the
parent's own slot on the offsets branch, where it belongs.
In release builds the mistyped column was read through an unchecked
static_cast, so the query returned a different arbitrary value on every run
while the parent column read correctly. Debug and sanitizer builds raised
`Bad cast from type DB::ColumnTuple to DB::ColumnVector<double>`. Any element
type is affected; three consumers were observed reading the mistyped column
(output formatting, merging, and function execution) and all are repaired by
this single change at the point where the block is built.
Extend 03742_test_flattened_crash with the subcolumn shapes: Float64 and String
elements, the ORDER BY form, function execution over the subcolumn, a two-part
case, Wide and Compact arms, and controls for the reads that were already
correct.
Internal second-model reviewTwo independent cold reviews of the resulting code (carriers enumerated from source before reading
What the two cold passes verified independently, beyond the author's own evidence:
One row drafted for this test was removed rather than asserted: reading a never-dropped sibling Post-fix release behaviour is inferred from the shared construction site rather than measured; the Session id: cron:clickhouse-review-slot-48:20260731-155920 |
Pre-PR validation gate
Performance: the predicate runs once per missing column per read step and replaces one name scan with a metadata hash lookup plus the same scan. Same complexity class, no hot loop touched. Validation: tag-free One row that was drafted for this test was removed after measurement: reading a never-dropped sibling column beside the re-added subcolumn diverges in about 10% of randomized runs, and reproduces at the same rate on an unmodified master build, so it is a pre-existing defect tracked separately rather than something this change should assert. |
|
cc @KochetovNicolai @Avogar, could you review this? The deferral in |
|
Workflow [PR], commit [4ed598c] Summary: ❌
AI ReviewSummaryThis PR fixes the original mistyped read when a re-added Findings❌ Blockers
Final Verdict❌ Changes requested |
| && requested_column->isSubcolumn() | ||
| && (available_columns.contains(requested_column->getNameInStorage()) | ||
| || additional_available_columns.contains(requested_column->getNameInStorage()))) | ||
| if (isSubcolumnOfAvailableColumn(storage_snapshot, *requested_column, available_columns, additional_available_columns)) |
There was a problem hiding this comment.
This only fixes the "parent already available" branch. fillMissingColumns still decides whether to leave a slot for evaluateMissingDefaults via hasDefault(storage_snapshot, *requested_column) above, and for a converted Nested element subcolumn that helper still probes arr.nested.b and arr, never the real parent arr.nested.
That means ALTER TABLE ... ADD COLUMN \arr.nested` Array(Tuple(...)) DEFAULT ...is still wrong on old parts: the element subcolumn will be synthesized from sibling offsets here instead of deferring to the parent'sDEFAULT, so arr.nested.breturns the type default rather than the DDL default. Can we resolve the parent throughStorageSnapshot` for the default check as well, and add a regression for that shape?
There was a problem hiding this comment.
You are right about the code: for a converted Nested element subcolumn hasDefault really does probe arr.nested.b and arr and never arr.nested, because Nested::convertToSubcolumns moves the delimiter and ColumnsDescription::getDefault only indexes declared columns.
The predicted symptom does not happen, though. I measured that fixture on an unmodified master build and the DDL default wins in every shape but one:
| shape | master | with this change |
|---|---|---|
arr.nested.b |
[42.5] |
[42.5] |
arr.nested.a |
['dflt'] |
['dflt'] |
arr.nested.size0 |
1 |
1 |
arraySum(arr.nested.b) |
42.5 |
42.5 |
arr.nested |
[('dflt',42.5)] |
[('dflt',42.5)] |
arr.nested.b, arr.nested |
Bad cast |
[42.5] [('dflt',42.5)] |
The reason is that injectRequiredColumnsRecursively asks the same default question one layer earlier, on the pre-conversion name, and resolves the parent through StorageSnapshot: MergeTreeBlockReadUtils.cpp:142-149 falls back to getDefault(column_in_storage->getNameInStorage()), which for arr.nested.b is exactly getDefault("arr.nested"). To show that path really runs rather than the offsets branch, I made the default depend on a sibling column, so only an evaluated expression can produce the right answer:
ALTER TABLE t ADD COLUMN `arr.nested` Array(Tuple(a String, b Float64))
DEFAULT arrayMap(x -> ('sib', toFloat64(id)), `arr.id`); -- row has id = 7
SELECT arr.nested.b FROM t; -- master: [7], not the type default [0]So the hasDefault miss is inert on the reachable paths, and I have not changed it. Resolving the parent there as well would be a reasonable hardening, but with no observable difference it is a separate change rather than part of this one.
The single failing shape is worth having, and it is not a second defect: it aborts with Bad cast from type DB::ColumnTuple to DB::ColumnVector<double>, the same signature this PR fixes, so the DEFAULT variant is another carrier of the same bug. I pushed the regression you asked for in 4ed598c90403bb, covering the element alone, the element beside its parent, the parent alone, the string element, and the sibling-dependent default above. Verified in both directions: the rows pass with the change and the second one aborts once the change to fillMissingColumns is reverted. Tag-free 50 runs with --jobs 8: 50 passed.
I also dropped a toTypeName row I had added earlier. It reads Array(Float64) with and without the change, so it could not witness a ColumnTuple stored under that declared type.
The re-added Nested element subcolumn was read with the wrong type whether or not the re-added column carried a DEFAULT expression, but the shipped rows only covered the no-DEFAULT case. On master the DEFAULT variant reaches the same wrongly typed slot: reading `arr.nested.b` beside `arr.nested` fails with `Bad cast from type DB::ColumnTuple to DB::ColumnVector<double>` in a debug build, while every other shape of that fixture (the element alone, the parent alone, the string element, `arraySum` over the element) already returns the DDL default correctly. The last case makes the default expression depend on a sibling column, so a correct result is only possible when the expression is evaluated with its dependencies read, rather than the element being synthesized from the shared offsets. That distinguishes the deferral this change relies on from the offsets branch, which would yield the type default instead. Verified in both directions: the five new rows pass on the fixed build (Build ID 1bad264eb6dea51a81260f6cb265480872baddab) and the second one aborts with the signature above once the change to `fillMissingColumns` is reverted (Build ID 9c015e77e67462927ce482def7b7de871cef1a79). Tag-free 50/50 run with `--jobs 8`: 50 passed, 0 failed. Co-Authored-By: Claude Fable 5 <[email protected]>
`toTypeName` reports the analyzed argument type without evaluating the column, so that row reads `Array(Float64)` both before and after the change and cannot witness the defect, which is a `ColumnTuple` stored under an advertised `Array(Float64)` type. Measured identical on a pristine build and on the fixed one. The paired value assertions around it already fail without the change, so removing the row loses no coverage. Co-Authored-By: Claude Fable 5 <[email protected]>
The five-line block restated the root cause that the commit message and the PR description already carry. Only the delimiter-moving detail matters at the call site, so keep that and drop the rest. Rebuilt and re-verified after the edit (Build ID 33757c01d12cc60d7c39277b19abe2de83c8e221): the regression test still passes. Co-Authored-By: Claude Fable 5 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 12/12 (100.00%) · Uncovered code |
CI finish ledger - 4ed598cEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
The other 173 checks are green (155 success, 18 skipped) and both aggregators, Config Workflow and Finish Workflow, completed successfully. Session id: cron:our-pr-ci-monitor:20260801-073000 |
299fb01
Related: #112668
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a wrongly typed read of a
Nestedelement subcolumn whose column was dropped and re-added. Selecting such a subcolumn together with its own parent column returned the whole element in the subcolumn's slot while the block still declared the element type, which produced arbitrary values in release builds and a logical error in debug and sanitizer builds.Description
SELECTof aNestedelement subcolumn together with its own parent column, after the column wasDROPped and re-ADDed, returned a column of the wrong type:On a release build this returned a different value on every run while the parent
column read correctly, so nothing indicated the subcolumn value was fabricated. Debug and sanitizer
builds raised
Logical error: 'Bad cast from type DB::ColumnTuple to DB::ColumnVector<double>'.Root cause:
fillMissingColumnsdecides between deferring toevaluateMissingDefaultsand synthesizing a default from aNestedsibling's sharedoffsets.
IMergeTreeReaderfirst runs its column lists throughNested::convertToSubcolumns, whichmoves the subcolumn delimiter, so
arr.nested.bbecomes{name_in_storage = "arr", subcolumn = "nested.b"}. The deferral compared thatname_in_storageagainst lists keyed by full name, nevermatched, and the offsets branch then produced the whole
Tupleunder a declared element type.The fix resolves the requested column through the storage snapshot, which is authoritative about
where the subcolumn delimiter belongs, and matches the resolved parent's full name against the
availability sets. Three consumers were observed reading the wrongly typed column (output
formatting, merging via
ORDER BY, and function execution), and all three are repaired by thissingle change at the point where the block is built. Reads that were already correct are covered as
controls in the test.
Found by the AST fuzzer, which mutated the existing
03742_test_flattened_crashtest to read theelement subcolumn instead of the whole column (
AST fuzzer (amd_debug), STID 2508-368d):https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112668&sha=3f7020cf4114402a81c9d3dd6e7bef755802e34b&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
Version info
26.8.1.598(included in26.8and later)