Skip to content

fix: prevent H0 aggregate spill panic - #26736

Merged
XuPeng-SH merged 1 commit into
matrixorigin:mainfrom
XuPeng-SH:fix/h0-agg-spill-panic-26453
Aug 5, 2026
Merged

XuPeng-SH merged 1 commit into
matrixorigin:mainfrom
XuPeng-SH:fix/h0-agg-spill-panic-26453

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

Fixes #26453

Summary

  • skip generic hash-table spill decisions for H0 scalar aggregates, which have no grouping hash table
  • preserve ordered GROUP_CONCAT H0 spill through its aggregate executor
  • add H0 threshold, Group -> MergeGroup reuse, and SQL BVT regressions

Validation

  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s ./pkg/sql/colexec/group
  • .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s ./pkg/sql/colexec/group
  • focused H0 regressions: -race -count=100 each
  • GOWORK=off go build/vet -mod=readonly ./pkg/sql/colexec/group ./pkg/sql/compile
  • dependent compile test: TestRefreshGroupConcatMaxLenForPreparedCompileReuse

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner August 5, 2026 15:10
@XuPeng-SH
XuPeng-SH requested a review from a user August 5, 2026 15:10
@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 →

@iamlinjunhong iamlinjunhong 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 from base c31b46d to head 2d14b8e. No P0-P3 findings.

The H0 guard matches the execution invariant: an ungrouped aggregate has one aggregate group and no grouping hash table, so MergeGroup must not enter generic hash-partition spill. H8/HStr group-count and byte-threshold behavior remains unchanged, memory accounting still runs, and ordered GROUP_CONCAT retains its aggregate-owned spill path. The tests cover low and byte thresholds, ordered-aggregate spill, the real partial Group -> MergeGroup hand-off, Reset reuse, mpool cleanup, and the public SQL regression. CI evidence for this head includes successful UT/coverage/SCA and group_h0_spill.sql (11/11 statements).

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Post-rebase review against main ab28d1b4ac: no blocking findings.

The only conflict was the common insertion point in group_test.go; the resolution preserves both the H0 spill regressions and #26727 reduced-hash-key/spill tests. The production H0 guard remains unchanged semantically: H0 has no grouping hash table, memory accounting still runs, H8/HStr generic spill is unchanged, and ordered GROUP_CONCAT keeps its aggregate-owned spill path.

Fresh validation on head 6173a85b6d: owning/dependent package list, build and vet passed; the four focused H0/reduced-hash regressions passed; the full pkg/sql/colexec/group package passed normally and under -race; dependent TestRefreshGroupConcatMaxLenForPreparedCompileReuse passed. Q1-Q3 review found no new resource owner, wait edge, or unbounded accumulation introduced by the diff.

@XuPeng-SH
XuPeng-SH merged commit 09e02ee into matrixorigin:main Aug 5, 2026
24 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/h0-agg-spill-panic-26453 branch August 5, 2026 17:57

@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 latest HEAD. The H0 guard matches the execution invariant: scalar aggregation has no grouping hash table and therefore cannot enter generic hash-partition spill. H8/HStr spill behavior and memory accounting remain intact, ordered GROUP_CONCAT keeps aggregate-owned spill, and the regressions cover Group to MergeGroup hand-off, Reset reuse, and mpool cleanup. No code blocker found.

VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Fixes matrixorigin#26453

## Summary
- skip generic hash-table spill decisions for H0 scalar aggregates,
which have no grouping hash table
- preserve ordered GROUP_CONCAT H0 spill through its aggregate executor
- add H0 threshold, Group -> MergeGroup reuse, and SQL BVT regressions

## Validation
- .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s
./pkg/sql/colexec/group
- .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s
./pkg/sql/colexec/group
- focused H0 regressions: -race -count=100 each
- GOWORK=off go build/vet -mod=readonly ./pkg/sql/colexec/group
./pkg/sql/compile
- dependent compile test:
TestRefreshGroupConcatMaxLenForPreparedCompileReuse
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Fixes matrixorigin#26453

## Summary
- skip generic hash-table spill decisions for H0 scalar aggregates,
which have no grouping hash table
- preserve ordered GROUP_CONCAT H0 spill through its aggregate executor
- add H0 threshold, Group -> MergeGroup reuse, and SQL BVT regressions

## Validation
- .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s
./pkg/sql/colexec/group
- .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s
./pkg/sql/colexec/group
- focused H0 regressions: -race -count=100 each
- GOWORK=off go build/vet -mod=readonly ./pkg/sql/colexec/group
./pkg/sql/compile
- dependent compile test:
TestRefreshGroupConcatMaxLenForPreparedCompileReuse
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Fixes matrixorigin#26453

## Summary
- skip generic hash-table spill decisions for H0 scalar aggregates,
which have no grouping hash table
- preserve ordered GROUP_CONCAT H0 spill through its aggregate executor
- add H0 threshold, Group -> MergeGroup reuse, and SQL BVT regressions

## Validation
- .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s
./pkg/sql/colexec/group
- .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s
./pkg/sql/colexec/group
- focused H0 regressions: -race -count=100 each
- GOWORK=off go build/vet -mod=readonly ./pkg/sql/colexec/group
./pkg/sql/compile
- dependent compile test:
TestRefreshGroupConcatMaxLenForPreparedCompileReuse
XuPeng-SH added a commit that referenced this pull request Nov 9, 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:

Related to #32233

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

On `4.2-dev`, running the first scalar aggregate on a freshly created
table
while the session holds a low debug `agg_spill_mem` threshold panics
with a
nil pointer dereference and surfaces internal error 20101:

```sql
CREATE TABLE fresh_count (id INT);
INSERT INTO fresh_count VALUES (1);
SET @@agg_spill_mem = 256;
SELECT COUNT(*) FROM fresh_count;  -- panic in group.(*container).needSpill
```

**Root cause**

`pkg/sql/colexec/group/helper.go` `needSpill()` evaluates the debug
group-count mode (`spillMem < 10000`) by dereferencing
`ctr.hr.Hash.GroupCount()`
without checking the container mode. An H0 container (no GROUP BY) never
builds the grouping hash table, so `ctr.hr.Hash` is nil and the call
panics.
MergeGroup reaches the same call on its H0 merge path, which is why
plain
`SELECT COUNT(*)` on a fresh table triggers it. Values of 10000 and
above are
a byte threshold and were never affected.

This is the 4.2-dev counterpart of main PR #26736 (issue #26453):
generic group-spill partitioning requires the grouping hash table, H0
holds
exactly one aggregate group, and aggregates that bring their own H0
spill
behavior (such as ordered GROUP_CONCAT) keep spilling through their own
executors.

**Exact change**

- `pkg/sql/colexec/group/helper.go`: `needSpill()` returns false for H0
  before either threshold branch; the analyzer memory accounting and the
  grouped (H8/HStr) threshold semantics are unchanged.
- `pkg/sql/colexec/group/group_test.go`: ports of the #26736 unit
regressions:
- `TestH0NeverRequestsGenericGroupSpill`: H0 requests no generic spill
at
the 256 group-count threshold and at a 10 KiB byte threshold with memory
    retained in the pool; `SetMemUsed` accounting is still published.
- `TestH0OrderedGroupConcatSpillsIndependently`: an H0 ordered
GROUP_CONCAT
still spills (SpillRows > 0) with zero generic `GroupSpillWriteCalls`.
- `TestMergeGroupH0SkipsGenericSpillAndReuses`: the real Group →
MergeGroup
    H0 hand-off over multiple partial batches returns the correct scalar
count, performs no generic spill, stays H0 with an empty hash, and
resets
    cleanly between generations.
- `test/distributed/cases/qexec/group_h0_spill.sql` + `.result`: BVT
that
creates a fresh one-row table, runs scalar COUNT at `agg_spill_mem =
256`
  and then at the normal 536870912 byte threshold on one connection, and
  resets the variable afterwards.

**Issue-to-test proof (red/green)**

- With the 4.2-dev base `helper.go` restored, the three focused UTs
panic with
  the reported `runtime error: invalid memory address or nil pointer
  dereference` at `needSpill` - the exact stack from issue #32233.
- With the fix, the same tests pass; the grouped spill behavior
exercised by
the package's existing tests is unchanged and the full package passes
under
  `-race`.
- End-to-end SQL on a locally built `mo-service` from this branch
(1 LOG / 1 TN / 1 CN on isolated ports): the issue's exact sequence
returns
`1` for 3/3 fresh tables at `agg_spill_mem = 256`, `1` at 536870912, the
grouped control returns `1 | 1`, and an empty-input scalar COUNT returns
  `0`; no internal error or panic appears in the service log.

**Test commands**

```
.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=600s \
  -run 'TestH0NeverRequestsGenericGroupSpill|TestH0OrderedGroupConcatSpillsIndependently|TestMergeGroupH0SkipsGenericSpillAndReuses' \
  ./pkg/sql/colexec/group            # red on base, green with fix
.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=100 -timeout=600s \
  -run 'TestH0NeverRequestsGenericGroupSpill|TestH0OrderedGroupConcatSpillsIndependently|TestMergeGroupH0SkipsGenericSpillAndReuses' \
  ./pkg/sql/colexec/group            # pass
.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=1200s \
  ./pkg/sql/colexec/group            # full package passes
.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=600s \
  -run TestRefreshGroupConcatMaxLenForPreparedCompileReuse \
  ./pkg/sql/compile                  # dependent package passes
GOWORK=off go build ./pkg/sql/colexec/group ./pkg/sql/compile   # clean
GOWORK=off go vet  ./pkg/sql/colexec/group ./pkg/sql/compile   # clean
```

The new BVT pair runs in the CI mo-tester matrix.

**Residual risk**

Low. `agg_spill_mem` values below 10000 are a debug group-count mode
rather
than a byte threshold, and the guard adds an H0 early return before it;
H8/HStr
grouping, distinct aggregate spilling rules and aggregate-owned spilling
are
untouched. The `.sql`/`.result` BVT pair was compared against the
observed
output on a local instance; the full mo-tester baseline remains a CI
job.

Upstream context: main fixed the same defect in PR #26736 for issue
#26453;
this PR carries that decision to `4.2-dev` with the same regressions.

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

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

size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: low agg spill threshold panics on ungrouped COUNT(*)

4 participants