Skip to content

fix(plan): reject nested recursive CTE references - #27326

Merged
mergify[bot] merged 5 commits into
matrixorigin:mainfrom
ck89119:issue-25958-main
Aug 20, 2026
Merged

mergify[bot] merged 5 commits into
matrixorigin:mainfrom
ck89119:issue-25958-main

Conversation

@ck89119

@ck89119 ck89119 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #25958

What this PR does / why we need it:

Recursive CTE references inside nested query blocks reused the recursive member's scan node without validating query-block ownership. As a result, unsupported recursive references in EXISTS or scalar subqueries could pass planning and later panic or hang during execution.

This change tracks the SELECT query block that owns the recursive scan and rejects recursive table sources from nested SELECT blocks with the existing parse error. Structural FROM contexts, such as JOIN operands, continue to inherit the owning query block, and correlated subqueries that do not introduce another recursive source remain valid.

Regression coverage includes planner unit tests for EXISTS and scalar subqueries, legal JOIN and correlated-subquery controls, and a distributed SQL case that verifies stable errors and service availability after each rejection.

Validation:

  • make build
  • make static-check
  • CGo-configured go build and go vet for ./pkg/sql/plan
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=300s ./pkg/sql/plan
  • .agents/skills/mo-dev/scripts/mo-cgo-test -race -v -count=1 -timeout=240s -run '^TestRecursiveCTEQueryBlockReferenceScope$' ./pkg/sql/plan
  • mo-tester regression: 5/5 statements passed

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@aunjgr aunjgr 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.

Reviewed exact head 8492495. The earlier nested-recursive stack-overflow path is closed by resolving the exact active CTE state across the BindContext parent chain instead of consulting only the replaceable current state. SELECT query-block identity is established at bindSelect, structural FROM and JOIN contexts inherit it, and nested query blocks are rejected before recursive expansion. Tests cover EXISTS, scalar, and nested-recursive failures plus legal JOIN, correlation-only, and independent nested-recursion controls, with a public SQL service-health regression. No blocking findings.

@XuPeng-SH XuPeng-SH 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.

Deep review of exact head 8492495770e2dc02353f6a02c69be70ee11fdfc9: APPROVED.

The previous nested-recursive stack-overflow blocker is closed. Recursive scan reuse now resolves the exact active CTE state across the BindContext parent chain, so replacing the current state with a nested recursive CTE cannot hide an still-active outer owner. SELECT query-block identity changes at nested SELECT boundaries while structural FROM and JOIN contexts inherit it, which rejects nested recursive sources before expansion without rejecting legal top-level joins or correlation-only subqueries.

The deterministic nested-WITH reproducer now returns the normal recursive-table validation error, and the controls cover legal nested recursion and recursive scan counts. Focused controlled-CGo TestRecursiveCTEQueryBlockReferenceScope passes on this exact head. I found no blocking issue in the complete PR diff.

@ck89119
ck89119 requested a review from aptend August 20, 2026 09:31

@aptend aptend 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.

Deep re-review completed against exact head 8492495. The previous nested-recursive stack-overflow blocker is closed: active recursive ownership is resolved by CTERef identity through the BindContext parent chain, while queryBlockOwner changes at nested SELECT boundaries and is inherited by structural FROM/JOIN contexts. I also exercised additional IN-subquery, nested non-recursive CTE, three-level recursive nesting, parenthesized JOIN, and shadowing controls; invalid recursive sources were rejected and valid same-block/shadowing cases planned successfully. Full pkg/sql/plan tests, the focused race test, go vet, and diff check passed with the required CGO/thirdparties setup. No blocking findings.

@mergify mergify Bot added the queued label Aug 20, 2026
@mergify

mergify Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-20 11:23 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-08-20 17:44 UTC · at 63e5b553ce8fbeee9bfd82aa7caed7158364655c · squash

This pull request spent 6 hours 20 minutes 52 seconds in the queue, including 44 minutes 34 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

@mergify
mergify Bot merged commit 9ec5ce0 into matrixorigin:main Aug 20, 2026
26 of 27 checks passed
@mergify mergify Bot removed the queued label Aug 20, 2026
@ck89119
ck89119 deleted the issue-25958-main branch August 21, 2026 03:23
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Recursive CTE references inside nested query blocks reused the recursive member's scan node without validating query-block ownership. As a result, unsupported recursive references in EXISTS or scalar subqueries could pass planning and later panic or hang during execution.

This change tracks the SELECT query block that owns the recursive scan and rejects recursive table sources from nested SELECT blocks with the existing parse error. Structural FROM contexts, such as JOIN operands, continue to inherit the owning query block, and correlated subqueries that do not introduce another recursive source remain valid.

Regression coverage includes planner unit tests for EXISTS and scalar subqueries, legal JOIN and correlated-subquery controls, and a distributed SQL case that verifies stable errors and service availability after each rejection.

Validation:

- `make build`
- `make static-check`
- CGo-configured `go build` and `go vet` for `./pkg/sql/plan`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=300s ./pkg/sql/plan`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race -v -count=1 -timeout=240s -run '^TestRecursiveCTEQueryBlockReferenceScope$' ./pkg/sql/plan`
- mo-tester regression: 5/5 statements passed

Approved by: @XuPeng-SH, @aptend, @aunjgr
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Recursive CTE references inside nested query blocks reused the recursive member's scan node without validating query-block ownership. As a result, unsupported recursive references in EXISTS or scalar subqueries could pass planning and later panic or hang during execution.

This change tracks the SELECT query block that owns the recursive scan and rejects recursive table sources from nested SELECT blocks with the existing parse error. Structural FROM contexts, such as JOIN operands, continue to inherit the owning query block, and correlated subqueries that do not introduce another recursive source remain valid.

Regression coverage includes planner unit tests for EXISTS and scalar subqueries, legal JOIN and correlated-subquery controls, and a distributed SQL case that verifies stable errors and service availability after each rejection.

Validation:

- `make build`
- `make static-check`
- CGo-configured `go build` and `go vet` for `./pkg/sql/plan`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=300s ./pkg/sql/plan`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race -v -count=1 -timeout=240s -run '^TestRecursiveCTEQueryBlockReferenceScope$' ./pkg/sql/plan`
- mo-tester regression: 5/5 statements passed

Approved by: @XuPeng-SH, @aptend, @aunjgr
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Recursive CTE references inside nested query blocks reused the recursive member's scan node without validating query-block ownership. As a result, unsupported recursive references in EXISTS or scalar subqueries could pass planning and later panic or hang during execution.

This change tracks the SELECT query block that owns the recursive scan and rejects recursive table sources from nested SELECT blocks with the existing parse error. Structural FROM contexts, such as JOIN operands, continue to inherit the owning query block, and correlated subqueries that do not introduce another recursive source remain valid.

Regression coverage includes planner unit tests for EXISTS and scalar subqueries, legal JOIN and correlated-subquery controls, and a distributed SQL case that verifies stable errors and service availability after each rejection.

Validation:

- `make build`
- `make static-check`
- CGo-configured `go build` and `go vet` for `./pkg/sql/plan`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=300s ./pkg/sql/plan`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race -v -count=1 -timeout=240s -run '^TestRecursiveCTEQueryBlockReferenceScope$' ./pkg/sql/plan`
- mo-tester regression: 5/5 statements passed

Approved by: @XuPeng-SH, @aptend, @aunjgr

This branch was previously deployed

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

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Recursive CTE self-reference in a subquery returns an internal planner error

5 participants