Repository navigation
Conversation
ColumnDynamic has an invariant: a logical type is stored either as a regular variant or inside the shared variant, never both. doInsertFrom and doInsertManyFrom broke this. When the source value lived in the source shared variant and the destination did not yet have that type as a regular variant, they always appended it to the destination shared variant, even when the destination still had room for a new regular variant. During a row-by-row gather (for example filling the non-joined rows of a RIGHT/FULL JOIN over a Dynamic column) the same type could then also be added as a regular variant for other rows, so it ended up in both storages. A Dynamic column in that state crashes function execution: in ExecutableFunctionDynamicAdaptor::executeImpl the shared-variant rows and the regular-variant rows of the duplicated type collide on a single selector slot, so the per-variant column size stops matching the scattered arguments and checkFunctionArgumentSizes raises "Expected the argument N to have X rows, but it has Y". This was found by the AST fuzzer (STID 2959-51fd) over a Dynamic(max_types=2) column with a multiIf whose else branch is a correlated EXISTS subquery (decorrelated into a join by the analyzer). Fix doInsertFrom and doInsertManyFrom to try addNewVariant first when the type coming from the source shared variant is not a regular variant yet, and only fall back to the shared variant when no regular variant can be added. This matches deserializeAndInsertFromArena, which already followed this logic. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-15:20260610-201800 |
|
Workflow [PR], commit [4582dad] Summary: ✅
AI ReviewSummaryThis PR keeps the Final VerdictStatus: ✅ Approve |
|
CI finished for HEAD
This PR touches only |
insertRangeFrom had the same invariant hole that doInsertFrom and doInsertManyFrom had. Its shared-variant source loop, in the path where the two Variant types are combined, copied a value coming from the source shared variant into our shared variant whenever the type was not yet a regular variant here, even when there was still room for a new regular variant. A later range copy could then add the same type as a regular variant, so the type ended up in both storages. That broken state crashes function execution over Dynamic: in ExecutableFunctionDynamicAdaptor::executeImpl the shared-variant rows and the regular-variant rows of the duplicated type collide on a single selector slot, the per-variant column size stops matching the scattered arguments and checkFunctionArgumentSizes raises "Expected the argument N to have X rows, but it has Y". This is reachable through the public range-copy path, not only through the row-by-row insert path fixed earlier (found by the AST fuzzer, STID 2959-51fd). Promote the new types from the source shared variant up front, while we still have room, before caching the destination column references (the promotion reallocates them and reassigns global discriminators). This matches doInsertFrom, doInsertManyFrom and deserializeAndInsertFromArena. The other shared-variant loop runs only when no more variants can be added, so it cannot create the duplicate and is left unchanged. Update InsertRangeFromOverflow7: with room available, types from the source shared variant are now promoted to regular variants, matching the row-by-row path. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (insertRangeFrom follow-up, commit afab1ab)
Session id: cron:clickhouse-worker-slot-10:20260611-051200 |
|
CI finished for HEAD |
Avogar
left a comment
There was a problem hiding this comment.
Is it possible to create an SQL test for that? I can suggest how to create a Dynamic column with reduced internal max_dynamic_paths and values in shared data:
create table test (id UInt64, d Dynamic) engine=MergeTree settings index_granularity=1, min_bytes_for_wide_part=100000000;
insert into test select number, NULL from numbers(10);
alter table test update d = 42::UInt64 where id > 4;
select dynamicType(d), isDynamicElementInSharedData(d), count() from test group by dynamicType(d), isDynamicElementInSharedData(d);
┌─dynamicType(d)─┬─isDynamicEle⋯aredData(d)─┬─count()─┐
1. │ UInt64 │ true │ 5 │
2. │ None │ false │ 5 │
└────────────────┴──────────────────────────┴─────────┘Maybe using this way we can craft an SQL test that will use insertFrom/insertRangeFrom on fresh Dynamic column from this one. If not, it's ok to keep unit test
Address review feedback: do the addNewVariant promotion inline in the existing combine loop via an extra else-if branch, mirroring doInsertFrom/doInsertManyFrom, instead of the separate pre-pass loop. This is safe because addNewVariant only reassigns global discriminators, not local ones (ColumnVariant::extend appends new variants at the end and remaps global_to_local). The cached local_discriminators, offsets, shared_variant and shared_variant_local_discr references therefore stay valid across a mid-loop addNewVariant, so the up-front shared_variant_discr re-fetch is no longer needed. Behavior is unchanged: the same source-shared types are promoted to regular variants while capacity remains, in the same row order, so the InsertRangeFromSharedVariantKeepsInvariant and InsertRangeFromOverflow7 unit tests still pass. Verified both directions: with the new branch disabled both tests fail (type ends up in both storages), with it they pass. Also simplify the comment at the three promotion sites. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The same-variants fast path at the top of insertFrom/insertRangeFrom/ insertManyFrom copied the source Variant column directly, including rows that live in the source shared variant. Two Dynamic columns can have the same variant layout but different max_dynamic_types, so a low-limit source can hold a type in its shared variant while a high-limit destination with the same layout still has room for it as a regular variant. The fast path left that type in the destination shared variant, and a later insert could then add the same type as a regular variant, leaving it in both storages and reproducing the ExecutableFunctionDynamicAdaptor row-count LOGICAL_ERROR. Skip the fast path when the source has values in its shared variant and the destination still has room, so those rows go through the combine path that promotes them via addNewVariant first. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (commit 4582dad)
Session id: cron:clickhouse-worker-slot-5:20260616-163500 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 136/138 (98.55%) | Lost baseline coverage: none · Uncovered code |
Cherry pick #107095 to 26.3: Keep Dynamic invariant when inserting from shared variant
Cherry pick #107095 to 26.5: Keep Dynamic invariant when inserting from shared variant
Backport #107095 to 26.5: Keep Dynamic invariant when inserting from shared variant
Backport #107095 to 26.3: Keep Dynamic invariant when inserting from shared variant
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
LOGICAL_ERROR(Expected the argument N to have X rows, but it has Y) when executing a function over aDynamiccolumn built by a JOIN overDynamic(for example the non-joined rows of aRIGHT/FULL JOIN, or a correlatedEXISTSsubquery decorrelated into a join).Description
ColumnDynamickeeps an invariant that a logical type is stored either as a regular variant or inside the shared variant, never both.doInsertFrom/doInsertManyFromcould break it: when a value coming from the source shared variant was inserted into a destination that still had room for a new regular variant, the type was always appended to the destination shared variant; during a row-by-row gather the same type could also become a regular variant for other rows, leaving it in both storages. Such a column then crashedExecutableFunctionDynamicAdaptor::executeImplwith the row-countLOGICAL_ERRORabove.The fix makes the shared-variant branch of
doInsertFrom/doInsertManyFromtryaddNewVariantfirst (falling back to the shared variant only when no regular variant can be added), matchingdeserializeAndInsertFromArena. Found by the AST fuzzer (STID 2959-51fd). Added aColumnDynamicunit test that reproduces the broken invariant through the public insert API.Version info
26.3.33.29