vtgate: fix union merging through reference-table alternates - #20755
Open
GrahamCampbell wants to merge 1 commit into
Open
vtgate: fix union merging through reference-table alternates#20755GrahamCampbell wants to merge 1 commit into
GrahamCampbell wants to merge 1 commit into
Conversation
GrahamCampbell
requested review from
arthurschreiber,
frouioui,
harshit-gangal and
systay
as code owners
July 29, 2026 19:29
Contributor
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
This was referenced Jul 29, 2026
GrahamCampbell
force-pushed
the
union-reference-alternate-merging
branch
from
July 29, 2026 19:43
cf59812 to
04337dd
Compare
Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
GrahamCampbell
force-pushed
the
union-reference-alternate-merging
branch
from
July 29, 2026 22:32
04337dd to
85b309b
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 85b309b2fb
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
This was referenced Jul 29, 2026
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.
Description
A UNION DISTINCT between a reference table and a table in the keyspace holding one of its copies panics with
VT13001: [BUG] did not expect this method to be called. When the two sides' keyspaces differ, the planner makes them mergeable by swapping in the alternate route prebuilt at route-creation time, but unions merge later, during horizon planning, and the prebuilt route still holds the unplanned bare table it was created with. As soon as the union pushes its result columns into that source — the weight-string columns DISTINCT needs — the bare table operator panics inTable.AddColumn. UNION ALL survives because it pushes nothing.The fix stops using the prebuilt alternate as the merge payload. A declined cross-keyspace pairing is retried by resolving every real table under the moved side to its copy in the other side's keyspace through the VSchema — the table itself, a ReferencedBy copy, or a reference source, so routing rules stay in effect — and pointing the planned operators at those copies in place, undoing the move when the retry still cannot merge. Predicates and projections pushed after route creation are preserved because the operators are kept, physical renames keep the original name as an alias so column references still resolve, and later column pushes land on operators the planner has analyzed. A side already composed of a merged union is not moved: the remaining phases no longer normalize the shape that produces, so composite pairings stay split for now. Join and subquery merging keep the prebuilt-route mechanism: they merge during physical planning, when every route source is still a bare table, and that path is covered and correct.
Nine planner cases in reference_cases.json cover both merge orders, the differing-physical-name rewrite, predicate preservation through the rewrite, the composite pairings that stay split, a no-copy control, and pin the already-working UNION ALL path; the four single-table DISTINCT cases fail on main with the panic. The reference-copy cases are skip_e2e like the rest of that file's, since the e2e fixture does not define the copy tables. Recursive CTE merging shares the swap through prepareInputRoutes and likely has the same latent panic, left for a follow-up. This is part one of the fix for #20754, and since the panic exists in released versions it is probably worth backporting.
Related Issue(s)
Part one of #20754.
Checklist
Deployment Notes
UNION DISTINCT queries pairing a reference table with a table in a keyspace holding one of its copies previously failed to plan with an internal error; they now plan as a single merged route. No migrations or configuration changes.
AI Disclosure
This PR was written primarily by Fable.
graph LR subgraph json [JSON support] direction TB PR20625["#20625 (merged)<br>mysql/json: fix MarshalTo discarding accumulated output for nested blob and bit values"] PR20632["#20632<br>vtgate: preserve IN value lists in complex aggregate projections"] PRA["#20691<br>evalengine: disable the static IN hash table for JSON operands"] PR20682["#20682 (draft)<br>evalengine: support constant-folded JSON values as literals"] PR20683["#20683 (draft)<br>evalengine, sqlparser: MySQL comparison domains; nested BETWEEN parentheses"] PR20626["#20626 (draft)<br>vtgate: support cross-shard JSON_ARRAYAGG and JSON_OBJECTAGG"] PR20625 --> PR20682 PRA --> PR20682 PR20682 --> PR20683 PR20632 --> PR20626 PR20683 --> PR20626 end subgraph union [Union routing] direction TB PR20628["#20628<br>vtgate: track per-source copies of pushed join predicates so merges skip them"] PR20629["#20629<br>vtgate: fix None routing handling when merging unions"] PR20630["#20630 (draft)<br>vtgate: merge unions on join-predicate-free routings when all sources agree"] PR20755["#20755<br>vtgate: fix union merging through reference-table alternates"] PR20756["#20756 (draft)<br>vtgate: merge empty reference branches through rewritten copies"] PR20628 --> PR20630 PR20629 --> PR20630 PR20630 --> PR20756 PR20755 --> PR20756 end subgraph cte [Recursive CTEs] direction TB PR20759["#20759<br>vtgate: expand qualified stars without JOIN USING coalescing"] PR20633["#20633 (draft)<br>vtgate: validate the declared column list length of CTEs and derived tables"] PR20631["#20631 (draft)<br>vtgate: bind recursive CTE column filters as arguments in unmerged term queries"] PR20759 --> PR20633 PR20633 --> PR20631 end json ~~~ union ~~~ cte