Skip to content

vtgate: bind recursive CTE column filters as arguments in unmerged term queries - #20631

Draft
GrahamCampbell wants to merge 3 commits into
vitessio:mainfrom
GrahamCampbell:recursive-cte-term-filter-arguments
Draft

GrahamCampbell wants to merge 3 commits into
vitessio:mainfrom
GrahamCampbell:recursive-cte-term-filter-arguments

Conversation

@GrahamCampbell

@GrahamCampbell GrahamCampbell commented Jul 19, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When a recursive CTE does not merge into a single route, WHERE predicates in the recursive term that reference CTE columns are never rewritten to per iteration bind variables. Filters over CTE columns alone are emitted as references to a table that does not exist in the term query, so every execution fails with an unknown column error. Predicates mixing CTE and term table columns fail the same way, including through comma join linkage, and OR predicates spanning both sides are silently dropped, which returns wrong results. A column referenced only in the WHERE clause is also never registered in the RecurseCTE primitive's join variables.

The term's WHERE path now binds these predicates at translation time, exactly as the join ON path always has. They become reserved arguments supplied per iteration, the referenced columns are registered in the join variables, and the tracked original form is restored when the whole CTE later merges into a single route, where the column reference is correct inside the pushed WITH RECURSIVE. Routes gain the same binding as a safety net for predicates arriving through other paths. Offset planning and the projection path both resolve recursion columns positionally through a declared column list; each previously failed with an internal error, the former reachable through the ON clause and the latter whenever the outer query selected a declared name. A declared list that reaches this resolution is guaranteed well formed by #20633, since only seed projections with a statically known cardinality plan this far.

Eight planner cases pin the behavior, covering the plain filter, the comma join, mixed, and OR forms, a filter on a column absent from the term select list, a declared column list selected through its declared names, a declared list whose names swap the seed aliases, and a fully merged CTE keeping its legal column form. Two end to end tests walk a chain of rows across two shards and compare results against MySQL, one through seed names and one through a declared column list, with the data sized so a dropped filter changes the result. Predicates containing subqueries settle after term translation and are not covered by this change.

Related Issue(s)

This PR depends on #20633 being merged first. It is stacked directly on that change, so the branch also carries #20759 and #20633 beneath it.

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

Recursive CTEs whose terms filter on CTE columns previously failed with unknown column errors or silently dropped those filters unless the whole CTE merged into a single route. Direct term predicates are now bound per iteration, and selecting the declared column names of such a CTE previously failed with an internal error and now works. Predicates containing subqueries remain affected in the unmerged form. No migrations or configuration changes.

AI Disclosure

This PR was written primarily by Fable with review from GPT 5.6 Sol.


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"]
        PR21205["#21205<br>evalengine, mysql/json: fix JSON and TIME equality in compiled and interpreted paths"]
        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
        PR21205 --> 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 (merged)<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

Copilot AI balanced review requested due to automatic review settings July 19, 2026 15:31

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.

@github-actions github-actions Bot added this to the v25.0.0 milestone Jul 19, 2026
@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 19, 2026
@vitess-bot

vitess-bot Bot commented Jul 19, 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.

@GrahamCampbell
GrahamCampbell marked this pull request as draft July 19, 2026 15:37
Copilot AI review requested due to automatic review settings July 19, 2026 16:28
@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from 9543ee5 to 290264a Compare July 19, 2026 16:28

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.

@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from 290264a to 40f4d18 Compare July 19, 2026 16:35
Copilot AI review requested due to automatic review settings July 19, 2026 16:35

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.

Copilot AI review requested due to automatic review settings July 19, 2026 16:35

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.

Copilot AI review requested due to automatic review settings July 19, 2026 17:05
@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from e37e61c to e4de51a Compare July 19, 2026 17:05

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.

@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from e4de51a to 0541e46 Compare July 19, 2026 17:07
Copilot AI review requested due to automatic review settings July 19, 2026 17:07

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.

@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from 0541e46 to b9d9685 Compare July 25, 2026 12:00
Copilot AI review requested due to automatic review settings July 29, 2026 23:20

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.

@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from 6efd084 to e3bd164 Compare August 22, 2026 10:17
Copilot AI review requested due to automatic review settings August 22, 2026 10:17

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.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@codecov

codecov Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.22078% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 34.04%. Comparing base (70c7a72) to head (186e350).
⚠️ Report is 597 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vtgate/semantics/cte_table.go 50.00% 7 Missing ⚠️
go/vt/vtgate/planbuilder/operators/recurse_cte.go 0.00% 4 Missing ⚠️
go/vt/vtgate/planbuilder/operators/ast_to_op.go 0.00% 2 Missing ⚠️
go/vt/vtgate/planbuilder/operators/route.go 0.00% 2 Missing ⚠️
go/vt/vtgate/semantics/table_collector.go 98.07% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (70c7a72) and HEAD (186e350). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (70c7a72) HEAD (186e350)
1 0
Additional details and impacted files
@@             Coverage Diff             @@
##             main   #20631       +/-   ##
===========================================
- Coverage   69.67%   34.04%   -35.64%     
===========================================
  Files        1614       81     -1533     
  Lines      216793    12829   -203964     
===========================================
- Hits       151044     4367   -146677     
+ Misses      65749     8462    -57287     
Flag Coverage Δ
partial 34.04% <79.22%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from e3bd164 to 9e78376 Compare August 22, 2026 12:50
Copilot AI review requested due to automatic review settings August 22, 2026 12:50

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.

Copilot AI review requested due to automatic review settings August 22, 2026 13:58
@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from 9e78376 to e90f4bb Compare August 22, 2026 13:58

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.

Copilot AI review requested due to automatic review settings August 24, 2026 12:25
@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from e90f4bb to c10f2be Compare August 24, 2026 12:25

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.

@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch 2 times, most recently from 007651b to b736d13 Compare August 28, 2026 18:22
Copilot AI review requested due to automatic review settings August 28, 2026 18:22

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.

@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from b736d13 to 9c60b88 Compare September 11, 2026 16:09
Copilot AI review requested due to automatic review settings September 11, 2026 16:09

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.

🔵 Needs a closer look

The stacked, cross-cutting semantic and planner changes require final human review and successful CI validation.

Review details
  • Files reviewed: 12/12 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from 9c60b88 to e40c6e1 Compare September 24, 2026 00:52
Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
…tables

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

Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
@GrahamCampbell
GrahamCampbell force-pushed the recursive-cte-term-filter-arguments branch from e40c6e1 to 186e350 Compare September 24, 2026 09:39

This branch has not been deployed

No deployments
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.

2 participants