Repository navigation
Fix in-place mutation of shared Variant subcolumn when converting Variant to a column - #107374
Conversation
…iant to a column
When a function is applied to a Variant column that contains a single non-empty
variant plus NULLs, both ConvertImplFromVariantToColumn::execute (the toString /
CAST-to-column path) and FunctionVariantAdaptor::executeImpl filter the variant to
its non-NULL rows, run the function, then expand the result back to the original
size, filling the filtered-out rows with defaults/NULLs.
The expand was done via nested_result->assumeMutable()->expand(filter, false).
assumeMutable() is a const_cast that does not clone shared columns. When the nested
function returns one of its input columns unchanged (e.g. toString of a String
variant, or concat of a single String argument returns that argument), the result
aliases the input Variant's subcolumn, which is still referenced by the source
ColumnVariant (use_count > 1). Expanding it in place grows that shared subcolumn,
so the source Variant ends up with a subcolumn larger than the number of
discriminators pointing at it. The next operation that validates the Variant
(arraySort -> ColumnVariant::permute, native serialization via blockSerializedSize,
DISTINCT/PREWHERE filtering, ...) then aborts with
"Variant N (T) has size X, but expected Y".
Fix: replace assumeMutable()->expand() with IColumn::mutate()->expand(), which
clones the column first when it is shared, so the in-place expand never touches a
column still owned by the source Variant. Applied to all four expand-after-filter
sites (one in ConvertImplFromVariantToColumn, three in FunctionVariantAdaptor).
Reproducers (all aborted before the fix):
SELECT arraySort(x -> toString(x), [NULL, CAST('hi', 'Variant(Int32, String)')]);
SELECT toString(v), blockSerializedSize(v) FROM (single-variant-plus-NULL column);
SELECT concat(v), blockSerializedSize(v) FROM (single-variant-plus-NULL column);
Found by the msan function_prop_fuzzer (FunctionsStress) and the AST fuzzer.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-0:20260612-230500 |
|
cc @Avogar @rschu1ze - could you review this? It fixes a |
|
Workflow [PR], commit [8e3b04f] Summary: ✅
AI ReviewSummaryThis PR fixes a Final VerdictStatus: ✅ Approve |
FunctionDynamicAdaptor has the same aliasing bug as FunctionVariantAdaptor: Dynamic is backed by a ColumnVariant, and the single-type-plus-NULL fast path expanded the (possibly shared) nested function result in place via assumeMutable()->expand(). When the nested function returns an input column unchanged (e.g. concat over one Dynamic argument returns the String subcolumn), the result still aliases a subcolumn of the source Dynamic's backing ColumnVariant; expanding it in place grows that shared subcolumn past its discriminator count, leaving the source ColumnVariant malformed. A following operation that re-validates it (here blockSerializedSize during Dynamic statistics serialization) then aborts with "Variant N (T) has size X, but expected Y". Route both expand-after-filter sites through expandColumnByFilter, which uses IColumn::mutate (clone-if-shared) instead of assumeMutable (const_cast that never clones), mirroring the FunctionVariantAdaptor fix. Extend 04337_variant_to_column_inplace_mutation with Dynamic regressions covering the adaptor const-array and table-backed paths. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (Dynamic adaptor extension)
Session id: cron:clickhouse-worker-slot-4:20260613-090000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 24/24 (100.00%) | Lost baseline coverage: none · Uncovered code |
| if (!isDynamic(result_type)) | ||
| { | ||
| /// Expand filtered result. If it's already Nullable, it will be filled with NULLs. | ||
| nested_result->assumeMutable()->expand(filter, false); |
There was a problem hiding this comment.
This is a very good change. Because assumeMutable should never be used in normal code. CC @Avogar
CI summary — head
|
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
LOGICAL_ERROR(Variant N (T) has size X, but expected Y) when a function such astoStringorconcatis applied to aVariantorDynamiccolumn that holds a single non-empty variant together with NULLs, and the function returns its input column unchanged.Description
Related: #101415
Found by the msan
function_prop_fuzzer(FunctionsStress) and the AST fuzzer. Related to the sameColumnVariant::validateStateerror family as #101415, but a distinct code path (no join, no runtime filter).Root cause
When a function is applied to a
Variantcolumn that contains a single non-empty variant plus NULLs, bothConvertImplFromVariantToColumn::execute(thetoString/ CAST-to-column path) andFunctionVariantAdaptor::executeImplfollow the same pattern: filter the variant to its non-NULL rows, run the function on the compact subcolumn, then expand the result back to the original size, filling the filtered-out rows with defaults/NULLs.FunctionDynamicAdaptor::executeImplhas the same fast path, sinceDynamicis backed by aColumnVariant.The expand was done with
nested_result->assumeMutable()->expand(filter, false).assumeMutable()is aconst_castthat does not clone shared columns. When the nested function returns one of its input columns unchanged (toStringof aStringvariant, orconcatof a singleStringargument both return that argument), the result aliases the inputVariant's subcolumn, which is still referenced by the sourceColumnVariant(use_count > 1). Expanding it in place grows that shared subcolumn, so the sourceVariantends up with a subcolumn larger than the number of discriminators pointing at it. The next operation that validates the variant (arraySort->ColumnVariant::permute, native serialization viablockSerializedSize,DISTINCT/PREWHEREfiltering) then aborts inColumnVariant::validateState.Fix
Replace
assumeMutable()->expand()withIColumn::mutate()->expand(), which clones the column first when it is shared, so the in-place expand never touches a column still owned by the sourceVariant. Applied to all six expand-after-filter sites: one inConvertImplFromVariantToColumn, three inFunctionVariantAdaptor, and two inFunctionDynamicAdaptor.Reproducers (all aborted before the fix)
Regression test:
tests/queries/0_stateless/04337_variant_to_column_inplace_mutation.sql.CI signal:
Unit tests (msan, function_prop_fuzzer)-> FunctionsStress, e.g. report https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107246&sha=d470f64aa201323633aa44d3d78ef31b74356954&name_0=PR&name_1=Unit%20tests%20%28msan%2C%20function_prop_fuzzer%29Version info
26.6.1.758