Skip to content

fix(agg): honor ordered percentile spill with allocation account - #29035

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
daviszhen:codex/issue-28944-percentile-spill
Sep 17, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
daviszhen:codex/issue-28944-percentile-spill

Conversation

@daviszhen

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:

Fixes #28944

What this PR does / why we need it:

Ordered-set percentile executors disabled their local spill configuration when Group installed an allocation account. Their spill watermark inspected only legacy vectors, while actual values lived in the accounted saved-argument arena. H0 also bypasses generic hash spill, so large percentile queries retained all inputs and materialized them again at Flush.

This change preserves spill configuration and drains occupied accounted arenas into sorted local runs. Scratch remains allocation-accounted and is sorted in place. Shared resident arenas/counts are reset only after all groups have been written successfully; failed drains restore published run descriptors. Flush drains the tail and selects rank endpoints from the runs, preserving continuous interpolation, discrete ranks, direction, NULL handling and the existing floating-point total order.

Local runs remain terminal state, not partial wire state. MergeGroup configures them for incoming H0 partials or the existing final grouped reload pass, not intermediate grouped destinations that may still enter generic hash spill. Defensive merge/serialization guards reject spilled partials. Existing percentile partial encoding is unchanged. Run compaction now leaves published descriptors intact on failed I/O.

Validation on macOS arm64 / Go 1.26.5 with rebuilt native CGo dependencies:

  • Full pkg/sql/colexec/aggexec and pkg/sql/colexec/group unit suites passed, including accounted H0, grouped H8/HStr pressure, terminal hot-group reload, repeated drains/compaction, tail/NULLs, transport and lifecycle cleanup.
  • Failure/counterexample coverage includes create/write/read/cancellation failures, failed-drain retry without duplicate rows, failed compaction, scratch/reset allocation rejection, configuration order, FLOAT32/64 NaN ordering, Decimal128 interpolation, direction and endpoint controls.
  • New SQL BVT passed all 21 checks through mo-tester: low-threshold/resident result comparison, positive Explain spill rows/bytes, parallel input, DESC, endpoints and all-NULL input.
  • Original 10,000,000-row BIGINT reproduction passed on a fresh local 3-CN cluster with agg_spill_mem=2097152. The AP multicn plan returned 9500000.05 / 9500000 and reported Aggregate MemorySize=4.52 MiB, SpillRows=20000000, SpillSize=152.59 MiB (two aggregate executors). This is current-code local validation, not a rerun of the historical TKE environment or a cross-host timing benchmark.

The spill setting triggers on occupied retained state; it is not a strict total-query memory ceiling. Arena allocation floors, one incoming work unit and sorting scratch can exceed the trigger. This bounds accumulated destination history but does not bound decoding an arbitrarily large incoming partial. External sorting adds disk I/O; distributed CI is pending.

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

GPT-6 medium deep review at exact head f6f4737.

I reviewed all 7 changed files and the accounted ordered-percentile spill lifecycle.

  • Installing an allocation account no longer disables terminal ordered-spill configuration. Accounted producers now use the same spill path while retaining the generic grouped-spill boundary.
  • FlushWithContext handles cancellation before work, drains accounted state into ordered runs, and computes percentile ranks from spilled values without reconstructing all retained values at once. Resident and spilled paths share the same rank/interpolation logic for continuous and discrete percentiles, including Decimal128.
  • Accounted scratch values and sort work are released on success, cancellation, allocation failure, and malformed state. Published run descriptors are preserved when a spill write/compaction fails, and compaction no longer destroys the existing run list before a replacement is ready.
  • Merge, BatchMerge, PreflightBatchMerge, and SaveSpillIntermediateRows reject spilled ordered partials instead of serializing or combining an unsupported local-run state. The terminal MergeGroup handoff configures ordered spill only for the incoming H0 path.
  • Tests cover resident/spilled equivalence, ascending/descending and endpoint percentiles, NULL/all-NULL input, allocation-accounted H0/H8/HStr merge modes, cancellation, allocation failure, failed compaction, and public BVT spill metrics.

Required CI is green, including Ubuntu UT/race, SCA, coverage, shared build, compose/standalone BVT, and CI Required. The PR is mergeable without conflict and only behind the base branch.

No evidence-backed P0/P1/P2 blocker found. Approving this exact head.

@mergify mergify Bot added the queued label Sep 17, 2026
@mergify

mergify Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-17 05:27 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-17 05:29 UTC · at 68ded16f0374c5906091f27e0fa917912bbc9cd3

This pull request spent 1 minute 47 seconds in the queue, with no time running CI.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Reason

Pull request #29035 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.

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

VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rixorigin#29035)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

Fixes matrixorigin#28944

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

Ordered-set percentile executors disabled their local spill
configuration when Group installed an allocation account. Their spill
watermark inspected only legacy vectors, while actual values lived in
the accounted saved-argument arena. H0 also bypasses generic hash spill,
so large percentile queries retained all inputs and materialized them
again at Flush.

This change preserves spill configuration and drains occupied accounted
arenas into sorted local runs. Scratch remains allocation-accounted and
is sorted in place. Shared resident arenas/counts are reset only after
all groups have been written successfully; failed drains restore
published run descriptors. Flush drains the tail and selects rank
endpoints from the runs, preserving continuous interpolation, discrete
ranks, direction, NULL handling and the existing floating-point total
order.

Local runs remain terminal state, not partial wire state. MergeGroup
configures them for incoming H0 partials or the existing final grouped
reload pass, not intermediate grouped destinations that may still enter
generic hash spill. Defensive merge/serialization guards reject spilled
partials. Existing percentile partial encoding is unchanged. Run
compaction now leaves published descriptors intact on failed I/O.

Validation on macOS arm64 / Go 1.26.5 with rebuilt native CGo
dependencies:

- Full `pkg/sql/colexec/aggexec` and `pkg/sql/colexec/group` unit suites
passed, including accounted H0, grouped H8/HStr pressure, terminal
hot-group reload, repeated drains/compaction, tail/NULLs, transport and
lifecycle cleanup.
- Failure/counterexample coverage includes
create/write/read/cancellation failures, failed-drain retry without
duplicate rows, failed compaction, scratch/reset allocation rejection,
configuration order, FLOAT32/64 NaN ordering, Decimal128 interpolation,
direction and endpoint controls.
- New SQL BVT passed all 21 checks through mo-tester:
low-threshold/resident result comparison, positive Explain spill
rows/bytes, parallel input, DESC, endpoints and all-NULL input.
- Original 10,000,000-row BIGINT reproduction passed on a fresh local
3-CN cluster with `agg_spill_mem=2097152`. The AP multicn plan returned
`9500000.05 / 9500000` and reported Aggregate `MemorySize=4.52 MiB`,
`SpillRows=20000000`, `SpillSize=152.59 MiB` (two aggregate executors).
This is current-code local validation, not a rerun of the historical TKE
environment or a cross-host timing benchmark.

The spill setting triggers on occupied retained state; it is not a
strict total-query memory ceiling. Arena allocation floors, one incoming
work unit and sorting scratch can exceed the trigger. This bounds
accumulated destination history but does not bound decoding an
arbitrarily large incoming partial. External sorting adds disk I/O;
distributed CI is pending.

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
…rixorigin#29035)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

Fixes matrixorigin#28944

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

Ordered-set percentile executors disabled their local spill
configuration when Group installed an allocation account. Their spill
watermark inspected only legacy vectors, while actual values lived in
the accounted saved-argument arena. H0 also bypasses generic hash spill,
so large percentile queries retained all inputs and materialized them
again at Flush.

This change preserves spill configuration and drains occupied accounted
arenas into sorted local runs. Scratch remains allocation-accounted and
is sorted in place. Shared resident arenas/counts are reset only after
all groups have been written successfully; failed drains restore
published run descriptors. Flush drains the tail and selects rank
endpoints from the runs, preserving continuous interpolation, discrete
ranks, direction, NULL handling and the existing floating-point total
order.

Local runs remain terminal state, not partial wire state. MergeGroup
configures them for incoming H0 partials or the existing final grouped
reload pass, not intermediate grouped destinations that may still enter
generic hash spill. Defensive merge/serialization guards reject spilled
partials. Existing percentile partial encoding is unchanged. Run
compaction now leaves published descriptors intact on failed I/O.

Validation on macOS arm64 / Go 1.26.5 with rebuilt native CGo
dependencies:

- Full `pkg/sql/colexec/aggexec` and `pkg/sql/colexec/group` unit suites
passed, including accounted H0, grouped H8/HStr pressure, terminal
hot-group reload, repeated drains/compaction, tail/NULLs, transport and
lifecycle cleanup.
- Failure/counterexample coverage includes
create/write/read/cancellation failures, failed-drain retry without
duplicate rows, failed compaction, scratch/reset allocation rejection,
configuration order, FLOAT32/64 NaN ordering, Decimal128 interpolation,
direction and endpoint controls.
- New SQL BVT passed all 21 checks through mo-tester:
low-threshold/resident result comparison, positive Explain spill
rows/bytes, parallel input, DESC, endpoints and all-NULL input.
- Original 10,000,000-row BIGINT reproduction passed on a fresh local
3-CN cluster with `agg_spill_mem=2097152`. The AP multicn plan returned
`9500000.05 / 9500000` and reported Aggregate `MemorySize=4.52 MiB`,
`SpillRows=20000000`, `SpillSize=152.59 MiB` (two aggregate executors).
This is current-code local validation, not a rerun of the historical TKE
environment or a cross-host timing benchmark.

The spill setting triggers on occupied retained state; it is not a
strict total-query memory ceiling. Arena allocation floors, one incoming
work unit and sorting scratch can exceed the trigger. This bounds
accumulated destination history but does not bound decoding an
arbitrarily large incoming partial. External sorting adds disk I/O;
distributed CI is pending.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Ordered-set percentiles ignore agg_spill_mem in multi-CN execution

3 participants