Skip to content

fix(compile): schedule union all branches lazily - #27009

Merged
XuPeng-SH merged 10 commits into
matrixorigin:mainfrom
aunjgr:fix/26066-lazy-union-all
Aug 12, 2026
Merged

XuPeng-SH merged 10 commits into
matrixorigin:mainfrom
aunjgr:fix/26066-lazy-union-all

Conversation

@aunjgr

@aunjgr aunjgr commented Aug 12, 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 #26066

What this PR does / why we need it:

Compiles UNION ALL with sequential branch activation only when a streaming LIMIT can short-circuit it. The demand is propagated before physical scopes are built, so ordinary UNION ALL keeps its original concurrent topology and pays no extra connector, spool, merge, channel, or goroutine hop.

An eligible UNION ALL starts only its first logical branch while retaining that branch's internal parallelism. Directly nested UNION ALL chains are absorbed into one scheduler; this prevents a buffered inner scheduler from starting a later leaf before the outer LIMIT can stop consumption. Semantic boundaries such as ORDER BY, window, assertion/barrier filters, and user-level lock side effects stop demand propagation, and an inner LIMIT remains intact.

On early LIMIT stop or cancellation, unstarted branches have no goroutine, reader, remote notification, or terminal signal to wait for. Cleanup drains only active receivers. Startup and cancellation races still deliver a terminal signal for every activated receiver, and execution-generation callbacks are cleared before reuse.

Coverage includes unchanged non-LIMIT topology, demand propagation and blocking boundaries, nested chain flattening and ANALYZE attribution, inner-LIMIT preservation, first-branch satisfaction, empty-branch advancement, full exhaustion, cancellation between receivers, startup failure, missing callbacks, active-only cleanup, and prepared-generation reuse.

Tests:

  • .agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/sql/colexec/unionall ./pkg/sql/colexec/merge ./pkg/sql/compile -count=1 -timeout=10m
  • .agents/skills/mo-dev/scripts/mo-cgo-test -race ./pkg/sql/colexec/unionall ./pkg/sql/compile -run '^(TestSequentialUnionAll.*|TestCompileUnionAll.*|TestStreamingUnionAll.*|TestCompilePlanScope.*|TestLazyUnionAll.*|TestLazyNestedUnionAll.*)$' -count=1 -timeout=5m
  • .agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/sql/colexec/unionall ./pkg/sql/compile -run '^(TestSequentialUnionAll.*|TestLazyUnionAll.*|TestLazyNestedUnionAll.*)$' -count=100 -timeout=2m

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

Reviewed the complete diff at ba5d758. Lazy admission is gated by streaming demand and semantic barriers, preserves ordinary UNION ALL topology, activates receivers before producers, and closes startup/error/cancel/early-limit/reuse cleanup paths without waiting on unstarted branches. Focused unionall/compile tests pass locally; CI is green.

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

mergify Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-12 13:25 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-08-12 13:35 UTC · at eee7c0a250acff82272ece5e4bbcfc0da3082d0e

This pull request spent 9 minutes 37 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable "Allow edits from maintainers" on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

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

Reviewed the complete diff at eee7c0a. Lazy admission is limited to streaming LIMIT demand across non-blocking nodes; ordinary UNION ALL keeps the concurrent topology, while nested eligible chains preserve branch order and internal parallelism. The receiver-before-producer handoff and active-only wait accounting close early-stop, cancellation, submission-failure, terminal-signal, and prepared-reuse paths without waiting on unstarted branches. Full affected-package tests, focused race tests, and 100x lifecycle stress tests pass locally.

VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26066

## What this PR does / why we need it:

Compiles UNION ALL with sequential branch activation only when a
streaming LIMIT can short-circuit it. The demand is propagated before
physical scopes are built, so ordinary UNION ALL keeps its original
concurrent topology and pays no extra connector, spool, merge, channel,
or goroutine hop.

An eligible UNION ALL starts only its first logical branch while
retaining that branch's internal parallelism. Directly nested UNION ALL
chains are absorbed into one scheduler; this prevents a buffered inner
scheduler from starting a later leaf before the outer LIMIT can stop
consumption. Semantic boundaries such as ORDER BY, window,
assertion/barrier filters, and user-level lock side effects stop demand
propagation, and an inner LIMIT remains intact.

On early LIMIT stop or cancellation, unstarted branches have no
goroutine, reader, remote notification, or terminal signal to wait for.
Cleanup drains only active receivers. Startup and cancellation races
still deliver a terminal signal for every activated receiver, and
execution-generation callbacks are cleared before reuse.

Coverage includes unchanged non-LIMIT topology, demand propagation and
blocking boundaries, nested chain flattening and ANALYZE attribution,
inner-LIMIT preservation, first-branch satisfaction, empty-branch
advancement, full exhaustion, cancellation between receivers, startup
failure, missing callbacks, active-only cleanup, and prepared-generation
reuse.

Tests:

- `.agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/sql/colexec/unionall
./pkg/sql/colexec/merge ./pkg/sql/compile -count=1 -timeout=10m`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race
./pkg/sql/colexec/unionall ./pkg/sql/compile -run
'^(TestSequentialUnionAll.*|TestCompileUnionAll.*|TestStreamingUnionAll.*|TestCompilePlanScope.*|TestLazyUnionAll.*|TestLazyNestedUnionAll.*)$'
-count=1 -timeout=5m`
- `.agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/sql/colexec/unionall
./pkg/sql/compile -run
'^(TestSequentialUnionAll.*|TestLazyUnionAll.*|TestLazyNestedUnionAll.*)$'
-count=100 -timeout=2m`

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26066

## What this PR does / why we need it:

Compiles UNION ALL with sequential branch activation only when a
streaming LIMIT can short-circuit it. The demand is propagated before
physical scopes are built, so ordinary UNION ALL keeps its original
concurrent topology and pays no extra connector, spool, merge, channel,
or goroutine hop.

An eligible UNION ALL starts only its first logical branch while
retaining that branch's internal parallelism. Directly nested UNION ALL
chains are absorbed into one scheduler; this prevents a buffered inner
scheduler from starting a later leaf before the outer LIMIT can stop
consumption. Semantic boundaries such as ORDER BY, window,
assertion/barrier filters, and user-level lock side effects stop demand
propagation, and an inner LIMIT remains intact.

On early LIMIT stop or cancellation, unstarted branches have no
goroutine, reader, remote notification, or terminal signal to wait for.
Cleanup drains only active receivers. Startup and cancellation races
still deliver a terminal signal for every activated receiver, and
execution-generation callbacks are cleared before reuse.

Coverage includes unchanged non-LIMIT topology, demand propagation and
blocking boundaries, nested chain flattening and ANALYZE attribution,
inner-LIMIT preservation, first-branch satisfaction, empty-branch
advancement, full exhaustion, cancellation between receivers, startup
failure, missing callbacks, active-only cleanup, and prepared-generation
reuse.

Tests:

- `.agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/sql/colexec/unionall
./pkg/sql/colexec/merge ./pkg/sql/compile -count=1 -timeout=10m`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race
./pkg/sql/colexec/unionall ./pkg/sql/compile -run
'^(TestSequentialUnionAll.*|TestCompileUnionAll.*|TestStreamingUnionAll.*|TestCompilePlanScope.*|TestLazyUnionAll.*|TestLazyNestedUnionAll.*)$'
-count=1 -timeout=5m`
- `.agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/sql/colexec/unionall
./pkg/sql/compile -run
'^(TestSequentialUnionAll.*|TestLazyUnionAll.*|TestLazyNestedUnionAll.*)$'
-count=100 -timeout=2m`

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26066

## What this PR does / why we need it:

Compiles UNION ALL with sequential branch activation only when a
streaming LIMIT can short-circuit it. The demand is propagated before
physical scopes are built, so ordinary UNION ALL keeps its original
concurrent topology and pays no extra connector, spool, merge, channel,
or goroutine hop.

An eligible UNION ALL starts only its first logical branch while
retaining that branch's internal parallelism. Directly nested UNION ALL
chains are absorbed into one scheduler; this prevents a buffered inner
scheduler from starting a later leaf before the outer LIMIT can stop
consumption. Semantic boundaries such as ORDER BY, window,
assertion/barrier filters, and user-level lock side effects stop demand
propagation, and an inner LIMIT remains intact.

On early LIMIT stop or cancellation, unstarted branches have no
goroutine, reader, remote notification, or terminal signal to wait for.
Cleanup drains only active receivers. Startup and cancellation races
still deliver a terminal signal for every activated receiver, and
execution-generation callbacks are cleared before reuse.

Coverage includes unchanged non-LIMIT topology, demand propagation and
blocking boundaries, nested chain flattening and ANALYZE attribution,
inner-LIMIT preservation, first-branch satisfaction, empty-branch
advancement, full exhaustion, cancellation between receivers, startup
failure, missing callbacks, active-only cleanup, and prepared-generation
reuse.

Tests:

- `.agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/sql/colexec/unionall
./pkg/sql/colexec/merge ./pkg/sql/compile -count=1 -timeout=10m`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race
./pkg/sql/colexec/unionall ./pkg/sql/compile -run
'^(TestSequentialUnionAll.*|TestCompileUnionAll.*|TestStreamingUnionAll.*|TestCompilePlanScope.*|TestLazyUnionAll.*|TestLazyNestedUnionAll.*)$'
-count=1 -timeout=5m`
- `.agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/sql/colexec/unionall
./pkg/sql/compile -run
'^(TestSequentialUnionAll.*|TestLazyUnionAll.*|TestLazyNestedUnionAll.*)$'
-count=100 -timeout=2m`

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

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/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants