Skip to content

perf(plan): prune right SINGLE probe scans with runtime filters - #25865

Merged
XuPeng-SH merged 12 commits into
mainfrom
codex/right-single-rf
Jul 20, 2026
Merged

XuPeng-SH merged 12 commits into
mainfrom
codex/right-single-rf

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Jul 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:

issue #25862

What this PR does / why we need it:

Right SINGLE joins produced by correlated scalar-subquery flattening preserve the build side, but previously could scan the entire discardable probe table because SINGLE joins could not build runtime filters.

This PR adds a conservative, exact-IN runtime-filter path for eligible physical right SINGLE joins.

  • Centralizes runtime-filter eligibility so planning, one-CN placement, statistics, and compile-time validation use the same policy.
  • Restricts the optimization to direct table scans, supported key types, a leading primary/cluster-key prefix, no probe LIMIT/OFFSET, and build/probe cardinalities that fit the exact-IN limit.
  • Colocates the producer and all consumers on one CN instead of extending the current-CN runtime-filter protocol across CNs.
  • Validates the final physical topology before pipelines start: exactly one producer, every consumer colocated, and no ambiguous partial metadata.
  • Records right-SINGLE nodes actually visited by compilePlanScope, so logically retained but physically pruned subtrees (for example branch-level LIMIT 0) are excluded without weakening validation for reachable subtrees.
  • Sends DROP for empty or aborted builds and preserves query-context cancellation while consumers wait.
  • Corrects right SINGLE cardinality after physical child swapping and propagates the corrected estimate to parent nodes.
  • Preserves the existing fallback for ineligible, expensive, non-leading-key, or uncertain shapes.
  • Keeps disableRightSingleRF=1 as a diagnostic rollback switch.

Correctness and unhappy-path coverage

  • Match, miss, NULL, empty build, residual predicates, duplicate scalar results, no-PK fallback, composite-key full/prefix cases, and UPDATE/INSERT/DELETE consumers.
  • Missing/duplicate producers, fully missing reachable topology, physically pruned topology, remote consumers, ambiguous metadata, standalone execution, shared scope graphs, and cancellation while waiting.
  • Branch-level LIMIT 0 + UNION ALL + right-SINGLE regression from review.
  • Single-CN and 2-CN BVT execution without assuming a fixed deployment topology.
  • Race testing covers plan, compile, hashbuild, hashjoin, and runtime-filter message packages.

Performance evidence

For an eligible 100,000-row probe table, EXPLAIN ANALYZE showed:

mode probe rows probe blocks
right SINGLE runtime filter enabled 5,000 1
disabled fallback 100,000 13

The compile-time no-filter topology path remains allocation-free at approximately 2.26 ns/op. Queries without a right-SINGLE join allocate no compiled-node tracking state.

Phase boundary and follow-up

Phase 1 intentionally keeps the cardinality correction in a post-swap-only pass. Under the current frozen physical orientation, a right SINGLE uses physical child 1 as the semantic preserved side; the downstream-join regression proves that consumers see the preserved cardinality instead of the discardable probe cardinality.

This positional rule is a tested bridge, not the long-term optimizer model. A follow-up should introduce an explicit semantic join descriptor with stable preserved/discardable relation identities, keep physical probe/build orientation separate, migrate stats/RF/compile/explain consumers, and then remove the IsRightJoin + child index inference.

Validation

  • Full tests: pkg/sql/plan, pkg/sql/compile, pkg/sql/colexec/hashbuild, pkg/sql/colexec/hashjoin, and pkg/vm/message
  • The same package set with -race -p=1
  • go vet for the same package set and a full make -j8 build
  • 34/34 semantic BVT statements from both CN entry points in a 2-CN deployment, then 34/34 again after membership converged to single-CN
  • Shuffled compile/plan package tests, 3 repetitions
  • 31,000 repeated standalone BVT statements
  • 50,000 successful 2-CN stress executions
  • 50 cancellation cycles while a consumer waited for its runtime filter; no stuck waiters or goroutine growth
  • Heap-profile review after stress found no runtime-filter-specific retained ownership chain

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner July 19, 2026 01:45
@XuPeng-SH
XuPeng-SH requested a review from a user July 19, 2026 01:45
@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 →

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Deep review found one reproducible compile-time regression in the new topology validation. The right-SINGLE semantics, physical side swap, exact-IN bounds, message delivery/cancellation, and resource lifecycle otherwise look consistent. Local full package tests, focused race tests, build, vet, and diff checks passed.

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

I confirmed the compile-time regression identified in the existing review. rightSingleLocalRuntimeFilterTags scans every node in the flat query, while compilePlanScope prunes literal LIMIT 0 subtrees before producing scopes. A right-SINGLE scalar subquery below a branch-level LIMIT 0 in UNION ALL therefore leaves a planned RF tag with no compiled consumer, and topology validation rejects the otherwise valid query with tag ... has no scan consumer.

Please derive the validated tags from subtrees that were actually compiled (or explicitly track compiled node IDs), while retaining missing/duplicate-producer validation for reachable subtrees. Add a regression covering branch-level LIMIT 0 + UNION ALL + right-SINGLE.

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed head 70a366a with a focus on data correctness and normal-query regressions. The parallel build scan remains parallel below a single Merge and therefore has exactly one HashBuild runtime-filter producer; topology validation also fails closed if that physical invariant changes. DefaultStats is no longer accepted as a hard build-size bound, compile-pruned LIMIT 0 branches remain excluded, and each received IN payload now retains its originating spec across preceding PASS messages. Right-SINGLE preserved-side stats, exact-IN eligibility, cancellation/reset termination, and existing join/runtime-filter policies remain consistent. Full plan and compile package tests, focused race tests, dependent hashbuild/hashjoin/message tests, build, vet, diff checks, and current CI all pass. No remaining blocker found.

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

Codex automated review

No concrete regressions found in the reviewed diff.

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

Re-reviewed current head ea10dc4. The parallel build scan is merged before the single HashBuild runtime-filter producer; the DefaultStats fallback and PASS/IN spec association are fixed. Relevant package tests, focused race tests, go vet, and go build all pass. No blocking issues found.

@XuPeng-SH
XuPeng-SH merged commit 862d9d3 into main Jul 20, 2026
38 of 39 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/right-single-rf branch July 20, 2026 04:31
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…ixorigin#25865)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

issue matrixorigin#25862

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

Right SINGLE joins produced by correlated scalar-subquery flattening
preserve the build side, but previously could scan the entire
discardable probe table because SINGLE joins could not build runtime
filters.

This PR adds a conservative, exact-IN runtime-filter path for eligible
physical right SINGLE joins.

- Centralizes runtime-filter eligibility so planning, one-CN placement,
statistics, and compile-time validation use the same policy.
- Restricts the optimization to direct table scans, supported key types,
a leading primary/cluster-key prefix, no probe LIMIT/OFFSET, and
build/probe cardinalities that fit the exact-IN limit.
- Colocates the producer and all consumers on one CN instead of
extending the current-CN runtime-filter protocol across CNs.
- Validates the final physical topology before pipelines start: exactly
one producer, every consumer colocated, and no ambiguous partial
metadata.
- Records right-SINGLE nodes actually visited by `compilePlanScope`, so
logically retained but physically pruned subtrees (for example
branch-level `LIMIT 0`) are excluded without weakening validation for
reachable subtrees.
- Sends DROP for empty or aborted builds and preserves query-context
cancellation while consumers wait.
- Corrects right SINGLE cardinality after physical child swapping and
propagates the corrected estimate to parent nodes.
- Preserves the existing fallback for ineligible, expensive,
non-leading-key, or uncertain shapes.
- Keeps `disableRightSingleRF=1` as a diagnostic rollback switch.

### Correctness and unhappy-path coverage

- Match, miss, NULL, empty build, residual predicates, duplicate scalar
results, no-PK fallback, composite-key full/prefix cases, and
UPDATE/INSERT/DELETE consumers.
- Missing/duplicate producers, fully missing reachable topology,
physically pruned topology, remote consumers, ambiguous metadata,
standalone execution, shared scope graphs, and cancellation while
waiting.
- Branch-level `LIMIT 0` + `UNION ALL` + right-SINGLE regression from
review.
- Single-CN and 2-CN BVT execution without assuming a fixed deployment
topology.
- Race testing covers plan, compile, hashbuild, hashjoin, and
runtime-filter message packages.

### Performance evidence

For an eligible 100,000-row probe table, `EXPLAIN ANALYZE` showed:

| mode | probe rows | probe blocks |
| --- | ---: | ---: |
| right SINGLE runtime filter enabled | 5,000 | 1 |
| disabled fallback | 100,000 | 13 |

The compile-time no-filter topology path remains allocation-free at
approximately 2.26 ns/op. Queries without a right-SINGLE join allocate
no compiled-node tracking state.

### Phase boundary and follow-up

Phase 1 intentionally keeps the cardinality correction in a
post-swap-only pass. Under the current frozen physical orientation, a
right SINGLE uses physical child 1 as the semantic preserved side; the
downstream-join regression proves that consumers see the preserved
cardinality instead of the discardable probe cardinality.

This positional rule is a tested bridge, not the long-term optimizer
model. A follow-up should introduce an explicit semantic join descriptor
with stable preserved/discardable relation identities, keep physical
probe/build orientation separate, migrate stats/RF/compile/explain
consumers, and then remove the `IsRightJoin + child index` inference.

### Validation

- Full tests: `pkg/sql/plan`, `pkg/sql/compile`,
`pkg/sql/colexec/hashbuild`, `pkg/sql/colexec/hashjoin`, and
`pkg/vm/message`
- The same package set with `-race -p=1`
- `go vet` for the same package set and a full `make -j8 build`
- 34/34 semantic BVT statements from both CN entry points in a 2-CN
deployment, then 34/34 again after membership converged to single-CN
- Shuffled compile/plan package tests, 3 repetitions
- 31,000 repeated standalone BVT statements
- 50,000 successful 2-CN stress executions
- 50 cancellation cycles while a consumer waited for its runtime filter;
no stuck waiters or goroutine growth
- Heap-profile review after stress found no runtime-filter-specific
retained ownership chain
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…ixorigin#25865)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

issue matrixorigin#25862

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

Right SINGLE joins produced by correlated scalar-subquery flattening
preserve the build side, but previously could scan the entire
discardable probe table because SINGLE joins could not build runtime
filters.

This PR adds a conservative, exact-IN runtime-filter path for eligible
physical right SINGLE joins.

- Centralizes runtime-filter eligibility so planning, one-CN placement,
statistics, and compile-time validation use the same policy.
- Restricts the optimization to direct table scans, supported key types,
a leading primary/cluster-key prefix, no probe LIMIT/OFFSET, and
build/probe cardinalities that fit the exact-IN limit.
- Colocates the producer and all consumers on one CN instead of
extending the current-CN runtime-filter protocol across CNs.
- Validates the final physical topology before pipelines start: exactly
one producer, every consumer colocated, and no ambiguous partial
metadata.
- Records right-SINGLE nodes actually visited by `compilePlanScope`, so
logically retained but physically pruned subtrees (for example
branch-level `LIMIT 0`) are excluded without weakening validation for
reachable subtrees.
- Sends DROP for empty or aborted builds and preserves query-context
cancellation while consumers wait.
- Corrects right SINGLE cardinality after physical child swapping and
propagates the corrected estimate to parent nodes.
- Preserves the existing fallback for ineligible, expensive,
non-leading-key, or uncertain shapes.
- Keeps `disableRightSingleRF=1` as a diagnostic rollback switch.

### Correctness and unhappy-path coverage

- Match, miss, NULL, empty build, residual predicates, duplicate scalar
results, no-PK fallback, composite-key full/prefix cases, and
UPDATE/INSERT/DELETE consumers.
- Missing/duplicate producers, fully missing reachable topology,
physically pruned topology, remote consumers, ambiguous metadata,
standalone execution, shared scope graphs, and cancellation while
waiting.
- Branch-level `LIMIT 0` + `UNION ALL` + right-SINGLE regression from
review.
- Single-CN and 2-CN BVT execution without assuming a fixed deployment
topology.
- Race testing covers plan, compile, hashbuild, hashjoin, and
runtime-filter message packages.

### Performance evidence

For an eligible 100,000-row probe table, `EXPLAIN ANALYZE` showed:

| mode | probe rows | probe blocks |
| --- | ---: | ---: |
| right SINGLE runtime filter enabled | 5,000 | 1 |
| disabled fallback | 100,000 | 13 |

The compile-time no-filter topology path remains allocation-free at
approximately 2.26 ns/op. Queries without a right-SINGLE join allocate
no compiled-node tracking state.

### Phase boundary and follow-up

Phase 1 intentionally keeps the cardinality correction in a
post-swap-only pass. Under the current frozen physical orientation, a
right SINGLE uses physical child 1 as the semantic preserved side; the
downstream-join regression proves that consumers see the preserved
cardinality instead of the discardable probe cardinality.

This positional rule is a tested bridge, not the long-term optimizer
model. A follow-up should introduce an explicit semantic join descriptor
with stable preserved/discardable relation identities, keep physical
probe/build orientation separate, migrate stats/RF/compile/explain
consumers, and then remove the `IsRightJoin + child index` inference.

### Validation

- Full tests: `pkg/sql/plan`, `pkg/sql/compile`,
`pkg/sql/colexec/hashbuild`, `pkg/sql/colexec/hashjoin`, and
`pkg/vm/message`
- The same package set with `-race -p=1`
- `go vet` for the same package set and a full `make -j8 build`
- 34/34 semantic BVT statements from both CN entry points in a 2-CN
deployment, then 34/34 again after membership converged to single-CN
- Shuffled compile/plan package tests, 3 repetitions
- 31,000 repeated standalone BVT statements
- 50,000 successful 2-CN stress executions
- 50 cancellation cycles while a consumer waited for its runtime filter;
no stuck waiters or goroutine growth
- Heap-profile review after stress found no runtime-filter-specific
retained ownership chain
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…ixorigin#25865)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

issue matrixorigin#25862

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

Right SINGLE joins produced by correlated scalar-subquery flattening
preserve the build side, but previously could scan the entire
discardable probe table because SINGLE joins could not build runtime
filters.

This PR adds a conservative, exact-IN runtime-filter path for eligible
physical right SINGLE joins.

- Centralizes runtime-filter eligibility so planning, one-CN placement,
statistics, and compile-time validation use the same policy.
- Restricts the optimization to direct table scans, supported key types,
a leading primary/cluster-key prefix, no probe LIMIT/OFFSET, and
build/probe cardinalities that fit the exact-IN limit.
- Colocates the producer and all consumers on one CN instead of
extending the current-CN runtime-filter protocol across CNs.
- Validates the final physical topology before pipelines start: exactly
one producer, every consumer colocated, and no ambiguous partial
metadata.
- Records right-SINGLE nodes actually visited by `compilePlanScope`, so
logically retained but physically pruned subtrees (for example
branch-level `LIMIT 0`) are excluded without weakening validation for
reachable subtrees.
- Sends DROP for empty or aborted builds and preserves query-context
cancellation while consumers wait.
- Corrects right SINGLE cardinality after physical child swapping and
propagates the corrected estimate to parent nodes.
- Preserves the existing fallback for ineligible, expensive,
non-leading-key, or uncertain shapes.
- Keeps `disableRightSingleRF=1` as a diagnostic rollback switch.

### Correctness and unhappy-path coverage

- Match, miss, NULL, empty build, residual predicates, duplicate scalar
results, no-PK fallback, composite-key full/prefix cases, and
UPDATE/INSERT/DELETE consumers.
- Missing/duplicate producers, fully missing reachable topology,
physically pruned topology, remote consumers, ambiguous metadata,
standalone execution, shared scope graphs, and cancellation while
waiting.
- Branch-level `LIMIT 0` + `UNION ALL` + right-SINGLE regression from
review.
- Single-CN and 2-CN BVT execution without assuming a fixed deployment
topology.
- Race testing covers plan, compile, hashbuild, hashjoin, and
runtime-filter message packages.

### Performance evidence

For an eligible 100,000-row probe table, `EXPLAIN ANALYZE` showed:

| mode | probe rows | probe blocks |
| --- | ---: | ---: |
| right SINGLE runtime filter enabled | 5,000 | 1 |
| disabled fallback | 100,000 | 13 |

The compile-time no-filter topology path remains allocation-free at
approximately 2.26 ns/op. Queries without a right-SINGLE join allocate
no compiled-node tracking state.

### Phase boundary and follow-up

Phase 1 intentionally keeps the cardinality correction in a
post-swap-only pass. Under the current frozen physical orientation, a
right SINGLE uses physical child 1 as the semantic preserved side; the
downstream-join regression proves that consumers see the preserved
cardinality instead of the discardable probe cardinality.

This positional rule is a tested bridge, not the long-term optimizer
model. A follow-up should introduce an explicit semantic join descriptor
with stable preserved/discardable relation identities, keep physical
probe/build orientation separate, migrate stats/RF/compile/explain
consumers, and then remove the `IsRightJoin + child index` inference.

### Validation

- Full tests: `pkg/sql/plan`, `pkg/sql/compile`,
`pkg/sql/colexec/hashbuild`, `pkg/sql/colexec/hashjoin`, and
`pkg/vm/message`
- The same package set with `-race -p=1`
- `go vet` for the same package set and a full `make -j8 build`
- 34/34 semantic BVT statements from both CN entry points in a 2-CN
deployment, then 34/34 again after membership converged to single-CN
- Shuffled compile/plan package tests, 3 repetitions
- 31,000 repeated standalone BVT statements
- 50,000 successful 2-CN stress executions
- 50 cancellation cycles while a consumer waited for its runtime filter;
no stuck waiters or goroutine growth
- Heap-profile review after stress found no runtime-filter-specific
retained ownership chain

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants