Skip to content

[Subtask]: Rollup using Sort #20653

Closed

Description

@fengttt

Parent Issue

#20650

Detail of Subtask

select c1, c2, avg(x) from T group by roll up (c1, c2);

The query plan should be Scan T -> Order by C1, C2 -> Rollup Agg

select

Describe implementation you've considered

No response

Additional information

No response

Activity

  1. added
    priority/p0
    on Dec 9, 2024
  2. added this to the 2.1.0 milestone on Dec 9, 2024
  3. added theissue type on Apr 27, 2025
  4. modified the milestones: 2.1.0, 2509 on Apr 27, 2025
  5. modified the milestones: MO-2509, MO-202602 on Feb 3, 2026
  6. iamlinjunhong commented on Sep 16, 2026

    @iamlinjunhong
    Contributor

    Sort-based ROLLUP: applicable scenarios

    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:

    SELECT d.c1, d.c2, AVG(d.x)
    FROM (
      SELECT c1, c2, x FROM T ORDER BY c1, c2
    ) AS d
    GROUP BY d.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.

  7. iamlinjunhong commented on Oct 28, 2026

    @iamlinjunhong
    Contributor

    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.

  8. XuPeng-SH commented on Oct 28, 2026

    @XuPeng-SH
    Contributor

    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.

  9. iamlinjunhong commented on Oct 28, 2026

    @iamlinjunhong
    Contributor

    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.

  10. 1 remaining item

  11. added
    ai-light
    on Oct 28, 2026
  12. Ariznawlll commented on Nov 3, 2026

    @Ariznawlll
    Collaborator

    结论

    本次大数据测试中,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 次从发出请求到收到分析计划的耗时中位数,单位秒;包含执行和分析开销,不包含百万行结果传输。

      场景 强制 SORT 强制 HASH 默认 COST
      100 万高基数组,COUNT/SUM/AVG/MIN/MAX,无序 26.128 0.405 0.421
      相同聚合,已有序输入 26.149 1.230 1.217
      500 万行低基数,五种聚合 1.192 0.524 0.502
      100 万高基数组,仅 AVG,无序 0.727 0.210 0.215
      100 万高基数组,仅 AVG,已有序输入 0.712 0.890 0.907

      这些是同版本不同策略的比较。所有场景初始 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。

    待解决点

    • 请研发评估高基数下强制 SORT 对 MIN/MAX 或其他不支持单组复用的聚合列表的性能成本,以及是否需要优化或明确使用边界。
    • 本次为单机、顺序、主要暖缓存测试;未测生产目标负载、并发吞吐、CPU/GC profile、冷存储及同条件旧版 A/B。已有序输入由子查询排序构造,排序成本包含在计划内。以上不足以判定生产影响、跨版本回归或最终性能验收。
  13. XuPeng-SH commented on Nov 3, 2026

    @XuPeng-SH
    Contributor

    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 复测,不表示验收通过。

    验证证据(远端记录,本轮未在本地运行测试):

    • PR 作者报告 aggexec/group 测试及 race、分配失败/取消/re-prepare 清理测试通过;SQL hint BVT 68/68 两次、含元数据比较 68/68、hint 目录 688/688,SORT/HASH 与独立期望值比较。
    • 作者同机同配置数据、各 30 次的高基数五聚合强制 SORT 中位数由 24.8800s 降到 1.5363s;这是作者报告,不是本轮实测。部分对照/消融仍因变异无法下结论。
    • 远端复核报告 Improve SORT ROLLUP reuse for MIN and MAX #32089 (review) 记录 rebased head 上 35 个聚合/group 顶层测试及 52 行新增 SQL fixture/元数据核对通过;race、SQL BVT、性能仍引用作者证据。合并队列记录 CI Required 成功;合并后分析检查尚在进行,不视为全量测试完成。

    建议 QA 在该 SHA 或包含它的 main 上重跑原 500 万行/100 万高基数组工作负载,比较完整结果、分组标识及 SORT/HASH/COST 计划;覆盖有序/无序、NULL、BIGINT/decimal、MIN/MAX 混合列表和不支持元数据回退,并检查取消/内存失败与再次 prepare。生产并发、多 CN、冷缓存、峰值 RSS、CPU/GC 归因和部分性能对照未覆盖,需按目标环境补测;不宣称所有控制场景均无回归。

    负责人交接给 @Ariznawlll。

  14. Ariznawlll commented on Nov 16, 2026

    @Ariznawlll
    Collaborator

    结论

    测试通过。#32089 合入后的 SORT ROLLUP 在本次单 CN、双 CN 和构造并发负载下结果正确;此前高基数 MIN/MAX 慢路径在本次负载中未再出现。以下结论仅覆盖所述数据、拓扑和场景。

    关键证据

    • 版本和环境:2026-11-16,官方 main 源码 3399778af96aa97c22dd8b752155eefb0c49dc97,自编译二进制构建信息 vcs.modified=false;本地 Apple M3 / 16 GiB。SQL git_version() 返回 unknown,因为构建时未注入发布版本参数;不是官方发布镜像。双 CN 分属两个进程,但位于同一台机器;本次未测跨机器部署。
    • 数据和正确性:构造并 flush 500 万行(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 行通过。
    • 双 CN 执行与并发:强制 SORT 的物理计划包含两个 Remote 扫描 scope 与 merge order;实际执行分析中扫描共输出 500 万行、8 个并行实例最小输出 618,688 行。四客户端同时从两个 CN 发起查询,SORT/HASH/COST 各 3 轮,共 36 次,每次逐行核对 50,017 个分组;另有两客户端同时扫描全部 500 万行,三种策略各 3 轮,共 18 次,每次核对全部 81 个分组;各轮均有正的实际重叠时长,结果均正确。两个 CN 进程在测试期间持续存活,日志未发现 panic 或 OOM。
    • 性能:单 CN 上交错执行 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 提升或断言跨版本无回归。
    • NULL、BIGINT、decimal、AVG、MIN/MAX 混合列表及 DISTINCT 已做小数据比较;单组复用、回退、分配失败、取消、重新 prepare 的定向 Go 单测各重复 3 次,相关生命周期定向 race 也各重复 3 次通过。现有主线回归入口:ROLLUP SQL 用例、group 复用与回退单测;本次新增的多 CN 和大数据并发检查为本地测试,未称其已加入 CI。

    待解决点

    本次未覆盖生产负载、跨机器多 CN、冷缓存、CPU/GC 归因、峰值 RSS 及同条件旧新版本基准;不将本次测量解释为生产性能 SLA 或覆盖全部聚合组合。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-lightkind/subtaskpriority/p0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions