Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Related: #27670 (ClickBench Q06 performance). This issue tracks a separately confirmed wrong-result bug in streaming partial aggregation, including ordinary grouped COUNT.
Branch Name
main
Commit ID
Other Environment Information
- Hardware: Apple M4, arm64, 16 GiB RAM.
- Native standalone MatrixOne, Go 1.27.1, GOEXPERIMENT=simd, GOMAXPROCS=4, GOMEMLIMIT=3GiB.
- Statement execution memory cap: 2 GiB; spill disk cap: 4 GiB.
- Reproduced with an unmodified-main binary in an isolated worktree. Experimental Q33 optimization edits were removed before this investigation.
- The deterministic operator regression uses only two 512-row batches and a one-shot allocation-capacity rejection; it does not depend on live host memory pressure.
Actual Behavior
A partial Group (NeedEval=false) can spill during hard-capacity recovery. Its output state machine assumes that streaming partials do not spill. After publishing the first reloaded bucket, it transitions to End or EvalReset while additional buckets remain queued. Downstream aggregation therefore receives an incomplete set of keys and aggregate contributions, and the SQL query succeeds with an incorrect result.
Confirmed public SQL symptoms on synthetic fixtures (these are not the original ClickBench dataset):
| Fixture | Expected COUNT(DISTINCT SearchPhrase) | Observed on main |
|---|---|---|
| 50,000,000 rows; 24,997,260 distinct 15–22-byte strings | 24,997,260 | 3,487,199; another run returned 390,823 |
| 10,005,480 rows; 5,000,000 distinct 97–103-byte strings | 5,000,000 | 77,850; another run returned 2,242,172 |
The wrong answer varies with the spill boundary. EXPLAIN ANALYZE for the long-string fixture reported 7,085,150 spilled rows, but the first partial grouping emitted only 2,540,573 rows, fewer than the 5,000,000 distinct input keys.
The deterministic operator regression creates 768 distinct keys across two overlapping 512-row batches. Both a key-only partial (the Q06 shape) and a partial with COUNT(*) return only one final key on unmodified main. The same test passes after draining every queued bucket through the existing reload and partial-output paths.
Expected Behavior
Every admitted input contribution must reach the downstream merge exactly once, including all generic spill buckets. Key-only grouping must preserve all distinct keys. Grouped COUNT must preserve both keys and counts across resident, spilled, and resumed-input phases.
Steps to Reproduce
The deterministic operator regression, TestStreamingGroupDrainsCapacitySpill, uses this construction:
- Create two VARCHAR batches. Each contains 512 long strings:
repeat("shared prefix ", 6) + decimal(i). Batch 1 uses i=0..511; batch 2 uses i=256..767. - Connect a
GroupwithNeedEval=falseto a realMergeGroupconsumer. Run both without aggregates and with COUNT(*). - Bind the partial Group to the existing allocation account and
rejectNextGroupAllocationController. Arm one capacity rejection when its child delivers the second batch. This forces the existingretryBuildBatchAfterCapacityspill/retry route. - Execute through the real consumer until EOF. Assert the complete key set 0..767; with COUNT, keys 256..511 must have count 2 and the others count 1.
- On main, both cases fail: the final key map has 1 item instead of 768. After the fix, both cases pass, including zero memory-account usage, spill-disk usage, and spill-FD usage after cleanup.
A SQL fixture for exercising the same long-string shape can be constructed with:
CREATE TABLE q6_spill_repro (SearchPhrase VARCHAR(1024) NOT NULL);
INSERT INTO q6_spill_repro
SELECT CONCAT(REPEAT('matrixone search phrase ', 4), CAST(result AS VARCHAR(30)))
FROM generate_series(1, 5000000) g;
INSERT INTO q6_spill_repro SELECT SearchPhrase FROM q6_spill_repro;
SELECT COUNT(DISTINCT SearchPhrase) FROM q6_spill_repro; -- must be 5000000
EXPLAIN ANALYZE SELECT COUNT(DISTINCT SearchPhrase) FROM q6_spill_repro;This SQL construction is a suggested equivalent fixture; the observed SQL results above came from the materialized shuffled fixtures. Triggering the public SQL bug requires hard-capacity recovery in the partial Group; an ordinary soft-threshold flush alone is insufficient. Use the deterministic operator test to reproduce independently of dataset order and host pressure.
Additional information
Root-cause closure:
pkg/sql/colexec/group/exec2.go:882:retryBuildBatchAfterCapacityspills resident group state without excluding streaming partials.Group.Callflushes the remaining resident state and loads the first generic spill bucket before output.Group.outputOneBatchhandles all exact-DISTINCT partial leaves, but lacks equivalent iteration for generic spill buckets. Its comment states that spill never happens in streaming mode.container.outputOneBatchFinalalready iterates generic spill buckets for final aggregation. ReuseloadSpilledDataand the existing intermediate serialization path for partials; no new storage format or state machine is needed.
Fix requirements: retain the Eval state until queued generic buckets are consumed; load each subsequent bucket only after the previously returned batch is released by the caller; preserve exact-DISTINCT partial-leaf behavior, cancellation, reset/reuse, and terminal memory/disk/FD cleanup. Validate non-spilling and ordinary streaming controls for performance regression. Timings from the old wrong-result large queries are not a valid complete-work performance baseline.
Activity
fix(group): drain streaming partial spills and preserve DISTINCT keys (…
Ariznawlll commentedon Oct 24, 2026
测试版本
main,commit6a9a4e22f94205846b36fcbd7396747c219095e7;修复 PR #30976 的 merge commitd32e5c7bf3e8184dc44a07bca1407d8cc3f2d349已核实在该 SHA 祖先链。GOMAXPROCS=4、GOMEMLIMIT=3GiB,CN 查询内存上限2GiB、spill 磁盘上限4GiB,与原报告资源约束一致。干净工作树构建的二进制vcs.revision为上述完整 SHA、vcs.modified=false,SHA-256c93327196edb7f6d4b403e687f22327cad46ff145531c9ee510ee6e92af8c056。原问题验证
构造并刷盘原报告的两种实际规模:
97–103字节:10,005,480行、5,000,000个不同键;COUNT(DISTINCT SearchPhrase)连续 3/3 返回5000000。这组在本次计划下未显示 spill,仅作规模/值对照,不冒充目标路径。15–22字节:50,000,000行、24,997,260个不同键;使用同一单 CN 的双并行 partial Group,COUNT(DISTINCT SearchPhrase)连续 3/3 返回24997260。EXPLAIN ANALYZE实际扫描 6,104 blocks / 50,000,000 rows;partial Group 向下游完整输出 24,997,260 个键,记录SpillRows=42,410,467、SpillSize=1.03 GiB。这确实走到了 spill,而不是仅在内存中通过。Happy Path 覆盖
在同一 50M fixture 上再执行普通
GROUP BY SearchPhrase, COUNT(*),对 25M 组做完整聚合校验:组数=24,997,260、SUM(count)=50,000,000、MIN(count)=2、MAX(count)=3、count=3 的键数=5,480,连续 3/3 精确一致。对应计划的 partial Group 实际SpillRows=43,580,882、SpillSize=1.38 GiB,扫描同样 50M 行。4 路无 spill 对照也正确;有 spill 和无 spill 两条路径均检查。Unhappy Path 覆盖
小表 NULL/重复键控制:5 行包含两个 NULL、两个
x、一个y,COUNT(DISTINCT k)=2、行数 5,按键分组计数分别 2/2/1,3/3;空过滤结果的 DISTINCT 为 0。原内部硬容量边界由下述定向 Go 测试施加一次确定性分配拒绝,不靠偶然系统 OOM。查询后任务专属临时 spill 目录仅约 96 KiB,没有 GiB 级残留;两套大表 fixture 已删除。白盒验证
在同一最新 SHA 运行
go test -race ./pkg/sql/colexec/group -run '^(TestStreamingGroupDrainsCapacitySpill|TestStreamingGroupMixedDistinctSpill)$' -count=10,通过。前者使用两个重叠的 512 行批次、768 个实际键,对 partial Group 强制一次容量拒绝,验证 key-only 和 COUNT(*) 的全部键/贡献,并断言实际 spill/reload 及内存、磁盘、FD 清零;后者覆盖混合 DISTINCT、恢复输入和取消/重置。不是仅靠研发自己的评论或仅靠没有 spill 的小 SQL。回归测试
上述仓库现有 Go 测试完整覆盖原内部硬容量/部分聚合产品路径及边界,本轮独立
-race10/10,再配合实际 50M SQL spill 的 3/3 验收;依既定分层不重复向 MOTR #278 增加需要 50M 行的日常 SQL golden,也未向 MO 新提 PR。未运行全量 MOTR suite、MatrixOne 全量 CI 或其它分布式拓扑;本 issue 的原报告本身为单机单 CN。测试证据
50M DISTINCT 计划:
inputRows=50000000、outputRows=24997260、SpillRows=42410467、SpillSize=1.03 GiB;50M 普通分组计划:SpillRows=43580882、SpillSize=1.38 GiB。两类精确值各 3/3;定向 Go race 10/10;正常与 NULL/空集控制均通过。测试结论
测试通过。