Skip to content

feat: support bounded exact distinct-key spill - #28003

Merged
XuPeng-SH merged 23 commits into
matrixorigin:mainfrom
aunjgr:feature/27698-bounded-distinct-spill
Sep 4, 2026
Merged

XuPeng-SH merged 23 commits into
matrixorigin:mainfrom
aunjgr:feature/27698-bounded-distinct-spill

Conversation

@aunjgr

@aunjgr aunjgr commented Sep 2, 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 #27698

What this PR does / why we need it:

Exact saved-argument COUNT(DISTINCT ...) currently retains one key per
distinct value in aggState.argSkl. Generic Group spill partitions by the
group hash, so it cannot subdivide the keys of one high-NDV hot group; reload
must reconstruct that complete set.

This PR adds a Group-owned bounded exact-key spill path while preserving the
existing skiplist normal path for low NDV:

  • transactionally drains resident exact keys only after a private spill wave
    has been flushed and published;
  • bounds subsequent resident keys to a small accounted work set;
  • partitions keys into eight radix buckets and processes leaves depth-first;
  • recursively repartitions oversized leaves and uses bounded pairwise
    external merge-sort when H0 or a proven single group cannot make hash
    progress;
  • emits compact exact count contributions during grouped finalization and
    composes them exactly once with recursive generic Group spill;
  • carries bounded exact-key subsets, rather than unsafe local counts, across
    intermediate Group/MergeGroup boundaries;
  • preserves ordinary aggregate state, multi-argument NULL behavior, signed
    zero equality, and exact full-key collision checks; and
  • closes allocation, file, FD, disk-budget, cancellation, corruption, and
    repeated-execution ownership paths.

Version 1 deliberately applies only to exact saved-argument
COUNT(DISTINCT ...). Other DISTINCT aggregate families retain their existing
behavior until their reducer, ordering, overflow, and partial-state contracts
are designed.

Design: docs/design/bounded_exact_distinct_spill.md

Validation:

  • full pkg/sql/colexec/aggexec, pkg/sql/colexec/group, and
    pkg/sql/compile unit suites;
  • planner guard for mixed aggregates;
  • full aggexec and Group suites under -race;
  • ownership-transfer, cancellation, forced-collision, recursive Group spill,
    and cross-worker MergeGroup cases 100 times under -race;
  • deterministic 2 MiB hard-account test with 2.5 MiB of unique argument
    payload;
  • current-head make build;
  • spill mode twice in one SQL session and a no-spill control, all returning
    COUNT(*) = 300 and COUNT(DISTINCT result) = 300; and
  • MatrixOne SCA/pre-push guard against a branch zero commits behind
    upstream/main.

Fixes #27698

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

Match mo-tester command parsing by removing SQL continuation indentation from the expected result file. The parser trims source command lines before matching, so preserving display indentation prevented the nested DISTINCT oracle and every following command from loading.

@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 on exact head 5e2aee6 against exact base 9f711e2. I read the complete review, inline-comment, reply, and thread history; compared the increment since my prior reviewed head 5208b1d; and rechecked the complete PR diff.

The prior multi-chunk recovery blocker is closed. BeginArgumentDrain is allocation-free, and commit/rehome now allocate, swap, and release one aggregate-state chunk at a time, so spill hash scratch no longer overlaps a replacement set proportional to chunk count. Exact Group and MergeGroup regressions span three disjoint 8,192-group chunks under a 2 MiB hard account. I also rechecked terminal errors and cleanup: a mid-commit capacity failure remains terminal and outer operator cleanup owns all partially replaced and unreplaced state; no retry can publish a partial result.

The prior BVT parser blocker is also closed by ef34513. Using the repository mo-tester parser check, the old exact head 95105e3 deterministically reproduces the CI line-38 missing-result error, while this exact head parses the same BVT file successfully. The current exact-head CI rerun is still in progress, so this approval relies on that direct old/new parser proof rather than claiming a completed CI result.

Validation on this exact production revision: make thirdparties; full aggexec, Group, and compile package tests; focused hard-account, continuation, contribution, lifecycle, and multi-chunk race tests at 10 repetitions; go vet for the owning packages; the low-NDV/no-spill and combined-spill benchmark paths; the mo-tester structural check; and git diff --check. All passed. The combined benchmark preserved one read per contribution record at the exercised level.

I independently approve design v2 in docs/design/bounded_exact_distinct_spill.md on this exact revision. Its ownership, bounded resident/disk/FD work sets, synchronous cancellation behavior, terminal-leaf continuation, exact equality, contribution routing, cleanup ordering, compatibility, observability, and validation contracts match the implementation and tested unhappy paths. No blocking correctness, lifecycle, bounded-progress, or acceptance issue remains in the reviewed diff.

@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 re-review completed on exact head f8071f2d1a9d8a7080993578cc00b92d4f866e5d.

I rechecked the complete change/risk map, prior blocking reviews, the delta from independently approved production/design revision 5e2aee62be147a42b7eef540f7f3544ee579c42e, and the current exact-head CI. The production implementation and design are unchanged since that approved revision; the only feature-file delta is the table-function alias correction in the public BVT.

The prior terminal-leaf, contribution reread amplification, final contribution allocation, multi-chunk hard-account, and BVT parser blockers are closed. Focused Group/aggexec tests and race tests covering ownership transfer, cancellation, terminal-leaf continuation, recursive contribution routing, Group/MergeGroup multi-chunk hard-account completion, NULL/multi-argument semantics, signed zero, and resource cleanup pass locally. Exact-head CI is green.

I also ran the same low-NDV/no-spill benchmark against the PR base and head: the base measured about 460.7 us/op versus 456.1 us/op at head, with effectively unchanged allocation count and no spill-controller activation. No material normal-path regression was observed.

No blocking correctness, boundedness, lifecycle, performance, compatibility, or acceptance issue remains.

@mergify

mergify Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-04 12:02 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-04 12:24 UTC · at 90ac50c9e2a5035a4dc5a2179ecf38c23da471e9

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

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

Reason

Pull request #28003 has been dequeued

Pull request from fork cannot be queued. This pull request comes 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.

Failing checks:

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, 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

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/feature size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support bounded distinct-key spill for high-NDV exact DISTINCT aggregates

4 participants