Repository navigation
Cherry pick #107095 to 26.3: Keep Dynamic invariant when inserting from shared variant - #115246
Merged
Merged
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
….3-107095 # Conflicts: # src/Columns/tests/gtest_column_dynamic.cpp
Collaborator
|
Same single-hunk conflict as the 26.5 sibling, and the same one-line resolution. Analysis and the decision question are on #115247 (comment) to keep it in one place. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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