You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Based on the current implementation and a 1M-row benchmark, sort-based ROLLUP is a good fit when:
The child already produces a proven global order on the complete ROLLUP keys, for example an ordered derived input (ORDER BY c1, c2) or an ordered scan/merge. The parent streaming ROLLUP can reuse that order and does not add a second sort.
There are many ROLLUP levels, or relatively few complete-key runs, so one scan plus streaming prefix states can amortize the fixed cost of the legacy grouping-set branches.
The UNION ALL branches of the hash/grouping-set plan cannot fully overlap, such as a constrained or serialized execution scope.
It is not a universal replacement for hash ROLLUP. With unordered input, the extra global sort can make sort ROLLUP slower than concurrent hash branches, especially for ordinary two- or three-key ROLLUP with high detail cardinality.
For the following shape:
SELECTd.c1, d.c2, AVG(d.x)
FROM (
SELECT c1, c2, x FROM T ORDER BY c1, c2
) AS d
GROUP BYd.c1, d.c2 WITH ROLLUP;
the SORT plan is one scan -> one sort -> one streaming ROLLUP AGG. The current HASH expansion repeats the derived subtree for each grouping set, so the child sort/scan is repeated per branch; UNION ALL may run those branches concurrently, but it does not remove the duplicated work.
On an Apple M4 CN (GOMAXPROCS=10), using 1M deterministic rows and five iterations across three runs:
Shape
SORT
HASH serial
HASH parallel
Derived ORDER BY + COUNT, 3 keys
24.49–24.64 ms
96.75–99.30 ms
40.65–41.55 ms
Derived ORDER BY + AVG, 2 keys + measure
23.84–25.27 ms
64.06–69.27 ms
36.15–42.28 ms
These timings are physical-operator measurements, not machine-independent end-to-end constants. The default COST mode should select SORT only when statistics and the global input-order proof are available; otherwise it should keep the existing hash path as the safe fallback.
The following code-review concerns were confirmed for follow-up by the user. They are not a claim that the original issue still reproduces, or that a local benchmark was run. Evidence is quoted in its original language.
QA status for merged main 970dc9b65334a74d25b231c0b7cdc5ded4cefb82: #31505 addresses DISTINCT/ordered admission and #31584 adds a single-group reuse fast path for supported COUNT/native SUM/AVG lists. This environment has not run current-head planner, aggexec/group tests or SQL: the existing resource guard leaves insufficient headroom for a safe large link or service build. git diff --check passed; it is not functional or performance validation.
The developer's post-#31505 follow-up #20653 (comment) concerns high-NDV forced-SORT CPU/GC costs. #31584 addresses part of that mechanism, but its PR explicitly leaves DISTINCT, dedicated decimal, MIN/MAX and unsupported mixed lists on the old path and reports forced SORT still slower than shared HASH in its pre-merge comparisons. Those are author-reported measurements, not a local current-head benchmark; the original reporter is asked to verify the remaining workload. Keep the developer owner while this follow-up is resolved; this is not a claim that the original correctness bug still reproduces.
The following code-review concerns were confirmed for follow-up by the user. They are not a claim that the original issue still reproduces, or that a local benchmark was run. Evidence is quoted in its original language.
测试 SQL:核心混合聚合为 SELECT c1,c_high,grouping(c1),grouping(c_high),count(*),sum(x),avg(x),min(x),max(x) FROM facts WHERE id<=1000000 GROUP BY c1,c_high WITH ROLLUP;另分别测试只含 COUNT/SUM/AVG、只含 AVG,以及通过子查询 ORDER BY c1,c_high 提供已有序输入。注意部分高基数计划实际从表扫描 500 万行,过滤后 100 万行进入聚合。
Activity
iamlinjunhong commentedon Sep 16, 2026
Sort-based ROLLUP: applicable scenarios
Based on the current implementation and a 1M-row benchmark, sort-based ROLLUP is a good fit when:
ORDER BY c1, c2) or an ordered scan/merge. The parent streaming ROLLUP can reuse that order and does not add a second sort.It is not a universal replacement for hash ROLLUP. With unordered input, the extra global sort can make sort ROLLUP slower than concurrent hash branches, especially for ordinary two- or three-key ROLLUP with high detail cardinality.
For the following shape:
the SORT plan is
one scan -> one sort -> one streaming ROLLUP AGG. The current HASH expansion repeats the derived subtree for each grouping set, so the child sort/scan is repeated per branch; UNION ALL may run those branches concurrently, but it does not remove the duplicated work.On an Apple M4 CN (
GOMAXPROCS=10), using 1M deterministic rows and five iterations across three runs:These timings are physical-operator measurements, not machine-independent end-to-end constants. The default
COSTmode should select SORT only when statistics and the global input-order proof are available; otherwise it should keep the existing hash path as the safe fallback.iamlinjunhong commentedon Oct 28, 2026
Human-confirmed change risks after #28987
Reviewed merge commit:
935811c.The following code-review concerns were confirmed for follow-up by the user. They are not a claim that the original issue still reproduces, or that a local benchmark was run. Evidence is quoted in its original language.
correctness
pkg/sql/plan/distinct_agg.go:64的tupleDistinctShuffleArgument先于:67调用的sort_rollup保护执行;:306不拒绝标记ROLLUP,:407把其输入换成hash pair聚合,:412清除DISTINCT。强制SORT、唯一聚合COUNT(DISTINCT x,y)、参数为可交换的非NULL直接列且NDV足够高、ROLLUP键NDV<64时可触发(shuffle.go:1733、1750);标记ROLLUP的GroupingFlag全true可通过。各明细键内去重不能替代总计跨明细去重,且新hash输入破坏原排序前提。例如两个c1各包含相同100万组(x,y),正确总DISTINCT为100万,改写后普通COUNT面对200万pair;返回多scope时还会被compile.go:2030拒绝。COST拒绝DISTINCT状态,因此主要影响强制SORT及旧hint。此为静态确认的优化器契约遗漏,没有本地运行输出。
performance
pkg/sql/colexec/group/sort_rollup.go:472、514、553对每个完成前缀Flush、Free、重新创建单组聚合,:611按run分配参数窗口。强制SORT遇高完整键NDV时,分配与状态构造随输出组数及聚合数增长,即使复用排序也有明显CPU/GC退化风险;docs/design/rollup-sort-rollup.md:424记录过显著高基数反例,仅作历史背景。默认COST通过rollup_cost.go:353的完成组成本及:197的80%门槛规避估计到的高NDV,强制SORT绕过这些成本保护;未测量本次退化倍数。
This issue is being returned to Loom's candidate list for follow-up. The PR remains merged.
fix(plan): preserve DISTINCT and ordered admission for sort ROLLUP (#…
XuPeng-SH commentedon Oct 28, 2026
QA status for merged main
970dc9b65334a74d25b231c0b7cdc5ded4cefb82: #31505 addresses DISTINCT/ordered admission and #31584 adds a single-group reuse fast path for supported COUNT/native SUM/AVG lists. This environment has not run current-head planner, aggexec/group tests or SQL: the existing resource guard leaves insufficient headroom for a safe large link or service build.git diff --checkpassed; it is not functional or performance validation.The developer's post-#31505 follow-up #20653 (comment) concerns high-NDV forced-SORT CPU/GC costs. #31584 addresses part of that mechanism, but its PR explicitly leaves DISTINCT, dedicated decimal, MIN/MAX and unsupported mixed lists on the old path and reports forced SORT still slower than shared HASH in its pre-merge comparisons. Those are author-reported measurements, not a local current-head benchmark; the original reporter is asked to verify the remaining workload. Keep the developer owner while this follow-up is resolved; this is not a claim that the original correctness bug still reproduces.
iamlinjunhong commentedon Oct 28, 2026
Human-confirmed change risks after #31505
Reviewed merge commit:
da0c931.The following code-review concerns were confirmed for follow-up by the user. They are not a claim that the original issue still reproduces, or that a local benchmark was run. Evidence is quoted in its original language.
performance
既有较大风险仍在:group/sort_rollup.go:472、:514、:553 对每个完成前缀 Flush/Free/重建,:611 按 run 分配窗口;完整键高 NDV 且聚合较多时,工作及分配随完成组数增长。rollup_cost.go:178 的强制 SORT/旧 hint 绕过成本保护,即使复用排序也可能显著增加 CPU/GC。默认 COST 保留 NDV、共享 HASH 和 80% 门槛;PR 未修改执行器,本次未测退化倍数,未发现新增逐行 RPC、IO 或锁。
This issue is being returned to Loom's candidate list for follow-up. The PR remains merged.
1 remaining item
Reduce SORT ROLLUP finalization and window allocations (#31584)
Ariznawlll commentedon Nov 3, 2026
结论
本次大数据测试中,ROLLUP 结果正确;但高基数下强制 SORT 使用 MIN/MAX 时,复现了明显慢路径。测试覆盖到的默认 COST 计划均选择 HASH。由于尚无明确性能验收阈值和同条件旧版基线,本条记录不判定最终验收或跨版本回归。
关键证据
版本与环境:2026-11-03,运行时
git_version()为main@d61a7206c16b30baa4efc525eb6067c4364035aa;本地隔离的 1 LOG / 1 TN / 1 CN,Apple M3、16 GiB,GOMAXPROCS=4、GOMEMLIMIT=6GiB(Go 软限制)。二进制构建信息标记 dirty;测试工作树未修改源码,非源码差异为第三方库install符号链接。数据与正确性(Happy case):构造并 flush 500 万行:
id=1..5000000,c1=id%16,c_low=id%64,c_high=id,x=id%101;表内COUNT(*)=5000000、SUM(x)=249999856。高基数场景筛选id<=1000000,产生 100 万明细组、16 个小计和 1 个总计;低基数场景使用全表,产生 64 个明细组、16 个小计和 1 个总计。SORT/HASH/COST 在各查询形状下的完整结果均逐行检查分组标识和所输出的 COUNT、SUM、AVG、MIN、MAX,同时检查重复和缺失;高基数每次核对 1,000,017 行、低基数 81 行,均正确。AVG 按返回的四位小数精度校验。测试 SQL:核心混合聚合为
SELECT c1,c_high,grouping(c1),grouping(c_high),count(*),sum(x),avg(x),min(x),max(x) FROM facts WHERE id<=1000000 GROUP BY c1,c_high WITH ROLLUP;另分别测试只含 COUNT/SUM/AVG、只含 AVG,以及通过子查询ORDER BY c1,c_high提供已有序输入。注意部分高基数计划实际从表扫描 500 万行,过滤后 100 万行进入聚合。性能风险(Unhappy case):相同实例和数据上交错执行
EXPLAIN ANALYZE,下表为各 10 次从发出请求到收到分析计划的耗时中位数,单位秒;包含执行和分析开销,不包含百万行结果传输。这些是同版本不同策略的比较。所有场景初始 COST 的
EXPLAIN与 HASH 相同;重复测量提取的 Aggregate、Sort、Scan 节点序列在 70/70 对 COST/HASH 中一致。MIN/MAX 消融与白盒定位:100 万高基数、无序输入、强制 SORT 下,只含 COUNT/SUM/AVG 的 10 次中位数为 1.276 秒;在其基础上单独加入 MIN 的 3 次为 22.631、25.162、20.888 秒,单独加入 MAX 的 3 次为 25.756、22.269、20.617 秒。某次混合聚合的分析计划中,ROLLUP Aggregate 记录 24,363 ms,输入 Sort 记录 393 ms。源码中
sort_rollup.go对整个聚合列表选择单组复用,不支持时会在分组完成时重置;rollup_single_group_test.go覆盖 MIN 不支持复用。TestSingleGroupReuseRejectsUnsupported和TestSortRollupSingleGroupReuseAndMixedFallback各重复 3 次通过。这些证据与回退路径成本有关的解释一致,但没有用 CPU profile 证明各调用的占比。已有回归:DISTINCT 明细/小计/总计正确性回归位于 MOTR PR #305;本次大数据测试是独立本地负载,未声称该性能场景已加入 CI。
待解决点
Improve SORT ROLLUP reuse for MIN and MAX (#32089)
XuPeng-SH commentedon Nov 3, 2026
QA 交接:#32089 已于 2026-11-03 13:06 UTC 合入 main,merge SHA
5eba6f6ddc50aabf1d30c565322f73b7f216bae1。本次扩展固定宽度 MIN/MAX 与通用 decimal SUM/AVG(含 BIGINT)的单组状态复用,并对不支持的聚合列表、富元数据保留回退。此前 #31505 的 DISTINCT/排序保护及 #31584 的复用已合并;本次针对评论 #20653 (comment) 中旧版本的高基数 MIN/MAX 慢路径。该旧版结果不能直接当作新合并版本反例。完整评论及 PR 讨论未见本次修复后的已验证反例或未完成的开发 follow-up,交给 QA 复测,不表示验收通过。
验证证据(远端记录,本轮未在本地运行测试):
建议 QA 在该 SHA 或包含它的 main 上重跑原 500 万行/100 万高基数组工作负载,比较完整结果、分组标识及 SORT/HASH/COST 计划;覆盖有序/无序、NULL、BIGINT/decimal、MIN/MAX 混合列表和不支持元数据回退,并检查取消/内存失败与再次 prepare。生产并发、多 CN、冷缓存、峰值 RSS、CPU/GC 归因和部分性能对照未覆盖,需按目标环境补测;不宣称所有控制场景均无回归。
负责人交接给 @Ariznawlll。
Ariznawlll commentedon Nov 16, 2026
结论
测试通过。
#32089合入后的 SORT ROLLUP 在本次单 CN、双 CN 和构造并发负载下结果正确;此前高基数 MIN/MAX 慢路径在本次负载中未再出现。以下结论仅覆盖所述数据、拓扑和场景。关键证据
3399778af96aa97c22dd8b752155eefb0c49dc97,自编译二进制构建信息vcs.modified=false;本地 Apple M3 / 16 GiB。SQLgit_version()返回unknown,因为构建时未注入发布版本参数;不是官方发布镜像。双 CN 分属两个进程,但位于同一台机器;本次未测跨机器部署。COUNT(*)=5000000、SUM(x)=249999856)。单 CN 的高基数有序、无序场景在 SORT/HASH/COST 各逐行检查 1,000,017 行,低基数全表检查 81 行,分别重复 3 轮;核对 GROUPING 标识、COUNT/SUM/AVG/MIN/MAX、重复和缺失。双 CN 下分别从两个 CN 发起高基数无序查询,三种策略各完整逐行检查 1,000,017 行通过。EXPLAIN ANALYZE,每种策略 10 次。高基数无序五聚合中位数 SORT 1.878 秒、HASH 0.393 秒、COST 0.371 秒;已有序输入分别 1.867、1.205、1.159 秒;低基数全表分别 0.947、0.414、0.431 秒。为包含分析开销、不含百万行结果传输的客户端耗时;本次 COST 在上述三种计划均选择 HASH。历史旧版 SORT 26.128 秒来自另一次运行,不能据此计算严格同条件 A/B 提升或断言跨版本无回归。待解决点
本次未覆盖生产负载、跨机器多 CN、冷缓存、CPU/GC 归因、峰值 RSS 及同条件旧新版本基准;不将本次测量解释为生产性能 SLA 或覆盖全部聚合组合。