Skip to content

fix(cte): support UNION DISTINCT in recursive CTE - #26501

Merged
mergify[bot] merged 3 commits into
matrixorigin:mainfrom
ck89119:issue-25955-main
Aug 1, 2026
Merged

mergify[bot] merged 3 commits into
matrixorigin:mainfrom
ck89119:issue-25955-main

Conversation

@ck89119

@ck89119 ck89119 commented Jul 31, 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:

issue #25955

What this PR does / why we need it:

Recursive CTE binding previously treated only UNION ALL as recursive. A plain
UNION or explicit UNION DISTINCT was bound as a non-recursive CTE and
incorrectly rejected when its recursive member referenced the CTE.

This change:

  • recognizes UNION and UNION DISTINCT recursive members;
  • propagates the distinct mode through the plan to MergeCTE;
  • keeps a query-scoped full-row seen set across the anchor and every recursive
    iteration, so duplicate rows are not fed into later iterations and recursion
    terminates at the correct fixpoint;
  • clears the seen set on Reset and Free while leaving the UNION ALL path
    unchanged;
  • rejects mixed UNION ALL and UNION DISTINCT recursive-member chains whose
    left-associative semantics cannot be represented by the current execution
    topology;
  • adds planner, operator, and distributed regression coverage, including NULL,
    multi-column, cross-batch, reset, fixpoint termination, and UNION ALL
    control cases.

Validation:

  • make pb
  • focused planner and MergeCTE unit tests
  • full unit tests for pkg/sql/plan, pkg/sql/compile, and
    pkg/sql/colexec/mergecte
  • focused regression tests with -race -count=100
  • go vet for all modified Go packages
  • make
  • distributed BVT: 5/5 passed
  • modified-function coverage is at least 75%
  • git diff --check

The full-package race build was also attempted, but the local machine exhausted
disk space while compiling dependencies (no space left on device); no test
failure or race report was produced.

@mergify mergify Bot added the kind/bug Something isn't working label Jul 31, 2026
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Jul 31, 2026
@ck89119
ck89119 marked this pull request as ready for review July 31, 2026 09:09
@ck89119
ck89119 requested review from XuPeng-SH and aunjgr as code owners July 31, 2026 09:09
@ck89119
ck89119 requested a review from a user July 31, 2026 09:09
@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 →

@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 completed on eec58f8. Binder mode detection, mixed-mode rejection, protobuf/deep-copy propagation, MergeCTE full-row seen-set semantics (including NULLs and cross-batch duplicates), empty-frontier fixpoint behavior, and Reset/Free ownership are consistent. Local build/vet and non-race plan/compile/pipeline/operator tests passed; all 4 changed Go tests passed individually under race with computed repeat counts (7–8), followed by full mergecte and sql/plan race tests. CI reruns supersede the earlier cancelled/failed attempts and are successful.

@XuPeng-SH

Copy link
Copy Markdown
Contributor

@ck89119 conflict

# Conflicts:
#	pkg/sql/colexec/mergecte/mergecte.go
#	pkg/sql/colexec/mergecte/types.go
@XuPeng-SH

Copy link
Copy Markdown
Contributor

@ck89119 conflict

@mergify

mergify Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-01 06:13 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-08-01 06:13 UTC · at 1eb618a1f07b333c6f55208504b9c9bca38e1e5c · squash

This pull request spent 24 seconds in the queue, including 2 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 Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • 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

@mergify
mergify Bot merged commit 15a2d07 into matrixorigin:main Aug 1, 2026
26 of 27 checks passed
@mergify mergify Bot removed the queued label Aug 1, 2026
@ck89119
ck89119 deleted the issue-25955-main branch August 1, 2026 06:36
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Recursive CTE binding previously treated only `UNION ALL` as recursive. A plain
`UNION` or explicit `UNION DISTINCT` was bound as a non-recursive CTE and
incorrectly rejected when its recursive member referenced the CTE.

This change:

- recognizes `UNION` and `UNION DISTINCT` recursive members;
- propagates the distinct mode through the plan to `MergeCTE`;
- keeps a query-scoped full-row seen set across the anchor and every recursive
iteration, so duplicate rows are not fed into later iterations and recursion
terminates at the correct fixpoint;
- clears the seen set on `Reset` and `Free` while leaving the `UNION ALL` path
unchanged;
- rejects mixed `UNION ALL` and `UNION DISTINCT` recursive-member chains whose
left-associative semantics cannot be represented by the current execution
topology;
- adds planner, operator, and distributed regression coverage, including NULL,
multi-column, cross-batch, reset, fixpoint termination, and `UNION ALL`
control cases.

Validation:

- `make pb`
- focused planner and `MergeCTE` unit tests
- full unit tests for `pkg/sql/plan`, `pkg/sql/compile`, and
`pkg/sql/colexec/mergecte`
- focused regression tests with `-race -count=100`
- `go vet` for all modified Go packages
- `make`
- distributed BVT: 5/5 passed
- modified-function coverage is at least 75%
- `git diff --check`

The full-package race build was also attempted, but the local machine exhausted
disk space while compiling dependencies (`no space left on device`); no test
failure or race report was produced.

Approved by: @XuPeng-SH, @heni02
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Recursive CTE binding previously treated only `UNION ALL` as recursive. A plain
`UNION` or explicit `UNION DISTINCT` was bound as a non-recursive CTE and
incorrectly rejected when its recursive member referenced the CTE.

This change:

- recognizes `UNION` and `UNION DISTINCT` recursive members;
- propagates the distinct mode through the plan to `MergeCTE`;
- keeps a query-scoped full-row seen set across the anchor and every recursive
iteration, so duplicate rows are not fed into later iterations and recursion
terminates at the correct fixpoint;
- clears the seen set on `Reset` and `Free` while leaving the `UNION ALL` path
unchanged;
- rejects mixed `UNION ALL` and `UNION DISTINCT` recursive-member chains whose
left-associative semantics cannot be represented by the current execution
topology;
- adds planner, operator, and distributed regression coverage, including NULL,
multi-column, cross-batch, reset, fixpoint termination, and `UNION ALL`
control cases.

Validation:

- `make pb`
- focused planner and `MergeCTE` unit tests
- full unit tests for `pkg/sql/plan`, `pkg/sql/compile`, and
`pkg/sql/colexec/mergecte`
- focused regression tests with `-race -count=100`
- `go vet` for all modified Go packages
- `make`
- distributed BVT: 5/5 passed
- modified-function coverage is at least 75%
- `git diff --check`

The full-package race build was also attempted, but the local machine exhausted
disk space while compiling dependencies (`no space left on device`); no test
failure or race report was produced.

Approved by: @XuPeng-SH, @heni02
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Recursive CTE binding previously treated only `UNION ALL` as recursive. A plain
`UNION` or explicit `UNION DISTINCT` was bound as a non-recursive CTE and
incorrectly rejected when its recursive member referenced the CTE.

This change:

- recognizes `UNION` and `UNION DISTINCT` recursive members;
- propagates the distinct mode through the plan to `MergeCTE`;
- keeps a query-scoped full-row seen set across the anchor and every recursive
iteration, so duplicate rows are not fed into later iterations and recursion
terminates at the correct fixpoint;
- clears the seen set on `Reset` and `Free` while leaving the `UNION ALL` path
unchanged;
- rejects mixed `UNION ALL` and `UNION DISTINCT` recursive-member chains whose
left-associative semantics cannot be represented by the current execution
topology;
- adds planner, operator, and distributed regression coverage, including NULL,
multi-column, cross-batch, reset, fixpoint termination, and `UNION ALL`
control cases.

Validation:

- `make pb`
- focused planner and `MergeCTE` unit tests
- full unit tests for `pkg/sql/plan`, `pkg/sql/compile`, and
`pkg/sql/colexec/mergecte`
- focused regression tests with `-race -count=100`
- `go vet` for all modified Go packages
- `make`
- distributed BVT: 5/5 passed
- modified-function coverage is at least 75%
- `git diff --check`

The full-package race build was also attempted, but the local machine exhausted
disk space while compiling dependencies (`no space left on device`); no test
failure or race report was produced.

Approved by: @XuPeng-SH, @heni02

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.

3 participants