Skip to content

Cherry pick #107095 to 26.5: Keep Dynamic invariant when inserting from shared variant - #115247

Merged
robot-ch-test-poll merged 6 commits into
backport/26.5/107095from
cherrypick/26.5/107095
Aug 18, 2026
Merged

robot-ch-test-poll merged 6 commits into
backport/26.5/107095from
cherrypick/26.5/107095

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 5 commits June 10, 2026 22:18
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
@robot-ch-test-poll3 robot-ch-test-poll3 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Aug 18, 2026
….5-107095

# Conflicts:
#	src/Columns/tests/gtest_column_dynamic.cpp
@groeneai

Copy link
Copy Markdown
Collaborator

Both cherry-picks conflict in exactly one hunk, and it is not in the fix.

Measured with git merge-tree --write-tree --merge-base=88b4aa61b3ff <tip> cf1fa302ba7a (rc read directly, not through a pipe):

branch conflicted files where
26.3 1 src/Columns/tests/gtest_column_dynamic.cpp
26.5 1 same

src/Columns/ColumnDynamic.cpp, which is the actual fix, merges clean on both. The merged file contains 5 getSharedVariant().empty() guards and 3 else if (addNewVariant(...)) arms, identical counts to master, with 0 conflict markers.

The single conflict is a context collision, not a semantic one. My 168 added test lines land immediately above checkInsertRangeFrom, and that function's signature differs between master and the release branches (static plus Names / UnorderedMapWithMemoryTracking on master, versus the plain form on the branch). So the anchor line ends up on both sides of the hunk. Resolving it means keeping the branch's form of that one line. I built that resolution locally for both branches: 0 conflict markers, all three new tests present, includes intact.

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. v26.4-must-backport produced nothing because 26.4 is not an active release branch. No prerequisite is missing: decodeDataType, ReadBufferFromMemory and getSharedVariant are all present on both branches.

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 cherrypick/26.3/107095 and cherrypick/26.5/107095 branches, or as separate PRs from my fork onto the backport bases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants