Skip to content

feat: support ordered-set aggregate windows - #29106

Merged
XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
daviszhen:codex/issue-25110-ordered-window
Sep 19, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
daviszhen:codex/issue-25110-ordered-window

Conversation

@daviszhen

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #25110
issue #25144

What this PR does / why we need it:

  • supports PERCENTILE_CONT, PERCENTILE_DISC, APPROX_PERCENTILE, and MEDIAN ordered-set forms followed by OVER (...);
  • shares ordered-set shape validation between scalar and window binding while keeping WITHIN GROUP ORDER BY independent from the window's ORDER BY and frame;
  • preserves direct prepared percentile parameters and the existing type/direction contracts;
  • evaluates full-partition ordered-set windows once per partition and broadcasts the result instead of retaining one identical ordered state per output row;
  • configures exact numeric percentile spill for window execution;
  • adds planner, executor, spill, distributed SQL, and design coverage.

Local verification:

  • go test ./pkg/sql/plan -count=1
  • go test ./pkg/sql/colexec/window -count=1
  • go test ./pkg/sql/compile -count=1

The distributed ordered-set case and expected result were updated but were not run against a locally started MatrixOne service.

@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 →

@XuPeng-SH XuPeng-SH 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.

Astra medium review on current head 2ca2bc3.\n\n建议 CHANGES_REQUESTED。未发现已证实的 P0/P1;发现两个 P2 代码问题,另有 design gate 阻塞。\n\nDesign gate:docs/design/20260919-ordered-set-window-functions.md:5 仍标记为 “Proposed for review”,当前 PR reviews 为空,未找到该设计修订的可追溯批准记录。该功能跨越公开 SQL 语义、planner/executor、窗口状态、spill 和资源模型,需先补齐设计批准,并明确“每个 partition 只计算一次”跨 output chunk 的实现约束、完整 partition/frame 的容量模型、取消/spill 失败、prepared reuse 和兼容性验收。\n\n[P2] full-partition 结果未跨 output chunk 缓存,导致整个 partition 重复计算。位置:pkg/sql/colexec/window/window.go:677-693。对超过 8192 行的单 partition,Call() 每次最多输出一个 batch,但 processOrderedSetFullPartitionRange() 每次重新遍历整个 partition、Fill、Flush 并释放状态;下一 batch 再重复。N 行单 partition 的 Fill 次数为 N * ceil(N/8192),例如 20,000 行会重复处理约 60,000 个输入。该行为违背设计声明的 once-per-partition,并使排序/sketch/spill 成本二次增长。建议缓存完成的 partition result 到最后一个输出 chunk,再统一释放,并增加跨 8192 边界的多次 Exec 验证。\n\n[P2] window Flush 丢失查询 context,取消无法中断 percentile spill/归并。位置:pkg/sql/colexec/window/window.go:609、693。window 直接调用 AggFuncExec.Flush(),ordered percentile 的 Flush() 使用 context.Background();ConfigureOrderedPercentileSpill 保存的 proc.Ctx 只覆盖 Fill 触发的 spill。查询在 Flush 归并 runs 期间取消时,tail write、compaction 和 selectSpilledValues 看不到取消,CPU/磁盘/资源会继续占用到最终计算结束。建议两个路径都传递 proc.Ctx,并补充 window 集成层在最终归并阶段取消的测试,验证取消错误、文件关闭、内存释放和后续 reuse。\n\n其他审查:WITHIN GROUP 与 OVER 的排序/frame 分离、四类函数的主要类型限制和 NULL/empty 语义未发现确定性错误。固定 head 的 UT 与 distributed BVT 均有成功证据,但不能替代上述跨 chunk、取消、设计批准和 prepared/window 专项验证。

@daviszhen

Copy link
Copy Markdown
Contributor Author

已按本轮 review 更新,修复提交:cea102a6aa。\n\n1. full-partition 跨 output chunk 重算\n- 当前输入 generation 只创建一次聚合状态,每个 logical partition 只执行一次 Fill/Flush。\n- Flush 后只缓存每个 partition 的一个标量结果,各 output chunk 从该紧凑缓存广播。\n- 缓存在最后一个 chunk、错误路径、Reset 或 Free 时释放。\n- 20,000 行单 partition 回归会连续执行 3 次 Exec,并断言 SpillRows == 20,000;旧实现会累计约 60,000。\n\n2. finalization 丢失查询 context\n- window 的普通、full-partition、cumulative、sliding 四条 finalization 路径全部改为 aggexec.FlushWithContext(proc.Ctx, ...)。\n- 新增 window 集成测试,确认查询取消会从 finalization 返回 context.Canceled,并立即释放 aggregate/cache。\n- ordered percentile 的 tail spill、compaction、失败保持与资源清理专项测试也已复跑通过。\n\n3. design gate\n- 设计文档补充了跨 chunk 生命周期、N/P 容量模型、取消与 spill 失败所有权、prepared reuse、mixed-version 边界和验收条件。\n- 文档仍诚实保留 Proposed for review,并明确合并前必须由 maintainer 留下可追溯的 design approval;我没有自行把它标记为 Approved。请 maintainer 审阅该修订并明确批准,或指出仍需调整的设计项。\n\n本地验证:\n- go test ./pkg/sql/colexec/window -count=1\n- go test ./pkg/sql/plan -count=1\n- go test ./pkg/sql/compile -count=1\n- ordered-percentile spill/failure/compaction 专项测试

@XuPeng-SH XuPeng-SH 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.

Astra medium re-review on current head cea102a.\n\n上一轮两个 P2 代码问题已确认修复:\n- full-partition 结果已跨 output chunk 缓存;\n- window Flush 已传递 proc.Ctx,spill/归并路径可见取消。\n\n但仍建议 CHANGES_REQUESTED,原因如下:\n\n1. [Design gate] docs/design/20260919-ordered-set-window-functions.md:5-8 仍标记为 Proposed for review,且文档明确要求显式 maintainer approval。当前未找到关联本次设计修订的可追溯批准记录;CI 或普通代码 review 不能替代该 gate。请补充批准记录,或明确更新设计状态与评审归属。\n\n2. [P2 验收缺口] window 层尚缺真实 spill/归并进行中取消 -> 文件关闭 -> Reset -> 再执行成功的集成测试。现有测试只在 finalizer 入口触发取消,能证明 context 传递,但不能证明 window 集成路径的资源回收。\n\n3. [P2 验收缺口] prepared window 只验证 ParamRef/建计划,尚缺 public SQL 连续执行不同 percentile、非法值后恢复、再次执行的结果和状态隔离验证。\n\n4. [P2 验收缺口] 尚缺 MEDIAN/APPROX_PERCENTILE window、空 frame、类型边界,以及 partition 边界与 8192 output chunk 边界相交时的组合覆盖;mixed-version/persisted round-trip 也没有本功能专项证据。\n\n固定 head 的 Ubuntu UT 与 distributed BVT 已成功;本次未发现新的可复现 P0/P1/P2 代码缺陷,但上述设计批准和专项验收仍未闭合。

@XuPeng-SH XuPeng-SH 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.

Astra medium deep re-review on exact head 21204fb.

REQUEST_CHANGES 仅因为 design-approval gate 尚未闭合;本轮未发现新的 P0/P1/P2 实现缺陷。

此前实现问题已确认修复:

  • full-partition 结果已跨 output chunk 缓存;20,000 行回归覆盖 3 个 chunk,并验证 SpillRows == 20,000。
  • 四条 window finalization 路径均传递 proc.Ctx;真实 spill merge 中取消后可关闭文件、清理资源,并 Reset 后再次成功执行。
  • prepared recovery、MEDIAN/APPROX_PERCENTILE、空 frame、类型边界、partition/8192 chunk 边界、protobuf round-trip 和 coordinator placement 的专项证据已补齐。
  • exact-head CI 通过;ordered-set SQL BVT 实际执行 58/58,0 ignored,0 abnormal。

剩余阻塞:docs/design/20260919-ordered-set-window-functions.md:5-11 仍明确标记为 pending explicit approval,当前没有绑定该设计 revision 的可追溯批准记录。请由 SQL planner/window maintainer 对精确设计 revision 留下明确批准并链接该决定后再合并。

这是设计验收缺口,不是已确认的 P2 代码 defect。

@mergify mergify Bot added the queued label Sep 19, 2026
@mergify

mergify Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-19 14:56 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-19 14:58 UTC · at 4cc5616b245bb14dcd71df689559c76bf8cd1145

This pull request spent 2 minutes 9 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable "Allow edits from maintainers" on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit be1591f into matrixorigin:main Sep 19, 2026
19 of 21 checks passed
@mergify mergify Bot added dequeued and removed queued labels Sep 19, 2026

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

kind/feature size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants