Skip to content

vtgate: merge empty reference branches through rewritten copies - #20756

Draft
GrahamCampbell wants to merge 5 commits into
vitessio:mainfrom
GrahamCampbell:union-none-reference-rewrite
Draft

vtgate: merge empty reference branches through rewritten copies#20756
GrahamCampbell wants to merge 5 commits into
vitessio:mainfrom
GrahamCampbell:union-none-reference-rewrite

Conversation

@GrahamCampbell

@GrahamCampbell GrahamCampbell commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Description

Statically empty reference branches used to merge blindly on main: select id from source_of_ref where id in (null) union select id from user planned SQL that named source_of_ref on user shards, where the table is physically ref_with_source, and failed at runtime with an unknown-table error. #20629 made those pairings decline instead, which is correct but plans a Concatenate where a single route used to suffice.

This change restores the merge, correctly. When the none side's keyspace does not match the other side's, every real table under the empty branch is resolved to its reference copy in the target keyspace — the table itself, a ReferencedBy copy, or a reference source — through the same lookup that builds alternate routes, so routing rules stay in effect and a copy that a rule sends elsewhere does not count. The branch's planned operators are then pointed at those copies in place with the original names kept as aliases, and the union merges into the other side's routing. The rewrite mechanism comes from #20755, which fixes the panic in the live-branch equivalent of this merge.

Three planner cases pin the restored merges: the same-name copy, the differing physical name (now rewritten where main used to emit the broken name), and the reversed order. Branches whose tables have no copy in the target keyspace keep planning as separate routes, pinned by the existing cross-keyspace cases, and the information_schema guard from #20629 still precedes the rewrite.

Related Issue(s)

Closes #20754 together with #20755. Builds on #20628, #20629, #20630, and #20755.

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Unions with a statically empty reference-table branch and a branch in a keyspace holding a copy of its tables now plan as a single merged route using the copy's physical name. 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
Loading

…kip them

Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
Copilot AI review requested due to automatic review settings July 29, 2026 19:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vitess-bot

vitess-bot Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot Bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jul 29, 2026
@GrahamCampbell
GrahamCampbell force-pushed the union-none-reference-rewrite branch from 3a8062c to fe1b8f8 Compare July 29, 2026 19:44
Copilot AI review requested due to automatic review settings July 29, 2026 19:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
… agree

Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
@GrahamCampbell
GrahamCampbell force-pushed the union-none-reference-rewrite branch from fe1b8f8 to 65f76b7 Compare July 29, 2026 22:42
Copilot AI review requested due to automatic review settings July 29, 2026 22:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

Component: VTGate NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: union merging through reference-table alternates panics with VT13001

2 participants