Skip to content

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

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

d527ac7

Other Environment Information

  • Hardware parameters: Apple Silicon local test host
  • OS type: macOS (arm64)
  • Others: fresh single-CN MatrixOne instance, dedicated data directory and port;
    the binary was built from a clean detached worktree at the commit above.

Actual Behavior

With agg_spill_mem in its documented low-threshold mode, a plain non-empty
COUNT(*) aggregate returns an internal nil-pointer panic instead of a query
result or a normal database error. The reproduction uses one ordinary table
with one row; it does not depend on joins, special data types, constraints, or
user spill files.

The error consistently starts at:

internal error: panic runtime error: invalid memory address or nil pointer dereference
github.com/matrixorigin/matrixone/pkg/sql/colexec/group.(*container).needSpill
pkg/sql/colexec/group/helper.go:784

Source inspection shows that the no-GROUP BY (H0) MergeGroup path has no
hash table, while needSpill unconditionally calls
ctr.hr.Hash.GroupCount() when the threshold is below 10,000.

Expected Behavior

COUNT(*) over a non-empty table should return the correct count regardless
of a valid agg_spill_mem setting. A no-GROUP BY aggregate must not attempt
to read hash-group state that is not created for that execution path.

Steps to Reproduce

create database spill_count_panic;
use spill_count_panic;

create table tiny_scalar (id int);
insert into tiny_scalar values (1);

set @@agg_spill_mem = 256;
select count(*) from tiny_scalar;

Reproduction Result

On the clean main build, the SQL above failed 3/3 times with error 20101 and
the same group.(*container).needSpill nil-pointer stack. It did not leave
spill files, and the service remained usable for subsequent queries.

The same SQL with a normal threshold was a stable control:

set @@agg_spill_mem = 536870912;
select count(*) from tiny_scalar;
-- 1 (3/3)

Additional low-threshold controls that returned 1 were SELECT SUM(id) and
SELECT COUNT(*) ... GROUP BY id, which narrows the failure to the ungrouped
COUNT MergeGroup path.

Duplicate Check

I checked open and closed issues for agg_spill_mem, needSpill, scalar
aggregate spill, and MergeGroup spill panic. Closed #23562 concerns a
multi-CN nested aggregation/partial-state ordering problem at a different
threshold; it does not cover this one-row ungrouped COUNT(*) nil-pointer
path.

Activity

  1. added this to the MO-202607 milestone on Jul 30, 2026
  2. added
    kind/bug
    on Jul 30, 2026
  3. added theissue type on Jul 30, 2026
  4. self-assigned this
    on Jul 30, 2026
  5. XuPeng-SH commented on Aug 1, 2026

    @XuPeng-SH
    Contributor

    Roadmap decision: #26563. Fix this by expressing the H0 execution capability: an ungrouped aggregate has no generic group hash table and must not enter the group-count hash-spill decision. Do not special-case COUNT(*), and do not use a broad Hash == nil return that could hide an invalid non-H0 state. Preserve byte-threshold and ordered-aggregate spill behavior and verify Reset/Free returns the mpool to baseline.

  6. XuPeng-SH commented on Aug 5, 2026

    @XuPeng-SH
    Contributor

    Implementation status: #26736 is open, mergeable, and currently at 6173a85b6d, rebased on main@ab28d1b4ac.

    The fix follows the capability boundary described here: H0 has no group hash table, so it cannot enter the generic grouped-spill predicate. The guard is based on execution mode rather than special-casing COUNT or treating every nil hash as valid. The regression matrix covers H0 with low row and byte thresholds, grouped spill preservation, ordered GROUP_CONCAT behavior, and Reset/Free memory cleanup.

    This fixes the panic and preserves existing spill behavior. It intentionally does not claim generic H0 spill support. Any genuinely unbounded H0 state and ordinary DISTINCT spill work is now called out separately in #26563 so this correctness fix does not grow into an unrelated framework.

    After #26736 merges and CI evidence is final, this issue can be closed.

  7. added a commit that references this issue on Aug 5, 2026
    09e02ee
  8. slyunnn commented on Aug 12, 2026

    @slyunnn

    Regression and extended verification for #26453 (2026-08-12)

    Follow-up to fix: prevent H0 aggregate spill panic (#26736).

    Verification environment

    • MatrixOne main: 9a7c98b8f3aa07fad24b411d54c7a9f6cb3a8731
      • fix(plan): enforce index metadata invariants safely (#26982)
    • Fix commit: 09e02eea38bfa87e4c4333ffb57b370091136bc8
      • fix: prevent H0 aggregate spill panic (#26736)
    • MOTR branch: test/issue-26453-h0-aggregate-spill
    • MOTR commit: de7de0868186cff76fb9b556cf37734db9bf5730
    • Topology: local single-node MatrixOne service.

    What was covered

    • Original regression:

      • agg_spill_mem = 256 with ungrouped COUNT(*).
      • Result: OK. The query returned the correct count and did not panic.
    • Happy path:

      • Low-threshold scalar COUNT(v), SUM(v), AVG(v), MIN(v), and MAX(v).
      • Result: OK. All aggregate values were correct.
    • Boundary path:

      • Low-threshold scalar aggregates over an empty input.
      • Result: OK. COUNT returned 0; SUM returned NULL.
    • Grouped control:

      • Low-threshold GROUP BY aggregate.
      • Result: OK. Grouped results remained correct.
    • Session cleanup:

      • Restored the original agg_spill_mem value after the test.
      • Result: OK.

    Findings

    The original nil-pointer panic on the no-GROUP BY H0 aggregate path was not reproduced on current main.

    The fix correctly skips the generic hash-group spill decision for H0 aggregates, which have no grouping hash table.
    This does not claim generic H0 spilling for all unbounded aggregate states; ordered aggregate spill behavior remains
    separately handled by the aggregate executor, as scoped by #26563.

    MOTR / BVT coverage

    Existing MatrixOne BVT already covers the minimal original COUNT(*) reproduction in:

    • test/distributed/cases/qexec/group_h0_spill.sql

    Added MOTR coverage:

    • suites/14_issue_regression/t/issue_26453_h0_aggregate_spill.test
    • suites/14_issue_regression/r/issue_26453_h0_aggregate_spill.result

    MOTR record and replay both passed: 13/13 statements.

    The MOTR files were added to retain the original panic regression and to verify scalar aggregate variants, empty-input
    semantics, grouped control behavior, and session-variable restoration. No MatrixOne service source code was modified.

    Final conclusion

    #26453 is fixed on current main.

    The verified MOTR commit is ready to be included in the daily regression PR.

  9. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    关闭说明:原问题已在 current main 完成扩展验证,低阈值下 ungrouped COUNT(*) 不再 panic,常见标量聚合、空输入、grouped control 和 session 配置恢复均通过;已有 MatrixOne BVT 与 MOTR 回归覆盖。

    该缺陷由受控 agg_spill_mem 阈值即可稳定触发并验证,不依赖数据规模,因此不需要额外加入 big-data 回归。测试证据见:#26453 (comment)

  10. added 3 commits that reference this issue on Sep 17, 2026
    50b7048
    8e8cae4
    48a0cef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions