Repository navigation
Fix subcolumns of a Merge table column that a child table does not have - #123633
alexey-milovidov merged 2 commits into
Conversation
…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]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261002-214503 |
|
Workflow [PR], commit [e97f90c] Summary: ✅
AI ReviewSummaryThis PR fixes the main Findings❌ Blockers
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit e97f90c.
Changed lines: Changed C/C++ lines covered: 37/37 (100.00%) · Uncovered code |
| { | ||
| const auto & type_in_storage = merge_column->getTypeInStorage(); | ||
| auto subcolumn = type_in_storage->getSubcolumn( | ||
| merge_column->getSubcolumnName(), type_in_storage->createColumnConstWithDefaultValue(1)); |
There was a problem hiding this comment.
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.nullbecomes1in this branch, while the stream path fillsxwith5and then derivesx.null = 0.SELECT n.a, length(n.b)over aDistributedchild that hasn.abut lacksn.b: this branch still substitutesn.b.size0with0, instead of matching the[NULL, NULL]fill thataddMissingDefaultsproduces fromn.a's offsets whenshare_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?
There was a problem hiding this comment.
🕵 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).
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 22 s compile time in total, 7 of them have a recent master baseline. |
…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]>
… 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]>
Closes: #122523
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results when a
Mergetable (or themergetable function) reads a subcolumn of a column that one of its child tables does not have: for that child,x.nullof a missingNullablecolumnxwas read as 0, sox IS NULLreturned no rows from it. Element subcolumns ofTuple,VariantandJSONcolumns and the sizes of a missing flattenedNestedmember 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 kwith one row andCREATE TABLE m (k UInt8, c0 Nullable(UInt8)) ENGINE = Merge(currentDatabase(), '^child$'),SELECT count() FROM m WHERE c0 IS NULLreturns 0 instead of 1 (1 withoptimize_functions_to_subcolumns = 0), andSELECT c0, c0.null FROM mreturns\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:
c0withNULL,c0.nullwith0. ADistributedchildevaluates 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 withcreateSubcolumnsExtractionActions; for a child that runs the query, substitute the subcolumn of the column'sdefault. 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 andDistributedchildren,
merge(),Nullable,Tuple,Variant,JSONand flattenedNested); reverting either half reddensonly its own cases; 50 randomized runs pass.
Not changed here: when a child that runs the query above
FetchColumnsreturns fewer columns than theMergetableexpects (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 aMergeTreechild and aDistributedchild lacking both. This ispre-existing and not specific to subcolumns.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123633]
Version info
26.10.1.1471-master(included in26.10and later)