Repository navigation
Cherry pick #107095 to 26.5: Keep Dynamic invariant when inserting from shared variant - #115247
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]>
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]>
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]>
…ariant-insert Keep Dynamic invariant when inserting from shared variant
….5-107095 # Conflicts: # src/Columns/tests/gtest_column_dynamic.cpp
|
Both cherry-picks conflict in exactly one hunk, and it is not in the fix. Measured with
The single conflict is a context collision, not a semantic one. My 168 added test lines land immediately above Both targets are genuinely unfixed, so this is not a vacuous port: 26.3 and 26.5 each still have 3 unguarded same-variant fast paths and 0 guards, while 26.6 and 26.7 already contain the merge commit as an ancestor, which is why only two cherry-picks exist for three labels. The label floor is v26.3, so 25.8 is out of scope by design. The same one-line resolution applies there if the LTS is wanted. @Avogar the conflicts are inactivity-closed after 7 days. I am happy to push the resolution. Do you want it on the |
Original pull-request #107095
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.
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