Repository navigation
Cherry pick #112338 to 26.7: Fix a crash when reading a nullable subcolumn of a Variant or Dynamic column - #119802
Conversation
… column
Reading a subcolumn whose own type is Nullable or LowCardinality(Nullable) out
of a Variant, Dynamic or JSON column stored in a MergeTree table crashed the
server. Three lines of plain SQL with default settings are enough:
CREATE TABLE m (id UInt64, value Dynamic) ENGINE = MergeTree ORDER BY id;
INSERT INTO m VALUES (1, CAST(tuple(CAST(1, 'Nullable(UInt32)'), 's'),
'Tuple(a Nullable(UInt32), b String)'));
SELECT value.`Tuple(a Nullable(UInt32), b String)`.a FROM m;
On a debug build this is a LOGICAL_ERROR ("Bad cast from type
DB::DataTypeNumber<unsigned int> to DB::DataTypeNullable"); on a release build
assert_cast degrades to a plain static_cast, so it is a segfault. Reproduced on
26.7.2.26, 26.6.2.120, 26.5.6.78 and 26.3.17.78. The same query against
ENGINE = Memory returns the correct value.
SerializationVariantElement recurses into the serialization of the on-disk
element, and the extraction framework exposes such an element as Nullable(T)
even though it is stored non-nullable. To keep the type it hands down consistent
with that nested serialization, it stripped nullability unconditionally. That
strip cannot distinguish nullability added by the extraction from nullability
intrinsic to the requested subcolumn: for a Nullable tuple element the nested
serialization is a SerializationNullable, and the strip removes exactly the
nullability it requires.
Strip only when the extraction's wrap actually changed the type. The decision is
made where the wrap happens and passed down as an explicit flag, which is also
part of the serialization's pooling key so that two serializations differing only
in it cannot share a pooled instance.
Both methods that recurse into the nested serialization are gated: gating only
enumerateStreams would move the crash into deserializeBinaryBulkWithMultipleStreams
instead of removing it. On the gated deserialization path the element's own null
map is read by the nested serialization, and the NULLs of rows belonging to other
variants come from insertDefault/insertManyDefaults.
Dynamic computes the flag in DataTypeDynamic::getSubcolumnData against the type
before the wrap. Variant derives it inside VariantSubcolumnCreator::create, where
prev_type is the selected subcolumn's pre-wrap type; deciding it once per variant
alternative is not sufficient, because with
allow_nullable_tuple_in_extracted_subcolumns = 1 the alternative's own wrap does
change the type and the strip would still fire on the intrinsically nullable
element. The shared-variant branch keeps an unconditional strip: its column is
always Nullable over the shared variant type while its nested serialization is a
plain String serialization.
IDataType::getSubcolumnData now applies a creator to the serialization before the
type, matching ISerialization::createFromPath, so the Variant predicate sees a
pre-wrap prev_type on both read paths. This is a prerequisite of the fix and is
behaviour-neutral for every creator that ignores its prev_type argument.
No on-disk format change and no new or changed setting. Parts written before this
change are read correctly afterwards; before it they could not be read at all.
Three lines of the test read as coverage but would pass byte-identically with the fix reverted, so the suite was weaker than the PR description. The JSON coverage only requested a non-nullable typed path, whose nullability is added by the extraction, i.e. the pre-existing always-correct path. A JSON object value is decomposed into one dynamic path per leaf, so a nested tuple never materialises as a single dynamic value and no untyped JSON path can carry the bug. A typed path keeps its declared type intact, so a path declared as a Variant whose alternative is the nullable-element tuple is the reachable route; it aborts on unfixed master. The original non-nullable line is kept as a control. The runner draws min_bytes_for_wide_part = 0 with about 30% probability, and only the wide table pinned its part thresholds, so on such a run every table was wide and the compact reader - the one whose stack produced the original report - was not covered at all. All the other tables now pin compact thresholds explicitly, so the split no longer depends on the draw. The paired query meant to build both serializations in one pool requested a type that was not an alternative of that column, so it silently took the shared variant branch instead. An extra row makes the type a real alternative. The two instances are still discriminated by their nested serialization, which no reachable query can make agree, so the comment now says that rather than claiming a pool collision the query cannot produce. Co-Authored-By: Claude Fable 5 <[email protected]>
master rewrote SerializationVariantElement::deserializeBinaryBulkWithMultipleStreams
twice since the branch point ("Remove rows_offset from the deserialization API" and
"Deserialize columns into IColumn& to fix substreams-cache clone regression"): the
discriminators and variant columns no longer live in the deserialization state, and
the result column is an IColumn& instead of a ColumnPtr &.
Both guards this branch adds to that function are conditions on expressions master
rewrote, so master's body is kept and only the conditions carry the
nullable_added_by_extraction test:
* the null map fill and the descent into the nested column now guard master's
isColumnNullable(result_column) / inner_column = &nullable_column.getNestedColumn();
* the LowCardinality(Nullable) strip now guards master's unconditional
isColumnLowCardinalityNullable(*inner_column) on the fresh variant clone.
The hand resolution touches one file: the two conditions, removal of the enclosing
block master deleted, and one comment compressed from 5 lines to 3. enumerateStreams,
the getHash and create signatures, and VariantSubcolumnCreator::create merged
unchanged, as did the other five files.
Both conflicts came from master renaming the dynamic-subcolumn result structure (`res->serialization`/`res->type` -> `res->data.*`, `dynamic_subcolumn_data` -> `dynamic_subcolumn_info->data`). Resolved by keeping this branch's semantics in master's spelling: the ordering in `DataTypeDynamic::getDynamicSubcolumnData` that computes `nullable_added_by_extraction` across the type wrap, and the serialization-before-type creator order in `IDataType::getSubcolumnData`. Master's own `ISerialization::createFromPath` already applies the creator in that same order. #116739 (`8f3f7df0d3150`) landed on master after this branch was last updated, adding `wrapColumnForDeserialization` overrides to `SerializationVariantElement` and `SerializationDynamicElement` that peel the result column's `Nullable` level whenever `isColumnNullable`. This branch makes the matching unwrap in `deserializeBinaryBulkWithMultipleStreams` conditional on `nullable_added_by_extraction`, so the wrap was checked against that flag too. Gating it changes no observable behavior: for an intrinsic `Nullable` the peel hands the nested column to a `SerializationNullable`, which does not override `wrapColumnForDeserialization`, so rebuilding the `ColumnNullable` around it reproduces the input layout. Measured on this tree, 04652, 04653 and 05045 all pass both with the gate and without it. The overrides are therefore left byte-identical to master, and only the comment that described the unwrap as unconditional is corrected. Validation on the merged tree (debug, build id a395d5b3272a6dd9d33e1a4bf2ee5a9ab4766540): 04652, 04653 and master's own 05045 all pass. With the six `src/` files reverted to master (build id ed053ed21015c958), 05045 still passes while 04652 and 04653 fail with `Bad cast from type DB::DataTypeNumber<unsigned int> to DB::DataTypeNullable`, so the fix this branch carries is still load-bearing and is not superseded by anything on master. Co-Authored-By: Claude Opus 5 <[email protected]>
…-nullable-tuple-subcolumn # Conflicts: # src/DataTypes/DataTypeDynamic.cpp
…-tuple-subcolumn Fix a crash when reading a nullable subcolumn of a Variant or Dynamic column
…column crash
Reading a subcolumn whose own type is Nullable out of a Variant or Dynamic column
segfaults on 26.7: SerializationVariantElement could not tell a Nullable level that
subcolumn extraction had added (whose null map it must fill from the discriminators
and peel off before recursing) from a Nullable level intrinsic to the requested type
(which belongs to nested_serialization), so it stripped the latter and handed the
nested serialization a column shape it does not read.
The robot cherry-pick conflicts because 26.7 predates two master refactors. Neither is
imported here; the fix is re-expressed in 26.7's own naming.
Axis A, the SubcolumnInfo refactor is master-only:
- res->data.{type,serialization} spelled res->{type,serialization};
- dynamic_subcolumn_info->data.X spelled dynamic_subcolumn_data->X;
- nested_name_to_store (a separate master PR's zero-array-level renaming) spelled
with the branch's inline String(subcolumn_nested_name).
Axis B, deserializeBinaryBulkWithMultipleStreams still has the older
(ColumnPtr & result_column, size_t rows_offset, ...) signature with the
mutable_column idiom and a persisted variant_element_state->variant. The branch's two
conditions are kept line for line and only gated with nullable_added_by_extraction,
which is semantically identical to master's inner_column form: when the flag is false
both trees leave the still-Nullable column with nested_serialization, and the later
insertManyDefaults/insertRangeFrom/insertFrom calls operate on the same column level.
wrapColumnForDeserialization does not exist on 26.7 (zero hits in the .cpp and the .h),
and #112338's only change to it was replacing three comment lines with three others, so
that hunk is dropped: no semantics are lost and no absent symbol is referenced. The
serialization pool does exist here, so the getHash update that threads the new flag
into the pooling key is kept, otherwise two serializations differing only in the flag
would collide.
Validated on this branch: the three-line reproducer segfaults on the official
26.7.8.5 build (rc=139, 3/3) and returns 1 here; 04652 and 04653 pass, both .reference
files byte-identical to master's blobs; forcing the flag to a constant false or true
reddens 04652 in either direction.
Related: #112338
Related: #119802
|
26.7 predates two master refactors, so 6 hunks in 3 files needed adapting: Validated on the resolved tree: the 3-line reproducer from #112338 segfaults on the official 26.7.8.5 build (rc=139, 3/3) and returns a value after the port; That is a strict fast-forward of |
|
🕵 Verified and pushed the resolved ref as a fast-forward of |
Original pull-request #112338
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job