Skip to content

perf(sql): distribute distinct pairs after local deduplication - #27790

Merged
XuPeng-SH merged 12 commits into
matrixorigin:mainfrom
aunjgr:codex/distinct-key-local-prededup
Aug 29, 2026
Merged

XuPeng-SH merged 12 commits into
matrixorigin:mainfrom
aunjgr:codex/distinct-key-local-prededup

Conversation

@aunjgr

@aunjgr aunjgr commented Aug 28, 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:

Part of issue #27720. Related bounded-memory work: #27698.

Design: Exact DISTINCT-key local pre-deduplication

What this PR does / why we need it:

Parallelize high-NDV single COUNT(DISTINCT d) queries with one or a few final groups without shuffling raw duplicate rows.

Keep the existing local GROUP BY (g..., d) as a pre-deduplication stage, hash-shuffle only the surviving pairs by d, and deduplicate those pairs across distributed owners before the final COUNT(d). This preserves exact equality and NULL semantics while bounding exchange volume to worker-local distinct pairs.

Retain the existing topology for mixed aggregate lists, SUM(DISTINCT), grouping sets, expressions, unsupported key types, missing or small NDV estimates, and 64+ final-group owners. The change adds no executor, aggregate state, function ID, protobuf field, or protocol generation and reuses the existing Group spill and lifecycle contracts.

@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-reviewed exact head 4865f72 against base 28769ff. This R3 planner/hot-path change is not ready to merge.

[P1 design/evidence] The mandatory design is still marked Status: proposed MVP, and its own pre-merge acceptance gate has no execution evidence in the PR. The added synthetic planner tests prove a hand-built node hierarchy, but not real SQL correctness or the compiled physical exchange boundary. Please provide the documented exact-result and physical evidence for global/few-group, NULL and empty input, integer/string, skew, spill/non-spill, DOP=1/multi-DOP, one-CN/multi-CN, plus shuffle rows/bytes, CPU, allocations, peak accounted memory, spill activity, wall time, and the balanced 64+ fallback. This is required to prove both the intended benefit and no DOP=1/control regression.

[P1 implementation/design deviation] shouldUseDistinctKeyPreAggregation ignores missing NDV on some active grouping keys once any other key has a positive low NDV. Reachable example: GROUP BY known_low_ndv_key, missing_stats_key with high DISTINCT NDV selects Path B because highestGroupNDV remains the known low value, even though the design says missing/unreliable statistics retain the existing topology and the unknown key may expose many final-group owners. That can unnecessarily add a second spillable Group and exchange all local distinct pairs by d. Require reliable estimates for every relevant active grouping key before selecting Path B, or revise and approve the cost contract with a bounded rationale and add this composite-key counterexample.

I found no separate semantic counterexample in the eligible fully-known-stat shape; equal (g,d) pairs are correctly co-located by d, and the final Group remains the equality authority. The blockers are the unresolved design gate, missing risk-proportional execution/performance proof, and the concrete fallback deviation above.

@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 review completed at exact head 4865f72 against base 28769ff. I read the complete review/comment/thread history and issue contract, then checked the full diff, planner-to-compiler topology, and exact-head tests. Two blocking topology/cost defects remain. The first means the claimed worker-local pre-dedup exchange is not the physical plan that executes; the second violates the conservative missing-stat fallback.

Comment thread pkg/sql/plan/shuffle.go Resolved
Comment thread pkg/sql/plan/shuffle.go Resolved
aunjgr added 3 commits August 29, 2026 19:14
Compile the redundant pair Group independently on each input scope so the downstream DISTINCT-key shuffle consumes locally deduplicated rows instead of a pre-shuffle MergeGroup result. Require reliable NDV estimates for every active final grouping key, cover the finalized remapped SQL shape and physical owner topology, and record the two-CN execution matrix.
@aunjgr

aunjgr commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

Resolved the design-artifact blocker on b1766c9365fe0dbb187854f7e990b63cdb625578.

Fresh local validation after the final merge:

  • ./pkg/sql/plan: PASS
  • ./pkg/sql/compile: PASS
  • TestIssue27718ConcurrentSnapshotQuota: PASS
  • fast PR guard and diff checks: PASS

Please perform the requested independent design review against this exact revision.

@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 exact head b1766c9365fe0dbb187854f7e990b63cdb625578 against merge-base fb56746be170fa7e51191853f9e9f5a518ef69e0.

I independently approve the tracked design revision and found no remaining implementation blocker.

The two earlier blockers are closed:

  • the compiler now recognizes only the exact adjacent aggregate-free local Group(X) -> shuffled Group(X) contract, preserves one local Group per input scope, and lets the parent exchange establish final ownership;
  • Path B now requires a finite positive NDV estimate for every active final grouping key, so a known-low key cannot mask an unknown composite component.

Correctness closes from first principles: equal (g,d) pairs necessarily share d, hash collisions only co-locate additional unequal pairs, and the receiving Group remains the exact equality authority. NULL d is reduced/routed as one pair and excluded by final COUNT(d); empty input follows the existing global-count terminal. DOP=1 removes the one-bucket exchange, while missing/small NDV, 64+ final owners, grouping sets, expressions, unsupported types, SUM(DISTINCT), and mixed aggregate lists keep the established topology.

Q1-Q3 are also closed: both stages reuse the existing Group ownership and Reset/Free paths; no new goroutine, wait, wire state, or persistent resource is introduced; local reduction emits bounded intermediate state and the final Group retains the existing accounted spill bound. Exact-head CI is terminal green for UT, SCA, build, coverage, and the selected standalone/multi-CN BVT jobs, and the recorded execution matrix covers the required one/two-CN, DOP, spill, skew, NULL/empty, string/integer, near-unique, and balanced-control dimensions.

No blocking correctness, lifecycle, compatibility, or validation gap remains.

@XuPeng-SH
XuPeng-SH merged commit 4ec5bca into matrixorigin:main Aug 29, 2026
27 checks passed
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…xorigin#27790)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

Part of issue matrixorigin#27720. Related bounded-memory work: matrixorigin#27698.

Design: [Exact DISTINCT-key local
pre-deduplication](https://github.com/aunjgr/matrixone/blob/b1766c9365fe0dbb187854f7e990b63cdb625578/docs/design/exact_distinct_key_local_prededup.md)

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

Parallelize high-NDV single `COUNT(DISTINCT d)` queries with one or a
few final groups without shuffling raw duplicate rows.

Keep the existing local `GROUP BY (g..., d)` as a pre-deduplication
stage, hash-shuffle only the surviving pairs by `d`, and deduplicate
those pairs across distributed owners before the final `COUNT(d)`. This
preserves exact equality and NULL semantics while bounding exchange
volume to worker-local distinct pairs.

Retain the existing topology for mixed aggregate lists, `SUM(DISTINCT)`,
grouping sets, expressions, unsupported key types, missing or small NDV
estimates, and 64+ final-group owners. The change adds no executor,
aggregate state, function ID, protobuf field, or protocol generation and
reuses the existing Group spill and lifecycle contracts.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…xorigin#27790)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

Part of issue matrixorigin#27720. Related bounded-memory work: matrixorigin#27698.

Design: [Exact DISTINCT-key local
pre-deduplication](https://github.com/aunjgr/matrixone/blob/b1766c9365fe0dbb187854f7e990b63cdb625578/docs/design/exact_distinct_key_local_prededup.md)

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

Parallelize high-NDV single `COUNT(DISTINCT d)` queries with one or a
few final groups without shuffling raw duplicate rows.

Keep the existing local `GROUP BY (g..., d)` as a pre-deduplication
stage, hash-shuffle only the surviving pairs by `d`, and deduplicate
those pairs across distributed owners before the final `COUNT(d)`. This
preserves exact equality and NULL semantics while bounding exchange
volume to worker-local distinct pairs.

Retain the existing topology for mixed aggregate lists, `SUM(DISTINCT)`,
grouping sets, expressions, unsupported key types, missing or small NDV
estimates, and 64+ final-group owners. The change adds no executor,
aggregate state, function ID, protobuf field, or protocol generation and
reuses the existing Group spill and lifecycle contracts.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…xorigin#27790)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

Part of issue matrixorigin#27720. Related bounded-memory work: matrixorigin#27698.

Design: [Exact DISTINCT-key local
pre-deduplication](https://github.com/aunjgr/matrixone/blob/b1766c9365fe0dbb187854f7e990b63cdb625578/docs/design/exact_distinct_key_local_prededup.md)

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

Parallelize high-NDV single `COUNT(DISTINCT d)` queries with one or a
few final groups without shuffling raw duplicate rows.

Keep the existing local `GROUP BY (g..., d)` as a pre-deduplication
stage, hash-shuffle only the surviving pairs by `d`, and deduplicate
those pairs across distributed owners before the final `COUNT(d)`. This
preserves exact equality and NULL semantics while bounding exchange
volume to worker-local distinct pairs.

Retain the existing topology for mixed aggregate lists, `SUM(DISTINCT)`,
grouping sets, expressions, unsupported key types, missing or small NDV
estimates, and 64+ final-group owners. The change adds no executor,
aggregate state, function ID, protobuf field, or protocol generation and
reuses the existing Group spill and lifecycle contracts.

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/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants